derivePrivateKey function
Uint8List
derivePrivateKey(
- dynamic seed,
- int index, {
- bool isSeedHexa = true,
})
Implementation
Uint8List derivePrivateKey(dynamic seed, int index, {bool isSeedHexa = true}) {
if (seed is String) {
if (isSeedHexa && !isHex(seed)) {
throw const FormatException("'seed' must be an hexadecimal string");
}
if (isSeedHexa) {
seed = Uint8List.fromList(hexToUint8List(seed));
} else {
seed = Uint8List.fromList(utf8.encode(seed));
}
}
/// Derive master keys
final sha512 = Digest('SHA-512');
final buf = sha512.process(seed);
final masterKey = buf.sublist(0, 32);
final masterEntropy = buf.sublist(32, 64);
/// Derive the final seed
final hmac = crypto.Hmac(crypto.sha512, masterEntropy);
final indexBuf = toByteArray(index, length: 4);
final extendedSeed = concatUint8List(<Uint8List>[masterKey, indexBuf]);
final digest = hmac.convert(extendedSeed);
final hmacBuf = Uint8List.fromList(digest.bytes.sublist(0, 32));
return hmacBuf;
}