fromFriendlyAddress static method

DecodeAddressResult fromFriendlyAddress(
  1. String address
)

Implementation

static DecodeAddressResult fromFriendlyAddress(String address) {
  final data = StringUtils.encode(address, type: StringEncoding.base64);
  // 1 byte tag + 1 byte workchain + 32 bytes hash + 2 byte crc
  if (data.length != _TonAddressConst.friendlyAddressBytesLength) {
    throw AddressConverterException(
        "Unknown address type. byte length is not equal to ${_TonAddressConst.friendlyAddressBytesLength}",
        details: {"length": data.length});
  }

  // Prepare data
  final addr = data.sublist(0, 34);
  final crc = data.sublist(34, 36);
  final calcedCrc = Crc16.quickIntDigest(addr);
  if (!BytesUtils.bytesEqual(crc, calcedCrc)) {
    throw AddressConverterException("Invalid checksum",
        details: {"excepted": calcedCrc, "checksum": crc});
  }
  List<FriendlyAddressFlags> flags = [];
  // Parse tag
  int tag = addr[0];
  // bool isTestOnly = false;
  // bool isBounceable = false;
  if ((tag & FriendlyAddressFlags.test.flag) != 0) {
    flags.add(FriendlyAddressFlags.test);
    tag ^= FriendlyAddressFlags.test.flag;
  }
  if (tag != FriendlyAddressFlags.bounceable.flag &&
      tag != FriendlyAddressFlags.nonBounceable.flag) {
    throw AddressConverterException("Unknown address tag",
        details: {"tag": tag});
  }
  if (tag == FriendlyAddressFlags.bounceable.flag) {
    flags.add(FriendlyAddressFlags.bounceable);
  } else {
    flags.add(FriendlyAddressFlags.nonBounceable);
  }
  int? workchain;
  if (addr[1] == mask8) {
    workchain = -1;
  } else {
    workchain = addr[1];
  }
  final hashPart = addr.sublist(2, 34);
  return DecodeAddressResult(
      workchain: workchain, hash: hashPart, flags: flags);
}