decodeAddr method

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

Overrides the base class method to decode a P2WPKH address.

This method decodes a P2WPKH address from the given input string using the Bech32 encoding. It expects an optional map of keyword arguments, with the 'hrp' key specifying the Human-Readable Part (HRP) of the address. It validates the arguments, decodes the address, checks the witness version, and returns the decoded address as a List

Parameters:

  • addr: The Bech32-encoded P2WPKH address to be decoded.
  • kwargs: Optional keyword arguments, with 'hrp' for the Human-Readable Part.

Returns: A List

Throws:

  • FormatException if the provided address has an incorrect witness version.
  • ArgumentException if the Bech32 checksum is invalid.

Implementation

@override
List<int> decodeAddr(String addr, [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'];

  /// Decode the Bech32-encoded P2WPKH address, and validate its length.
  final decoded = SegwitBech32Decoder.decode(hrp, addr);
  final witVerGot = decoded.item1;
  final addrDecBytes = decoded.item2;

  /// Check the witness version.
  if (witVerGot != P2WPKHAddrConst.witnessVer) {
    throw AddressConverterException(
        'Invalid witness version (expected ${P2WPKHAddrConst.witnessVer}, got $witVerGot)');
  }

  /// Return the decoded P2WPKH address as a List<int>.
  return List<int>.from(addrDecBytes);
}