Skip to content

Commit

Permalink
feat: update SubAccount.formID to handle bigger numbers (#526)
Browse files Browse the repository at this point in the history
# Motivation

I am having a scenario where I need to handle more than 256 subaccounts. This is my workaround for now:
```
const principal = identity.getPrincipal()

const generatedAccountId = AccountIdentifier.fromPrincipal({
    principal,
    subAccount: SubAccount.fromBytes(numberToUint8Array32(newIndexNumber)),
})
```
where `numberToUint8Array32` is the updated `fromID` method.


# Changes

Updates the `SubAccount.fromID` method to handle up to nine quadrillion subaccounts (which is the max safe int).

It creates a buffer of 32 bytes and uses `writeBigInt64BE` to write the `id` using a 24 offest.

# Tests

Added one more test for `fromID` method under `account_identifier.spec.ts`. All tests are passing.

# Todos

- Enjoy!
  • Loading branch information
ENJATZ committed Feb 14, 2024
1 parent 9286fe2 commit a235169
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 11 additions & 2 deletions packages/ledger-icp/src/account_identifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ describe("SubAccount", () => {
expect(SubAccount.fromID(1)).toEqual(SubAccount.fromBytes(bytes));
bytes[31] = 255;
expect(SubAccount.fromID(255)).toEqual(SubAccount.fromBytes(bytes));

// Number 18791 in big endian 32 bytes
const numberInBytes = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 73, 103,
];
expect(SubAccount.fromID(18791)).toEqual(
SubAccount.fromBytes(new Uint8Array(numberInBytes)),
);
});

it("throws an exception if initialized with an ID < 0", () => {
Expand All @@ -68,9 +77,9 @@ describe("SubAccount", () => {
}).toThrow();
});

it("throws an exception if initialized with an ID > 255", () => {
it("throws an exception if initialized with an ID > max int", () => {
expect(() => {
SubAccount.fromID(256);
SubAccount.fromID(Number.MAX_SAFE_INTEGER + 1);
}).toThrow();
});
});
Expand Down
22 changes: 17 additions & 5 deletions packages/ledger-icp/src/account_identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,25 @@ export class SubAccount {
}

public static fromID(id: number): SubAccount {
if (id < 0 || id > 255) {
throw "Subaccount ID must be >= 0 and <= 255";
if (id < 0) throw new Error("Number cannot be negative");

if (id > Number.MAX_SAFE_INTEGER) {
throw new Error("Number is too large to fit in 32 bytes.");
}

const bytes: Uint8Array = new Uint8Array(32).fill(0);
bytes[31] = id;
return new SubAccount(bytes);
const view = new DataView(new ArrayBuffer(32));

// Fix for IOS < 14.8 setBigUint64 absence
if (typeof view.setBigUint64 === "function") {
view.setBigUint64(24, BigInt(id));
} else {
const TWO_TO_THE_32 = BigInt(1) << BigInt(32);
view.setUint32(24, Number(BigInt(id) >> BigInt(32)));
view.setUint32(28, Number(BigInt(id) % TWO_TO_THE_32));
}

const uint8Arary = new Uint8Array(view.buffer);
return new SubAccount(uint8Arary);
}

public toUint8Array(): Uint8Array {
Expand Down

0 comments on commit a235169

Please sign in to comment.