derivePrivateKey function

Uint8List derivePrivateKey(
  1. dynamic seed,
  2. int index
)

Implementation

Uint8List derivePrivateKey(seed, int index) {
  if (seed is String) {
    if (isHex(seed)) {
      seed = hexToUint8List(seed);
    } else {
      seed = Uint8List.fromList(utf8.encode(seed));
    }
  }

  /// Derive master keys
  final Digest sha512 = Digest('SHA-512');
  final Uint8List buf = sha512.process(seed);
  final Uint8List masterKey = buf.sublist(0, 32);
  final Uint8List masterEntropy = buf.sublist(32, 64);

  /// Derive the final seed
  final crypto.Hmac hmac = crypto.Hmac(crypto.sha512, masterEntropy);
  final Uint8List indexBuf = encodeInt32(index);
  final Uint8List extendedSeed =
      concatUint8List(<Uint8List>[masterKey, indexBuf]);
  final crypto.Digest digest = hmac.convert(extendedSeed);

  final Uint8List hmacBuf = Uint8List.fromList(digest.bytes.sublist(0, 32));

  return hmacBuf;
}