decode method

XlmAddrDecoderResult decode(
  1. String addr, {
  2. XlmAddrTypes? addrType,
})

Implementation

XlmAddrDecoderResult decode(String addr, {XlmAddrTypes? addrType}) {
  final addrDecBytes = Base32Decoder.decode(addr);

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

  final addrTypeGot = payloadBytes[0];

  final type = XlmAddrTypes.fromTag(addrTypeGot);
  if (addrType != null && addrType != type) {
    throw AddressConverterException.addressValidationFailed(
      reason: "Invalid address type.",
    );
  }
  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 > BinaryOps.maxU64 || accountId < BigInt.zero) {
      throw AddressConverterException.addressValidationFailed(
        reason: "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,
  );
}