From a7bcced41e94e446016684dfb9b1e248312c6ad6 Mon Sep 17 00:00:00 2001 From: Sushant Chandla Date: Thu, 9 Sep 2021 03:05:55 +0530 Subject: [PATCH 1/3] changed uint8list to bytelist --- .../impl/encrypted_secret_key_to_seed.dart | 3 +- .../crypto/impl/external_crypto_wrapper.dart | 6 ++-- .../impl/secret_key_seed_conversion.dart | 2 +- lib/src/keystore/impl/keystore.dart | 17 ++++++---- lib/src/signature/impl/signature.dart | 32 +++++++++++++------ pubspec.yaml | 2 +- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart b/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart index 927023c7..2f5d1be5 100644 --- a/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart +++ b/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart @@ -23,7 +23,8 @@ String encryptedSecretKeyToSeed({ ); final secretbox = SecretBox(encryptionKey); - final decryptedBytes = secretbox.decrypt(secretKeyBytes, nonce: nonce); + final decryptedBytes = + secretbox.decrypt(ByteList.fromList(secretKeyBytes), nonce: nonce); final seed = crypto.encodeWithPrefix( prefix: Prefixes.edsk2, diff --git a/lib/src/crypto/impl/external_crypto_wrapper.dart b/lib/src/crypto/impl/external_crypto_wrapper.dart index 1dbafbd2..d0c22920 100644 --- a/lib/src/crypto/impl/external_crypto_wrapper.dart +++ b/lib/src/crypto/impl/external_crypto_wrapper.dart @@ -19,14 +19,14 @@ Uint8List seedBytesFromMnemonic(String mnemonic, {String passphrase = ''}) { return seedBytes.sublist(0, 32); } -Uint8List secretKeyBytesFromSeedBytes(Uint8List seed) => signingKeyFromSeedBytes(seed); +ByteList secretKeyBytesFromSeedBytes(Uint8List seed) => signingKeyFromSeedBytes(seed); -Uint8List publicKeyBytesFromSeedBytes(Uint8List seed) => signingKeyFromSeedBytes(seed).verifyKey; +ByteList publicKeyBytesFromSeedBytes(Uint8List seed) => signingKeyFromSeedBytes(seed).verifyKey; @visibleForTesting SigningKey signingKeyFromSeedBytes(Uint8List seed) => SigningKey(seed: seed); -Uint8List signDetached({required Uint8List bytes, required Uint8List secretKey}) { +ByteList signDetached({required Uint8List bytes, required Uint8List secretKey}) { final seed = secretKey.sublist(0, 32); // the seed is the first 32 bytes of the secret key return SigningKey(seed: seed).sign(bytes).sublist(0, 64); diff --git a/lib/src/crypto/impl/secret_key_seed_conversion.dart b/lib/src/crypto/impl/secret_key_seed_conversion.dart index 768ef120..6edaea5d 100644 --- a/lib/src/crypto/impl/secret_key_seed_conversion.dart +++ b/lib/src/crypto/impl/secret_key_seed_conversion.dart @@ -34,6 +34,6 @@ String seedToSecretKey(String seed) { return encodeWithPrefix( prefix: secretKeyPrefix, - bytes: secretKey, + bytes: Uint8List.fromList(secretKey.toList()), ); } diff --git a/lib/src/keystore/impl/keystore.dart b/lib/src/keystore/impl/keystore.dart index 6ef2e6e7..4e16b75f 100644 --- a/lib/src/keystore/impl/keystore.dart +++ b/lib/src/keystore/impl/keystore.dart @@ -43,7 +43,8 @@ class Keystore extends Equatable { factory Keystore.fromSecretKey(String secretKey) { return crypto.catchUnhandledErrors(() { if (secretKey.length != _secretKeyLength) { - throw crypto.CryptoError(type: crypto.CryptoErrorTypes.secretKeyLengthError); + throw crypto.CryptoError( + type: crypto.CryptoErrorTypes.secretKeyLengthError); } _validateChecksum(secretKey); @@ -79,11 +80,14 @@ class Keystore extends Equatable { /// ``` /// /// Throws [CryptoError] if [mnemonic] is invalid. - factory Keystore.fromMnemonic(String mnemonic, {String email = '', String password = ''}) { + factory Keystore.fromMnemonic(String mnemonic, + {String email = '', String password = ''}) { return crypto.catchUnhandledErrors(() { final passphrase = '$email$password'; - final seedBytes = crypto.seedBytesFromMnemonic(mnemonic, passphrase: passphrase); - final seed = crypto.encodeWithPrefix(prefix: _seedPrefix, bytes: seedBytes); + final seedBytes = + crypto.seedBytesFromMnemonic(mnemonic, passphrase: passphrase); + final seed = + crypto.encodeWithPrefix(prefix: _seedPrefix, bytes: seedBytes); final secretKey = crypto.seedToSecretKey(seed); return Keystore._( @@ -99,7 +103,8 @@ class Keystore extends Equatable { ) { return crypto.catchUnhandledErrors(() { if (encryptedSecretKey.length != _encryptedSecretKeyLength) { - throw crypto.CryptoError(type: crypto.CryptoErrorTypes.encryptedSecretKeyLengthError); + throw crypto.CryptoError( + type: crypto.CryptoErrorTypes.encryptedSecretKeyLengthError); } final seed = crypto.encryptedSecretKeyToSeed( @@ -130,7 +135,7 @@ class Keystore extends Equatable { return crypto.encodeWithPrefix( prefix: _publicKeyPrefix, - bytes: pk, + bytes: Uint8List.fromList(pk.toList()), ); }); diff --git a/lib/src/signature/impl/signature.dart b/lib/src/signature/impl/signature.dart index 0f88da84..64c8058d 100644 --- a/lib/src/signature/impl/signature.dart +++ b/lib/src/signature/impl/signature.dart @@ -2,6 +2,7 @@ import 'dart:typed_data'; import 'package:equatable/equatable.dart'; import 'package:meta/meta.dart'; +import 'package:pinenacl/ed25519.dart'; import 'package:tezart/src/common/validators/hex_validator.dart'; import 'package:tezart/src/keystore/keystore.dart'; @@ -37,7 +38,10 @@ class Signature extends Equatable { /// A factory that computes the signature of [bytes] (prefixed by [watermark]) using [keystore]. /// /// [watermark] is optional and will be ignored if missing. - factory Signature.fromBytes({required Uint8List bytes, required Keystore keystore, Watermarks? watermark}) { + factory Signature.fromBytes( + {required Uint8List bytes, + required Keystore keystore, + Watermarks? watermark}) { return Signature._(bytes: bytes, watermark: watermark, keystore: keystore); } @@ -47,25 +51,33 @@ class Signature extends Equatable { /// Throws a [CryptoError] if : /// - [data] is not hexadecimal /// - [data] length is odd (because it must be the hexadecimal of a list of bytes (a single byte represent two hexadecimal digits)) - factory Signature.fromHex({required String data, required Keystore keystore, Watermarks? watermark}) { + factory Signature.fromHex( + {required String data, + required Keystore keystore, + Watermarks? watermark}) { return crypto.catchUnhandledErrors(() { HexValidator(data).validate(); // Because two hexadecimal digits correspond to a single byte, this will throw an error if the length of the data is odd if (data.length.isOdd) { - throw crypto.CryptoError(type: crypto.CryptoErrorTypes.invalidHexDataLength); + throw crypto.CryptoError( + type: crypto.CryptoErrorTypes.invalidHexDataLength); } var bytes = crypto.hexDecode(data); - return Signature.fromBytes(bytes: bytes, keystore: keystore, watermark: watermark); + return Signature.fromBytes( + bytes: bytes, keystore: keystore, watermark: watermark); }); } /// Signed bytes of this. - Uint8List get signedBytes { + ByteList get signedBytes { return crypto.catchUnhandledErrors(() { - final watermarkedBytes = - watermark == null ? bytes : Uint8List.fromList(crypto.hexDecode(_watermarkToHex[watermark]!) + bytes); - var hashedBytes = crypto.hashWithDigestSize(size: 256, bytes: watermarkedBytes); + final watermarkedBytes = 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); @@ -76,7 +88,9 @@ class Signature extends Equatable { /// Base 58 encoding of this using 'edsig' prefix. String get edsig { return crypto.catchUnhandledErrors(() { - return crypto.encodeWithPrefix(prefix: crypto.Prefixes.edsig, bytes: signedBytes); + return crypto.encodeWithPrefix( + prefix: crypto.Prefixes.edsig, + bytes: Uint8List.fromList(signedBytes.toList())); }); } diff --git a/pubspec.yaml b/pubspec.yaml index 70ba138b..6e7f6131 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ dependencies: convert: ^3.0.0 # Encoders and decoders for converting between different data representations dio: ^4.0.0 # http client equatable: ^2.0.0 - pinenacl: ^0.2.0 # Dart implementation of TweetNacl + pinenacl: ^0.3.3 # Dart implementation of TweetNacl json_serializable: ^4.1.0 # code generation for toJson and fromJson methods json_annotation: ^4.0.1 # Classes and helper functions that support JSON code generation via the `json_serializable` package. retry: ^3.1.0 # retry a function if an error happens From c2e683af4b7214497e53610fdc8288535633024e Mon Sep 17 00:00:00 2001 From: Guillaume Lavignotte Date: Thu, 7 Oct 2021 12:10:48 +0200 Subject: [PATCH 2/3] fix(lint): apply dartfmt --- lib/src/contracts/impl/contract.dart | 2 +- lib/src/core/client/impl/tezart_client.dart | 26 ++++++++--------- .../impl/encrypted_secret_key_to_seed.dart | 3 +- lib/src/keystore/impl/keystore.dart | 15 ++++------ .../impl/address_decoder.dart | 4 ++- .../impl/option_encoder.dart | 2 +- .../micheline_encoder/impl/pair_encoder.dart | 2 +- lib/src/signature/impl/signature.dart | 29 +++++-------------- 8 files changed, 32 insertions(+), 51 deletions(-) diff --git a/lib/src/contracts/impl/contract.dart b/lib/src/contracts/impl/contract.dart index b0aeef87..51fec526 100644 --- a/lib/src/contracts/impl/contract.dart +++ b/lib/src/contracts/impl/contract.dart @@ -45,7 +45,7 @@ class Contract { /// - when [params] are incompatible with the entrypoint signature, a [TypeError] is thrown Future callOperation({ String entrypoint = 'default', - dynamic? params, + dynamic params, required Keystore source, int amount = 0, int? customFee, diff --git a/lib/src/core/client/impl/tezart_client.dart b/lib/src/core/client/impl/tezart_client.dart index 72a53176..40cd5ecd 100644 --- a/lib/src/core/client/impl/tezart_client.dart +++ b/lib/src/core/client/impl/tezart_client.dart @@ -51,20 +51,18 @@ class TezartClient { bool reveal = true, }) async { return _catchHttpError(() async { - log.info( - 'request transfer $amount µtz from $source.address to the destination $destination'); - - final operationsList = - OperationsList(source: source, rpcInterface: rpcInterface) - ..appendOperation( - TransactionOperation( - amount: amount, - destination: destination, - customFee: customFee, - customGasLimit: customGasLimit, - customStorageLimit: customStorageLimit, - ), - ); + log.info('request transfer $amount µtz from $source.address to the destination $destination'); + + final operationsList = OperationsList(source: source, rpcInterface: rpcInterface) + ..appendOperation( + TransactionOperation( + amount: amount, + destination: destination, + customFee: customFee, + customGasLimit: customGasLimit, + customStorageLimit: customStorageLimit, + ), + ); if (reveal) { await _prependRevealIfNotRevealed( operationsList, diff --git a/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart b/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart index 2f5d1be5..9116eeab 100644 --- a/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart +++ b/lib/src/crypto/impl/encrypted_secret_key_to_seed.dart @@ -23,8 +23,7 @@ String encryptedSecretKeyToSeed({ ); final secretbox = SecretBox(encryptionKey); - final decryptedBytes = - secretbox.decrypt(ByteList.fromList(secretKeyBytes), nonce: nonce); + final decryptedBytes = secretbox.decrypt(ByteList.fromList(secretKeyBytes), nonce: nonce); final seed = crypto.encodeWithPrefix( prefix: Prefixes.edsk2, diff --git a/lib/src/keystore/impl/keystore.dart b/lib/src/keystore/impl/keystore.dart index 4e16b75f..e9f2c6a1 100644 --- a/lib/src/keystore/impl/keystore.dart +++ b/lib/src/keystore/impl/keystore.dart @@ -43,8 +43,7 @@ class Keystore extends Equatable { factory Keystore.fromSecretKey(String secretKey) { return crypto.catchUnhandledErrors(() { if (secretKey.length != _secretKeyLength) { - throw crypto.CryptoError( - type: crypto.CryptoErrorTypes.secretKeyLengthError); + throw crypto.CryptoError(type: crypto.CryptoErrorTypes.secretKeyLengthError); } _validateChecksum(secretKey); @@ -80,14 +79,11 @@ class Keystore extends Equatable { /// ``` /// /// Throws [CryptoError] if [mnemonic] is invalid. - factory Keystore.fromMnemonic(String mnemonic, - {String email = '', String password = ''}) { + factory Keystore.fromMnemonic(String mnemonic, {String email = '', String password = ''}) { return crypto.catchUnhandledErrors(() { final passphrase = '$email$password'; - final seedBytes = - crypto.seedBytesFromMnemonic(mnemonic, passphrase: passphrase); - final seed = - crypto.encodeWithPrefix(prefix: _seedPrefix, bytes: seedBytes); + final seedBytes = crypto.seedBytesFromMnemonic(mnemonic, passphrase: passphrase); + final seed = crypto.encodeWithPrefix(prefix: _seedPrefix, bytes: seedBytes); final secretKey = crypto.seedToSecretKey(seed); return Keystore._( @@ -103,8 +99,7 @@ class Keystore extends Equatable { ) { return crypto.catchUnhandledErrors(() { if (encryptedSecretKey.length != _encryptedSecretKeyLength) { - throw crypto.CryptoError( - type: crypto.CryptoErrorTypes.encryptedSecretKeyLengthError); + throw crypto.CryptoError(type: crypto.CryptoErrorTypes.encryptedSecretKeyLengthError); } final seed = crypto.encryptedSecretKeyToSeed( diff --git a/lib/src/micheline_decoder/impl/address_decoder.dart b/lib/src/micheline_decoder/impl/address_decoder.dart index 7f6ce84e..19aa6b49 100644 --- a/lib/src/micheline_decoder/impl/address_decoder.dart +++ b/lib/src/micheline_decoder/impl/address_decoder.dart @@ -26,7 +26,9 @@ class AddressDecoder implements MichelineDecoder { prefix = Prefixes.tz2; } else if (stringBytes.startsWith('0002')) { prefix = Prefixes.tz3; - } else if (stringBytes.startsWith('01')) prefix = Prefixes.KT; + } else if (stringBytes.startsWith('01')) { + prefix = Prefixes.KT; + } return prefix == null ? data['bytes'] : encodeWithPrefix(prefix: prefix, bytes: bytes); } diff --git a/lib/src/micheline_encoder/impl/option_encoder.dart b/lib/src/micheline_encoder/impl/option_encoder.dart index 2ac52be5..217c55da 100644 --- a/lib/src/micheline_encoder/impl/option_encoder.dart +++ b/lib/src/micheline_encoder/impl/option_encoder.dart @@ -2,7 +2,7 @@ import 'micheline_encoder.dart'; class OptionEncoder implements MichelineEncoder { @override - final dynamic? params; + final dynamic params; @override final Map type; diff --git a/lib/src/micheline_encoder/impl/pair_encoder.dart b/lib/src/micheline_encoder/impl/pair_encoder.dart index 5a1ba141..55deb987 100644 --- a/lib/src/micheline_encoder/impl/pair_encoder.dart +++ b/lib/src/micheline_encoder/impl/pair_encoder.dart @@ -33,7 +33,7 @@ class PairEncoder implements MichelineEncoder { throw TypeError(); } - dynamic? get _secondParams { + dynamic get _secondParams { if (params is Map) return params; if (params is List) { if (params.length > 2) return params.skip(1).toList(); diff --git a/lib/src/signature/impl/signature.dart b/lib/src/signature/impl/signature.dart index 64c8058d..f61bdfd9 100644 --- a/lib/src/signature/impl/signature.dart +++ b/lib/src/signature/impl/signature.dart @@ -38,10 +38,7 @@ class Signature extends Equatable { /// A factory that computes the signature of [bytes] (prefixed by [watermark]) using [keystore]. /// /// [watermark] is optional and will be ignored if missing. - factory Signature.fromBytes( - {required Uint8List bytes, - required Keystore keystore, - Watermarks? watermark}) { + factory Signature.fromBytes({required Uint8List bytes, required Keystore keystore, Watermarks? watermark}) { return Signature._(bytes: bytes, watermark: watermark, keystore: keystore); } @@ -51,33 +48,25 @@ class Signature extends Equatable { /// Throws a [CryptoError] if : /// - [data] is not hexadecimal /// - [data] length is odd (because it must be the hexadecimal of a list of bytes (a single byte represent two hexadecimal digits)) - factory Signature.fromHex( - {required String data, - required Keystore keystore, - Watermarks? watermark}) { + factory Signature.fromHex({required String data, required Keystore keystore, Watermarks? watermark}) { return crypto.catchUnhandledErrors(() { HexValidator(data).validate(); // Because two hexadecimal digits correspond to a single byte, this will throw an error if the length of the data is odd if (data.length.isOdd) { - throw crypto.CryptoError( - type: crypto.CryptoErrorTypes.invalidHexDataLength); + throw crypto.CryptoError(type: crypto.CryptoErrorTypes.invalidHexDataLength); } var bytes = crypto.hexDecode(data); - return Signature.fromBytes( - bytes: bytes, keystore: keystore, watermark: watermark); + return Signature.fromBytes(bytes: bytes, keystore: keystore, watermark: watermark); }); } /// Signed bytes of this. ByteList get signedBytes { return crypto.catchUnhandledErrors(() { - final watermarkedBytes = watermark == null - ? bytes - : Uint8List.fromList( - crypto.hexDecode(_watermarkToHex[watermark]!) + bytes); - var hashedBytes = - crypto.hashWithDigestSize(size: 256, bytes: watermarkedBytes); + final watermarkedBytes = + 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); @@ -88,9 +77,7 @@ class Signature extends Equatable { /// Base 58 encoding of this using 'edsig' prefix. String get edsig { return crypto.catchUnhandledErrors(() { - return crypto.encodeWithPrefix( - prefix: crypto.Prefixes.edsig, - bytes: Uint8List.fromList(signedBytes.toList())); + return crypto.encodeWithPrefix(prefix: crypto.Prefixes.edsig, bytes: Uint8List.fromList(signedBytes.toList())); }); } From a4be29b5da62422a5d928ef0e83d7875cf4c098d Mon Sep 17 00:00:00 2001 From: Guillaume Lavignotte Date: Thu, 7 Oct 2021 12:24:30 +0200 Subject: [PATCH 3/3] chore: upgrade dart sdk version for ci --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c1531918..15af5410 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - sdk: [2.12.4] + sdk: [2.14.3] services: flextesa: