Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update SubAccount.formID to handle bigger numbers #526

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading