-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathnonce.test.ts
More file actions
204 lines (162 loc) · 6.67 KB
/
Copy pathnonce.test.ts
File metadata and controls
204 lines (162 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { describe, it, expect } from 'vitest';
import { createNonceTerms } from '../../src/caveats/nonce';
describe('createNonceTerms', () => {
const EXPECTED_BYTE_LENGTH = 32; // 32 bytes for nonce
it('creates valid terms for simple nonce', () => {
const nonce = '0x1234567890abcdef';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x0000000000000000000000000000000000000000000000001234567890abcdef',
);
});
it('creates valid terms for zero nonce', () => {
const nonce = '0x0';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x0000000000000000000000000000000000000000000000000000000000000000',
);
});
it('creates valid terms for minimal nonce', () => {
const nonce = '0x1';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x0000000000000000000000000000000000000000000000000000000000000001',
);
});
it('creates valid terms for full 32-byte nonce', () => {
const nonce =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(nonce);
});
it('creates valid terms for uppercase hex nonce', () => {
const nonce = '0x1234567890ABCDEF';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x0000000000000000000000000000000000000000000000001234567890ABCDEF',
);
});
it('creates valid terms for mixed case hex nonce', () => {
const nonce = '0x1234567890AbCdEf';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x0000000000000000000000000000000000000000000000001234567890AbCdEf',
);
});
it('pads shorter hex values with leading zeros', () => {
const nonce = '0xff';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x00000000000000000000000000000000000000000000000000000000000000ff',
);
});
it('throws an error for empty nonce', () => {
const nonce = '0x';
expect(() => createNonceTerms({ nonce })).toThrow(
'Invalid nonce: must be a non-empty hex string',
);
});
it('throws an error for undefined nonce', () => {
expect(() => createNonceTerms({ nonce: undefined as any })).toThrow(
'Invalid nonce: must be a non-empty hex string',
);
});
it('throws an error for null nonce', () => {
expect(() => createNonceTerms({ nonce: null as any })).toThrow(
'Invalid nonce: must be a non-empty hex string',
);
});
it('throws an error for nonce without 0x prefix', () => {
const nonce = '1234567890abcdef' as any;
expect(() => createNonceTerms({ nonce })).toThrow(
'Invalid nonce: must be a valid hex string',
);
});
it('throws an error for invalid hex characters', () => {
const nonce = '0x1234567890abcdefg' as any;
expect(() => createNonceTerms({ nonce })).toThrow(
'Invalid nonce: must be a valid hex string',
);
});
it('throws an error for non-string nonce', () => {
const nonce = 123456 as any;
expect(() => createNonceTerms({ nonce })).toThrow(
'Invalid nonce: must be a valid hex string',
);
});
it('throws an error for nonce longer than 32 bytes', () => {
// 33 bytes (66 hex chars + 0x prefix = 68 chars total, which exceeds 66)
const nonce =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12' as any;
expect(() => createNonceTerms({ nonce })).toThrow(
'Invalid nonce: must be 32 bytes or less in length',
);
});
it('accepts nonce with exactly 32 bytes', () => {
// 32 bytes (64 hex chars + 0x prefix = 66 chars total)
const nonce =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(nonce);
});
it('throws an error for string that looks like hex but has odd length', () => {
const nonce = '0x123' as any;
// This should still work as we pad it
const result = createNonceTerms({ nonce });
expect(result).toStrictEqual(
'0x0000000000000000000000000000000000000000000000000000000000000123',
);
});
// Tests for bytes return type
describe('bytes return type', () => {
it('returns Uint8Array when bytes encoding is specified', () => {
const nonce = '0x1234567890abcdef';
const result = createNonceTerms({ nonce }, { out: 'bytes' });
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(EXPECTED_BYTE_LENGTH);
});
it('returns Uint8Array for minimal nonce with bytes encoding', () => {
const nonce = '0x1';
const result = createNonceTerms({ nonce }, { out: 'bytes' });
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(EXPECTED_BYTE_LENGTH);
// Should be 31 zeros followed by 1
const expectedBytes = new Array(EXPECTED_BYTE_LENGTH).fill(0);
expectedBytes[EXPECTED_BYTE_LENGTH - 1] = 1;
expect(Array.from(result)).toEqual(expectedBytes);
});
it('returns Uint8Array for zero nonce with bytes encoding', () => {
const nonce = '0x0';
const result = createNonceTerms({ nonce }, { out: 'bytes' });
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(EXPECTED_BYTE_LENGTH);
// Should be all zeros
const expectedBytes = new Array(EXPECTED_BYTE_LENGTH).fill(0);
expect(Array.from(result)).toEqual(expectedBytes);
});
it('returns Uint8Array for full nonce with bytes encoding', () => {
const nonce =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
const result = createNonceTerms({ nonce }, { out: 'bytes' });
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(EXPECTED_BYTE_LENGTH);
// Convert expected hex to bytes for comparison
const expectedBytes = [
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78,
0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
];
expect(Array.from(result)).toEqual(expectedBytes);
});
it('returns Uint8Array for padded hex values with bytes encoding', () => {
const nonce = '0xff';
const result = createNonceTerms({ nonce }, { out: 'bytes' });
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(EXPECTED_BYTE_LENGTH);
// Should be 31 zeros followed by 0xff
const expectedBytes = new Array(EXPECTED_BYTE_LENGTH).fill(0);
expectedBytes[EXPECTED_BYTE_LENGTH - 1] = 0xff;
expect(Array.from(result)).toEqual(expectedBytes);
});
});
});