@bsv/wallet-toolbox 1.1.25 → 1.1.26

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.
Files changed (123) hide show
  1. package/docs/client.md +78 -2313
  2. package/docs/wallet.md +78 -2313
  3. package/out/src/Wallet.d.ts +1 -6
  4. package/out/src/Wallet.d.ts.map +1 -1
  5. package/out/src/Wallet.js +2 -29
  6. package/out/src/Wallet.js.map +1 -1
  7. package/out/src/index.all.d.ts +0 -9
  8. package/out/src/index.all.d.ts.map +1 -1
  9. package/out/src/index.all.js +0 -9
  10. package/out/src/index.all.js.map +1 -1
  11. package/out/src/index.client.d.ts +0 -9
  12. package/out/src/index.client.d.ts.map +1 -1
  13. package/out/src/index.client.js +0 -9
  14. package/out/src/index.client.js.map +1 -1
  15. package/out/tsconfig.all.tsbuildinfo +1 -1
  16. package/package.json +3 -3
  17. package/src/Wallet.ts +3 -47
  18. package/src/index.all.ts +0 -9
  19. package/src/index.client.ts +0 -9
  20. package/out/src/CWIStyleWalletManager.d.ts +0 -411
  21. package/out/src/CWIStyleWalletManager.d.ts.map +0 -1
  22. package/out/src/CWIStyleWalletManager.js +0 -1131
  23. package/out/src/CWIStyleWalletManager.js.map +0 -1
  24. package/out/src/SetupClient.d.ts +0 -249
  25. package/out/src/SetupClient.d.ts.map +0 -1
  26. package/out/src/SetupClient.js +0 -252
  27. package/out/src/SetupClient.js.map +0 -1
  28. package/out/src/SimpleWalletManager.d.ts +0 -169
  29. package/out/src/SimpleWalletManager.d.ts.map +0 -1
  30. package/out/src/SimpleWalletManager.js +0 -315
  31. package/out/src/SimpleWalletManager.js.map +0 -1
  32. package/out/src/WalletAuthenticationManager.d.ts +0 -33
  33. package/out/src/WalletAuthenticationManager.d.ts.map +0 -1
  34. package/out/src/WalletAuthenticationManager.js +0 -107
  35. package/out/src/WalletAuthenticationManager.js.map +0 -1
  36. package/out/src/WalletPermissionsManager.d.ts +0 -575
  37. package/out/src/WalletPermissionsManager.d.ts.map +0 -1
  38. package/out/src/WalletPermissionsManager.js +0 -1807
  39. package/out/src/WalletPermissionsManager.js.map +0 -1
  40. package/out/src/WalletSettingsManager.d.ts +0 -59
  41. package/out/src/WalletSettingsManager.d.ts.map +0 -1
  42. package/out/src/WalletSettingsManager.js +0 -168
  43. package/out/src/WalletSettingsManager.js.map +0 -1
  44. package/out/src/__tests/CWIStyleWalletManager.test.d.ts +0 -2
  45. package/out/src/__tests/CWIStyleWalletManager.test.d.ts.map +0 -1
  46. package/out/src/__tests/CWIStyleWalletManager.test.js +0 -472
  47. package/out/src/__tests/CWIStyleWalletManager.test.js.map +0 -1
  48. package/out/src/__tests/WalletPermissionsManager.callbacks.test.d.ts +0 -2
  49. package/out/src/__tests/WalletPermissionsManager.callbacks.test.d.ts.map +0 -1
  50. package/out/src/__tests/WalletPermissionsManager.callbacks.test.js +0 -239
  51. package/out/src/__tests/WalletPermissionsManager.callbacks.test.js.map +0 -1
  52. package/out/src/__tests/WalletPermissionsManager.checks.test.d.ts +0 -2
  53. package/out/src/__tests/WalletPermissionsManager.checks.test.d.ts.map +0 -1
  54. package/out/src/__tests/WalletPermissionsManager.checks.test.js +0 -644
  55. package/out/src/__tests/WalletPermissionsManager.checks.test.js.map +0 -1
  56. package/out/src/__tests/WalletPermissionsManager.encryption.test.d.ts +0 -2
  57. package/out/src/__tests/WalletPermissionsManager.encryption.test.d.ts.map +0 -1
  58. package/out/src/__tests/WalletPermissionsManager.encryption.test.js +0 -295
  59. package/out/src/__tests/WalletPermissionsManager.encryption.test.js.map +0 -1
  60. package/out/src/__tests/WalletPermissionsManager.fixtures.d.ts +0 -82
  61. package/out/src/__tests/WalletPermissionsManager.fixtures.d.ts.map +0 -1
  62. package/out/src/__tests/WalletPermissionsManager.fixtures.js +0 -260
  63. package/out/src/__tests/WalletPermissionsManager.fixtures.js.map +0 -1
  64. package/out/src/__tests/WalletPermissionsManager.flows.test.d.ts +0 -2
  65. package/out/src/__tests/WalletPermissionsManager.flows.test.d.ts.map +0 -1
  66. package/out/src/__tests/WalletPermissionsManager.flows.test.js +0 -389
  67. package/out/src/__tests/WalletPermissionsManager.flows.test.js.map +0 -1
  68. package/out/src/__tests/WalletPermissionsManager.initialization.test.d.ts +0 -2
  69. package/out/src/__tests/WalletPermissionsManager.initialization.test.d.ts.map +0 -1
  70. package/out/src/__tests/WalletPermissionsManager.initialization.test.js +0 -227
  71. package/out/src/__tests/WalletPermissionsManager.initialization.test.js.map +0 -1
  72. package/out/src/__tests/WalletPermissionsManager.proxying.test.d.ts +0 -2
  73. package/out/src/__tests/WalletPermissionsManager.proxying.test.d.ts.map +0 -1
  74. package/out/src/__tests/WalletPermissionsManager.proxying.test.js +0 -566
  75. package/out/src/__tests/WalletPermissionsManager.proxying.test.js.map +0 -1
  76. package/out/src/__tests/WalletPermissionsManager.tokens.test.d.ts +0 -2
  77. package/out/src/__tests/WalletPermissionsManager.tokens.test.d.ts.map +0 -1
  78. package/out/src/__tests/WalletPermissionsManager.tokens.test.js +0 -460
  79. package/out/src/__tests/WalletPermissionsManager.tokens.test.js.map +0 -1
  80. package/out/src/utility/identityUtils.d.ts +0 -31
  81. package/out/src/utility/identityUtils.d.ts.map +0 -1
  82. package/out/src/utility/identityUtils.js +0 -114
  83. package/out/src/utility/identityUtils.js.map +0 -1
  84. package/out/src/wab-client/WABClient.d.ts +0 -38
  85. package/out/src/wab-client/WABClient.d.ts.map +0 -1
  86. package/out/src/wab-client/WABClient.js +0 -95
  87. package/out/src/wab-client/WABClient.js.map +0 -1
  88. package/out/src/wab-client/__tests/WABClient.test.d.ts +0 -2
  89. package/out/src/wab-client/__tests/WABClient.test.d.ts.map +0 -1
  90. package/out/src/wab-client/__tests/WABClient.test.js +0 -47
  91. package/out/src/wab-client/__tests/WABClient.test.js.map +0 -1
  92. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.d.ts +0 -34
  93. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.d.ts.map +0 -1
  94. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.js +0 -16
  95. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.js.map +0 -1
  96. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.d.ts +0 -7
  97. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.d.ts.map +0 -1
  98. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.js +0 -40
  99. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.js.map +0 -1
  100. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.d.ts +0 -28
  101. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.d.ts.map +0 -1
  102. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.js +0 -73
  103. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.js.map +0 -1
  104. package/src/CWIStyleWalletManager.ts +0 -1891
  105. package/src/SimpleWalletManager.ts +0 -553
  106. package/src/WalletAuthenticationManager.ts +0 -183
  107. package/src/WalletPermissionsManager.ts +0 -2639
  108. package/src/WalletSettingsManager.ts +0 -241
  109. package/src/__tests/CWIStyleWalletManager.test.ts +0 -709
  110. package/src/__tests/WalletPermissionsManager.callbacks.test.ts +0 -328
  111. package/src/__tests/WalletPermissionsManager.checks.test.ts +0 -857
  112. package/src/__tests/WalletPermissionsManager.encryption.test.ts +0 -407
  113. package/src/__tests/WalletPermissionsManager.fixtures.ts +0 -283
  114. package/src/__tests/WalletPermissionsManager.flows.test.ts +0 -490
  115. package/src/__tests/WalletPermissionsManager.initialization.test.ts +0 -333
  116. package/src/__tests/WalletPermissionsManager.proxying.test.ts +0 -753
  117. package/src/__tests/WalletPermissionsManager.tokens.test.ts +0 -584
  118. package/src/utility/identityUtils.ts +0 -170
  119. package/src/wab-client/WABClient.ts +0 -103
  120. package/src/wab-client/__tests/WABClient.test.ts +0 -58
  121. package/src/wab-client/auth-method-interactors/AuthMethodInteractor.ts +0 -47
  122. package/src/wab-client/auth-method-interactors/PersonaIDInteractor.ts +0 -45
  123. package/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.ts +0 -82
@@ -1,753 +0,0 @@
1
- import {
2
- mockUnderlyingWallet,
3
- MockedBSV_SDK,
4
- MockTransaction
5
- } from './WalletPermissionsManager.fixtures'
6
- import {
7
- WalletPermissionsManager,
8
- PermissionsManagerConfig
9
- } from '../WalletPermissionsManager'
10
-
11
- jest.mock('@bsv/sdk', () => MockedBSV_SDK)
12
-
13
- describe('WalletPermissionsManager - Regression & Integration with Underlying Wallet', () => {
14
- let underlying: jest.Mocked<any>
15
- let manager: WalletPermissionsManager
16
-
17
- beforeEach(() => {
18
- // Create a fresh underlying mock wallet
19
- underlying = mockUnderlyingWallet()
20
- // Default config: everything enforced for maximum coverage
21
- const defaultConfig: PermissionsManagerConfig = {
22
- seekProtocolPermissionsForSigning: true,
23
- seekProtocolPermissionsForEncrypting: true,
24
- seekProtocolPermissionsForHMAC: true,
25
- seekPermissionsForKeyLinkageRevelation: true,
26
- seekPermissionsForPublicKeyRevelation: true,
27
- seekPermissionsForIdentityKeyRevelation: true,
28
- seekPermissionsForIdentityResolution: true,
29
- seekBasketInsertionPermissions: true,
30
- seekBasketRemovalPermissions: true,
31
- seekBasketListingPermissions: true,
32
- seekPermissionWhenApplyingActionLabels: true,
33
- seekPermissionWhenListingActionsByLabel: true,
34
- seekCertificateDisclosurePermissions: true,
35
- seekCertificateAcquisitionPermissions: true,
36
- seekCertificateRelinquishmentPermissions: true,
37
- seekCertificateListingPermissions: true,
38
- encryptWalletMetadata: true,
39
- seekSpendingPermissions: true,
40
- differentiatePrivilegedOperations: true
41
- }
42
- // We pass "admin.test" as the admin origin
43
- manager = new WalletPermissionsManager(
44
- underlying,
45
- 'admin.test',
46
- defaultConfig
47
- )
48
-
49
- // For these tests, we don't want to deal with UI prompts or real user interactions.
50
- // We stub out any permission requests by auto-granting ephemeral in all cases
51
- manager.bindCallback('onProtocolPermissionRequested', async req => {
52
- await manager.grantPermission({
53
- requestID: req.requestID,
54
- ephemeral: true
55
- })
56
- })
57
- manager.bindCallback('onBasketAccessRequested', async req => {
58
- await manager.grantPermission({
59
- requestID: req.requestID,
60
- ephemeral: true
61
- })
62
- })
63
- manager.bindCallback('onCertificateAccessRequested', async req => {
64
- await manager.grantPermission({
65
- requestID: req.requestID,
66
- ephemeral: true
67
- })
68
- })
69
- manager.bindCallback('onSpendingAuthorizationRequested', async req => {
70
- // If the request is for a netSpent above some threshold, let's simulate a denial for one test scenario
71
- // By default, we'll just ephemeral-grant.
72
- await manager.grantPermission({
73
- requestID: req.requestID,
74
- ephemeral: true
75
- })
76
- })
77
- })
78
-
79
- afterEach(() => {
80
- jest.clearAllMocks()
81
- })
82
-
83
- /* -------------------------------------------------------------------------
84
- * createAction / signAction / abortAction
85
- * ----------------------------------------------------------------------- */
86
-
87
- it('should pass createAction calls through, label them, handle metadata encryption, and check spending authorization', async () => {
88
- // We'll mock the "netSpent" scenario to be >0 by returning some mock input & output satoshis from the signableTransaction.
89
- // The underlying mock createAction returns a signableTransaction with tx = []
90
- // We can stub out the mock so that the manager sees inputs/outputs with certain sat amounts.
91
- // But we have to remember the manager is parsing the signableTransaction via fromAtomicBEEF(…).
92
- // We'll control that by adjusting the mock signableTransaction in the underlying.
93
-
94
- // let's set a custom signableTransaction that returns 500 sat in inputs, 1000 in outputs, and 100 in fee
95
- underlying.createAction.mockResolvedValueOnce({
96
- signableTransaction: {
97
- // The manager calls Transaction.fromAtomicBEEF() on this
98
- tx: [0xde, 0xad], // not used in detail, but let's just pass some array
99
- reference: 'test-ref'
100
- }
101
- })
102
-
103
- // We also need to configure the fromAtomicBEEF mock so it returns a transaction with the specified inputs/outputs
104
- const mockTx = new MockTransaction()
105
- mockTx.fee = 100
106
- // We'll define exactly one input we consider "originator-provided" with 500 sat
107
- mockTx.inputs = [
108
- {
109
- sourceTXID: 'aaa',
110
- sourceOutputIndex: 0,
111
- sourceTransaction: {
112
- outputs: [{ satoshis: 500 }]
113
- }
114
- }
115
- ]
116
- // We'll define 2 outputs. The manager will read the output amounts from the createAction call's "args.outputs" too,
117
- // but we also set them here in case it cross-references them. We'll keep it consistent (2 outputs with total 1000).
118
- mockTx.outputs = [{ satoshis: 600 }, { satoshis: 400 }]
119
-
120
- // Now override fromAtomicBEEF to return our mockTx:
121
- ;(MockedBSV_SDK.Transaction.fromAtomicBEEF as jest.Mock).mockReturnValue(
122
- mockTx
123
- )
124
-
125
- // Attempt to create an action from a non-admin origin
126
- await manager.createAction(
127
- {
128
- description: 'User purchase',
129
- inputs: [
130
- {
131
- outpoint: 'aaa.0',
132
- unlockingScriptLength: 73,
133
- inputDescription: 'My input'
134
- }
135
- ],
136
- outputs: [
137
- {
138
- lockingScript: '00abcd',
139
- satoshis: 1000,
140
- outputDescription: 'Purchase output',
141
- basket: 'my-basket'
142
- }
143
- ],
144
- labels: ['user-label', 'something-else']
145
- },
146
- 'shop.example.com'
147
- )
148
-
149
- // The manager should have:
150
- // 1) Called underlying.createAction
151
- // 2) Inserted "admin originator shop.example.com" & "admin month YYYY-MM" into labels
152
- // 3) Encrypted the metadata fields (description, inputDescription, outputDescription)
153
- // 4) Ensured we needed spending permission for netSpent= (1000 + fee100) - 500 = 600
154
- // The onSpendingAuthorizationRequested callback ephemeral-granted it.
155
- expect(underlying.createAction).toHaveBeenCalledTimes(1)
156
- const callArgs = underlying.createAction.mock.calls[0][0]
157
- expect(callArgs.labels).toContain('admin originator shop.example.com')
158
- expect(callArgs.labels).toEqual(
159
- expect.arrayContaining([
160
- expect.stringContaining('admin month'),
161
- 'user-label',
162
- 'something-else',
163
- 'admin originator shop.example.com'
164
- ])
165
- )
166
- // Confirm the metadata was replaced with some ciphertext array in createAction call
167
- expect(callArgs.description).not.toBe('User purchase') // manager encrypts it
168
- if (callArgs.inputs[0].inputDescription) {
169
- expect(callArgs.inputs[0].inputDescription).not.toBe('My input')
170
- }
171
- if (callArgs.outputs[0].outputDescription) {
172
- expect(callArgs.outputs[0].outputDescription).not.toBe('Purchase output')
173
- }
174
-
175
- // Also confirm we set signAndProcess to false if origin is non-admin
176
- expect(callArgs.options.signAndProcess).toBe(false)
177
-
178
- // The manager will parse the resulting signableTransaction, see netSpent=600, and request spending permission.
179
- // Our callback ephemeral-granted. So everything should proceed with no error.
180
- // The manager returns the partial result from underlying
181
- // We don't have a final sign call from the manager because signAndProcess is forcibly false.
182
- })
183
-
184
- it('should abort the action if spending permission is denied', async () => {
185
- // This time let's forcibly DENY the onSpendingAuthorizationRequested callback
186
- manager.unbindCallback('onSpendingAuthorizationRequested', 0) // Unbind the ephemeral-grant
187
- manager.bindCallback('onSpendingAuthorizationRequested', async req => {
188
- await manager.denyPermission(req.requestID)
189
- })
190
-
191
- // We'll use the same approach: netSpent > 0 triggers the spending authorization check.
192
- underlying.createAction.mockResolvedValueOnce({
193
- signableTransaction: {
194
- tx: [0xde],
195
- reference: 'test-ref-2'
196
- }
197
- })
198
-
199
- // Mock parse tx for netSpent
200
- const mockTx = new MockTransaction()
201
- mockTx.fee = 100
202
- mockTx.inputs = [
203
- {
204
- sourceTXID: 'bbb',
205
- sourceOutputIndex: 0,
206
- sourceTransaction: {
207
- outputs: [{ satoshis: 0 }]
208
- }
209
- }
210
- ]
211
- mockTx.outputs = [{ satoshis: 100 }]
212
- ;(MockedBSV_SDK.Transaction.fromAtomicBEEF as jest.Mock).mockReturnValue(
213
- mockTx
214
- )
215
-
216
- await expect(
217
- manager.createAction(
218
- {
219
- description:
220
- 'User tries to spend 100 + fee=100 from 0 input => netSpent=200',
221
- outputs: [
222
- {
223
- lockingScript: 'abc123',
224
- satoshis: 100,
225
- outputDescription: 'some out desc',
226
- basket: 'some-basket'
227
- }
228
- ]
229
- },
230
- 'user.example.com'
231
- )
232
- ).rejects.toThrow(/Permission denied/)
233
-
234
- // We expect the manager to call underlying.abortAction with reference 'test-ref-2'
235
- expect(underlying.abortAction).toHaveBeenCalledTimes(1)
236
- expect(underlying.abortAction).toHaveBeenCalledWith({
237
- reference: 'test-ref-2'
238
- })
239
- })
240
-
241
- it('should throw an error if a non-admin tries signAndProcess=true', async () => {
242
- // Non-admin tries signAndProcess=true => manager throws
243
- await expect(
244
- manager.createAction(
245
- {
246
- description: 'Trying signAndProcess from non-admin',
247
- outputs: [
248
- {
249
- lockingScript: '1234',
250
- satoshis: 50,
251
- basket: 'user-basket',
252
- outputDescription: 'Description'
253
- }
254
- ],
255
- options: {
256
- signAndProcess: true
257
- }
258
- },
259
- 'someuser.com'
260
- )
261
- ).rejects.toThrow(/Only the admin originator can set signAndProcess=true/)
262
- })
263
-
264
- it('should proxy signAction calls directly if invoked by the user', async () => {
265
- // Typically, signAction is used after createAction returns a partial signableTransaction
266
- // We'll confirm it passes arguments verbatim to underlying
267
- const result = await manager.signAction(
268
- {
269
- reference: 'my-ref',
270
- spends: {
271
- 0: {
272
- unlockingScript: 'my-script'
273
- }
274
- }
275
- },
276
- 'nonadmin.com'
277
- )
278
- expect(underlying.signAction).toHaveBeenCalledTimes(1)
279
- expect(underlying.signAction).toHaveBeenCalledWith(
280
- {
281
- reference: 'my-ref',
282
- spends: {
283
- 0: {
284
- unlockingScript: 'my-script'
285
- }
286
- }
287
- },
288
- 'nonadmin.com'
289
- )
290
- // returns the underlying result
291
- expect(result.txid).toBe('fake-txid')
292
- })
293
-
294
- it('should proxy abortAction calls directly', async () => {
295
- const result = await manager.abortAction(
296
- { reference: 'abort-me' },
297
- 'someuser.com'
298
- )
299
- expect(underlying.abortAction).toHaveBeenCalledTimes(1)
300
- expect(underlying.abortAction).toHaveBeenCalledWith(
301
- { reference: 'abort-me' },
302
- 'someuser.com'
303
- )
304
- expect(result).toEqual({ aborted: true })
305
- })
306
-
307
- /* -------------------------------------------------------------------------
308
- * listActions / internalizeAction
309
- * ----------------------------------------------------------------------- */
310
-
311
- it('should call listActions on the underlying wallet and decrypt metadata fields if encryptWalletMetadata=true', async () => {
312
- // Underlying returns some encrypted metadata
313
- underlying.listActions.mockResolvedValueOnce({
314
- totalActions: 1,
315
- actions: [
316
- {
317
- actionTXID: 'aaa',
318
- description: 'EncryptedStuff',
319
- inputs: [
320
- {
321
- outpoint: 'xxx.0',
322
- inputDescription: 'EncryptedIn'
323
- }
324
- ],
325
- outputs: [
326
- {
327
- lockingScript: 'deadbeef',
328
- outputDescription: 'EncryptedOut',
329
- customInstructions: 'EncryptedCustom'
330
- }
331
- ],
332
- labels: ['user-label']
333
- }
334
- ]
335
- })
336
- // We'll have the manager attempt to decrypt. The manager calls `underlying.decrypt`
337
- // which is mocked to return plaintext [42, 42]. That is "asterisk-asterisk" in ASCII
338
- // So let's see how the manager transforms it back to a string: fromCharCode(42,42) => "**"
339
- // However, note that the manager's "maybeDecryptMetadata()" tries to decrypt the field
340
- // If it works, it returns the decrypted string. Our underlying mock decrypt => "[42,42]" => "**"
341
- // So let's expect the final returned fields to be "**".
342
-
343
- const result = await manager.listActions(
344
- { labels: ['some-label'] },
345
- 'nonadmin.com'
346
- )
347
-
348
- expect(underlying.listActions).toHaveBeenCalledTimes(1)
349
- // The manager calls ensureLabelAccess first, which triggers a protocol permission request
350
- // we ephemeral-grant. Then it calls underlying.listActions.
351
- expect(result.actions[0].description).toBe('**') // Decrypted from [42, 42]
352
- expect(result.actions[0].inputs![0].inputDescription).toBe('**')
353
- expect(result.actions[0].outputs![0].outputDescription).toBe('**')
354
- expect(result.actions[0].outputs![0].customInstructions).toBe('**')
355
- })
356
-
357
- it('should pass internalizeAction calls to underlying, after ensuring basket permissions and encrypting customInstructions if config=on', async () => {
358
- await manager.internalizeAction(
359
- {
360
- tx: [],
361
- description: 'Internalizing outputs with basket insertion',
362
- outputs: [
363
- {
364
- outputIndex: 0,
365
- protocol: 'basket insertion',
366
- insertionRemittance: {
367
- basket: 'some-basket',
368
- customInstructions: 'plaintext instructions'
369
- }
370
- }
371
- ]
372
- },
373
- 'someuser.com'
374
- )
375
-
376
- // The manager ensures basket insertion => ephemeral permission granted
377
- // Then it encrypts 'plaintext instructions' before passing it to underlying
378
- expect(underlying.internalizeAction).toHaveBeenCalledTimes(1)
379
- const callArgs = underlying.internalizeAction.mock.calls[0][0]
380
- expect(callArgs.outputs[0].insertionRemittance.customInstructions).not.toBe(
381
- 'plaintext instructions'
382
- )
383
- // There's no direct check that the string is "**" or something, because it's encrypted.
384
- // We just confirm it was changed from the original plaintext.
385
- })
386
-
387
- /* -------------------------------------------------------------------------
388
- * listOutputs / relinquishOutput
389
- * ----------------------------------------------------------------------- */
390
-
391
- it('should ensure basket listing permission then call listOutputs, decrypting customInstructions', async () => {
392
- underlying.listOutputs.mockResolvedValue({
393
- totalOutputs: 1,
394
- outputs: [
395
- {
396
- outpoint: 'zzz.1',
397
- satoshis: 100,
398
- lockingScript: 'mockscript',
399
- customInstructions: 'EncryptedWeird'
400
- }
401
- ]
402
- })
403
-
404
- const result = await manager.listOutputs(
405
- { basket: 'user-basket' },
406
- 'app.example.com'
407
- )
408
- // manager ephemeral-grants basket permission
409
- expect(underlying.listOutputs).toHaveBeenCalledTimes(2)
410
- expect(underlying.listOutputs.mock.calls).toEqual([
411
- [
412
- {
413
- basket: 'admin basket-access',
414
- include: 'locking scripts',
415
- tagQueryMode: 'all',
416
- tags: ['originator app.example.com', 'basket user-basket']
417
- },
418
- 'admin.test' // querying to see if we have permission
419
- ],
420
- [
421
- {
422
- basket: 'user-basket'
423
- },
424
- 'app.example.com' // the actual underlying call
425
- ]
426
- ])
427
- expect(result.outputs[0].customInstructions).toBe('**') // from [42,42] decryption
428
- })
429
-
430
- it('should ensure basket removal permission then call relinquishOutput', async () => {
431
- await manager.relinquishOutput(
432
- {
433
- output: 'xxx.0',
434
- basket: 'some-basket'
435
- },
436
- 'nonadmin.com'
437
- )
438
- expect(underlying.relinquishOutput).toHaveBeenCalledTimes(1)
439
- expect(underlying.relinquishOutput).toHaveBeenCalledWith(
440
- { output: 'xxx.0', basket: 'some-basket' },
441
- 'nonadmin.com'
442
- )
443
- })
444
-
445
- /* -------------------------------------------------------------------------
446
- * getPublicKey / revealCounterpartyKeyLinkage / revealSpecificKeyLinkage
447
- * ----------------------------------------------------------------------- */
448
-
449
- it('should call getPublicKey on underlying after ensuring protocol permission', async () => {
450
- const result = await manager.getPublicKey(
451
- {
452
- protocolID: [1, 'test-pubkey'],
453
- keyID: 'my-key'
454
- },
455
- 'user.example.com'
456
- )
457
-
458
- expect(underlying.getPublicKey).toHaveBeenCalledTimes(1)
459
- expect(underlying.getPublicKey).toHaveBeenCalledWith(
460
- {
461
- protocolID: [1, 'test-pubkey'],
462
- keyID: 'my-key'
463
- },
464
- 'user.example.com'
465
- )
466
- expect(result.publicKey).toBe('029999...')
467
- })
468
-
469
- it('should call revealCounterpartyKeyLinkage with permission check, pass result', async () => {
470
- const result = await manager.revealCounterpartyKeyLinkage(
471
- {
472
- privileged: true,
473
- verifier: '0222aaa',
474
- counterparty: '02bbbccc',
475
- privilegedReason: 'test reason'
476
- },
477
- 'user.example.com'
478
- )
479
-
480
- expect(underlying.revealCounterpartyKeyLinkage).toHaveBeenCalledTimes(1)
481
- expect(underlying.revealCounterpartyKeyLinkage).toHaveBeenCalledWith(
482
- {
483
- privileged: true,
484
- verifier: '0222aaa',
485
- counterparty: '02bbbccc',
486
- privilegedReason: 'test reason'
487
- },
488
- 'user.example.com'
489
- )
490
- expect(result.prover).toBe('02abcdef...')
491
- })
492
-
493
- it('should call revealSpecificKeyLinkage with permission check, pass result', async () => {
494
- const result = await manager.revealSpecificKeyLinkage(
495
- {
496
- privileged: false,
497
- verifier: '0222ddd',
498
- protocolID: [2, 'special'],
499
- keyID: '5',
500
- counterparty: '022222',
501
- privilegedReason: 'need to check link'
502
- },
503
- 'user.example.com'
504
- )
505
-
506
- expect(underlying.revealSpecificKeyLinkage).toHaveBeenCalledTimes(1)
507
- expect(underlying.revealSpecificKeyLinkage).toHaveBeenCalledWith(
508
- {
509
- privileged: false,
510
- verifier: '0222ddd',
511
- protocolID: [2, 'special'],
512
- keyID: '5',
513
- counterparty: '022222',
514
- privilegedReason: 'need to check link'
515
- },
516
- 'user.example.com'
517
- )
518
- expect(result.prover).toBe('02abcdef...')
519
- })
520
-
521
- /* -------------------------------------------------------------------------
522
- * encrypt / decrypt / createHmac / verifyHmac / createSignature / verifySignature
523
- * ----------------------------------------------------------------------- */
524
-
525
- it('should proxy encrypt() calls after checking protocol permission', async () => {
526
- const result = await manager.encrypt(
527
- {
528
- protocolID: [1, 'secret-proto'],
529
- plaintext: [1, 2, 3],
530
- keyID: 'mykey'
531
- },
532
- 'user.example.com'
533
- )
534
-
535
- expect(underlying.encrypt).toHaveBeenCalledTimes(1)
536
- expect(result.ciphertext).toEqual([42, 42, 42]) // from the mock
537
- })
538
-
539
- it('should proxy decrypt() calls after checking protocol permission', async () => {
540
- const result = await manager.decrypt(
541
- {
542
- protocolID: [1, 'secret-proto'],
543
- ciphertext: [99, 99],
544
- keyID: 'somekey'
545
- },
546
- 'user.example.com'
547
- )
548
- expect(underlying.decrypt).toHaveBeenCalledTimes(1)
549
- expect(result.plaintext).toEqual([42, 42])
550
- })
551
-
552
- it('should proxy createHmac() calls', async () => {
553
- const result = await manager.createHmac(
554
- {
555
- protocolID: [2, 'hmac-proto'],
556
- data: [11, 22],
557
- keyID: 'hmacKey'
558
- },
559
- 'someone.com'
560
- )
561
- expect(underlying.createHmac).toHaveBeenCalledTimes(1)
562
- expect(result.hmac).toEqual([0xaa])
563
- })
564
-
565
- it('should proxy verifyHmac() calls', async () => {
566
- const result = await manager.verifyHmac(
567
- {
568
- protocolID: [2, 'hmac-proto'],
569
- data: [11, 22],
570
- hmac: [0xaa],
571
- keyID: 'hmacKey'
572
- },
573
- 'someone.com'
574
- )
575
- expect(underlying.verifyHmac).toHaveBeenCalledTimes(1)
576
- expect(result.valid).toBe(true)
577
- })
578
-
579
- it('should proxy createSignature() calls (already tested the netSpent logic in createAction, but let’s double-check)', async () => {
580
- // We tested permission checks for signing in earlier tests, but let's confirm pass-through
581
- const result = await manager.createSignature(
582
- {
583
- protocolID: [1, 'sign-proto'],
584
- data: [10, 20],
585
- keyID: '1'
586
- },
587
- 'user.com'
588
- )
589
- expect(underlying.createSignature).toHaveBeenCalledTimes(1)
590
- expect(result.signature).toEqual([0x30, 0x44])
591
- })
592
-
593
- it('should proxy verifySignature() calls', async () => {
594
- const result = await manager.verifySignature(
595
- {
596
- protocolID: [1, 'verify-proto'],
597
- data: [3, 4],
598
- signature: [0x30, 0x44],
599
- keyID: '2'
600
- },
601
- 'user.com'
602
- )
603
- expect(underlying.verifySignature).toHaveBeenCalledTimes(1)
604
- expect(result.valid).toBe(true)
605
- })
606
-
607
- /* -------------------------------------------------------------------------
608
- * acquireCertificate / listCertificates / proveCertificate / relinquishCertificate
609
- * ----------------------------------------------------------------------- */
610
-
611
- it('should call acquireCertificate, verifying permission if config.seekCertificateAcquisitionPermissions=true', async () => {
612
- const result = await manager.acquireCertificate(
613
- {
614
- type: 'my-cert',
615
- certifier: '02aaaa...',
616
- acquisitionProtocol: 'direct',
617
- fields: { hello: 'world' }
618
- },
619
- 'user.cert.com'
620
- )
621
- expect(underlying.acquireCertificate).toHaveBeenCalledTimes(1)
622
- expect(result.type).toBe('some-cert-type')
623
- })
624
-
625
- it('should call listCertificates, verifying permission if config.seekCertificateListingPermissions=true', async () => {
626
- const result = await manager.listCertificates(
627
- {
628
- privileged: false,
629
- certifiers: [],
630
- types: []
631
- },
632
- 'some.corp'
633
- )
634
- expect(underlying.listCertificates).toHaveBeenCalledTimes(1)
635
- expect(result.totalCertificates).toBe(0)
636
- })
637
-
638
- it('should call proveCertificate after ensuring certificate permission', async () => {
639
- const result = await manager.proveCertificate(
640
- {
641
- privileged: true,
642
- verifier: '02vvvv',
643
- certificate: {
644
- type: 'kyc',
645
- subject: '02aaaa...',
646
- certifier: '02cccc...',
647
- fields: { name: 'Alice' }
648
- },
649
- fieldsToReveal: ['name']
650
- },
651
- 'user.corp'
652
- )
653
- expect(underlying.proveCertificate).toHaveBeenCalledTimes(1)
654
- expect(result.keyringForVerifier).toBeDefined()
655
- })
656
-
657
- it('should call relinquishCertificate if config.seekCertificateRelinquishmentPermissions=true', async () => {
658
- const result = await manager.relinquishCertificate(
659
- {
660
- type: 'some-cert',
661
- serialNumber: 'raisin bran',
662
- certifier: '023333'
663
- },
664
- 'user-abc.com'
665
- )
666
- expect(underlying.relinquishCertificate).toHaveBeenCalledTimes(1)
667
- expect(result).toEqual({ relinquished: true })
668
- })
669
-
670
- /* -------------------------------------------------------------------------
671
- * discoverByIdentityKey / discoverByAttributes
672
- * ----------------------------------------------------------------------- */
673
-
674
- it('should call discoverByIdentityKey after ensuring identity resolution permission', async () => {
675
- const result = await manager.discoverByIdentityKey(
676
- { identityKey: '0222fff...' },
677
- 'someone-trying-lookup.com'
678
- )
679
- expect(underlying.discoverByIdentityKey).toHaveBeenCalledTimes(1)
680
- expect(result.certificates.length).toBe(0)
681
- })
682
-
683
- it('should call discoverByAttributes after ensuring identity resolution permission', async () => {
684
- const result = await manager.discoverByAttributes(
685
- { attributes: { name: 'Bob' } },
686
- 'someone-trying-lookup.com'
687
- )
688
- expect(underlying.discoverByAttributes).toHaveBeenCalledTimes(1)
689
- expect(result.certificates.length).toBe(0)
690
- })
691
-
692
- /* -------------------------------------------------------------------------
693
- * isAuthenticated / waitForAuthentication / getHeight / getHeaderForHeight
694
- * getNetwork / getVersion
695
- * ----------------------------------------------------------------------- */
696
-
697
- it('should proxy isAuthenticated without any special permission checks', async () => {
698
- const result = await manager.isAuthenticated({}, 'someone.com')
699
- expect(result.authenticated).toBe(true)
700
- expect(underlying.isAuthenticated).toHaveBeenCalledTimes(1)
701
- })
702
-
703
- it('should proxy waitForAuthentication without any special permission checks', async () => {
704
- const result = await manager.waitForAuthentication({}, 'someone.com')
705
- expect(result.authenticated).toBe(true)
706
- expect(underlying.waitForAuthentication).toHaveBeenCalledTimes(1)
707
- })
708
-
709
- it('should proxy getHeight', async () => {
710
- const result = await manager.getHeight({}, 'someone.com')
711
- expect(result.height).toBe(777777)
712
- expect(underlying.getHeight).toHaveBeenCalledTimes(1)
713
- })
714
-
715
- it('should proxy getHeaderForHeight', async () => {
716
- const result = await manager.getHeaderForHeight(
717
- { height: 100000 },
718
- 'someone.com'
719
- )
720
- expect(result.header).toMatch(/000000000000abc/)
721
- expect(underlying.getHeaderForHeight).toHaveBeenCalledTimes(1)
722
- })
723
-
724
- it('should proxy getNetwork', async () => {
725
- const result = await manager.getNetwork({}, 'someone.com')
726
- expect(result.network).toBe('testnet')
727
- expect(underlying.getNetwork).toHaveBeenCalledTimes(1)
728
- })
729
-
730
- it('should proxy getVersion', async () => {
731
- const result = await manager.getVersion({}, 'someone.com')
732
- expect(result.version).toBe('vendor-1.0.0')
733
- expect(underlying.getVersion).toHaveBeenCalledTimes(1)
734
- })
735
-
736
- /* -------------------------------------------------------------------------
737
- * Error propagation from underlying
738
- * ----------------------------------------------------------------------- */
739
-
740
- it('should propagate errors from the underlying wallet calls', async () => {
741
- // Let's have underlying.createAction throw
742
- underlying.createAction.mockRejectedValueOnce(
743
- new Error('Under-wallet failure')
744
- )
745
-
746
- await expect(
747
- manager.createAction(
748
- { description: 'test error', outputs: [] },
749
- 'someuser.com'
750
- )
751
- ).rejects.toThrow(/Under-wallet failure/)
752
- })
753
- })