Skip to content

Commit 0f7e334

Browse files
committed
IdBuilder now accepts either number or bigint
1 parent 5ab1045 commit 0f7e334

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

packages/delegation-toolkit/src/caveatBuilder/idBuilder.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@ export const id = 'id';
1414
*/
1515
export function idBuilder(
1616
environment: DeleGatorEnvironment,
17-
idValue: number,
17+
idValue: bigint | number,
1818
): Caveat {
19-
if (!Number.isInteger(idValue)) {
20-
throw new Error('Invalid id: must be an integer');
19+
let idValueBigInt: bigint;
20+
21+
if (typeof idValue === 'number') {
22+
if (!Number.isInteger(idValue)) {
23+
throw new Error('Invalid id: must be an integer');
24+
}
25+
26+
idValueBigInt = BigInt(idValue);
27+
} else if (typeof idValue === 'bigint') {
28+
idValueBigInt = idValue;
29+
} else {
30+
throw new Error('Invalid id: must be a bigint or number');
2131
}
2232

23-
if (idValue < 0) {
33+
if (idValueBigInt < 0n) {
2434
throw new Error('Invalid id: must be positive');
2535
}
2636

27-
if (idValue > maxUint256) {
37+
if (idValueBigInt > maxUint256) {
2838
throw new Error('Invalid id: must be less than 2^256');
2939
}
3040

31-
const terms = toHex(idValue, { size: 32 });
41+
const terms = toHex(idValueBigInt, { size: 32 });
3242

3343
const {
3444
caveatEnforcers: { IdEnforcer },

packages/delegation-toolkit/test/caveatBuilder/createCaveatBuilder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe('createCaveatBuilder()', () => {
238238
it("should add an 'id' caveat", () => {
239239
const builder = createCaveatBuilder(environment);
240240

241-
const id = Math.floor(Math.random() * 2 ** 32);
241+
const id = BigInt(Math.floor(Math.random() * 2 ** 32));
242242
const caveats = builder.addCaveat('id', id).build();
243243

244244
expect(caveats).to.deep.equal([

packages/delegation-toolkit/test/caveatBuilder/idBuilder.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import { size, toHex } from 'viem';
2+
import { maxUint256, size, toHex } from 'viem';
33

44
import { idBuilder } from '../../src/caveatBuilder/idBuilder';
55
import type { DeleGatorEnvironment } from '../../src/types';
@@ -11,42 +11,35 @@ describe('idBuilder()', () => {
1111
caveatEnforcers: { IdEnforcer: randomAddress() },
1212
} as any as DeleGatorEnvironment;
1313

14-
const buildWithId = (id: number) => {
14+
const buildWithId = (id: bigint | number) => {
1515
return idBuilder(environment, id);
1616
};
1717

1818
describe('validation', () => {
19-
it('should fail with an invalid id (not a number)', () => {
20-
const invalidId = 'not-a-hex-string' as any;
21-
expect(() => buildWithId(invalidId)).to.throw(
22-
'Invalid id: must be an integer',
23-
);
24-
});
25-
2619
it('should fail with an invalid id (not an integer)', () => {
27-
const invalidId = Math.random() * 2 ** 32;
20+
const invalidId = Math.random();
2821
expect(() => buildWithId(invalidId)).to.throw(
2922
'Invalid id: must be an integer',
3023
);
3124
});
3225

3326
it('should fail with an invalid id (negative)', () => {
34-
const invalidId = -1;
27+
const invalidId = -1n;
3528
expect(() => buildWithId(invalidId)).to.throw(
3629
'Invalid id: must be positive',
3730
);
3831
});
3932

4033
it('should fail with an invalid id (too high)', () => {
4134
const invalidId = 2n ** 256n;
42-
expect(() => buildWithId(Number(invalidId))).to.throw(
35+
expect(() => buildWithId(invalidId)).to.throw(
4336
'Invalid id: must be less than 2^256',
4437
);
4538
});
4639
});
4740

4841
describe('builds a caveat', () => {
49-
it('should build a caveat with a valid id', () => {
42+
it('should build a caveat with a valid Number', () => {
5043
const validId = Math.floor(Math.random() * 2 ** 32);
5144

5245
const caveat = buildWithId(validId);
@@ -59,6 +52,18 @@ describe('idBuilder()', () => {
5952
args: '0x',
6053
});
6154
});
55+
56+
it('should build a caveat with a large bigint', () => {
57+
const caveat = buildWithId(maxUint256);
58+
59+
const expectedTerms = toHex(maxUint256, { size: 32 });
60+
61+
expect(caveat).to.deep.equal({
62+
enforcer: environment.caveatEnforcers.IdEnforcer,
63+
terms: expectedTerms,
64+
args: '0x',
65+
});
66+
});
6267
});
6368

6469
it('should create a caveat with terms length matching number of targets', () => {

0 commit comments

Comments
 (0)