getPublicKey method

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

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

Implementation

Future<String> getPublicKey(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.publicKey);
  } else if (keyType == KeyType.ed25519) {
    var key = await ED25519_HD_KEY.derivePath(
        hdPath, _keyBox!.get('seed').toList());
    return bytesToHex(
        ed.public(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(
        _edPrivateToXPublic(ed.newKeyFromSeed(Uint8List.fromList(key.key))));
  } else if (keyType == KeyType.p384 ||
      keyType == KeyType.p256 ||
      keyType == KeyType.p521) {
    Curve c;
    if (keyType == KeyType.p521) {
      c = getP521();
    } else if (keyType == KeyType.p384) {
      c = getP384();
    } else {
      c = getP256();
    }
    var k = PrivateKey(c, hexToInt(hdPath));
    return k.publicKey.toHex();
  } else {
    throw Exception('Unknown KeyType');
  }
}