encodeKey method
Encode an Aptos blockchain address from a public key.
This method encodes an Aptos blockchain address from the provided pubKey
by performing the following steps:
- Validate the public key and extract its raw compressed bytes.
- Prepare the payload by appending a single-sig suffix byte.
- Compute the SHA-3-256 hash of the payload.
- Concatenate the address prefix and the hash bytes.
- Remove leading zeros from the resulting hex-encoded address.
Parameters:
pubKey
: The public key for which to generate the address.kwargs
(optional): Additional arguments, though none are used in this method.
Returns:
- A hex-encoded string representing the generated Aptos blockchain address.
This method is used to create an Aptos blockchain address from a public key. The resulting address is a hexadecimal string without leading zeros.
Implementation
@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
final pubKeyBytes = pubKey;
final pubKeyObj = AddrKeyValidator.validateAndGetEd25519Key(pubKeyBytes);
/// Prepare the payload by appending a single-sig suffix byte
final payloadBytes = List<int>.from([
...List<int>.from(pubKeyObj.compressed.sublist(1)),
...AptosAddrConst.singleSigSuffixByte
]);
/// Compute the SHA-3-256 hash of the payload
final keyHashBytes = QuickCrypto.sha3256Hash(payloadBytes);
/// Concatenate the address prefix and the hash bytes, removing leading zeros
return CoinsConf.aptos.params.addrPrefix! +
BytesUtils.toHexString(keyHashBytes).replaceFirst(RegExp('^0+'), '');
}