of_the_id static method

Icrc1Account of_the_id(
  1. String icrc1_id
)

Implementation

static Icrc1Account of_the_id(String icrc1_id) {

    if (icrc1_id.contains('.') == false) {
        return Icrc1Account(owner: Principal.text(icrc1_id));
    }
    List<String> id_split_period = icrc1_id.split('.');
    if (id_split_period.length > 2) {
        throw Exception('invalid icrc1-id.');
    }

    String subaccount_hex = id_split_period.last;
    while (subaccount_hex.length < 64) { subaccount_hex = '0' + subaccount_hex; }
    Uint8List subaccount = hexstringasthebytes(subaccount_hex);

    String principal_and_checksum = id_split_period.first;
    Principal principal = Principal.text(principal_and_checksum.substring(0, principal_and_checksum.lastIndexOf('-')));
    String checksum_base32 = principal_and_checksum.substring(principal_and_checksum.lastIndexOf('-')+1);
    if (checksum_base32.length%2!=0) { checksum_base32+='='; } // add padding for the decode function
    List<int> checksum = base32.decode(checksum_base32);

    Crc32 crc32 = Crc32();
    crc32.add(principal.bytes);
    crc32.add(subaccount);
    List<int> calculate_crc32 = crc32.close();
    if (aresamebytes(checksum, calculate_crc32) == false) {
        throw Exception('crc32 checksum is invalid.');
    }

    return Icrc1Account(
        owner: principal,
        subaccount: subaccount
    );
}