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

applied lint recommendation from the Dart team #22

Open
wants to merge 1 commit into
base: master
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
12 changes: 6 additions & 6 deletions example/bip39_example.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:bip39/bip39.dart' as bip39;

main() async {
String randomMnemonic = bip39.generateMnemonic();
void main() async {
var randomMnemonic = bip39.generateMnemonic();
print(randomMnemonic);
String seed = bip39.mnemonicToSeedHex("update elbow source spin squeeze horror world become oak assist bomb nuclear");
var seed = bip39.mnemonicToSeedHex('update elbow source spin squeeze horror world become oak assist bomb nuclear');
print(seed);
String mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000');
var mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000');
print(mnemonic);
bool isValid = bip39.validateMnemonic(mnemonic);
var isValid = bip39.validateMnemonic(mnemonic);
print(isValid);
isValid = bip39.validateMnemonic('basket actual');
print(isValid);
String entropy = bip39.mnemonicToEntropy(mnemonic);
var entropy = bip39.mnemonicToEntropy(mnemonic);
print(entropy);
}
39 changes: 13 additions & 26 deletions lib/src/bip39_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const _INVALID_MNEMONIC = 'Invalid mnemonic';
const _INVALID_ENTROPY = 'Invalid entropy';
const _INVALID_CHECKSUM = 'Invalid mnemonic checksum';

typedef Uint8List RandomBytes(int size);
typedef RandomBytes = Uint8List Function(int size);

int _binaryToByte(String binary) {
return int.parse(binary, radix: 2);
Expand All @@ -22,14 +22,6 @@ String _bytesToBinary(Uint8List bytes) {
return bytes.map((byte) => byte.toRadixString(2).padLeft(8, '0')).join('');
}

//Uint8List _createUint8ListFromString( String s ) {
// var ret = new Uint8List(s.length);
// for( var i=0 ; i<s.length ; i++ ) {
// ret[i] = s.codeUnitAt(i);
// }
// return ret;
//}

String _deriveChecksumBits(Uint8List entropy) {
final ENT = entropy.length * 8;
final CS = ENT ~/ 32;
Expand Down Expand Up @@ -67,23 +59,23 @@ String entropyToMnemonic(String entropyString) {
final entropyBits = _bytesToBinary(entropy);
final checksumBits = _deriveChecksumBits(entropy);
final bits = entropyBits + checksumBits;
final regex = new RegExp(r".{1,11}", caseSensitive: false, multiLine: false);
final regex = RegExp(r'.{1,11}', caseSensitive: false, multiLine: false);
final chunks = regex
.allMatches(bits)
.map((match) => match.group(0)!)
.toList(growable: false);
List<String> wordlist = WORDLIST;
String words =
chunks.map((binary) => wordlist[_binaryToByte(binary)]).join(' ');
var wordlist = WORDLIST;
var words =
chunks.map((binary) => wordlist[_binaryToByte(binary)]).join(' ');
return words;
}

Uint8List mnemonicToSeed(String mnemonic, {String passphrase = ""}) {
final pbkdf2 = new PBKDF2();
Uint8List mnemonicToSeed(String mnemonic, {String passphrase = ''}) {
final pbkdf2 = PBKDF2();
return pbkdf2.process(mnemonic, passphrase: passphrase);
}

String mnemonicToSeedHex(String mnemonic, {String passphrase = ""}) {
String mnemonicToSeedHex(String mnemonic, {String passphrase = ''}) {
return mnemonicToSeed(mnemonic, passphrase: passphrase).map((byte) {
return byte.toRadixString(16).padLeft(2, '0');
}).join('');
Expand All @@ -101,14 +93,14 @@ bool validateMnemonic(String mnemonic) {
String mnemonicToEntropy(mnemonic) {
var words = mnemonic.split(' ');
if (words.length % 3 != 0) {
throw new ArgumentError(_INVALID_MNEMONIC);
throw ArgumentError(_INVALID_MNEMONIC);
}
final wordlist = WORDLIST;
// convert word indices to 11 bit binary strings
final bits = words.map((word) {
final index = wordlist.indexOf(word);
final index = wordlist.indexOf(word.toString());
if (index == -1) {
throw new ArgumentError(_INVALID_MNEMONIC);
throw ArgumentError(_INVALID_MNEMONIC);
}
return index.toRadixString(2).padLeft(11, '0');
}).join('');
Expand All @@ -118,9 +110,9 @@ String mnemonicToEntropy(mnemonic) {
final checksumBits = bits.substring(dividerIndex);

// calculate the checksum and compare
final regex = RegExp(r".{1,8}");
final regex = RegExp(r'.{1,8}');
final entropyBytes = Uint8List.fromList(regex
.allMatches(entropyBits)
.allMatches(entropyBits.toString())
.map((match) => _binaryToByte(match.group(0)!))
.toList(growable: false));
if (entropyBytes.length < 16) {
Expand All @@ -140,8 +132,3 @@ String mnemonicToEntropy(mnemonic) {
return byte.toRadixString(16).padLeft(2, '0');
}).join('');
}
// List<String>> _loadWordList() {
// final res = new Resource('package:bip39/src/wordlists/english.json').readAsString();
// List<String> words = (json.decode(res) as List).map((e) => e.toString()).toList();
// return words;
// }
17 changes: 8 additions & 9 deletions lib/src/utils/pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ class PBKDF2 {
final int blockLength;
final int iterationCount;
final int desiredKeyLength;
final String saltPrefix = "mnemonic";
final String saltPrefix = 'mnemonic';

PBKDF2KeyDerivator _derivator;
final PBKDF2KeyDerivator _derivator;

PBKDF2({
this.blockLength = 128,
this.iterationCount = 2048,
this.desiredKeyLength = 64,
}) : _derivator =
new PBKDF2KeyDerivator(new HMac(new SHA512Digest(), blockLength));
}) : _derivator = PBKDF2KeyDerivator(HMac(SHA512Digest(), blockLength));

Uint8List process(String mnemonic, {passphrase: ""}) {
final salt = Uint8List.fromList(utf8.encode(saltPrefix + passphrase));
Uint8List process(String mnemonic, {passphrase = ''}) {
final salt =
Uint8List.fromList(utf8.encode(saltPrefix + passphrase.toString()));
_derivator.reset();
_derivator
.init(new Pbkdf2Parameters(salt, iterationCount, desiredKeyLength));
return _derivator.process(new Uint8List.fromList(mnemonic.codeUnits));
_derivator.init(Pbkdf2Parameters(salt, iterationCount, desiredKeyLength));
return _derivator.process(Uint8List.fromList(mnemonic.codeUnits));
}
}
Loading