decodeAddr method

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

Decodes an Ergo address into its respective public key bytes.

addr: The Ergo address to decode. kwargs: Optional parameters.

  • net_type: The network type for the Ergo address (mainnet or testnet).

Returns a List

Implementation

@override
List<int> decodeAddr(String addr, [Map<String, dynamic> kwargs = const {}]) {
  final netType = kwargs['net_type'] ?? ErgoNetworkTypes.mainnet;

  /// Validate network type
  if (netType is! ErgoNetworkTypes) {
    throw const AddressConverterException(
        'Address type is not an enumerative of ErgoNetworkTypes');
  }
  final addrDecBytes = Base58Decoder.decode(addr);
  AddrDecUtils.validateBytesLength(
      addrDecBytes,
      EcdsaKeysConst.pubKeyCompressedByteLen +
          ErgoAddrConst.checksumByteLen +
          1);

  final decode = AddrDecUtils.splitPartsByChecksum(
      addrDecBytes, ErgoAddrConst.checksumByteLen);

  /// Extract checksum and public key bytes
  final addrWithPrefix = decode.item1;
  final checksumBytes = decode.item2;

  /// Validate checksum
  AddrDecUtils.validateChecksum(addrWithPrefix, checksumBytes,
      (pubKeyBytes) => _ErgoAddrUtils.computeChecksum(pubKeyBytes));

  /// Extract public key bytes and remove the prefix
  final pubKeyBytes = AddrDecUtils.validateAndRemovePrefixBytes(
      addrWithPrefix,
      _ErgoAddrUtils.encodePrefix(ErgoAddressTypes.p2pkh, netType));

  return pubKeyBytes;
}