derivePrivateKey function

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

Implementation

Uint8List derivePrivateKey(dynamic seed, int index) {
  if (seed is String) {
    if (isHex(seed)) {
      seed = Uint8List.fromList(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 = toByteArray(index, length: 4);
  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;
}