@cofhe/sdk 0.0.0-alpha-20260409113701
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +146 -0
- package/adapters/ethers5.test.ts +174 -0
- package/adapters/ethers5.ts +36 -0
- package/adapters/ethers6.test.ts +169 -0
- package/adapters/ethers6.ts +36 -0
- package/adapters/hardhat-node.ts +167 -0
- package/adapters/hardhat.hh2.test.ts +159 -0
- package/adapters/hardhat.ts +36 -0
- package/adapters/index.test.ts +20 -0
- package/adapters/index.ts +5 -0
- package/adapters/smartWallet.ts +99 -0
- package/adapters/test-utils.ts +53 -0
- package/adapters/types.ts +6 -0
- package/adapters/wagmi.test.ts +156 -0
- package/adapters/wagmi.ts +17 -0
- package/chains/chains/arbSepolia.ts +14 -0
- package/chains/chains/baseSepolia.ts +14 -0
- package/chains/chains/hardhat.ts +15 -0
- package/chains/chains/localcofhe.ts +14 -0
- package/chains/chains/sepolia.ts +14 -0
- package/chains/chains.test.ts +50 -0
- package/chains/defineChain.ts +18 -0
- package/chains/index.ts +35 -0
- package/chains/types.ts +32 -0
- package/core/baseBuilder.ts +119 -0
- package/core/client.test.ts +429 -0
- package/core/client.ts +341 -0
- package/core/clientTypes.ts +119 -0
- package/core/config.test.ts +242 -0
- package/core/config.ts +225 -0
- package/core/consts.ts +22 -0
- package/core/decrypt/MockThresholdNetworkAbi.ts +179 -0
- package/core/decrypt/cofheMocksDecryptForTx.ts +84 -0
- package/core/decrypt/cofheMocksDecryptForView.ts +48 -0
- package/core/decrypt/decryptForTxBuilder.ts +359 -0
- package/core/decrypt/decryptForViewBuilder.ts +332 -0
- package/core/decrypt/decryptUtils.ts +28 -0
- package/core/decrypt/pollCallbacks.test.ts +194 -0
- package/core/decrypt/polling.ts +14 -0
- package/core/decrypt/tnDecryptUtils.ts +65 -0
- package/core/decrypt/tnDecryptV1.ts +171 -0
- package/core/decrypt/tnDecryptV2.ts +365 -0
- package/core/decrypt/tnSealOutputV1.ts +59 -0
- package/core/decrypt/tnSealOutputV2.ts +324 -0
- package/core/decrypt/verifyDecryptResult.ts +52 -0
- package/core/encrypt/MockZkVerifierAbi.ts +106 -0
- package/core/encrypt/cofheMocksZkVerifySign.ts +281 -0
- package/core/encrypt/encryptInputsBuilder.test.ts +747 -0
- package/core/encrypt/encryptInputsBuilder.ts +583 -0
- package/core/encrypt/encryptUtils.ts +67 -0
- package/core/encrypt/zkPackProveVerify.ts +335 -0
- package/core/error.ts +168 -0
- package/core/fetchKeys.test.ts +195 -0
- package/core/fetchKeys.ts +144 -0
- package/core/index.ts +106 -0
- package/core/keyStore.test.ts +226 -0
- package/core/keyStore.ts +154 -0
- package/core/permits.test.ts +493 -0
- package/core/permits.ts +201 -0
- package/core/types.ts +419 -0
- package/core/utils.ts +130 -0
- package/dist/adapters.cjs +88 -0
- package/dist/adapters.d.cts +14576 -0
- package/dist/adapters.d.ts +14576 -0
- package/dist/adapters.js +83 -0
- package/dist/chains.cjs +111 -0
- package/dist/chains.d.cts +121 -0
- package/dist/chains.d.ts +121 -0
- package/dist/chains.js +1 -0
- package/dist/chunk-36FBWLUS.js +3310 -0
- package/dist/chunk-7HLGHV67.js +990 -0
- package/dist/chunk-TBLR7NNE.js +102 -0
- package/dist/clientTypes-AVSCBet7.d.cts +998 -0
- package/dist/clientTypes-flH1ju82.d.ts +998 -0
- package/dist/core.cjs +4362 -0
- package/dist/core.d.cts +138 -0
- package/dist/core.d.ts +138 -0
- package/dist/core.js +3 -0
- package/dist/node.cjs +4225 -0
- package/dist/node.d.cts +22 -0
- package/dist/node.d.ts +22 -0
- package/dist/node.js +91 -0
- package/dist/permit-jRirYqFt.d.cts +376 -0
- package/dist/permit-jRirYqFt.d.ts +376 -0
- package/dist/permits.cjs +1025 -0
- package/dist/permits.d.cts +353 -0
- package/dist/permits.d.ts +353 -0
- package/dist/permits.js +1 -0
- package/dist/types-YiAC4gig.d.cts +33 -0
- package/dist/types-YiAC4gig.d.ts +33 -0
- package/dist/web.cjs +4434 -0
- package/dist/web.d.cts +42 -0
- package/dist/web.d.ts +42 -0
- package/dist/web.js +256 -0
- package/dist/zkProve.worker.cjs +93 -0
- package/dist/zkProve.worker.d.cts +2 -0
- package/dist/zkProve.worker.d.ts +2 -0
- package/dist/zkProve.worker.js +91 -0
- package/node/client.test.ts +159 -0
- package/node/config.test.ts +68 -0
- package/node/encryptInputs.test.ts +155 -0
- package/node/index.ts +97 -0
- package/node/storage.ts +51 -0
- package/package.json +121 -0
- package/permits/index.ts +68 -0
- package/permits/localstorage.test.ts +113 -0
- package/permits/onchain-utils.ts +221 -0
- package/permits/permit.test.ts +534 -0
- package/permits/permit.ts +386 -0
- package/permits/sealing.test.ts +84 -0
- package/permits/sealing.ts +131 -0
- package/permits/signature.ts +79 -0
- package/permits/store.test.ts +88 -0
- package/permits/store.ts +156 -0
- package/permits/test-utils.ts +28 -0
- package/permits/types.ts +204 -0
- package/permits/utils.ts +58 -0
- package/permits/validation.test.ts +361 -0
- package/permits/validation.ts +327 -0
- package/web/client.web.test.ts +159 -0
- package/web/config.web.test.ts +69 -0
- package/web/const.ts +2 -0
- package/web/encryptInputs.web.test.ts +172 -0
- package/web/index.ts +166 -0
- package/web/storage.ts +49 -0
- package/web/worker.builder.web.test.ts +148 -0
- package/web/worker.config.web.test.ts +329 -0
- package/web/worker.output.web.test.ts +84 -0
- package/web/workerManager.test.ts +80 -0
- package/web/workerManager.ts +214 -0
- package/web/workerManager.web.test.ts +114 -0
- package/web/zkProve.worker.ts +133 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
PermitUtils,
|
|
4
|
+
type CreateSelfPermitOptions,
|
|
5
|
+
type CreateSharingPermitOptions,
|
|
6
|
+
type ImportSharedPermitOptions,
|
|
7
|
+
} from './index.js';
|
|
8
|
+
import { createPublicClient, createWalletClient, http, type PublicClient, type WalletClient } from 'viem';
|
|
9
|
+
import { arbitrumSepolia } from 'viem/chains';
|
|
10
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
11
|
+
|
|
12
|
+
// Test private keys (well-known test keys from Anvil/Hardhat)
|
|
13
|
+
const BOB_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; // Bob - always issuer
|
|
14
|
+
const ALICE_PRIVATE_KEY = '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'; // Alice - always recipient
|
|
15
|
+
|
|
16
|
+
// Create real viem clients for Arbitrum Sepolia
|
|
17
|
+
const publicClient: PublicClient = createPublicClient({
|
|
18
|
+
chain: arbitrumSepolia,
|
|
19
|
+
transport: http(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const bobWalletClient: WalletClient = createWalletClient({
|
|
23
|
+
chain: arbitrumSepolia,
|
|
24
|
+
transport: http(),
|
|
25
|
+
account: privateKeyToAccount(BOB_PRIVATE_KEY),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const aliceWalletClient: WalletClient = createWalletClient({
|
|
29
|
+
chain: arbitrumSepolia,
|
|
30
|
+
transport: http(),
|
|
31
|
+
account: privateKeyToAccount(ALICE_PRIVATE_KEY),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Helper to get the wallet addresses
|
|
35
|
+
const bobAddress = bobWalletClient.account!.address;
|
|
36
|
+
const aliceAddress = aliceWalletClient.account!.address;
|
|
37
|
+
|
|
38
|
+
describe('PermitUtils Tests', () => {
|
|
39
|
+
describe('createSelf', () => {
|
|
40
|
+
it('should create a self permit with valid options', async () => {
|
|
41
|
+
const options: CreateSelfPermitOptions = {
|
|
42
|
+
type: 'self',
|
|
43
|
+
issuer: bobAddress,
|
|
44
|
+
name: 'Test Permit',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const permit = PermitUtils.createSelf(options);
|
|
48
|
+
|
|
49
|
+
expect(permit.hash).toBe(PermitUtils.getHash(permit));
|
|
50
|
+
expect(permit.type).toBe('self');
|
|
51
|
+
expect(permit.name).toBe('Test Permit');
|
|
52
|
+
expect(permit.type).toBe('self');
|
|
53
|
+
expect(permit.issuer).toBe(bobAddress);
|
|
54
|
+
expect(permit.sealingPair).toBeDefined();
|
|
55
|
+
expect(permit.sealingPair.privateKey).toBeDefined();
|
|
56
|
+
expect(permit.sealingPair.publicKey).toBeDefined();
|
|
57
|
+
|
|
58
|
+
// Should not be signed yet
|
|
59
|
+
expect(permit.issuerSignature).toBe('0x');
|
|
60
|
+
expect(permit.recipientSignature).toBe('0x');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should throw error for invalid options', async () => {
|
|
64
|
+
const options: CreateSelfPermitOptions = {
|
|
65
|
+
type: 'self',
|
|
66
|
+
issuer: 'invalid-address',
|
|
67
|
+
name: 'Test Permit',
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
expect(() => PermitUtils.createSelf(options)).toThrowError();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('createSharing', () => {
|
|
75
|
+
it('should create a sharing permit with valid options', async () => {
|
|
76
|
+
const options: CreateSharingPermitOptions = {
|
|
77
|
+
type: 'sharing',
|
|
78
|
+
issuer: bobAddress,
|
|
79
|
+
recipient: aliceAddress,
|
|
80
|
+
name: 'Test Sharing Permit',
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const permit = PermitUtils.createSharing(options);
|
|
84
|
+
|
|
85
|
+
expect(permit.hash).toBe(PermitUtils.getHash(permit));
|
|
86
|
+
expect(permit.type).toBe('sharing');
|
|
87
|
+
expect(permit.name).toBe('Test Sharing Permit');
|
|
88
|
+
expect(permit.type).toBe('sharing');
|
|
89
|
+
expect(permit.issuer).toBe(bobAddress);
|
|
90
|
+
expect(permit.recipient).toBe(aliceAddress);
|
|
91
|
+
expect(permit.sealingPair).toBeDefined();
|
|
92
|
+
expect(permit.sealingPair.privateKey).toBeDefined();
|
|
93
|
+
expect(permit.sealingPair.publicKey).toBeDefined();
|
|
94
|
+
|
|
95
|
+
// Should not be signed yet
|
|
96
|
+
expect(permit.issuerSignature).toBe('0x');
|
|
97
|
+
expect(permit.recipientSignature).toBe('0x');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should throw error for invalid recipient', async () => {
|
|
101
|
+
const options: CreateSharingPermitOptions = {
|
|
102
|
+
type: 'sharing',
|
|
103
|
+
issuer: bobAddress,
|
|
104
|
+
recipient: 'invalid-address',
|
|
105
|
+
name: 'Test Sharing Permit',
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
expect(() => PermitUtils.createSharing(options)).toThrow();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('importShared', () => {
|
|
113
|
+
it('should import a shared permit with valid options', async () => {
|
|
114
|
+
const options: ImportSharedPermitOptions = {
|
|
115
|
+
issuer: bobAddress,
|
|
116
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
117
|
+
recipient: aliceAddress,
|
|
118
|
+
issuerSignature: '0x1234567890abcdef',
|
|
119
|
+
name: 'Test Import Permit',
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const permit = PermitUtils.importShared(options);
|
|
123
|
+
|
|
124
|
+
expect(permit.hash).toBe(PermitUtils.getHash(permit));
|
|
125
|
+
expect(permit.type).toBe('recipient');
|
|
126
|
+
expect(permit.name).toBe('Test Import Permit');
|
|
127
|
+
expect(permit.issuer).toBe(bobAddress);
|
|
128
|
+
expect(permit.recipient).toBe(aliceAddress);
|
|
129
|
+
expect(permit.issuerSignature).toBe('0x1234567890abcdef');
|
|
130
|
+
expect(permit.sealingPair).toBeDefined();
|
|
131
|
+
expect(permit.sealingPair.privateKey).toBeDefined();
|
|
132
|
+
expect(permit.sealingPair.publicKey).toBeDefined();
|
|
133
|
+
|
|
134
|
+
// Should not be signed yet
|
|
135
|
+
expect(permit.recipientSignature).toBe('0x');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should import a shared permit with valid options as string', async () => {
|
|
139
|
+
const options: ImportSharedPermitOptions = {
|
|
140
|
+
issuer: bobAddress,
|
|
141
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
142
|
+
recipient: aliceAddress,
|
|
143
|
+
issuerSignature: '0x1234567890abcdef',
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const stringOptions = JSON.stringify(options);
|
|
147
|
+
|
|
148
|
+
const permit = PermitUtils.importShared(stringOptions);
|
|
149
|
+
|
|
150
|
+
expect(permit.type).toBe('recipient');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should throw error for invalid permit type', async () => {
|
|
154
|
+
const options = {
|
|
155
|
+
type: 'self',
|
|
156
|
+
issuer: bobAddress,
|
|
157
|
+
recipient: aliceAddress,
|
|
158
|
+
issuerSignature: '0x1234567890abcdef',
|
|
159
|
+
} as unknown as ImportSharedPermitOptions;
|
|
160
|
+
|
|
161
|
+
expect(() => PermitUtils.importShared(options)).toThrow();
|
|
162
|
+
|
|
163
|
+
const options2 = {
|
|
164
|
+
type: 'recipient',
|
|
165
|
+
issuer: bobAddress,
|
|
166
|
+
recipient: aliceAddress,
|
|
167
|
+
issuerSignature: '0x1234567890abcdef',
|
|
168
|
+
} as unknown as ImportSharedPermitOptions;
|
|
169
|
+
|
|
170
|
+
expect(() => PermitUtils.importShared(options2)).toThrow();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should throw error for missing issuerSignature', async () => {
|
|
174
|
+
const options: ImportSharedPermitOptions = {
|
|
175
|
+
issuer: bobAddress,
|
|
176
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
177
|
+
recipient: aliceAddress,
|
|
178
|
+
issuerSignature: '0x', // Invalid empty signature
|
|
179
|
+
name: 'Test Import Permit',
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
expect(() => PermitUtils.importShared(options)).toThrow();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should throw error for missing expiration', async () => {
|
|
186
|
+
const options = {
|
|
187
|
+
issuer: bobAddress,
|
|
188
|
+
recipient: aliceAddress,
|
|
189
|
+
issuerSignature: '0x1234567890abcdef',
|
|
190
|
+
} as unknown as ImportSharedPermitOptions;
|
|
191
|
+
expect(() => PermitUtils.importShared(options)).toThrow();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('createSelfAndSign', () => {
|
|
196
|
+
it('should create and sign a self permit', async () => {
|
|
197
|
+
const options: CreateSelfPermitOptions = {
|
|
198
|
+
issuer: bobAddress,
|
|
199
|
+
name: 'Test Permit',
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const permit = await PermitUtils.createSelfAndSign(options, publicClient, bobWalletClient);
|
|
203
|
+
|
|
204
|
+
expect(permit.type).toBe('self');
|
|
205
|
+
expect(permit.issuerSignature).toBeDefined();
|
|
206
|
+
expect(permit.issuerSignature).not.toBe('0x');
|
|
207
|
+
expect(permit.recipientSignature).toBe('0x');
|
|
208
|
+
expect(permit._signedDomain).toBeDefined();
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('createSharingAndSign', () => {
|
|
213
|
+
it('should create and sign a sharing permit', async () => {
|
|
214
|
+
const options: CreateSharingPermitOptions = {
|
|
215
|
+
issuer: bobAddress,
|
|
216
|
+
recipient: aliceAddress,
|
|
217
|
+
name: 'Test Sharing Permit',
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const permit = await PermitUtils.createSharingAndSign(options, publicClient, bobWalletClient);
|
|
221
|
+
|
|
222
|
+
expect(permit.type).toBe('sharing');
|
|
223
|
+
expect(permit.issuerSignature).toBeDefined();
|
|
224
|
+
expect(permit.issuerSignature).not.toBe('0x');
|
|
225
|
+
expect(permit.recipientSignature).toBe('0x');
|
|
226
|
+
expect(permit._signedDomain).toBeDefined();
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('importSharedAndSign', () => {
|
|
231
|
+
it('should import and sign a shared permit', async () => {
|
|
232
|
+
const options: ImportSharedPermitOptions = {
|
|
233
|
+
issuer: bobAddress,
|
|
234
|
+
recipient: aliceAddress,
|
|
235
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
236
|
+
issuerSignature: '0x1234567890abcdef',
|
|
237
|
+
name: 'Test Import Permit',
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const permit = await PermitUtils.importSharedAndSign(options, publicClient, aliceWalletClient);
|
|
241
|
+
|
|
242
|
+
expect(permit.type).toBe('recipient');
|
|
243
|
+
expect(permit.recipientSignature).toBeDefined();
|
|
244
|
+
expect(permit.recipientSignature).not.toBe('0x');
|
|
245
|
+
expect(permit._signedDomain).toBeDefined();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should import and sign a shared permit string', async () => {
|
|
249
|
+
const options: ImportSharedPermitOptions = {
|
|
250
|
+
issuer: bobAddress,
|
|
251
|
+
recipient: aliceAddress,
|
|
252
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
253
|
+
issuerSignature: '0x1234567890abcdef',
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const stringOptions = JSON.stringify(options);
|
|
257
|
+
|
|
258
|
+
const permit = await PermitUtils.importSharedAndSign(stringOptions, publicClient, aliceWalletClient);
|
|
259
|
+
|
|
260
|
+
expect(permit.type).toBe('recipient');
|
|
261
|
+
expect(permit.recipientSignature).toBeDefined();
|
|
262
|
+
expect(permit.recipientSignature).not.toBe('0x');
|
|
263
|
+
expect(permit._signedDomain).toBeDefined();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('should import and sign a shared permit json object', async () => {
|
|
267
|
+
const options: ImportSharedPermitOptions = {
|
|
268
|
+
issuer: bobAddress,
|
|
269
|
+
recipient: aliceAddress,
|
|
270
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
271
|
+
issuerSignature: '0x1234567890abcdef',
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const jsonOptions = JSON.parse(JSON.stringify(options));
|
|
275
|
+
|
|
276
|
+
const permit = await PermitUtils.importSharedAndSign(jsonOptions, publicClient, aliceWalletClient);
|
|
277
|
+
|
|
278
|
+
expect(permit.type).toBe('recipient');
|
|
279
|
+
expect(permit.recipientSignature).toBeDefined();
|
|
280
|
+
expect(permit.recipientSignature).not.toBe('0x');
|
|
281
|
+
expect(permit._signedDomain).toBeDefined();
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
describe('sign', () => {
|
|
286
|
+
it('should sign a self permit', async () => {
|
|
287
|
+
const permit = PermitUtils.createSelf({
|
|
288
|
+
issuer: bobAddress,
|
|
289
|
+
name: 'Test Permit',
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const signedPermit = await PermitUtils.sign(permit, publicClient, bobWalletClient);
|
|
293
|
+
|
|
294
|
+
expect(signedPermit.type).toBe('self');
|
|
295
|
+
expect(signedPermit.issuerSignature).toBeDefined();
|
|
296
|
+
expect(signedPermit.issuerSignature).not.toBe('0x');
|
|
297
|
+
expect(signedPermit._signedDomain).toBeDefined();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('should sign a recipient permit', async () => {
|
|
301
|
+
const permit = PermitUtils.importShared({
|
|
302
|
+
issuer: bobAddress,
|
|
303
|
+
recipient: aliceAddress,
|
|
304
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
305
|
+
issuerSignature: '0x1111111111111111111111111111111111111111111111111111111111111111',
|
|
306
|
+
name: 'Test Permit',
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const signedPermit = await PermitUtils.sign(permit, publicClient, aliceWalletClient);
|
|
310
|
+
|
|
311
|
+
expect(signedPermit.recipientSignature).toBeDefined();
|
|
312
|
+
expect(signedPermit.recipientSignature).not.toBe('0x');
|
|
313
|
+
expect(signedPermit._signedDomain).toBeDefined();
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('should throw error for undefined signer', async () => {
|
|
317
|
+
const permit = PermitUtils.createSelf({
|
|
318
|
+
issuer: bobAddress,
|
|
319
|
+
name: 'Test Permit',
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
await expect(
|
|
323
|
+
// @ts-expect-error - undefined signer
|
|
324
|
+
PermitUtils.sign(permit, publicClient, undefined)
|
|
325
|
+
).rejects.toThrow();
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
describe('serialize/deserialize', () => {
|
|
330
|
+
it('should serialize and deserialize a permit', async () => {
|
|
331
|
+
const originalPermit = PermitUtils.createSelf({
|
|
332
|
+
issuer: bobAddress,
|
|
333
|
+
name: 'Test Permit',
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
const serialized = PermitUtils.serialize(originalPermit);
|
|
337
|
+
const deserialized = PermitUtils.deserialize(serialized);
|
|
338
|
+
|
|
339
|
+
expect(deserialized.type).toBe('self');
|
|
340
|
+
expect(deserialized.name).toBe(originalPermit.name);
|
|
341
|
+
expect(deserialized.type).toBe(originalPermit.type);
|
|
342
|
+
expect(deserialized.issuer).toBe(originalPermit.issuer);
|
|
343
|
+
expect(deserialized.sealingPair.privateKey).toBe(originalPermit.sealingPair.privateKey);
|
|
344
|
+
expect(deserialized.sealingPair.publicKey).toBe(originalPermit.sealingPair.publicKey);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
describe('getPermission', () => {
|
|
349
|
+
it('should extract permission from permit', async () => {
|
|
350
|
+
const permit = await PermitUtils.createSelfAndSign(
|
|
351
|
+
{
|
|
352
|
+
issuer: bobAddress,
|
|
353
|
+
name: 'Test Permit',
|
|
354
|
+
},
|
|
355
|
+
publicClient,
|
|
356
|
+
bobWalletClient
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const permission = PermitUtils.getPermission(permit);
|
|
360
|
+
|
|
361
|
+
expect(permission.issuer).toBe(permit.issuer);
|
|
362
|
+
expect(permission.sealingKey).toBe(`0x${permit.sealingPair.publicKey}`);
|
|
363
|
+
expect(permission).not.toHaveProperty('name');
|
|
364
|
+
expect(permission).not.toHaveProperty('type');
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
describe('getHash', () => {
|
|
369
|
+
it('should generate consistent hash for same permit data', async () => {
|
|
370
|
+
const expiration = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
|
|
371
|
+
const permit1 = PermitUtils.createSelf({
|
|
372
|
+
expiration,
|
|
373
|
+
issuer: bobAddress,
|
|
374
|
+
name: 'Test Permit',
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const permit2 = PermitUtils.createSelf({
|
|
378
|
+
expiration,
|
|
379
|
+
issuer: bobAddress,
|
|
380
|
+
name: 'Test Permit',
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
expect(permit1.hash).toBe(permit2.hash);
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
describe('export', () => {
|
|
388
|
+
it('should export permit data without sensitive fields', async () => {
|
|
389
|
+
const permit = PermitUtils.createSelf({
|
|
390
|
+
issuer: bobAddress,
|
|
391
|
+
name: 'Test Permit',
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
const exported = PermitUtils.export(permit);
|
|
395
|
+
const parsed = JSON.parse(exported);
|
|
396
|
+
|
|
397
|
+
expect(parsed.name).toBe('Test Permit');
|
|
398
|
+
expect(parsed.issuer).toBe(bobAddress);
|
|
399
|
+
expect(parsed).not.toHaveProperty('sealingPair');
|
|
400
|
+
expect(parsed).not.toHaveProperty('issuerSignature');
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
describe('updateName', () => {
|
|
405
|
+
it('should update permit name immutably', async () => {
|
|
406
|
+
const permit = PermitUtils.createSelf({
|
|
407
|
+
issuer: bobAddress,
|
|
408
|
+
name: 'Original Name',
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
const updatedPermit = PermitUtils.updateName(permit, 'New Name');
|
|
412
|
+
|
|
413
|
+
expect(updatedPermit.name).toBe('New Name');
|
|
414
|
+
expect(permit.name).toBe('Original Name'); // Original should be unchanged
|
|
415
|
+
expect(updatedPermit).not.toBe(permit); // Should be a new object
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
describe('validation helpers', () => {
|
|
420
|
+
it('should check if permit is expired', async () => {
|
|
421
|
+
const expiredPermit = PermitUtils.createSelf({
|
|
422
|
+
issuer: bobAddress,
|
|
423
|
+
name: 'Test Permit',
|
|
424
|
+
expiration: Math.floor(Date.now() / 1000) - 3600, // 1 hour ago
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
const validPermit = PermitUtils.createSelf({
|
|
428
|
+
issuer: bobAddress,
|
|
429
|
+
name: 'Test Permit',
|
|
430
|
+
expiration: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
expect(PermitUtils.isExpired(expiredPermit)).toBe(true);
|
|
434
|
+
expect(PermitUtils.isExpired(validPermit)).toBe(false);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('should check if permit is signed', async () => {
|
|
438
|
+
const unsignedPermit = PermitUtils.createSelf({
|
|
439
|
+
issuer: bobAddress,
|
|
440
|
+
name: 'Test Permit',
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const signedPermit = await PermitUtils.sign(unsignedPermit, publicClient, bobWalletClient);
|
|
444
|
+
|
|
445
|
+
expect(PermitUtils.isSigned(unsignedPermit)).toBe(false);
|
|
446
|
+
expect(PermitUtils.isSigned(signedPermit)).toBe(true);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('should check overall validity', async () => {
|
|
450
|
+
const validPermit = PermitUtils.createSelf({
|
|
451
|
+
issuer: bobAddress,
|
|
452
|
+
name: 'Test Permit',
|
|
453
|
+
expiration: Math.floor(Date.now() / 1000) + 3600,
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
const signedPermit = await PermitUtils.sign(validPermit, publicClient, bobWalletClient);
|
|
457
|
+
|
|
458
|
+
const validation = PermitUtils.isValid(signedPermit);
|
|
459
|
+
expect(validation.valid).toBe(true);
|
|
460
|
+
expect(validation.error).toBeNull();
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it('should throw on validate() for expired signed permit', async () => {
|
|
464
|
+
const expiredPermit = PermitUtils.createSelf({
|
|
465
|
+
issuer: bobAddress,
|
|
466
|
+
name: 'Expired Permit',
|
|
467
|
+
expiration: Math.floor(Date.now() / 1000) - 3600,
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
const signedExpiredPermit = await PermitUtils.sign(expiredPermit, publicClient, bobWalletClient);
|
|
471
|
+
expect(() => PermitUtils.validate(signedExpiredPermit)).toThrow('Permit is expired');
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
describe('real contract interactions', () => {
|
|
476
|
+
it('should fetch EIP712 domain from real Arbitrum Sepolia contract', async () => {
|
|
477
|
+
// This test uses the real public client to fetch actual contract data
|
|
478
|
+
const domain = await PermitUtils.fetchEIP712Domain(publicClient);
|
|
479
|
+
|
|
480
|
+
expect(domain).toBeDefined();
|
|
481
|
+
expect(domain.name).toBeDefined();
|
|
482
|
+
expect(domain.version).toBeDefined();
|
|
483
|
+
expect(domain.chainId).toBeDefined();
|
|
484
|
+
expect(domain.verifyingContract).toBeDefined();
|
|
485
|
+
expect(domain.verifyingContract).toMatch(/^0x[a-fA-F0-9]{40}$/); // Valid Ethereum address
|
|
486
|
+
}, 10000); // 10 second timeout for network call
|
|
487
|
+
|
|
488
|
+
it('should check signed domain validity with real contract data', async () => {
|
|
489
|
+
const permit = PermitUtils.createSelf({
|
|
490
|
+
type: 'self',
|
|
491
|
+
issuer: bobAddress,
|
|
492
|
+
name: 'Test Permit',
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
// Sign the permit to get a domain
|
|
496
|
+
const signedPermit = await PermitUtils.sign(permit, publicClient, bobWalletClient);
|
|
497
|
+
|
|
498
|
+
// Check if the signed domain is valid against the real contract
|
|
499
|
+
const isValid = await PermitUtils.checkSignedDomainValid(signedPermit, publicClient);
|
|
500
|
+
|
|
501
|
+
expect(typeof isValid).toBe('boolean');
|
|
502
|
+
}, 10000); // 10 second timeout for network call
|
|
503
|
+
|
|
504
|
+
// TODO: Uncomment when updated ACL with checkPermitValidity function is deployed
|
|
505
|
+
|
|
506
|
+
// it('should check permit validity on chain with real contract data', async () => {
|
|
507
|
+
// const permit = PermitUtils.createSelf({
|
|
508
|
+
// type: 'self',
|
|
509
|
+
// issuer: bobAddress,
|
|
510
|
+
// name: 'Test Permit',
|
|
511
|
+
// });
|
|
512
|
+
|
|
513
|
+
// const signedPermit = await PermitUtils.sign(permit, publicClient, bobWalletClient);
|
|
514
|
+
|
|
515
|
+
// const isValid = await PermitUtils.checkValidityOnChain(signedPermit, publicClient);
|
|
516
|
+
|
|
517
|
+
// expect(typeof isValid).toBe('boolean');
|
|
518
|
+
// expect(isValid).toBe(true);
|
|
519
|
+
|
|
520
|
+
// const permitInvalid = PermitUtils.createSelf({
|
|
521
|
+
// type: 'self',
|
|
522
|
+
// issuer: bobAddress,
|
|
523
|
+
// name: 'Test Permit',
|
|
524
|
+
// expiration: Math.floor(Date.now() / 1000) - 3600, // 1 hour ago
|
|
525
|
+
// });
|
|
526
|
+
|
|
527
|
+
// const signedPermitInvalid = await PermitUtils.sign(permitInvalid, publicClient, bobWalletClient);
|
|
528
|
+
// const isValidInvalid = await PermitUtils.checkValidityOnChain(signedPermitInvalid, publicClient);
|
|
529
|
+
|
|
530
|
+
// expect(typeof isValidInvalid).toBe('boolean');
|
|
531
|
+
// expect(isValidInvalid).toBe(false);
|
|
532
|
+
// });
|
|
533
|
+
});
|
|
534
|
+
});
|