diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index 0388df6..80852e7 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 074f70f..45d4750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,4 +49,9 @@ - isValid function formats input (NFKD) ## 3.0.4 -- Mnemonic.fromSentence formats (NFKD) each words before checking their validity \ No newline at end of file +- 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 \ No newline at end of file diff --git a/README.md b/README.md index 64b9fb5..e5f48a6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/example/bip39_mnemonic_example.dart b/example/bip39_mnemonic_example.dart index 12cc25e..27ab57b 100644 --- a/example/bip39_mnemonic_example.dart +++ b/example/bip39_mnemonic_example.dart @@ -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 diff --git a/lib/src/mnemonic.dart b/lib/src/mnemonic.dart index f0c34f8..dec9a5b 100644 --- a/lib/src/mnemonic.dart +++ b/lib/src/mnemonic.dart @@ -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.generate(32, (i) => random.nextInt(256)); + entropy = List.generate( + entropyLength ~/ 8, + (i) => random.nextInt(256), + ); } /// Constructs Mnemonic from a sentence by retrieving the original entropy. diff --git a/pubspec.yaml b/pubspec.yaml index 1614917..7e0b1ec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/mnemonic_test.dart b/test/mnemonic_test.dart index 9222bac..1da6b0c 100644 --- a/test/mnemonic_test.dart +++ b/test/mnemonic_test.dart @@ -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 가격 가격 가격 가격 가격 가격 가격 가격 가격 가격 가능";