encodeKey method
Encodes a public key as a Tezos (XTZ) blockchain address using the specified prefix.
This method takes a public key in the form of a List<int> and optional keyword arguments, including the "prefix" specifying the address prefix to use for encoding. It first validates the public key, then derives the address by hashing and prepending the prefix. The resulting address is returned as a string.
Throws an exception if the public key is not in the expected format or if the prefix is invalid.
Parameters:
pubKey
: The public key as a List<int> to encode.kwargs
: Optional keyword arguments, including "prefix" to specify the address prefix.
Returns: A string representing the encoded Tezos address.
Example usage:
final encoder = XtzAddrEncoder();
final publicKey = List<int>.from([0x03, 0x7f, 0x12, ...]);
final address = encoder.encodeKey(publicKey, {"prefix": XtzAddrPrefixes.tz1});
Implementation
@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
/// Validate and retrieve the address prefix from the keyword arguments.
AddrKeyValidator.validateAddressArgs<XtzAddrPrefixes>(kwargs, "prefix");
final XtzAddrPrefixes prefix = kwargs["prefix"];
/// Validate the provided public key.
final pubKeyObj = AddrKeyValidator.validateAndGetEd25519Key(pubKey);
/// Derive the address by hashing and prepending the prefix.
final blakeBytes =
QuickCrypto.blake2b160Hash(pubKeyObj.compressed.sublist(1));
/// Encode the address using base58 and the specified prefix.
return Base58Encoder.checkEncode(
List<int>.from([...prefix.value, ...blakeBytes]));
}