getPrivateKey method

Future<String> getPrivateKey(
  1. String hdPath, [
  2. KeyType keyType = KeyType.secp256k1
])

Returns the private key as hex-String associated with hdPath.

Implementation

Future<String> getPrivateKey(String hdPath,
    [KeyType keyType = KeyType.secp256k1]) async {
  if (keyType == KeyType.secp256k1) {
    var master = BIP32.fromSeed(_keyBox!.get('seed'));
    var key = master.derivePath(hdPath);
    return bytesToHex(key.privateKey!);
  } else if (keyType == KeyType.ed25519) {
    var key = await ED25519_HD_KEY.derivePath(
        hdPath, _keyBox!.get('seed').toList());
    return bytesToHex(ed.newKeyFromSeed(Uint8List.fromList(key.key)).bytes);
  } else if (keyType == KeyType.x25519) {
    var key = await ED25519_HD_KEY.derivePath(
        hdPath, _keyBox!.get('seed').toList());
    return bytesToHex(
        _edPrivateToXPrivate(ed.newKeyFromSeed(Uint8List.fromList(key.key))));
  } else if (keyType == KeyType.p384 ||
      keyType == KeyType.p256 ||
      keyType == KeyType.p521) {
    return hdPath;
  } else {
    throw Exception('Unknown KeyType');
  }
}