Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Magic signer" #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions lib/src/keystore/impl/keystore.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:typed_data';
import 'package:blockchain_signer/blockchain_signer.dart';
import 'package:meta/meta.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -30,18 +29,8 @@ class Keystore extends Equatable {

final String? mnemonic;

RemoteSigner? signer;

Keystore._({required this.secretKey, this.mnemonic});

/// Generate a keyStore with a remote signer
///
/// ```dart
/// await magic.tezos.fetchRemoteSigner();
/// Keystore.fromRemoteSigner(magic.tezos);
/// ```
Keystore.fromRemoteSigner(RemoteSigner this.signer) : secretKey = '', mnemonic = null;

/// A factory that generates a keystore from a secret key.
///
/// ```dart
Expand Down Expand Up @@ -136,37 +125,28 @@ class Keystore extends Equatable {

/// The public key of this.
String get publicKey => crypto.catchUnhandledErrors(() {
if (signer != null) {
return signer?.publicKey as String;
}

final seedBytes = crypto.decodeWithoutPrefix(seed);
var pk = crypto.publicKeyBytesFromSeedBytes(seedBytes);
final seedBytes = crypto.decodeWithoutPrefix(seed);
var pk = crypto.publicKeyBytesFromSeedBytes(seedBytes);

return crypto.encodeWithPrefix(
prefix: _publicKeyPrefix,
bytes: Uint8List.fromList(pk.toList()),
);
});
return crypto.encodeWithPrefix(
prefix: _publicKeyPrefix,
bytes: Uint8List.fromList(pk.toList()),
);
});

/// The address of this.
String get address => crypto.catchUnhandledErrors(() {
if (signer != null) {
return signer?.address as String;
}

final publicKeyBytes = crypto.decodeWithoutPrefix(publicKey);
final hash = crypto.hashWithDigestSize(
size: 160,
bytes: publicKeyBytes,
);

return crypto.encodeWithPrefix(
prefix: _addressPrefix,
bytes: hash,
);

});
final publicKeyBytes = crypto.decodeWithoutPrefix(publicKey);
final hash = crypto.hashWithDigestSize(
size: 160,
bytes: publicKeyBytes,
);

return crypto.encodeWithPrefix(
prefix: _addressPrefix,
bytes: hash,
);
});

/// The seed of this.
String get seed => crypto.catchUnhandledErrors(() {
Expand Down
8 changes: 2 additions & 6 deletions lib/src/models/operations_list/impl/operations_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ class OperationsList {
await _catchHttpError<void>(() async {
if (result.signature == null) throw ArgumentError.notNull('result.signature');

final edsig = await result.signature!.edsig;

final simulationResults = await rpcInterface.preapplyOperations(
operationsList: this,
signature: edsig,
signature: result.signature!.edsig,
);

for (var i = 0; i < simulationResults.length; i++) {
Expand Down Expand Up @@ -102,9 +100,7 @@ class OperationsList {
await _catchHttpError<void>(() async {
if (result.signature == null) throw ArgumentError.notNull('result.signature');

final signatureWithPayload = await result.signature!.hexIncludingPayload;

result.id = await rpcInterface.injectOperation(signatureWithPayload);
result.id = await rpcInterface.injectOperation(result.signature!.hexIncludingPayload);
});
}

Expand Down
39 changes: 12 additions & 27 deletions lib/src/signature/impl/signature.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:blockchain_signer/signer/response/signed_result.dart';
import 'package:convert/convert.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:pinenacl/ed25519.dart';
Expand Down Expand Up @@ -27,7 +25,7 @@ class Signature extends Equatable {
final Keystore keystore;
final Watermarks? watermark;

static final watermarkToHex = {
static final _watermarkToHex = {
Watermarks.block: '01',
Watermarks.endorsement: '02',
Watermarks.generic: '03',
Expand Down Expand Up @@ -62,42 +60,29 @@ class Signature extends Equatable {
}

/// Signed bytes of this.
Future<ByteList> get signedBytes async {
return crypto.catchUnhandledErrors(() async {
ByteList get signedBytes {
return crypto.catchUnhandledErrors(() {
final watermarkedBytes =
watermark == null ? bytes : Uint8List.fromList(crypto.hexDecode(watermarkToHex[watermark]!) + bytes);

if (keystore.signer != null) {
SignedResult res = await keystore.signer?.sign(hex.encode(bytes), Uint8List.fromList(crypto.hexDecode(watermarkToHex[watermark]!))) as SignedResult;

/// sbytes from Taquito remoteSign is in the format of bytes+signature,
/// extract the signature out and return
String signedBytesHex = res.sbytes.replaceAll(res.bytes, '');
final signedBytesInList = hex.decode(signedBytesHex);
final signed = ByteList.fromList(Uint8List.fromList(signedBytesInList));
return signed;
}

watermark == null ? bytes : Uint8List.fromList(crypto.hexDecode(_watermarkToHex[watermark]!) + bytes);
var hashedBytes = crypto.hashWithDigestSize(size: 256, bytes: watermarkedBytes);
var secretKey = keystore.secretKey;
var secretKeyBytes = crypto.decodeWithoutPrefix(secretKey);
var signed = crypto.signDetached(
bytes: hashedBytes, secretKey: secretKeyBytes);
return signed;

return crypto.signDetached(bytes: hashedBytes, secretKey: secretKeyBytes);
});
}

/// Base 58 encoding of this using 'edsig' prefix.
Future<String> get edsig {
return crypto.catchUnhandledErrors(() async {
return crypto.encodeWithPrefix(prefix: crypto.Prefixes.edsig, bytes: Uint8List.fromList((await signedBytes).toList()));
String get edsig {
return crypto.catchUnhandledErrors(() {
return crypto.encodeWithPrefix(prefix: crypto.Prefixes.edsig, bytes: Uint8List.fromList(signedBytes.toList()));
});
}

/// Hexadecimal signature of this prefixed with hexadecimal payload to sign.
Future<String> get hexIncludingPayload {
return crypto.catchUnhandledErrors(() async {
return crypto.hexEncode(Uint8List.fromList(bytes + (await signedBytes)));
String get hexIncludingPayload {
return crypto.catchUnhandledErrors(() {
return crypto.hexEncode(Uint8List.fromList(bytes + signedBytes));
});
}

Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies:
quiver: ^3.0.1 # used to zip two lists
meta: ^1.3.0
pretty_dio_logger: ^1.1.1
blockchain_signer: 0.1.0 # Remote Signer

dev_dependencies:
lints: ^1.0.1 # linter
Expand Down