@bsv/wallet-toolbox 1.1.62 → 1.2.2

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