encodeKey method
Overrides the base class method to encode a public key as a P2TR (Pay-to-Taproot) address.
This method encodes a public key as a P2TR 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, tweaks the public key, and encodes it as a P2TR address.
Parameters:
- pubKey: The public key to be encoded as a P2TR address.
- kwargs: Optional keyword arguments, with 'hrp' for the Human-Readable Part.
Returns: A String representing the P2TR 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 String hrp = kwargs["hrp"];
/// Validate and process the public key as a Secp256k1 key.
final pubKeyObj = AddrKeyValidator.validateAndGetSecp256k1Key(pubKey);
/// Tweak the public key to create a P2TR address.
final tweakedPubKey = BigintUtils.toBytes(
P2TRUtils.tweakPublicKey(pubKeyObj.point as ProjectiveECCPoint).x,
length: Curves.curveSecp256k1.baselen);
/// Encode the tweaked public key as a P2TR address using Bech32.
return SegwitBech32Encoder.encode(hrp, P2TRConst.witnessVer, tweakedPubKey);
}