encodeKey method

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

Overrides the base class method to encode a public key as a P2WPKH address.

This method encodes a public key as a Pay-to-Witness-Public-Key-Hash (P2WPKH) address using Bech32 encoding. It expects an optional map of keyword arguments, with the 'hrp' key specifying the Human-Readable Part (HRP) for the address. It validates the arguments, processes the public key as a Secp256k1 key, and encodes it as a P2WPKH address.

Parameters:

  • pubKey: The public key to be encoded as a P2WPKH address.
  • kwargs: Optional keyword arguments, with 'hrp' for the Human-Readable Part.

Returns: A String representing the P2WPKH address encoded from the provided public key.

Implementation

@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
  /// Validate address arguments and retrieve the Human-Readable Part (HRP).
  AddrKeyValidator.validateAddressArgs<String>(kwargs, "hrp");
  final hrp = kwargs['hrp'] as String;

  /// Validate and process the public key as a Secp256k1 key.
  final pubKeyObj = AddrKeyValidator.validateAndGetSecp256k1Key(pubKey);

  /// Set the witness version and obtain the public key bytes.
  const witnessVer = P2WPKHAddrConst.witnessVer;
  final pubKeyBytes = pubKeyObj.compressed;

  /// Encode the processed public key as a P2WPKH address using Bech32.
  return SegwitBech32Encoder.encode(
      hrp, witnessVer, QuickCrypto.hash160(pubKeyBytes));
}