encodeKey method

  1. @override
String encodeKey(
  1. List<int> pubKey, [
  2. Map<String, dynamic> kwargs = const {}
])
override

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:

  1. Validate the public key and extract its raw compressed bytes.
  2. Prepare the payload by appending a single-sig suffix byte.
  3. Compute the SHA-3-256 hash of the payload.
  4. Concatenate the address prefix and the hash bytes.
  5. 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+'), '');
}