Skip to content

Commit 17c16a1

Browse files
committed
Remove direct call to console.error, update add new permission docs, add tests for validateHexInteger
1 parent c527499 commit 17c16a1

4 files changed

Lines changed: 237 additions & 5 deletions

File tree

docs/addingNewPermissionTypes.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ export const DEFAULT_OFFERS: GatorPermission[] = [
202202
];
203203
```
204204

205-
### 9. Add the Permission to the demo dapp
206-
207-
### 10. Implement Permission Form Component
205+
### 9. Implement Permission Form Component
208206

209207
Create a new form component in `packages/site/src/components/permissions/` for your permission type. For example, `YourPermissionForm.tsx`:
210208

packages/gator-permissions-snap/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snap-7715-permissions.git"
88
},
99
"source": {
10-
"shasum": "rNRclGmH8r9z7JF1tMzA9Hfqu1ZO5ZghIAw2Es9bg1U=",
10+
"shasum": "lfJ1LUj3jXFYfyT1jw1tIzBxKyC8HButKqdCVUp2RQ4=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/gator-permissions-snap/src/clients/blockchainMetadataClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class BlockchainTokenMetadataClient implements TokenMetadataClient {
226226
symbol,
227227
};
228228
} catch (error) {
229-
console.error(
229+
logger.error(
230230
`Failed to fetch token balance and metadata: ${(error as Error).message}.`,
231231
);
232232

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
import type { Hex } from 'viem';
3+
import { validateHexInteger } from '../../src/permissions/validation';
4+
5+
describe('validateHexInteger', () => {
6+
describe('valid cases', () => {
7+
it('should pass for valid hex integers', () => {
8+
expect(() =>
9+
validateHexInteger({
10+
name: 'testValue',
11+
value: '0x1' as Hex,
12+
allowZero: false,
13+
required: true,
14+
}),
15+
).not.toThrow();
16+
17+
expect(() =>
18+
validateHexInteger({
19+
name: 'testValue',
20+
value: '0xFF' as Hex,
21+
allowZero: false,
22+
required: true,
23+
}),
24+
).not.toThrow();
25+
26+
expect(() =>
27+
validateHexInteger({
28+
name: 'testValue',
29+
value: '0x123456789ABCDEF' as Hex,
30+
allowZero: false,
31+
required: true,
32+
}),
33+
).not.toThrow();
34+
});
35+
36+
it('should pass for zero values when allowZero is true', () => {
37+
expect(() =>
38+
validateHexInteger({
39+
name: 'testValue',
40+
value: '0x0' as Hex,
41+
allowZero: true,
42+
required: true,
43+
}),
44+
).not.toThrow();
45+
});
46+
47+
it('should pass for undefined values when not required', () => {
48+
expect(() =>
49+
validateHexInteger({
50+
name: 'testValue',
51+
value: undefined,
52+
allowZero: false,
53+
required: false,
54+
}),
55+
).not.toThrow();
56+
57+
expect(() =>
58+
validateHexInteger({
59+
name: 'testValue',
60+
value: undefined,
61+
allowZero: true,
62+
required: false,
63+
}),
64+
).not.toThrow();
65+
});
66+
});
67+
68+
describe('required validation', () => {
69+
it('should throw error when value is undefined and required is true', () => {
70+
expect(() =>
71+
validateHexInteger({
72+
name: 'testValue',
73+
value: undefined,
74+
allowZero: false,
75+
required: true,
76+
}),
77+
).toThrow('Invalid testValue: must be defined');
78+
});
79+
80+
it('should throw error when value is null and required is true', () => {
81+
expect(() =>
82+
validateHexInteger({
83+
name: 'testValue',
84+
value: null as any,
85+
allowZero: false,
86+
required: true,
87+
}),
88+
).toThrow('Invalid testValue: must be defined');
89+
});
90+
91+
it('should use the provided name in error messages', () => {
92+
expect(() =>
93+
validateHexInteger({
94+
name: 'customFieldName',
95+
value: undefined,
96+
allowZero: false,
97+
required: true,
98+
}),
99+
).toThrow('Invalid customFieldName: must be defined');
100+
});
101+
});
102+
103+
describe('hex format validation', () => {
104+
it('should throw error for invalid hex strings', () => {
105+
expect(() =>
106+
validateHexInteger({
107+
name: 'testValue',
108+
value: 'invalid' as Hex,
109+
allowZero: false,
110+
required: true,
111+
}),
112+
).toThrow('Invalid testValue: must be a valid hex integer');
113+
114+
expect(() =>
115+
validateHexInteger({
116+
name: 'testValue',
117+
value: '0xGG' as Hex,
118+
allowZero: false,
119+
required: true,
120+
}),
121+
).toThrow('Invalid testValue: must be a valid hex integer');
122+
123+
expect(() =>
124+
validateHexInteger({
125+
name: 'testValue',
126+
value: 'notahex' as Hex,
127+
allowZero: false,
128+
required: true,
129+
}),
130+
).toThrow('Invalid testValue: must be a valid hex integer');
131+
});
132+
});
133+
134+
describe('zero value validation', () => {
135+
it('should throw error for zero values when allowZero is false', () => {
136+
expect(() =>
137+
validateHexInteger({
138+
name: 'testValue',
139+
value: '0x0' as Hex,
140+
allowZero: false,
141+
required: true,
142+
}),
143+
).toThrow('Invalid testValue: must be greater than 0');
144+
145+
expect(() =>
146+
validateHexInteger({
147+
name: 'testValue',
148+
value: '0x00' as Hex,
149+
allowZero: false,
150+
required: true,
151+
}),
152+
).toThrow('Invalid testValue: must be greater than 0');
153+
});
154+
155+
it('should accept zero values when allowZero is true', () => {
156+
expect(() =>
157+
validateHexInteger({
158+
name: 'testValue',
159+
value: '0x0' as Hex,
160+
allowZero: true,
161+
required: true,
162+
}),
163+
).not.toThrow();
164+
165+
expect(() =>
166+
validateHexInteger({
167+
name: 'testValue',
168+
value: '0x00' as Hex,
169+
allowZero: true,
170+
required: true,
171+
}),
172+
).not.toThrow();
173+
});
174+
});
175+
176+
describe('edge cases', () => {
177+
it('should handle large hex values', () => {
178+
expect(() =>
179+
validateHexInteger({
180+
name: 'testValue',
181+
value:
182+
'0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' as Hex,
183+
allowZero: false,
184+
required: true,
185+
}),
186+
).not.toThrow();
187+
});
188+
189+
it('should handle hex values without 0x prefix if BigInt can parse them', () => {
190+
// Note: This test depends on how BigInt handles strings without 0x prefix
191+
// BigInt('1') should work, but BigInt('FF') might not work as hex
192+
expect(() =>
193+
validateHexInteger({
194+
name: 'testValue',
195+
value: '1' as Hex,
196+
allowZero: false,
197+
required: true,
198+
}),
199+
).not.toThrow();
200+
});
201+
202+
it('should handle different parameter combinations', () => {
203+
// Non-required, allowZero false, undefined value
204+
expect(() =>
205+
validateHexInteger({
206+
name: 'testValue',
207+
value: undefined,
208+
allowZero: false,
209+
required: false,
210+
}),
211+
).not.toThrow();
212+
213+
// Non-required, allowZero true, undefined value
214+
expect(() =>
215+
validateHexInteger({
216+
name: 'testValue',
217+
value: undefined,
218+
allowZero: true,
219+
required: false,
220+
}),
221+
).not.toThrow();
222+
223+
// Required, allowZero true, valid non-zero value
224+
expect(() =>
225+
validateHexInteger({
226+
name: 'testValue',
227+
value: '0x1' as Hex,
228+
allowZero: true,
229+
required: true,
230+
}),
231+
).not.toThrow();
232+
});
233+
});
234+
});

0 commit comments

Comments
 (0)