decodeAddr method

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

Decodes a given blockchain address string.

This method takes an address string and optional keyword arguments (kwargs) and decodes it into a List

  • addr: The blockchain address string to be decoded.
  • kwargs: Optional keyword arguments that can be used for configuration.

Returns a List

Implementation

@override
List<int> decodeAddr(String addr, [Map<String, dynamic> kwargs = const {}]) {
  /// Decode a Stellar (XLM) address and return the public key.
  ///
  /// This method decodes a Stellar address and extracts the public key part, returning it as a List<int>.
  ///
  /// - [addr]: The Stellar address to decode.
  /// - [kwargs]: A map of optional keyword arguments.
  ///   - [addr_type]: The address type, either XlmAddrTypes.pubKey or XlmAddrTypes.privKey.
  ///
  /// Throws an [ArgumentException] if the address type is not valid or if there's a validation error.
  ///
  /// Example usage:
  /// ```dart
  /// final decoder = XlmAddrDecoder();
  /// final addr = 'GC2Z66U3A3I5VGM3S5INUT4FVC3VGCUJ7ALDCTF6WLYBMXNO5KNOHWZL';
  /// final publicKey = decoder.decodeAddr(addr, {'addr_type': XlmAddrTypes.pubKey});
  /// ```
  final addrType = kwargs['addr_type'] ?? XlmAddrTypes.pubKey;
  if (addrType is! XlmAddrTypes) {
    throw ArgumentException(
        'Address type is not an enumerative of XlmAddrTypes');
  }

  final addrDecBytes = Base32Decoder.decode(addr);

  AddrDecUtils.validateBytesLength(
      addrDecBytes,
      Ed25519KeysConst.pubKeyByteLen +
          Ed25519KeysConst.pubKeyPrefix.length +
          XlmAddrConst.checksumByteLen);

  final payloadBytes = AddrDecUtils.splitPartsByChecksum(
          addrDecBytes, XlmAddrConst.checksumByteLen)
      .item1;

  final addrTypeGot = payloadBytes[0];
  if (addrType.value != addrTypeGot) {
    throw ArgumentException(
        'Invalid address type (expected ${addrType.value}, got $addrTypeGot)');
  }

  AddrDecUtils.validateChecksum(
      payloadBytes,
      addrDecBytes
          .sublist(addrDecBytes.length - XlmAddrConst.checksumByteLen),
      _XlmAddrUtils.computeChecksum);
  final pubKeyBytes = payloadBytes.sublist(1);
  return pubKeyBytes;
}