EthereumAddress.fromPublicKey constructor
Creates an EthereumAddress from a public key.
The public key should be the uncompressed 64-byte public key (without the 0x04 prefix) or the 65-byte public key (with prefix).
Note: This requires keccak256 hashing which is in the crypto module. This factory is provided for API completeness but requires the keccak256 function to be passed in.
Implementation
factory EthereumAddress.fromPublicKey(
Uint8List publicKey,
Uint8List Function(Uint8List) keccak256,
) {
Uint8List key;
if (publicKey.length == 65 && publicKey[0] == 0x04) {
// Remove the 0x04 prefix
key = BytesUtils.slice(publicKey, 1);
} else if (publicKey.length == 64) {
key = publicKey;
} else {
throw InvalidAddressException(
HexUtils.encode(publicKey),
'Invalid public key length: ${publicKey.length}',
);
}
// Take the last 20 bytes of the keccak256 hash
final hash = keccak256(key);
return EthereumAddress(BytesUtils.slice(hash, 12));
}