decode method

XlmAddrDecoderResult decode(
  1. String addr, [
  2. Map<String, dynamic> kwargs = const {}
])

Implementation

XlmAddrDecoderResult decode(String addr,
    [Map<String, dynamic> kwargs = const {}]) {
  final addrType = AddrKeyValidator.nullOrValidateAddressArgs<XlmAddrTypes>(
      kwargs, "addr_type");
  final addrDecBytes = Base32Decoder.decode(addr);

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

  final addrTypeGot = payloadBytes[0];

  final type = XlmAddrTypes.fromTag(addrTypeGot);
  if (addrType != null && addrType != type) {
    throw AddressConverterException(
        'Invalid address type (expected ${addrType.value}, got $addrTypeGot)');
  }
  AddrDecUtils.validateBytesLength(
      addrDecBytes,
      type == XlmAddrTypes.muxed
          ? XlmAddrConst.muxedAddrLen
          : XlmAddrConst.pubkeyAddrLength);

  AddrDecUtils.validateChecksum(
      payloadBytes,
      addrDecBytes
          .sublist(addrDecBytes.length - XlmAddrConst.checksumByteLen),
      _XlmAddrUtils.computeChecksum);
  List<int> pubKeyBytes = payloadBytes.sublist(1);
  BigInt? accountId;
  if (type == XlmAddrTypes.muxed) {
    accountId = BigintUtils.fromBytes(
        pubKeyBytes.sublist(pubKeyBytes.length - XlmAddrConst.muxedIdLength));
    if (accountId > maxU64 || accountId < BigInt.zero) {
      throw const AddressConverterException(
          "Invalid muxed address account id.");
    }
    pubKeyBytes = List<int>.unmodifiable(pubKeyBytes.sublist(
        0, pubKeyBytes.length - XlmAddrConst.muxedIdLength));
    addr = XlmAddrEncoder().encodeKey(pubKeyBytes);
  }
  return XlmAddrDecoderResult(
      type: type,
      pubKeyBytes: pubKeyBytes,
      baseAddress: addr,
      accountId: accountId);
}