Skip to content

Commit

Permalink
Merge pull request #3 from andromeda911/main
Browse files Browse the repository at this point in the history
feat: add "entropyLength" parameter for Mnemonic.generate
  • Loading branch information
ethicnology authored Nov 14, 2022
2 parents 55ba3dc + f6d99ab commit 24b59b5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

steps:
- name: Checkout the code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install and set Flutter version
uses: subosito/flutter-action@v2
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@
- isValid function formats input (NFKD)

## 3.0.4
- Mnemonic.fromSentence formats (NFKD) each words before checking their validity
- Mnemonic.fromSentence formats (NFKD) each words before checking their validity

## 3.0.5
- Mnemonic.generate has a new entropyLength params
- Update dependencies
- Update github actions
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ void main() {
// DO NOT USE THESE DATA!
// Constructs Mnemonic from random secure 256bits entropy with optional passphrase
var mnemonic =
Mnemonic.generate(Language.french, passphrase: "SomethingR0bùst");
var mnemonic = Mnemonic.generate(
Language.french,
passphrase: "SomethingR0bùst",
entropyLength: 256,
);
print(mnemonic.sentence);
// million alpaga revivre calmer dogme verdure capsule folie déborder facette lanceur saboter recycler tripler symbole savant rieur jeudi outrager volume situer jardin civil reculer
String hexSeed = hex.encode(mnemonic.seed); // convert to hex
Expand Down
7 changes: 5 additions & 2 deletions example/bip39_mnemonic_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ void main() {
// DO NOT USE THESE DATA!

// Constructs Mnemonic from random secure 256bits entropy with optional passphrase
var mnemonic =
Mnemonic.generate(Language.french, passphrase: "SomethingR0bùst");
var mnemonic = Mnemonic.generate(
Language.french,
passphrase: "SomethingR0bùst",
entropyLength: 256,
);
print(mnemonic.sentence);
// million alpaga revivre calmer dogme verdure capsule folie déborder facette lanceur saboter recycler tripler symbole savant rieur jeudi outrager volume situer jardin civil reculer
String hexSeed = hex.encode(mnemonic.seed); // convert to hex
Expand Down
17 changes: 14 additions & 3 deletions lib/src/mnemonic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,21 @@ class Mnemonic {
}
}

/// Constructs Mnemonic from random secure 256 bits entropy.
Mnemonic.generate(this.language, {this.passphrase = ""}) {
/// Constructs Mnemonic from one of these entropy lengths: [128, 160, 192, 224, 256]
Mnemonic.generate(
this.language, {
this.passphrase = "",
int entropyLength = 256,
}) {
if (![128, 160, 192, 224, 256].contains(entropyLength)) {
throw Exception(
"mnemonic: unexpected entropy length, choose one of: [128, 160, 192, 224, 256]");
}
var random = Random.secure();
entropy = List<int>.generate(32, (i) => random.nextInt(256));
entropy = List<int>.generate(
entropyLength ~/ 8,
(i) => random.nextInt(256),
);
}

/// Constructs Mnemonic from a sentence by retrieving the original entropy.
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: bip39_mnemonic
description: Mnemonic code for generating deterministic keys a.k.a. BIP39.
version: 3.0.4
version: 3.0.5
homepage: https://github.com/ethicnology/dart-bip39-mnemonic

environment:
sdk: '>=2.17.0 <3.0.0'
dev_dependencies:
lints: ^2.0.0
test: ^1.21.3
convert: ^3.0.2
lints: ^2.0.1
test: ^1.22.0
convert: ^3.1.1
dependencies:
pointycastle: ^3.6.0
pointycastle: ^3.6.2
unorm_dart: ^0.2.0
7 changes: 7 additions & 0 deletions test/mnemonic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ void main() {
);
});

test('Mnemonic.generate from invalid entropy length', () {
expect(
() => Mnemonic.generate(Language.french, entropyLength: 64),
throwsException,
);
});

test('Mnemonic.fromSentence foreign word', () {
String sentence =
"esperanto 가격 가격 가격 가격 가격 가격 가격 가격 가격 가격 가능";
Expand Down

0 comments on commit 24b59b5

Please sign in to comment.