@bsv/wallet-toolbox 1.7.11 → 1.7.12
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 +7 -0
- package/docs/client.md +213 -52
- package/docs/wallet.md +213 -52
- package/mobile/out/src/WalletPermissionsManager.d.ts +60 -0
- package/mobile/out/src/WalletPermissionsManager.d.ts.map +1 -1
- package/mobile/out/src/WalletPermissionsManager.js +200 -35
- package/mobile/out/src/WalletPermissionsManager.js.map +1 -1
- package/mobile/package-lock.json +2 -2
- package/mobile/package.json +1 -1
- package/out/src/WalletPermissionsManager.d.ts +60 -0
- package/out/src/WalletPermissionsManager.d.ts.map +1 -1
- package/out/src/WalletPermissionsManager.js +200 -35
- package/out/src/WalletPermissionsManager.js.map +1 -1
- package/out/src/__tests/WalletPermissionsManager.fixtures.d.ts.map +1 -1
- package/out/src/__tests/WalletPermissionsManager.fixtures.js.map +1 -1
- package/out/src/__tests/WalletPermissionsManager.pmodules.test.d.ts +2 -0
- package/out/src/__tests/WalletPermissionsManager.pmodules.test.d.ts.map +1 -0
- package/out/src/__tests/WalletPermissionsManager.pmodules.test.js +624 -0
- package/out/src/__tests/WalletPermissionsManager.pmodules.test.js.map +1 -0
- package/out/src/__tests/WalletPermissionsManager.proxying.test.js.map +1 -1
- package/out/src/storage/remoting/StorageServer.d.ts.map +1 -1
- package/out/src/storage/remoting/StorageServer.js.map +1 -1
- package/out/tsconfig.all.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/Wallet.ts +2 -2
- package/src/WalletLogger.ts +1 -1
- package/src/WalletPermissionsManager.ts +350 -42
- package/src/__tests/WalletPermissionsManager.fixtures.ts +1 -2
- package/src/__tests/WalletPermissionsManager.pmodules.test.ts +798 -0
- package/src/__tests/WalletPermissionsManager.proxying.test.ts +2 -2
- package/src/storage/remoting/StorageServer.ts +0 -2
|
@@ -0,0 +1,798 @@
|
|
|
1
|
+
import { mockUnderlyingWallet, MockedBSV_SDK } from './WalletPermissionsManager.fixtures'
|
|
2
|
+
import { WalletPermissionsManager, PermissionsManagerConfig, PermissionsModule } from '../WalletPermissionsManager'
|
|
3
|
+
|
|
4
|
+
jest.mock('@bsv/sdk', () => MockedBSV_SDK)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Test suite for Permission Modules
|
|
8
|
+
* Tests both P-basket and P-protocol delegation to custom permission modules.
|
|
9
|
+
*/
|
|
10
|
+
describe('WalletPermissionsManager - Permission Module Support', () => {
|
|
11
|
+
let underlying: jest.Mocked<any>
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
underlying = mockUnderlyingWallet()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
jest.clearAllMocks()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('Permission Module Registration', () => {
|
|
22
|
+
it('should accept permissionModules in config', () => {
|
|
23
|
+
const testModule: PermissionsModule = {
|
|
24
|
+
onRequest: jest.fn(async req => req),
|
|
25
|
+
onResponse: jest.fn(async res => res)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const config: PermissionsManagerConfig = {
|
|
29
|
+
permissionModules: {
|
|
30
|
+
'test-scheme': testModule
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
35
|
+
|
|
36
|
+
// Verify module is stored in config
|
|
37
|
+
const storedConfig = (manager as any).config as PermissionsManagerConfig
|
|
38
|
+
expect(storedConfig.permissionModules).toBeDefined()
|
|
39
|
+
expect(storedConfig.permissionModules?.['test-scheme']).toBe(testModule)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should support multiple permission modules for different schemes', () => {
|
|
43
|
+
const module1: PermissionsModule = {
|
|
44
|
+
onRequest: jest.fn(async req => req),
|
|
45
|
+
onResponse: jest.fn(async res => res)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const module2: PermissionsModule = {
|
|
49
|
+
onRequest: jest.fn(async req => req),
|
|
50
|
+
onResponse: jest.fn(async res => res)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const config: PermissionsManagerConfig = {
|
|
54
|
+
permissionModules: {
|
|
55
|
+
scheme1: module1,
|
|
56
|
+
scheme2: module2
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
61
|
+
const storedConfig = (manager as any).config as PermissionsManagerConfig
|
|
62
|
+
|
|
63
|
+
expect(Object.keys(storedConfig.permissionModules || {}).length).toBe(2)
|
|
64
|
+
expect(storedConfig.permissionModules?.['scheme1']).toBe(module1)
|
|
65
|
+
expect(storedConfig.permissionModules?.['scheme2']).toBe(module2)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('P-Basket Delegation - listOutputs', () => {
|
|
70
|
+
it('should delegate to permission when basket starts with "p "', async () => {
|
|
71
|
+
const testModule: PermissionsModule = {
|
|
72
|
+
onRequest: jest.fn(async req => {
|
|
73
|
+
// Module can inspect and transform the request
|
|
74
|
+
expect(req.method).toBe('listOutputs')
|
|
75
|
+
expect((req.args as any).basket).toBe('p myscheme some-data')
|
|
76
|
+
// Transform basket for underlying call
|
|
77
|
+
return {
|
|
78
|
+
...req,
|
|
79
|
+
args: { ...(req.args as any), basket: 'transformed-basket' }
|
|
80
|
+
}
|
|
81
|
+
}),
|
|
82
|
+
onResponse: jest.fn(async res => {
|
|
83
|
+
// Module can transform the response
|
|
84
|
+
return { ...res, transformedByModule: true }
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const config: PermissionsManagerConfig = {
|
|
89
|
+
permissionModules: {
|
|
90
|
+
myscheme: testModule
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
95
|
+
|
|
96
|
+
// Mock underlying response
|
|
97
|
+
underlying.listOutputs.mockResolvedValue({ outputs: [] })
|
|
98
|
+
|
|
99
|
+
const result = await manager.listOutputs({ basket: 'p myscheme some-data' }, 'app.com')
|
|
100
|
+
|
|
101
|
+
// Verify module's onRequest was called
|
|
102
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
103
|
+
// Note: args array includes both the request args and originator
|
|
104
|
+
expect(testModule.onRequest).toHaveBeenCalledWith({
|
|
105
|
+
method: 'listOutputs',
|
|
106
|
+
args: { basket: 'p myscheme some-data' },
|
|
107
|
+
originator: 'app.com'
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// Verify underlying was called with transformed basket
|
|
111
|
+
// Note: originator stays as 'app.com' not changed to admin
|
|
112
|
+
expect(underlying.listOutputs).toHaveBeenCalledWith({ basket: 'transformed-basket' }, 'app.com')
|
|
113
|
+
|
|
114
|
+
// Verify module's onResponse was called
|
|
115
|
+
expect(testModule.onResponse).toHaveBeenCalledTimes(1)
|
|
116
|
+
|
|
117
|
+
// Verify response was transformed
|
|
118
|
+
expect(result).toHaveProperty('transformedByModule', true)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('should throw error if P-basket scheme has no registered module', async () => {
|
|
122
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com')
|
|
123
|
+
|
|
124
|
+
await expect(manager.listOutputs({ basket: 'p unregistered-scheme data' }, 'app.com')).rejects.toThrow(
|
|
125
|
+
/Unsupported P-module scheme: p unregistered-scheme/
|
|
126
|
+
)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('should handle normal baskets without delegation', async () => {
|
|
130
|
+
const testModule: PermissionsModule = {
|
|
131
|
+
onRequest: jest.fn(async req => req),
|
|
132
|
+
onResponse: jest.fn(async res => res)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const config: PermissionsManagerConfig = {
|
|
136
|
+
permissionModules: {
|
|
137
|
+
myscheme: testModule
|
|
138
|
+
},
|
|
139
|
+
seekBasketListingPermissions: false
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
143
|
+
|
|
144
|
+
underlying.listOutputs.mockResolvedValue({ outputs: [] })
|
|
145
|
+
|
|
146
|
+
await manager.listOutputs({ basket: 'normal-basket' }, 'app.com')
|
|
147
|
+
|
|
148
|
+
// Module should NOT be called for normal baskets
|
|
149
|
+
expect(testModule.onRequest).not.toHaveBeenCalled()
|
|
150
|
+
expect(testModule.onResponse).not.toHaveBeenCalled()
|
|
151
|
+
|
|
152
|
+
// Underlying should be called with original basket
|
|
153
|
+
expect(underlying.listOutputs).toHaveBeenCalledWith({ basket: 'normal-basket' }, 'app.com')
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('should decrypt metadata in P-basket responses', async () => {
|
|
157
|
+
const testModule: PermissionsModule = {
|
|
158
|
+
onRequest: jest.fn(async req => req),
|
|
159
|
+
onResponse: jest.fn(async res => res)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const config: PermissionsManagerConfig = {
|
|
163
|
+
permissionModules: {
|
|
164
|
+
myscheme: testModule
|
|
165
|
+
},
|
|
166
|
+
encryptWalletMetadata: true
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
170
|
+
|
|
171
|
+
// Mock decryption
|
|
172
|
+
const decryptMetadata = jest.spyOn(manager as any, 'decryptListOutputsMetadata')
|
|
173
|
+
decryptMetadata.mockImplementation(async outputs => outputs)
|
|
174
|
+
|
|
175
|
+
underlying.listOutputs.mockResolvedValue({
|
|
176
|
+
outputs: [{ outpoint: 'txid.0', satoshis: 100 }]
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
await manager.listOutputs({ basket: 'p myscheme data' }, 'app.com')
|
|
180
|
+
|
|
181
|
+
// Verify metadata decryption was called
|
|
182
|
+
expect(decryptMetadata).toHaveBeenCalled()
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
describe('P-Basket Delegation - relinquishOutput', () => {
|
|
187
|
+
it('should delegate to P-module when basket starts with "p "', async () => {
|
|
188
|
+
const testModule: PermissionsModule = {
|
|
189
|
+
onRequest: jest.fn(async req => {
|
|
190
|
+
expect(req.method).toBe('relinquishOutput')
|
|
191
|
+
expect((req.args as any).basket).toBe('p token admin-basket-data')
|
|
192
|
+
return {
|
|
193
|
+
...req,
|
|
194
|
+
args: { ...(req.args as any), basket: 'admin-real-basket' }
|
|
195
|
+
}
|
|
196
|
+
}),
|
|
197
|
+
onResponse: jest.fn(async res => res)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const config: PermissionsManagerConfig = {
|
|
201
|
+
permissionModules: {
|
|
202
|
+
token: testModule
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
207
|
+
|
|
208
|
+
underlying.relinquishOutput.mockResolvedValue(undefined)
|
|
209
|
+
|
|
210
|
+
await manager.relinquishOutput({ basket: 'p token admin-basket-data', output: 'txid.0' }, 'app.com')
|
|
211
|
+
|
|
212
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
213
|
+
// Note: originator remains 'app.com' - only basket is transformed
|
|
214
|
+
expect(underlying.relinquishOutput).toHaveBeenCalledWith(
|
|
215
|
+
{ basket: 'admin-real-basket', output: 'txid.0' },
|
|
216
|
+
'app.com'
|
|
217
|
+
)
|
|
218
|
+
})
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
describe('P-Basket Delegation - createAction', () => {
|
|
222
|
+
it('should delegate to P-module for outputs with P-baskets', async () => {
|
|
223
|
+
const testModule: PermissionsModule = {
|
|
224
|
+
onRequest: jest.fn(async req => {
|
|
225
|
+
// Verify we receive createAction request
|
|
226
|
+
expect(req.method).toBe('createAction')
|
|
227
|
+
return req
|
|
228
|
+
}),
|
|
229
|
+
onResponse: jest.fn(async res => {
|
|
230
|
+
// Module can inspect the transaction result
|
|
231
|
+
return res
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const config: PermissionsManagerConfig = {
|
|
236
|
+
permissionModules: {
|
|
237
|
+
bottle: testModule
|
|
238
|
+
},
|
|
239
|
+
seekSpendingPermissions: false,
|
|
240
|
+
seekBasketInsertionPermissions: false
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
244
|
+
|
|
245
|
+
underlying.createAction.mockResolvedValue({
|
|
246
|
+
txid: 'abc123',
|
|
247
|
+
tx: []
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
await manager.createAction(
|
|
251
|
+
{
|
|
252
|
+
description: 'Test action',
|
|
253
|
+
outputs: [
|
|
254
|
+
{
|
|
255
|
+
lockingScript: 'abcd',
|
|
256
|
+
satoshis: 1000,
|
|
257
|
+
basket: 'p bottle token-data',
|
|
258
|
+
outputDescription: 'Test output'
|
|
259
|
+
}
|
|
260
|
+
]
|
|
261
|
+
},
|
|
262
|
+
'app.com'
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
// Verify P-module was invoked
|
|
266
|
+
expect(testModule.onRequest).toHaveBeenCalled()
|
|
267
|
+
expect(testModule.onResponse).toHaveBeenCalled()
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('should chain multiple P-modules in correct order: req1->req2->req3 then res3->res2->res1', async () => {
|
|
271
|
+
const callOrder: string[] = []
|
|
272
|
+
|
|
273
|
+
const module1: PermissionsModule = {
|
|
274
|
+
onRequest: jest.fn(async req => {
|
|
275
|
+
callOrder.push('req1')
|
|
276
|
+
// First module receives original args without any processing markers
|
|
277
|
+
expect((req.args as any as any).req1Processed).toBeUndefined()
|
|
278
|
+
expect((req.args as any as any).req2Processed).toBeUndefined()
|
|
279
|
+
// Transform args - add marker to track this module processed them
|
|
280
|
+
return {
|
|
281
|
+
...req,
|
|
282
|
+
args: { ...(req.args as any), req1Processed: true }
|
|
283
|
+
}
|
|
284
|
+
}),
|
|
285
|
+
onResponse: jest.fn(async res => {
|
|
286
|
+
callOrder.push('res1')
|
|
287
|
+
// Last module in response chain should see transformations from res2 and res3
|
|
288
|
+
expect((res as any).processedBy).toBe('module2')
|
|
289
|
+
return { ...res, finalProcessedBy: 'module1' }
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const module2: PermissionsModule = {
|
|
294
|
+
onRequest: jest.fn(async req => {
|
|
295
|
+
callOrder.push('req2')
|
|
296
|
+
// Second module receives args transformed by module1
|
|
297
|
+
// (each module gets fresh request object, but args are chained)
|
|
298
|
+
expect((req.args as any as any).req1Processed).toBe(true)
|
|
299
|
+
expect((req.args as any as any).req2Processed).toBeUndefined()
|
|
300
|
+
return {
|
|
301
|
+
...req,
|
|
302
|
+
args: { ...(req.args as any), req2Processed: true }
|
|
303
|
+
}
|
|
304
|
+
}),
|
|
305
|
+
onResponse: jest.fn(async res => {
|
|
306
|
+
callOrder.push('res2')
|
|
307
|
+
// Second-to-last in response chain should see transformation from res3
|
|
308
|
+
expect((res as any).processedBy).toBe('module3')
|
|
309
|
+
return { ...res, processedBy: 'module2' }
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const module3: PermissionsModule = {
|
|
314
|
+
onRequest: jest.fn(async req => {
|
|
315
|
+
callOrder.push('req3')
|
|
316
|
+
// Third module receives args with transformations from both module1 and module2
|
|
317
|
+
expect((req.args as any as any).req1Processed).toBe(true)
|
|
318
|
+
expect((req.args as any as any).req2Processed).toBe(true)
|
|
319
|
+
expect((req.args as any as any).req3Processed).toBeUndefined()
|
|
320
|
+
return {
|
|
321
|
+
...req,
|
|
322
|
+
args: { ...(req.args as any), req3Processed: true }
|
|
323
|
+
}
|
|
324
|
+
}),
|
|
325
|
+
onResponse: jest.fn(async res => {
|
|
326
|
+
callOrder.push('res3')
|
|
327
|
+
// First module in response chain receives raw response from underlying wallet
|
|
328
|
+
expect((res as any).processedBy).toBeUndefined()
|
|
329
|
+
return { ...res, processedBy: 'module3' }
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const config: PermissionsManagerConfig = {
|
|
334
|
+
permissionModules: {
|
|
335
|
+
scheme1: module1,
|
|
336
|
+
scheme2: module2,
|
|
337
|
+
scheme3: module3
|
|
338
|
+
},
|
|
339
|
+
seekSpendingPermissions: false,
|
|
340
|
+
seekBasketInsertionPermissions: false
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
344
|
+
|
|
345
|
+
underlying.createAction.mockResolvedValue({ txid: 'abc', tx: [] })
|
|
346
|
+
|
|
347
|
+
const result = await manager.createAction(
|
|
348
|
+
{
|
|
349
|
+
description: 'Multi-module chain test',
|
|
350
|
+
outputs: [
|
|
351
|
+
{ lockingScript: '1234', satoshis: 100, basket: 'p scheme1 data1', outputDescription: 'Output 1' },
|
|
352
|
+
{ lockingScript: '5678', satoshis: 200, basket: 'p scheme2 data2', outputDescription: 'Output 2' },
|
|
353
|
+
{ lockingScript: '9abc', satoshis: 300, basket: 'p scheme3 data3', outputDescription: 'Output 3' }
|
|
354
|
+
]
|
|
355
|
+
},
|
|
356
|
+
'app.com'
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
// Verify all modules were called
|
|
360
|
+
expect(module1.onRequest).toHaveBeenCalledTimes(1)
|
|
361
|
+
expect(module2.onRequest).toHaveBeenCalledTimes(1)
|
|
362
|
+
expect(module3.onRequest).toHaveBeenCalledTimes(1)
|
|
363
|
+
expect(module1.onResponse).toHaveBeenCalledTimes(1)
|
|
364
|
+
expect(module2.onResponse).toHaveBeenCalledTimes(1)
|
|
365
|
+
expect(module3.onResponse).toHaveBeenCalledTimes(1)
|
|
366
|
+
|
|
367
|
+
// Verify correct order: req1 -> req2 -> req3 then res3 -> res2 -> res1
|
|
368
|
+
expect(callOrder).toEqual(['req1', 'req2', 'req3', 'res3', 'res2', 'res1'])
|
|
369
|
+
|
|
370
|
+
// Verify final result has the complete chain of transformations
|
|
371
|
+
expect((result as any).finalProcessedBy).toBe('module1')
|
|
372
|
+
})
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
describe('P-Basket Delegation - internalizeAction', () => {
|
|
376
|
+
it('should delegate to P-module when P-basket is specified in insertionRemittance', async () => {
|
|
377
|
+
const testModule: PermissionsModule = {
|
|
378
|
+
onRequest: jest.fn(async req => {
|
|
379
|
+
expect(req.method).toBe('internalizeAction')
|
|
380
|
+
return req
|
|
381
|
+
}),
|
|
382
|
+
onResponse: jest.fn(async res => res)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const config: PermissionsManagerConfig = {
|
|
386
|
+
permissionModules: {
|
|
387
|
+
myscheme: testModule
|
|
388
|
+
},
|
|
389
|
+
encryptWalletMetadata: false
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
393
|
+
|
|
394
|
+
underlying.internalizeAction.mockResolvedValue(undefined)
|
|
395
|
+
|
|
396
|
+
await manager.internalizeAction(
|
|
397
|
+
{
|
|
398
|
+
tx: [],
|
|
399
|
+
description: 'Test internalize action',
|
|
400
|
+
outputs: [
|
|
401
|
+
{
|
|
402
|
+
outputIndex: 0,
|
|
403
|
+
protocol: 'basket insertion',
|
|
404
|
+
paymentRemittance: { derivationPrefix: '', derivationSuffix: '', senderIdentityKey: '' },
|
|
405
|
+
insertionRemittance: {
|
|
406
|
+
basket: 'p myscheme data',
|
|
407
|
+
customInstructions: ''
|
|
408
|
+
}
|
|
409
|
+
} as any
|
|
410
|
+
] // Use 'as any' to avoid strict type checking in test
|
|
411
|
+
},
|
|
412
|
+
'app.com'
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
416
|
+
expect(testModule.onResponse).toHaveBeenCalledTimes(1)
|
|
417
|
+
})
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
describe('P-Protocol Delegation', () => {
|
|
421
|
+
it('should delegate getPublicKey to P-protocol module', async () => {
|
|
422
|
+
const testModule: PermissionsModule = {
|
|
423
|
+
onRequest: jest.fn(async req => {
|
|
424
|
+
expect(req.method).toBe('getPublicKey')
|
|
425
|
+
expect((req.args as any).protocolID).toEqual([0, 'p bottle test'])
|
|
426
|
+
return req
|
|
427
|
+
}),
|
|
428
|
+
onResponse: jest.fn(async res => {
|
|
429
|
+
// Module can verify the key or add metadata
|
|
430
|
+
return { ...res, verifiedByModule: true }
|
|
431
|
+
})
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const config: PermissionsManagerConfig = {
|
|
435
|
+
permissionModules: {
|
|
436
|
+
bottle: testModule
|
|
437
|
+
},
|
|
438
|
+
seekPermissionsForPublicKeyRevelation: false
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
442
|
+
|
|
443
|
+
underlying.getPublicKey.mockResolvedValue({ publicKey: '02abc...' })
|
|
444
|
+
|
|
445
|
+
const result = await manager.getPublicKey(
|
|
446
|
+
{
|
|
447
|
+
protocolID: [0, 'p bottle test'],
|
|
448
|
+
keyID: '1',
|
|
449
|
+
counterparty: 'self'
|
|
450
|
+
},
|
|
451
|
+
'app.com'
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
455
|
+
expect(testModule.onResponse).toHaveBeenCalledTimes(1)
|
|
456
|
+
expect(result).toHaveProperty('verifiedByModule', true)
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
it('should delegate createSignature to P-protocol module', async () => {
|
|
460
|
+
const testModule: PermissionsModule = {
|
|
461
|
+
onRequest: jest.fn(async req => {
|
|
462
|
+
expect(req.method).toBe('createSignature')
|
|
463
|
+
expect((req.args as any).protocolID).toEqual([1, 'p token spend'])
|
|
464
|
+
// Module can validate spend amounts, check limits, etc.
|
|
465
|
+
return req
|
|
466
|
+
}),
|
|
467
|
+
onResponse: jest.fn(async res => res)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const config: PermissionsManagerConfig = {
|
|
471
|
+
permissionModules: {
|
|
472
|
+
token: testModule
|
|
473
|
+
},
|
|
474
|
+
seekProtocolPermissionsForSigning: false
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
478
|
+
|
|
479
|
+
underlying.createSignature.mockResolvedValue({ signature: 'abc123' })
|
|
480
|
+
|
|
481
|
+
await manager.createSignature(
|
|
482
|
+
{
|
|
483
|
+
protocolID: [1, 'p token spend'],
|
|
484
|
+
keyID: '1',
|
|
485
|
+
data: [0x01, 0x02]
|
|
486
|
+
},
|
|
487
|
+
'app.com'
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
491
|
+
expect(testModule.onResponse).toHaveBeenCalledTimes(1)
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
it('should delegate verifySignature to P-protocol module', async () => {
|
|
495
|
+
const testModule: PermissionsModule = {
|
|
496
|
+
onRequest: jest.fn(async req => {
|
|
497
|
+
expect(req.method).toBe('verifySignature')
|
|
498
|
+
return req
|
|
499
|
+
}),
|
|
500
|
+
onResponse: jest.fn(async res => res)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const config: PermissionsManagerConfig = {
|
|
504
|
+
permissionModules: {
|
|
505
|
+
secure: testModule
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
510
|
+
|
|
511
|
+
underlying.verifySignature.mockResolvedValue(true)
|
|
512
|
+
|
|
513
|
+
await manager.verifySignature(
|
|
514
|
+
{
|
|
515
|
+
protocolID: [1, 'p secure verify'],
|
|
516
|
+
keyID: '1',
|
|
517
|
+
data: [0x01],
|
|
518
|
+
signature: [0x02]
|
|
519
|
+
},
|
|
520
|
+
'app.com'
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
524
|
+
})
|
|
525
|
+
|
|
526
|
+
it('should delegate encrypt to P-protocol module', async () => {
|
|
527
|
+
const testModule: PermissionsModule = {
|
|
528
|
+
onRequest: jest.fn(async req => {
|
|
529
|
+
expect(req.method).toBe('encrypt')
|
|
530
|
+
expect((req.args as any).protocolID).toEqual([2, 'p secure encrypt'])
|
|
531
|
+
return req
|
|
532
|
+
}),
|
|
533
|
+
onResponse: jest.fn(async res => res)
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const config: PermissionsManagerConfig = {
|
|
537
|
+
permissionModules: {
|
|
538
|
+
secure: testModule
|
|
539
|
+
},
|
|
540
|
+
seekProtocolPermissionsForEncrypting: false
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
544
|
+
|
|
545
|
+
underlying.encrypt.mockResolvedValue({ ciphertext: [0x01, 0x02] })
|
|
546
|
+
|
|
547
|
+
await manager.encrypt(
|
|
548
|
+
{
|
|
549
|
+
protocolID: [2, 'p secure encrypt'],
|
|
550
|
+
keyID: '1',
|
|
551
|
+
plaintext: [0x48, 0x65, 0x6c, 0x6c, 0x6f]
|
|
552
|
+
},
|
|
553
|
+
'app.com'
|
|
554
|
+
)
|
|
555
|
+
|
|
556
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
557
|
+
})
|
|
558
|
+
|
|
559
|
+
it('should delegate decrypt to P-protocol module', async () => {
|
|
560
|
+
const testModule: PermissionsModule = {
|
|
561
|
+
onRequest: jest.fn(async req => {
|
|
562
|
+
expect(req.method).toBe('decrypt')
|
|
563
|
+
return req
|
|
564
|
+
}),
|
|
565
|
+
onResponse: jest.fn(async res => {
|
|
566
|
+
// Module could verify decrypted content
|
|
567
|
+
return res
|
|
568
|
+
})
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const config: PermissionsManagerConfig = {
|
|
572
|
+
permissionModules: {
|
|
573
|
+
secure: testModule
|
|
574
|
+
},
|
|
575
|
+
seekProtocolPermissionsForEncrypting: false
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
579
|
+
|
|
580
|
+
underlying.decrypt.mockResolvedValue({ plaintext: [0x48, 0x65] })
|
|
581
|
+
|
|
582
|
+
await manager.decrypt(
|
|
583
|
+
{
|
|
584
|
+
protocolID: [2, 'p secure decrypt'],
|
|
585
|
+
keyID: '1',
|
|
586
|
+
ciphertext: [0x01, 0x02]
|
|
587
|
+
},
|
|
588
|
+
'app.com'
|
|
589
|
+
)
|
|
590
|
+
|
|
591
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
592
|
+
expect(testModule.onResponse).toHaveBeenCalledTimes(1)
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
it('should delegate createHmac to P-protocol module', async () => {
|
|
596
|
+
const testModule: PermissionsModule = {
|
|
597
|
+
onRequest: jest.fn(async req => {
|
|
598
|
+
expect(req.method).toBe('createHmac')
|
|
599
|
+
return req
|
|
600
|
+
}),
|
|
601
|
+
onResponse: jest.fn(async res => res)
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const config: PermissionsManagerConfig = {
|
|
605
|
+
permissionModules: {
|
|
606
|
+
hmac: testModule
|
|
607
|
+
},
|
|
608
|
+
seekProtocolPermissionsForHMAC: false
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
612
|
+
|
|
613
|
+
underlying.createHmac.mockResolvedValue({ hmac: [0x01] })
|
|
614
|
+
|
|
615
|
+
await manager.createHmac(
|
|
616
|
+
{
|
|
617
|
+
protocolID: [2, 'p hmac test'],
|
|
618
|
+
keyID: '1',
|
|
619
|
+
data: [0x48]
|
|
620
|
+
},
|
|
621
|
+
'app.com'
|
|
622
|
+
)
|
|
623
|
+
|
|
624
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
625
|
+
})
|
|
626
|
+
|
|
627
|
+
it('should delegate verifyHmac to P-protocol module', async () => {
|
|
628
|
+
const testModule: PermissionsModule = {
|
|
629
|
+
onRequest: jest.fn(async req => {
|
|
630
|
+
expect(req.method).toBe('verifyHmac')
|
|
631
|
+
return req
|
|
632
|
+
}),
|
|
633
|
+
onResponse: jest.fn(async res => res)
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const config: PermissionsManagerConfig = {
|
|
637
|
+
permissionModules: {
|
|
638
|
+
hmac: testModule
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
643
|
+
|
|
644
|
+
underlying.verifyHmac.mockResolvedValue(true)
|
|
645
|
+
|
|
646
|
+
await manager.verifyHmac(
|
|
647
|
+
{
|
|
648
|
+
protocolID: [2, 'p hmac verify'],
|
|
649
|
+
keyID: '1',
|
|
650
|
+
data: [0x48],
|
|
651
|
+
hmac: [0x01]
|
|
652
|
+
},
|
|
653
|
+
'app.com'
|
|
654
|
+
)
|
|
655
|
+
|
|
656
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
657
|
+
})
|
|
658
|
+
})
|
|
659
|
+
|
|
660
|
+
describe('P-Module Error Handling', () => {
|
|
661
|
+
it('should throw if P-module onRequest throws', async () => {
|
|
662
|
+
const testModule: PermissionsModule = {
|
|
663
|
+
onRequest: jest.fn(async () => {
|
|
664
|
+
throw new Error('Module validation failed')
|
|
665
|
+
}),
|
|
666
|
+
onResponse: jest.fn(async res => res)
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
const config: PermissionsManagerConfig = {
|
|
670
|
+
permissionModules: {
|
|
671
|
+
failing: testModule
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
676
|
+
|
|
677
|
+
await expect(manager.listOutputs({ basket: 'p failing data' }, 'app.com')).rejects.toThrow(
|
|
678
|
+
'Module validation failed'
|
|
679
|
+
)
|
|
680
|
+
|
|
681
|
+
// Underlying should not be called if module fails
|
|
682
|
+
expect(underlying.listOutputs).not.toHaveBeenCalled()
|
|
683
|
+
})
|
|
684
|
+
|
|
685
|
+
it('should throw if P-module onResponse throws', async () => {
|
|
686
|
+
const testModule: PermissionsModule = {
|
|
687
|
+
onRequest: jest.fn(async req => req),
|
|
688
|
+
onResponse: jest.fn(async () => {
|
|
689
|
+
throw new Error('Module response processing failed')
|
|
690
|
+
})
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
const config: PermissionsManagerConfig = {
|
|
694
|
+
permissionModules: {
|
|
695
|
+
failing: testModule
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
700
|
+
|
|
701
|
+
underlying.listOutputs.mockResolvedValue({ outputs: [] })
|
|
702
|
+
|
|
703
|
+
await expect(manager.listOutputs({ basket: 'p failing data' }, 'app.com')).rejects.toThrow(
|
|
704
|
+
'Module response processing failed'
|
|
705
|
+
)
|
|
706
|
+
|
|
707
|
+
// Underlying was called, but response processing failed
|
|
708
|
+
expect(underlying.listOutputs).toHaveBeenCalled()
|
|
709
|
+
})
|
|
710
|
+
|
|
711
|
+
it('should call P-module onRequest even when permission checks are enabled', async () => {
|
|
712
|
+
const testModule: PermissionsModule = {
|
|
713
|
+
onRequest: jest.fn(async req => {
|
|
714
|
+
// Module validation happens before permission checks
|
|
715
|
+
return req
|
|
716
|
+
}),
|
|
717
|
+
onResponse: jest.fn(async res => res)
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
const config: PermissionsManagerConfig = {
|
|
721
|
+
permissionModules: {
|
|
722
|
+
myscheme: testModule
|
|
723
|
+
},
|
|
724
|
+
seekProtocolPermissionsForSigning: false // Disable to simplify test
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
728
|
+
|
|
729
|
+
underlying.createSignature.mockResolvedValue({ signature: [0x01] })
|
|
730
|
+
|
|
731
|
+
// Create a signature request with P-protocol
|
|
732
|
+
await manager.createSignature(
|
|
733
|
+
{
|
|
734
|
+
protocolID: [1, 'p myscheme sign'],
|
|
735
|
+
keyID: '1',
|
|
736
|
+
data: [0x01]
|
|
737
|
+
},
|
|
738
|
+
'app.com'
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
// Module should have been called
|
|
742
|
+
expect(testModule.onRequest).toHaveBeenCalled()
|
|
743
|
+
expect(testModule.onResponse).toHaveBeenCalled()
|
|
744
|
+
})
|
|
745
|
+
})
|
|
746
|
+
|
|
747
|
+
describe('P-Module Admin Bypass', () => {
|
|
748
|
+
it('should still use P-module even for admin originator', async () => {
|
|
749
|
+
const testModule: PermissionsModule = {
|
|
750
|
+
onRequest: jest.fn(async req => {
|
|
751
|
+
// Module should be called even for admin
|
|
752
|
+
expect(req.originator).toBe('admin.com')
|
|
753
|
+
return req
|
|
754
|
+
}),
|
|
755
|
+
onResponse: jest.fn(async res => res)
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const config: PermissionsManagerConfig = {
|
|
759
|
+
permissionModules: {
|
|
760
|
+
myscheme: testModule
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
const manager = new WalletPermissionsManager(underlying, 'admin.com', config)
|
|
765
|
+
|
|
766
|
+
underlying.listOutputs.mockResolvedValue({ outputs: [] })
|
|
767
|
+
|
|
768
|
+
await manager.listOutputs({ basket: 'p myscheme data' }, 'admin.com')
|
|
769
|
+
|
|
770
|
+
// Module should be invoked even for admin calls
|
|
771
|
+
expect(testModule.onRequest).toHaveBeenCalledTimes(1)
|
|
772
|
+
expect(testModule.onResponse).toHaveBeenCalledTimes(1)
|
|
773
|
+
})
|
|
774
|
+
|
|
775
|
+
it('should still block non-admin access to admin baskets even with P-module', async () => {
|
|
776
|
+
const testModule: PermissionsModule = {
|
|
777
|
+
onRequest: jest.fn(async req => req),
|
|
778
|
+
onResponse: jest.fn(async res => res)
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
const config: PermissionsManagerConfig = {
|
|
782
|
+
permissionModules: {
|
|
783
|
+
myscheme: testModule
|
|
784
|
+
},
|
|
785
|
+
seekBasketListingPermissions: false
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
const manager = new WalletPermissionsManager(underlying, 'customToken.domain.com', config)
|
|
789
|
+
|
|
790
|
+
// Try to access admin basket with non-admin originator
|
|
791
|
+
// Admin baskets are prefixed with 'admin_' by convention
|
|
792
|
+
await expect(manager.listOutputs({ basket: 'admin_someAdminBasket' }, 'app.com')).rejects.toThrow(/admin-only/)
|
|
793
|
+
|
|
794
|
+
// P-module should NOT have been called since admin check happens before P-module delegation
|
|
795
|
+
expect(testModule.onRequest).not.toHaveBeenCalled()
|
|
796
|
+
})
|
|
797
|
+
})
|
|
798
|
+
})
|