Skip to content

Commit

Permalink
dart analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann committed Jul 10, 2023
1 parent 3401e42 commit dc70ebc
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 128 deletions.
25 changes: 15 additions & 10 deletions packages/substrate_bip39/lib/crypto_scheme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ abstract class CryptoScheme {
};
final entropy = wordsToEntropy[words];
if (entropy == null) {
throw SecretStringException.invalidEntropy('Invalid number of words given for phrase, must be 12/15/18/21/24');
throw SecretStringException.invalidEntropy(
'Invalid number of words given for phrase, must be 12/15/18/21/24');
}
return Mnemonic.generate(Language.english, entropyLength: entropy);
}
Expand Down Expand Up @@ -76,7 +77,8 @@ abstract class CryptoScheme {
}

/// Derive a child key from a series of given junctions.
Future<Uint8List> derive(List<int> seed, Iterable<DeriveJunction> path, {Uint8List? output});
Future<Uint8List> derive(List<int> seed, Iterable<DeriveJunction> path,
{Uint8List? output});

/// Returns the 32 bytes seed from the English BIP39 seed `phrase`
///
Expand All @@ -85,12 +87,13 @@ abstract class CryptoScheme {
Future<List<int>> seedFromUri(String uri, {String? password}) async {
try {
final entropy = Mnemonic.fromSentence(uri, Language.english).entropy;
final seed = await CryptoScheme.seedFromEntropy(entropy, password: password);
final seed =
await CryptoScheme.seedFromEntropy(entropy, password: password);
return seed.sublist(0, 32);
// ignore: empty_catches
} catch (e) {}

SecretUri secretUri = SecretUri.fromStr(uri);
final secretUri = SecretUri.fromStr(uri);

// The phrase is hex encoded secret seed
if (secretUri.phrase.startsWith('0x')) {
Expand All @@ -101,15 +104,17 @@ abstract class CryptoScheme {
}
}

String? passwordOverride = password ?? secretUri.password;
final entropy;
final passwordOverride = password ?? secretUri.password;
final List<int> entropy;
try {
entropy = Mnemonic.fromSentence(secretUri.phrase, Language.english).entropy;
} on Exception catch(e) {
entropy =
Mnemonic.fromSentence(secretUri.phrase, Language.english).entropy;
} on Exception catch (e) {
throw SecretStringException.fromException(e);
}

List<int> seed = await CryptoScheme.seedFromEntropy(entropy, password: passwordOverride);

List<int> seed =
await CryptoScheme.seedFromEntropy(entropy, password: passwordOverride);
seed = seed.sublist(0, 32);

return await derive(seed, secretUri.junctions);
Expand Down
72 changes: 45 additions & 27 deletions packages/substrate_bip39/lib/exceptions.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
enum SecretStringError {
/// The overall format was invalid (e.g. the seed phrase contained symbols).
invalidFormat('Invalid format', 'The overall format was invalid (e.g. the seed phrase contained symbols).'),
invalidFormat('Invalid format',
'The overall format was invalid (e.g. the seed phrase contained symbols).'),

/// The seed phrase provided is not a valid BIP39 phrase.
invalidPhrase('Invalid phrase', 'The seed phrase provided is not a valid BIP39 phrase.'),
invalidPhrase('Invalid phrase',
'The seed phrase provided is not a valid BIP39 phrase.'),

/// The supplied password was invalid.
invalidPassword('Invalid password', 'The supplied password was invalid.'),
/// The seed is invalid (bad content).
invalidSeed('Invalid seed', 'The seed is invalid (bad content).'),
/// The seed has an invalid length.
invalidSeedLength('Invalid seed length', 'The seed has an invalid length.'),
/// The derivation path was invalid (e.g. contains soft junctions when they are not supported).
invalidPath('Invalid path', 'The derivation path was invalid (e.g. contains soft junctions when they are not supported).'),
invalidPassword('Invalid password', 'The supplied password was invalid.'),

/// The seed is invalid (bad content).
invalidSeed('Invalid seed', 'The seed is invalid (bad content).'),

/// The seed has an invalid length.
invalidSeedLength('Invalid seed length', 'The seed has an invalid length.'),

/// The derivation path was invalid (e.g. contains soft junctions when they are not supported).
invalidPath('Invalid path',
'The derivation path was invalid (e.g. contains soft junctions when they are not supported).'),

/// The entropy byte length was invalid
invalidEntropy('Invalid Entropy', 'entropy byte length must be between 16 and 32 and multiple of 4');
invalidEntropy('Invalid Entropy',
'entropy byte length must be between 16 and 32 and multiple of 4');

const SecretStringError(this.message, this.description);

final String message;
Expand All @@ -23,26 +34,33 @@ class SecretStringException implements Exception {
final SecretStringError cause;
final String? description;

factory SecretStringException.invalidFormat([ String? description ]) => SecretStringException(SecretStringError.invalidFormat, description);
factory SecretStringException.invalidPhrase([ String? description ]) => SecretStringException(SecretStringError.invalidPhrase, description);
factory SecretStringException.invalidPassword([ String? description ]) => SecretStringException(SecretStringError.invalidPassword, description);
factory SecretStringException.invalidSeed([ String? description ]) => SecretStringException(SecretStringError.invalidSeed, description);
factory SecretStringException.invalidSeedLength([ String? description ]) => SecretStringException(SecretStringError.invalidSeedLength, description);
factory SecretStringException.invalidPath([ String? description ]) => SecretStringException(SecretStringError.invalidPath, description);
factory SecretStringException.invalidEntropy([ String? description ]) => SecretStringException(SecretStringError.invalidEntropy, description);
factory SecretStringException.invalidFormat([String? description]) =>
SecretStringException(SecretStringError.invalidFormat, description);
factory SecretStringException.invalidPhrase([String? description]) =>
SecretStringException(SecretStringError.invalidPhrase, description);
factory SecretStringException.invalidPassword([String? description]) =>
SecretStringException(SecretStringError.invalidPassword, description);
factory SecretStringException.invalidSeed([String? description]) =>
SecretStringException(SecretStringError.invalidSeed, description);
factory SecretStringException.invalidSeedLength([String? description]) =>
SecretStringException(SecretStringError.invalidSeedLength, description);
factory SecretStringException.invalidPath([String? description]) =>
SecretStringException(SecretStringError.invalidPath, description);
factory SecretStringException.invalidEntropy([String? description]) =>
SecretStringException(SecretStringError.invalidEntropy, description);

const SecretStringException(this.cause, [ this.description ]);
const SecretStringException(this.cause, [this.description]);

factory SecretStringException.fromException(Exception e) {
final message = e.toString();
if (message.contains('mnemonic: unexpected sentence length')) {
return SecretStringException.invalidSeedLength();
} else if (message.contains('does not exist in english')) {
return SecretStringException.invalidPhrase();
} else if (message.contains('mnemonic: invalid checksum')) {
return SecretStringException.invalidSeed();
}
return SecretStringException.invalidFormat(message);
final message = e.toString();
if (message.contains('mnemonic: unexpected sentence length')) {
return SecretStringException.invalidSeedLength();
} else if (message.contains('does not exist in english')) {
return SecretStringException.invalidPhrase();
} else if (message.contains('mnemonic: invalid checksum')) {
return SecretStringException.invalidSeed();
}
return SecretStringException.invalidFormat(message);
}

@override
Expand Down
7 changes: 3 additions & 4 deletions packages/substrate_bip39/lib/schemes/ed25519.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import '../crypto_scheme.dart' show CryptoScheme;
import '../secret_uri.dart' show DeriveJunction;

class Ed25519 extends CryptoScheme {
const Ed25519() : super(32);

const Ed25519(): super(32);

/// Derive a child key from a series of given junctions.
///
/// Reference: https://github.com/paritytech/substrate/blob/polkadot-v0.9.43/primitives/core/src/ed25519.rs#L385-L399
Expand All @@ -19,7 +18,8 @@ class Ed25519 extends CryptoScheme {
output ??= Uint8List.fromList(seed);
for (final junction in path) {
if (junction.isSoft) {
throw SecretStringException.invalidPath('Soft key derivation is not supported for ED25519');
throw SecretStringException.invalidPath(
'Soft key derivation is not supported for ED25519');
}
deriveHardJunction(output, junction.junctionId, output: output);
}
Expand All @@ -41,4 +41,3 @@ class Ed25519 extends CryptoScheme {
return output;
}
}

10 changes: 6 additions & 4 deletions packages/substrate_bip39/lib/secret_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class DeriveJunction {

final n = BigInt.tryParse(code, radix: 10);
final Uint8List bytes;
if (n != null && n >= BigInt.zero && n < BigInt.parse('18446744073709551616')) {
if (n != null &&
n >= BigInt.zero &&
n < BigInt.parse('18446744073709551616')) {
// number
bytes = U64Codec.codec.encode(n);
} else {
Expand Down Expand Up @@ -103,11 +105,11 @@ class DeriveJunction {
/// Notably, integer junction indices may be legally prefixed with arbitrary number of zeros.
/// Similarly an empty password (ending the `SURI` with `///`) is perfectly valid and will
/// generally be equivalent to no password at all.
///
///
class SecretUri {
/// The phrase to derive the private key.
///
/// This can either be a 64-bit hex string or a BIP-39 key phrase.
///
/// This can either be a 64-bit hex string or a BIP-39 key phrase.
final String phrase;

/// Optional password as given as part of the uri.
Expand Down
3 changes: 2 additions & 1 deletion packages/substrate_bip39/test/secret_uri_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:substrate_bip39/substrate_bip39.dart' show SecretUri, DeriveJunction;
import 'package:substrate_bip39/substrate_bip39.dart'
show SecretUri, DeriveJunction;
import 'package:test/test.dart';

void main() {
Expand Down
Loading

0 comments on commit dc70ebc

Please sign in to comment.