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 P2SH (Pay-to-Script-Hash) address.

This method encodes a public key as a P2SH address using Base58 encoding. It expects an optional map of keyword arguments, with the 'net_ver' key specifying the network version bytes for the address. It validates the arguments, processes the public key as a Secp256k1 key, generates a script signature, and combines it with the network version bytes to create the P2SH address, which is then Base58-encoded.

Parameters:

  • pubKey: The public key to be encoded as a P2SH address.
  • kwargs: Optional keyword arguments, with 'net_ver' for the network version bytes.

Returns: A String representing the Base58-encoded P2SH address derived from the provided public key.

Implementation

@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
  /// Validate network version arguments and retrieve the network version bytes.
  AddrKeyValidator.validateAddressArgs<List<int>>(kwargs, 'net_ver');
  List<int> netVerBytes = kwargs['net_ver'];

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

  /// Generate the script signature from the public key.
  List<int> scriptSigBytes = _P2SHAddrUtils.addScriptSig(pubKeyObj);

  /// Combine the network version and script signature to form the address bytes.
  List<int> addressBytes =
      List<int>.filled(netVerBytes.length + scriptSigBytes.length, 0);
  addressBytes.setAll(0, netVerBytes);
  addressBytes.setAll(netVerBytes.length, scriptSigBytes);

  /// Encode the address bytes as a Base58-encoded P2SH address.
  return Base58Encoder.checkEncode(addressBytes);
}