@cofhe/sdk 0.6.0 → 0.7.0
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 +49 -0
- package/acps/acp.ts +411 -0
- package/acps/index.ts +70 -0
- package/{permits → acps}/onchain-utils.ts +49 -24
- package/acps/sealing.ts +90 -0
- package/acps/signature.ts +89 -0
- package/acps/store.ts +172 -0
- package/acps/test/acp.test.ts +615 -0
- package/acps/test/localstorage.test.ts +105 -0
- package/acps/test/sealing.test.ts +77 -0
- package/acps/test/store.test.ts +88 -0
- package/acps/test/validation.test.ts +361 -0
- package/acps/test-utils.ts +32 -0
- package/acps/types.ts +252 -0
- package/acps/validation.ts +392 -0
- package/adapters/test/ethers5.test.ts +4 -1
- package/adapters/test/ethers6.test.ts +4 -1
- package/adapters/test/wagmi.test.ts +5 -2
- package/chains/chains/stagingCofhe.ts +21 -0
- package/chains/index.ts +3 -1
- package/chains/test/chains.test.ts +2 -1
- package/core/acps.ts +625 -0
- package/core/client.ts +136 -39
- package/core/clientTypes.ts +52 -41
- package/core/config.ts +66 -5
- package/core/debug.ts +72 -0
- package/core/decrypt/MockThresholdNetworkAbi.ts +20 -11
- package/core/decrypt/apiError.ts +104 -0
- package/core/decrypt/cofheMocksDecryptForTx.ts +11 -11
- package/core/decrypt/cofheMocksDecryptForView.ts +7 -7
- package/core/decrypt/decryptForTxBuilder.ts +102 -102
- package/core/decrypt/decryptForViewBuilder.ts +90 -90
- package/core/decrypt/submitRetry.ts +38 -30
- package/core/decrypt/tnDecryptV1.ts +5 -5
- package/core/decrypt/tnDecryptV2.ts +45 -34
- package/core/decrypt/tnSealOutputV1.ts +4 -3
- package/core/decrypt/tnSealOutputV2.ts +44 -31
- package/core/encrypt/cofheMocksZkVerifySign.ts +59 -74
- package/core/encrypt/encryptInputsBuilder.ts +86 -52
- package/core/encrypt/zkPackProveVerify.ts +25 -18
- package/core/error.ts +34 -6
- package/core/index.ts +14 -14
- package/core/test/acpDefaults.test.ts +52 -0
- package/core/test/acps.test.ts +596 -0
- package/core/test/apiError.test.ts +130 -0
- package/core/test/client.test.ts +22 -19
- package/core/test/config.test.ts +25 -5
- package/core/test/decrypt.test.ts +40 -35
- package/core/test/decryptBuilders.test.ts +68 -68
- package/core/test/decryptErrorCodes.test.ts +217 -0
- package/core/test/encryptInputsBuilder.test.ts +72 -41
- package/core/test/pollCallbacks.test.ts +89 -18
- package/core/test/stagingRedirect.ts +18 -0
- package/core/test/submitRetry.test.ts +182 -0
- package/core/types.ts +9 -69
- package/dist/acp-Wi6isVQI.d.cts +407 -0
- package/dist/acp-Wi6isVQI.d.ts +407 -0
- package/dist/acps.cjs +1081 -0
- package/dist/acps.d.cts +480 -0
- package/dist/acps.d.ts +480 -0
- package/dist/acps.js +2 -0
- package/dist/chains.cjs +14 -1
- package/dist/chains.d.cts +30 -1
- package/dist/chains.d.ts +30 -1
- package/dist/chains.js +1 -1
- package/dist/{chunk-PE5V5CCV.js → chunk-43USWPEH.js} +1000 -604
- package/dist/{chunk-MTRAXQXC.js → chunk-N6IDQRRU.js} +14 -2
- package/dist/chunk-Q7CBWGQX.js +1029 -0
- package/dist/{clientTypes-CyUvRRzA.d.ts → clientTypes-BN3nbzYM.d.ts} +342 -165
- package/dist/{clientTypes-BDy1qIBu.d.cts → clientTypes-CYZjFznO.d.cts} +342 -165
- package/dist/core.cjs +1431 -1026
- package/dist/core.d.cts +55 -10
- package/dist/core.d.ts +55 -10
- package/dist/core.js +3 -3
- package/dist/node.cjs +1356 -976
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +3 -3
- package/dist/web.cjs +1356 -976
- package/dist/web.d.cts +2 -2
- package/dist/web.d.ts +2 -2
- package/dist/web.js +3 -3
- package/node/test/inherited.test.ts +75 -65
- package/node/test/tfheinit.test.ts +23 -8
- package/package.json +6 -6
- package/web/test/client.web.test.ts +5 -1
- package/web/test/inherited.web.test.ts +75 -65
- package/web/test/tfheinit.web.test.ts +14 -5
- package/web/test/worker.config.web.test.ts +38 -23
- package/web/test/worker.output.web.test.ts +25 -24
- package/core/encrypt/encryptUtils.ts +0 -67
- package/core/permits.ts +0 -206
- package/core/test/permits.test.ts +0 -534
- package/dist/chunk-VB62WYPL.js +0 -978
- package/dist/permit-DnVMDT5h.d.cts +0 -376
- package/dist/permit-DnVMDT5h.d.ts +0 -376
- package/dist/permits.cjs +0 -1026
- package/dist/permits.d.cts +0 -353
- package/dist/permits.d.ts +0 -353
- package/dist/permits.js +0 -2
- package/permits/index.ts +0 -68
- package/permits/permit.ts +0 -385
- package/permits/sealing.ts +0 -131
- package/permits/signature.ts +0 -79
- package/permits/store.ts +0 -157
- package/permits/test/localstorage.test.ts +0 -113
- package/permits/test/permit.test.ts +0 -557
- package/permits/test/sealing.test.ts +0 -84
- package/permits/test/store.test.ts +0 -88
- package/permits/test/validation.test.ts +0 -361
- package/permits/test-utils.ts +0 -28
- package/permits/types.ts +0 -204
- package/permits/validation.ts +0 -327
- /package/{permits → acps}/utils.ts +0 -0
|
@@ -1,534 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment happy-dom
|
|
3
|
-
*/
|
|
4
|
-
import { permitStore } from '@/permits';
|
|
5
|
-
|
|
6
|
-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
7
|
-
import { createPublicClient, createWalletClient, http, type PublicClient, type WalletClient } from 'viem';
|
|
8
|
-
import { arbitrumSepolia } from 'viem/chains';
|
|
9
|
-
import { privateKeyToAccount } from 'viem/accounts';
|
|
10
|
-
import { permits } from '../permits.js';
|
|
11
|
-
|
|
12
|
-
// Type declarations for happy-dom environment
|
|
13
|
-
declare const localStorage: {
|
|
14
|
-
clear: () => void;
|
|
15
|
-
getItem: (name: string) => string | null;
|
|
16
|
-
setItem: (name: string, value: string) => void;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
// Test private keys (well-known test keys from Anvil/Hardhat)
|
|
20
|
-
const BOB_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; // Bob - always issuer
|
|
21
|
-
const ALICE_PRIVATE_KEY = '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'; // Alice - always recipient
|
|
22
|
-
|
|
23
|
-
// Create real viem clients for Arbitrum Sepolia
|
|
24
|
-
const publicClient: PublicClient = createPublicClient({
|
|
25
|
-
chain: arbitrumSepolia,
|
|
26
|
-
transport: http(),
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const bobWalletClient: WalletClient = createWalletClient({
|
|
30
|
-
chain: arbitrumSepolia,
|
|
31
|
-
transport: http(),
|
|
32
|
-
account: privateKeyToAccount(BOB_PRIVATE_KEY),
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const aliceWalletClient: WalletClient = createWalletClient({
|
|
36
|
-
chain: arbitrumSepolia,
|
|
37
|
-
transport: http(),
|
|
38
|
-
account: privateKeyToAccount(ALICE_PRIVATE_KEY),
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// Helper to get the wallet addresses
|
|
42
|
-
const bobAddress = bobWalletClient.account!.address;
|
|
43
|
-
const aliceAddress = aliceWalletClient.account!.address;
|
|
44
|
-
const chainId = 421614; // Arbitrum Sepolia
|
|
45
|
-
|
|
46
|
-
describe('Core Permits Tests', () => {
|
|
47
|
-
beforeEach(() => {
|
|
48
|
-
// Clear localStorage and reset stores
|
|
49
|
-
localStorage.clear();
|
|
50
|
-
permitStore.store.setState({ permits: {}, activePermitHash: {} });
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
afterEach(() => {
|
|
54
|
-
localStorage.clear();
|
|
55
|
-
permitStore.store.setState({ permits: {}, activePermitHash: {} });
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('Permit Creation', () => {
|
|
59
|
-
it('should create and store self permit', async () => {
|
|
60
|
-
const permit = await permits.createSelf(
|
|
61
|
-
{ name: 'Test Self Permit', issuer: bobAddress },
|
|
62
|
-
publicClient,
|
|
63
|
-
bobWalletClient
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
expect(permit).toBeDefined();
|
|
67
|
-
expect(permit.name).toBe('Test Self Permit');
|
|
68
|
-
expect(permit.type).toBe('self');
|
|
69
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
70
|
-
expect(permit.issuerSignature).toBeDefined();
|
|
71
|
-
expect(permit.issuerSignature).not.toBe('0x');
|
|
72
|
-
|
|
73
|
-
// Verify localStorage
|
|
74
|
-
const storedData = localStorage.getItem('cofhesdk-permits');
|
|
75
|
-
expect(storedData).toBeDefined();
|
|
76
|
-
const parsedData = JSON.parse(storedData!);
|
|
77
|
-
expect(parsedData.state.permits[chainId][bobAddress]).toBeDefined();
|
|
78
|
-
expect(parsedData.state.activePermitHash[chainId][bobAddress]).toBeDefined();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should create and store sharing permit', async () => {
|
|
82
|
-
const permit = await permits.createSharing(
|
|
83
|
-
{
|
|
84
|
-
name: 'Test Sharing Permit',
|
|
85
|
-
issuer: bobAddress,
|
|
86
|
-
recipient: aliceAddress,
|
|
87
|
-
},
|
|
88
|
-
publicClient,
|
|
89
|
-
bobWalletClient
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
expect(permit.name).toBe('Test Sharing Permit');
|
|
93
|
-
expect(permit.type).toBe('sharing');
|
|
94
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
95
|
-
expect(permit.recipient).toBe(aliceAddress);
|
|
96
|
-
expect(permit.issuerSignature).toBeDefined();
|
|
97
|
-
expect(permit.issuerSignature).not.toBe('0x');
|
|
98
|
-
|
|
99
|
-
// Verify localStorage
|
|
100
|
-
const storedData = localStorage.getItem('cofhesdk-permits');
|
|
101
|
-
expect(storedData).toBeDefined();
|
|
102
|
-
const parsedData = JSON.parse(storedData!);
|
|
103
|
-
expect(parsedData.state.permits[chainId][bobAddress]).toBeDefined();
|
|
104
|
-
expect(parsedData.state.activePermitHash[chainId][bobAddress]).toBeDefined();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should import shared permit from JSON string', async () => {
|
|
108
|
-
// First create a sharing permit to import
|
|
109
|
-
const sharingPermit = await permits.createSharing(
|
|
110
|
-
{
|
|
111
|
-
name: 'Original Sharing Permit',
|
|
112
|
-
issuer: bobAddress,
|
|
113
|
-
recipient: aliceAddress,
|
|
114
|
-
},
|
|
115
|
-
publicClient,
|
|
116
|
-
bobWalletClient
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
// Export the permit as JSON string
|
|
120
|
-
const permitJson = JSON.stringify({
|
|
121
|
-
name: sharingPermit.name,
|
|
122
|
-
type: sharingPermit.type,
|
|
123
|
-
issuer: sharingPermit.issuer,
|
|
124
|
-
expiration: sharingPermit.expiration,
|
|
125
|
-
recipient: sharingPermit.recipient,
|
|
126
|
-
validatorId: sharingPermit.validatorId,
|
|
127
|
-
validatorContract: sharingPermit.validatorContract,
|
|
128
|
-
issuerSignature: sharingPermit.issuerSignature,
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// Import the permit as Alice (recipient)
|
|
132
|
-
const permit = await permits.importShared(permitJson, publicClient, aliceWalletClient);
|
|
133
|
-
|
|
134
|
-
expect(permit.name).toBe('Original Sharing Permit');
|
|
135
|
-
expect(permit.type).toBe('recipient');
|
|
136
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
137
|
-
expect(permit.recipient).toBe(aliceAddress);
|
|
138
|
-
expect(permit.recipientSignature).toBeDefined();
|
|
139
|
-
expect(permit.recipientSignature).not.toBe('0x');
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe('Permit Retrieval', () => {
|
|
144
|
-
let createdPermit: any;
|
|
145
|
-
let permitHash: string;
|
|
146
|
-
|
|
147
|
-
beforeEach(async () => {
|
|
148
|
-
// Create a real permit for testing
|
|
149
|
-
createdPermit = await permits.createSelf(
|
|
150
|
-
{ name: 'Test Permit', issuer: bobAddress },
|
|
151
|
-
publicClient,
|
|
152
|
-
bobWalletClient
|
|
153
|
-
);
|
|
154
|
-
permitHash = createdPermit.hash;
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should get permit by hash', async () => {
|
|
158
|
-
const permit = await permits.getPermit(chainId, bobAddress, permitHash);
|
|
159
|
-
expect(permit?.name).toBe('Test Permit');
|
|
160
|
-
expect(permit?.type).toBe('self');
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('should get all permits', async () => {
|
|
164
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
165
|
-
expect(Object.keys(allPermits).length).toBeGreaterThan(0);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it('should get active permit', async () => {
|
|
169
|
-
const permit = await permits.getActivePermit(chainId, bobAddress);
|
|
170
|
-
expect(permit?.name).toBe('Test Permit');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('should get active permit hash', async () => {
|
|
174
|
-
const hash = await permits.getActivePermitHash(chainId, bobAddress);
|
|
175
|
-
expect(typeof hash).toBe('string');
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
describe('localStorage Integration', () => {
|
|
180
|
-
it('should persist permits to localStorage', async () => {
|
|
181
|
-
const createdPermit = await permits.createSelf(
|
|
182
|
-
{ name: 'Test Permit', issuer: bobAddress },
|
|
183
|
-
publicClient,
|
|
184
|
-
bobWalletClient
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
const storedData = localStorage.getItem('cofhesdk-permits');
|
|
188
|
-
expect(storedData).toBeDefined();
|
|
189
|
-
|
|
190
|
-
const parsedData = JSON.parse(storedData!);
|
|
191
|
-
expect(parsedData.state.permits[chainId][bobAddress]).toBeDefined();
|
|
192
|
-
expect(parsedData.state.activePermitHash[chainId][bobAddress]).toBeDefined();
|
|
193
|
-
|
|
194
|
-
// Verify the permit data structure
|
|
195
|
-
const permitKeys = Object.keys(parsedData.state.permits[chainId][bobAddress]);
|
|
196
|
-
expect(permitKeys.length).toBeGreaterThan(0);
|
|
197
|
-
|
|
198
|
-
const serializedPermit = permits.serialize(createdPermit);
|
|
199
|
-
expect(parsedData.state.permits[chainId][bobAddress][createdPermit.hash]).toEqual(serializedPermit);
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
describe('Real Network Integration', () => {
|
|
204
|
-
it('should create permit with real EIP712 domain from Arbitrum Sepolia', async () => {
|
|
205
|
-
const permit = await permits.createSelf(
|
|
206
|
-
{ name: 'Real Network Permit', issuer: bobAddress },
|
|
207
|
-
publicClient,
|
|
208
|
-
bobWalletClient
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
expect(permit._signedDomain).toBeDefined();
|
|
212
|
-
expect(permit._signedDomain?.chainId).toBe(chainId);
|
|
213
|
-
expect(permit._signedDomain?.name).toBeDefined();
|
|
214
|
-
expect(permit._signedDomain?.version).toBeDefined();
|
|
215
|
-
expect(permit._signedDomain?.verifyingContract).toBeDefined();
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('should handle multiple permits on real network', async () => {
|
|
219
|
-
// Create multiple permits
|
|
220
|
-
await permits.createSelf({ name: 'Permit 1', issuer: bobAddress }, publicClient, bobWalletClient);
|
|
221
|
-
await permits.createSharing(
|
|
222
|
-
{
|
|
223
|
-
name: 'Permit 2',
|
|
224
|
-
issuer: bobAddress,
|
|
225
|
-
recipient: aliceAddress,
|
|
226
|
-
},
|
|
227
|
-
publicClient,
|
|
228
|
-
bobWalletClient
|
|
229
|
-
);
|
|
230
|
-
|
|
231
|
-
// Verify both permits exist
|
|
232
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
233
|
-
expect(Object.keys(allPermits).length).toBeGreaterThanOrEqual(2);
|
|
234
|
-
|
|
235
|
-
// Verify active permit is the last created one
|
|
236
|
-
const activePermit = await permits.getActivePermit(chainId, bobAddress);
|
|
237
|
-
expect(activePermit?.name).toBe('Permit 2');
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
describe('getOrCreateSelfPermit', () => {
|
|
242
|
-
it('should create a new self permit when none exists', async () => {
|
|
243
|
-
const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
|
|
244
|
-
issuer: bobAddress,
|
|
245
|
-
name: 'New Self Permit',
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
expect(permit).toBeDefined();
|
|
249
|
-
expect(permit.name).toBe('New Self Permit');
|
|
250
|
-
expect(permit.type).toBe('self');
|
|
251
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
252
|
-
expect(permit.issuerSignature).toBeDefined();
|
|
253
|
-
|
|
254
|
-
// Verify it was stored and set as active
|
|
255
|
-
const activePermit = await permits.getActivePermit(chainId, bobAddress);
|
|
256
|
-
expect(activePermit?.name).toBe('New Self Permit');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it('should return existing self permit when one exists', async () => {
|
|
260
|
-
// Create an initial self permit
|
|
261
|
-
const firstPermit = await permits.createSelf(
|
|
262
|
-
{ name: 'First Self Permit', issuer: bobAddress },
|
|
263
|
-
publicClient,
|
|
264
|
-
bobWalletClient
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
// Call getOrCreateSelfPermit - should return existing
|
|
268
|
-
const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
|
|
269
|
-
issuer: bobAddress,
|
|
270
|
-
name: 'Should Not Create This',
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
expect(permit.name).toBe('First Self Permit');
|
|
274
|
-
expect(permit.hash).toBe(firstPermit.hash);
|
|
275
|
-
|
|
276
|
-
// Verify no new permit was created
|
|
277
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
278
|
-
expect(Object.keys(allPermits).length).toBe(1);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('should create new self permit when active permit is sharing type', async () => {
|
|
282
|
-
// Create a sharing permit first
|
|
283
|
-
await permits.createSharing(
|
|
284
|
-
{
|
|
285
|
-
name: 'Sharing Permit',
|
|
286
|
-
issuer: bobAddress,
|
|
287
|
-
recipient: aliceAddress,
|
|
288
|
-
},
|
|
289
|
-
publicClient,
|
|
290
|
-
bobWalletClient
|
|
291
|
-
);
|
|
292
|
-
|
|
293
|
-
// Call getOrCreateSelfPermit - should create new since active is sharing type
|
|
294
|
-
const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
|
|
295
|
-
issuer: bobAddress,
|
|
296
|
-
name: 'New Self Permit',
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
expect(permit.name).toBe('New Self Permit');
|
|
300
|
-
expect(permit.type).toBe('self');
|
|
301
|
-
|
|
302
|
-
// Verify two permits exist now
|
|
303
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
304
|
-
expect(Object.keys(allPermits).length).toBe(2);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it('should use default options when none provided', async () => {
|
|
308
|
-
const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress);
|
|
309
|
-
|
|
310
|
-
expect(permit).toBeDefined();
|
|
311
|
-
expect(permit.type).toBe('self');
|
|
312
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
313
|
-
expect(permit.name).toBe('Autogenerated Self Permit');
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it('should use default chainId and account when not provided', async () => {
|
|
317
|
-
const permit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, undefined, undefined, {
|
|
318
|
-
issuer: bobAddress,
|
|
319
|
-
name: 'Test Permit',
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
expect(permit).toBeDefined();
|
|
323
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
324
|
-
|
|
325
|
-
// Verify it was stored with the chain's actual chainId
|
|
326
|
-
const activePermit = await permits.getActivePermit(chainId, bobAddress);
|
|
327
|
-
expect(activePermit?.name).toBe('Test Permit');
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
describe('getOrCreateSharingPermit', () => {
|
|
332
|
-
it('should create a new sharing permit when none exists', async () => {
|
|
333
|
-
const permit = await permits.getOrCreateSharingPermit(
|
|
334
|
-
publicClient,
|
|
335
|
-
bobWalletClient,
|
|
336
|
-
{
|
|
337
|
-
issuer: bobAddress,
|
|
338
|
-
recipient: aliceAddress,
|
|
339
|
-
name: 'New Sharing Permit',
|
|
340
|
-
},
|
|
341
|
-
chainId,
|
|
342
|
-
bobAddress
|
|
343
|
-
);
|
|
344
|
-
|
|
345
|
-
expect(permit).toBeDefined();
|
|
346
|
-
expect(permit.name).toBe('New Sharing Permit');
|
|
347
|
-
expect(permit.type).toBe('sharing');
|
|
348
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
349
|
-
expect(permit.recipient).toBe(aliceAddress);
|
|
350
|
-
expect(permit.issuerSignature).toBeDefined();
|
|
351
|
-
|
|
352
|
-
// Verify it was stored and set as active
|
|
353
|
-
const activePermit = await permits.getActivePermit(chainId, bobAddress);
|
|
354
|
-
expect(activePermit?.name).toBe('New Sharing Permit');
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
it('should return existing sharing permit when one exists', async () => {
|
|
358
|
-
// Create an initial sharing permit
|
|
359
|
-
const firstPermit = await permits.createSharing(
|
|
360
|
-
{
|
|
361
|
-
name: 'First Sharing Permit',
|
|
362
|
-
issuer: bobAddress,
|
|
363
|
-
recipient: aliceAddress,
|
|
364
|
-
},
|
|
365
|
-
publicClient,
|
|
366
|
-
bobWalletClient
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
// Call getOrCreateSharingPermit - should return existing
|
|
370
|
-
const permit = await permits.getOrCreateSharingPermit(
|
|
371
|
-
publicClient,
|
|
372
|
-
bobWalletClient,
|
|
373
|
-
{
|
|
374
|
-
issuer: bobAddress,
|
|
375
|
-
recipient: aliceAddress,
|
|
376
|
-
name: 'Should Not Create This',
|
|
377
|
-
},
|
|
378
|
-
chainId,
|
|
379
|
-
bobAddress
|
|
380
|
-
);
|
|
381
|
-
|
|
382
|
-
expect(permit.name).toBe('First Sharing Permit');
|
|
383
|
-
expect(permit.hash).toBe(firstPermit.hash);
|
|
384
|
-
|
|
385
|
-
// Verify no new permit was created
|
|
386
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
387
|
-
expect(Object.keys(allPermits).length).toBe(1);
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
it('should create new sharing permit when active permit is self type', async () => {
|
|
391
|
-
// Create a self permit first
|
|
392
|
-
await permits.createSelf({ name: 'Self Permit', issuer: bobAddress }, publicClient, bobWalletClient);
|
|
393
|
-
|
|
394
|
-
// Call getOrCreateSharingPermit - should create new since active is self type
|
|
395
|
-
const permit = await permits.getOrCreateSharingPermit(
|
|
396
|
-
publicClient,
|
|
397
|
-
bobWalletClient,
|
|
398
|
-
{
|
|
399
|
-
issuer: bobAddress,
|
|
400
|
-
recipient: aliceAddress,
|
|
401
|
-
name: 'New Sharing Permit',
|
|
402
|
-
},
|
|
403
|
-
chainId,
|
|
404
|
-
bobAddress
|
|
405
|
-
);
|
|
406
|
-
|
|
407
|
-
expect(permit.name).toBe('New Sharing Permit');
|
|
408
|
-
expect(permit.type).toBe('sharing');
|
|
409
|
-
|
|
410
|
-
// Verify two permits exist now
|
|
411
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
412
|
-
expect(Object.keys(allPermits).length).toBe(2);
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('should use default chainId and account when not provided', async () => {
|
|
416
|
-
const permit = await permits.getOrCreateSharingPermit(
|
|
417
|
-
publicClient,
|
|
418
|
-
bobWalletClient,
|
|
419
|
-
{
|
|
420
|
-
issuer: bobAddress,
|
|
421
|
-
recipient: aliceAddress,
|
|
422
|
-
name: 'Test Sharing Permit',
|
|
423
|
-
},
|
|
424
|
-
undefined,
|
|
425
|
-
undefined
|
|
426
|
-
);
|
|
427
|
-
|
|
428
|
-
expect(permit).toBeDefined();
|
|
429
|
-
expect(permit.issuer).toBe(bobAddress);
|
|
430
|
-
expect(permit.recipient).toBe(aliceAddress);
|
|
431
|
-
|
|
432
|
-
// Verify it was stored with the chain's actual chainId
|
|
433
|
-
const activePermit = await permits.getActivePermit(chainId, bobAddress);
|
|
434
|
-
expect(activePermit?.name).toBe('Test Sharing Permit');
|
|
435
|
-
});
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
describe('Export', () => {
|
|
439
|
-
it('should export self permit data without sensitive fields', async () => {
|
|
440
|
-
const permit = await permits.createSelf(
|
|
441
|
-
{ name: 'Test Self Permit', issuer: bobAddress },
|
|
442
|
-
publicClient,
|
|
443
|
-
bobWalletClient
|
|
444
|
-
);
|
|
445
|
-
|
|
446
|
-
const exported = permits.export(permit);
|
|
447
|
-
const parsed = JSON.parse(exported);
|
|
448
|
-
|
|
449
|
-
expect(parsed.name).toBe('Test Self Permit');
|
|
450
|
-
expect(parsed.issuer).toBe(bobAddress);
|
|
451
|
-
expect(parsed).not.toHaveProperty('sealingPair');
|
|
452
|
-
expect(parsed).not.toHaveProperty('issuerSignature');
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
it('should export sharing permit data with recipient and issuerSignature', async () => {
|
|
456
|
-
const permit = await permits.createSharing(
|
|
457
|
-
{
|
|
458
|
-
name: 'Test Sharing Permit',
|
|
459
|
-
issuer: bobAddress,
|
|
460
|
-
recipient: aliceAddress,
|
|
461
|
-
},
|
|
462
|
-
publicClient,
|
|
463
|
-
bobWalletClient
|
|
464
|
-
);
|
|
465
|
-
|
|
466
|
-
const exported = permits.export(permit);
|
|
467
|
-
const parsed = JSON.parse(exported);
|
|
468
|
-
|
|
469
|
-
expect(parsed.name).toBe('Test Sharing Permit');
|
|
470
|
-
expect(parsed.type).toBe('sharing');
|
|
471
|
-
expect(parsed.issuer).toBe(bobAddress);
|
|
472
|
-
expect(parsed.recipient).toBe(aliceAddress);
|
|
473
|
-
expect(parsed.issuerSignature).toBeDefined();
|
|
474
|
-
expect(parsed.issuerSignature).not.toBe('0x');
|
|
475
|
-
expect(parsed).not.toHaveProperty('sealingPair');
|
|
476
|
-
});
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
describe('getOrCreate - Multiple Types Scenarios', () => {
|
|
480
|
-
it('should handle switching between self and sharing permits', async () => {
|
|
481
|
-
// Create self permit
|
|
482
|
-
const selfPermit = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
|
|
483
|
-
issuer: bobAddress,
|
|
484
|
-
name: 'Self Permit',
|
|
485
|
-
});
|
|
486
|
-
expect(selfPermit.type).toBe('self');
|
|
487
|
-
|
|
488
|
-
// Create sharing permit (should create new one)
|
|
489
|
-
const sharingPermit = await permits.getOrCreateSharingPermit(
|
|
490
|
-
publicClient,
|
|
491
|
-
bobWalletClient,
|
|
492
|
-
{
|
|
493
|
-
issuer: bobAddress,
|
|
494
|
-
recipient: aliceAddress,
|
|
495
|
-
name: 'Sharing Permit',
|
|
496
|
-
},
|
|
497
|
-
chainId,
|
|
498
|
-
bobAddress
|
|
499
|
-
);
|
|
500
|
-
expect(sharingPermit.type).toBe('sharing');
|
|
501
|
-
|
|
502
|
-
// Both should exist
|
|
503
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
504
|
-
expect(Object.keys(allPermits).length).toBe(2);
|
|
505
|
-
|
|
506
|
-
// Active permit should be the sharing one
|
|
507
|
-
const activePermit = await permits.getActivePermit(chainId, bobAddress);
|
|
508
|
-
expect(activePermit?.type).toBe('sharing');
|
|
509
|
-
expect(activePermit?.name).toBe('Sharing Permit');
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
it('should correctly handle sequential getOrCreate calls', async () => {
|
|
513
|
-
// First call - creates new
|
|
514
|
-
const permit1 = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
|
|
515
|
-
issuer: bobAddress,
|
|
516
|
-
name: 'Permit 1',
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
// Second call - returns existing
|
|
520
|
-
const permit2 = await permits.getOrCreateSelfPermit(publicClient, bobWalletClient, chainId, bobAddress, {
|
|
521
|
-
issuer: bobAddress,
|
|
522
|
-
name: 'Permit 2',
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
// Should be the same permit
|
|
526
|
-
expect(permit1.hash).toBe(permit2.hash);
|
|
527
|
-
expect(permit2.name).toBe('Permit 1'); // Original name
|
|
528
|
-
|
|
529
|
-
// Only one permit should exist
|
|
530
|
-
const allPermits = await permits.getPermits(chainId, bobAddress);
|
|
531
|
-
expect(Object.keys(allPermits).length).toBe(1);
|
|
532
|
-
});
|
|
533
|
-
});
|
|
534
|
-
});
|