@bsv/wallet-toolbox 1.1.61 → 1.2.1

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 (199) 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 +1153 -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/ARC.test.js +9 -0
  85. package/out/src/services/__tests/ARC.test.js.map +1 -1
  86. package/out/src/services/__tests/bitrails.test.js +7 -2
  87. package/out/src/services/__tests/bitrails.test.js.map +1 -1
  88. package/out/src/services/providers/ARC.js +1 -1
  89. package/out/src/services/providers/ARC.js.map +1 -1
  90. package/out/src/services/providers/__tests/WhatsOnChain.test.js +3 -3
  91. package/out/src/services/providers/__tests/WhatsOnChain.test.js.map +1 -1
  92. package/out/src/signer/methods/proveCertificate.d.ts.map +1 -1
  93. package/out/src/signer/methods/proveCertificate.js +3 -19
  94. package/out/src/signer/methods/proveCertificate.js.map +1 -1
  95. package/out/src/storage/__test/WalletStorageManager.test.js +1 -1
  96. package/out/src/storage/__test/WalletStorageManager.test.js.map +1 -1
  97. package/out/src/storage/methods/listOutputs.js +1 -1
  98. package/out/src/storage/methods/listOutputs.js.map +1 -1
  99. package/out/src/storage/remoting/StorageClient.d.ts +2 -2
  100. package/out/src/storage/remoting/StorageClient.d.ts.map +1 -1
  101. package/out/src/storage/remoting/StorageClient.js +1 -1
  102. package/out/src/storage/remoting/StorageClient.js.map +1 -1
  103. package/out/src/utility/identityUtils.d.ts +31 -0
  104. package/out/src/utility/identityUtils.d.ts.map +1 -0
  105. package/out/src/utility/identityUtils.js +116 -0
  106. package/out/src/utility/identityUtils.js.map +1 -0
  107. package/out/src/wab-client/WABClient.d.ts +49 -0
  108. package/out/src/wab-client/WABClient.d.ts.map +1 -0
  109. package/out/src/wab-client/WABClient.js +83 -0
  110. package/out/src/wab-client/WABClient.js.map +1 -0
  111. package/out/src/wab-client/__tests/WABClient.man.test.d.ts +2 -0
  112. package/out/src/wab-client/__tests/WABClient.man.test.d.ts.map +1 -0
  113. package/out/src/wab-client/__tests/WABClient.man.test.js +52 -0
  114. package/out/src/wab-client/__tests/WABClient.man.test.js.map +1 -0
  115. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.d.ts +34 -0
  116. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.d.ts.map +1 -0
  117. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.js +16 -0
  118. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.js.map +1 -0
  119. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.d.ts +7 -0
  120. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.d.ts.map +1 -0
  121. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.js +36 -0
  122. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.js.map +1 -0
  123. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.d.ts +28 -0
  124. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.d.ts.map +1 -0
  125. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.js +69 -0
  126. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.js.map +1 -0
  127. package/out/test/Wallet/action/internalizeAction.a.test.js +1 -1
  128. package/out/test/Wallet/action/internalizeAction.a.test.js.map +1 -1
  129. package/out/test/Wallet/certificate/acquireCertificate.test.js +26 -29
  130. package/out/test/Wallet/certificate/acquireCertificate.test.js.map +1 -1
  131. package/out/test/Wallet/local/localWallet.man.test.d.ts.map +1 -1
  132. package/out/test/Wallet/local/localWallet.man.test.js +25 -10
  133. package/out/test/Wallet/local/localWallet.man.test.js.map +1 -1
  134. package/out/test/storage/KnexMigrations.test.js +1 -1
  135. package/out/test/storage/KnexMigrations.test.js.map +1 -1
  136. package/out/test/storage/update.test.js +1 -1
  137. package/out/test/storage/update.test.js.map +1 -1
  138. package/out/test/utils/TestUtilsWalletStorage.d.ts +9 -5
  139. package/out/test/utils/TestUtilsWalletStorage.d.ts.map +1 -1
  140. package/out/test/utils/TestUtilsWalletStorage.js +15 -9
  141. package/out/test/utils/TestUtilsWalletStorage.js.map +1 -1
  142. package/out/test/wallet/action/internalizeAction.test.js +1 -1
  143. package/out/test/wallet/action/internalizeAction.test.js.map +1 -1
  144. package/out/test/wallet/list/listActions2.test.js +1 -1
  145. package/out/test/wallet/list/listActions2.test.js.map +1 -1
  146. package/out/test/wallet/sync/Wallet.sync.test.js +1 -1
  147. package/out/test/wallet/sync/Wallet.sync.test.js.map +1 -1
  148. package/out/tsconfig.all.tsbuildinfo +1 -1
  149. package/package.json +3 -4
  150. package/src/CWIStyleWalletManager.ts +1738 -0
  151. package/src/SimpleWalletManager.ts +526 -0
  152. package/src/Wallet.ts +70 -7
  153. package/src/WalletAuthenticationManager.ts +150 -0
  154. package/src/WalletPermissionsManager.ts +2424 -0
  155. package/src/WalletSettingsManager.ts +243 -0
  156. package/src/__tests/CWIStyleWalletManager.test.ts +604 -0
  157. package/src/__tests/WalletPermissionsManager.callbacks.test.ts +323 -0
  158. package/src/__tests/WalletPermissionsManager.checks.test.ts +839 -0
  159. package/src/__tests/WalletPermissionsManager.encryption.test.ts +370 -0
  160. package/src/__tests/WalletPermissionsManager.fixtures.ts +284 -0
  161. package/src/__tests/WalletPermissionsManager.flows.test.ts +457 -0
  162. package/src/__tests/WalletPermissionsManager.initialization.test.ts +300 -0
  163. package/src/__tests/WalletPermissionsManager.proxying.test.ts +706 -0
  164. package/src/__tests/WalletPermissionsManager.tokens.test.ts +546 -0
  165. package/src/index.all.ts +9 -0
  166. package/src/index.client.ts +9 -0
  167. package/src/sdk/CertOpsWallet.ts +18 -0
  168. package/src/sdk/__test/CertificateLifeCycle.test.ts +66 -113
  169. package/src/sdk/index.ts +1 -1
  170. package/src/sdk/validationHelpers.ts +12 -11
  171. package/src/services/__tests/ARC.test.ts +14 -1
  172. package/src/services/__tests/bitrails.test.ts +7 -2
  173. package/src/services/processingErrors/arcSuccessError.json +76 -0
  174. package/src/services/providers/ARC.ts +1 -1
  175. package/src/services/providers/__tests/WhatsOnChain.test.ts +3 -3
  176. package/src/signer/methods/proveCertificate.ts +14 -21
  177. package/src/storage/__test/WalletStorageManager.test.ts +1 -1
  178. package/src/storage/methods/listOutputs.ts +1 -1
  179. package/src/storage/remoting/StorageClient.ts +4 -4
  180. package/src/utility/identityUtils.ts +159 -0
  181. package/src/wab-client/WABClient.ts +94 -0
  182. package/src/wab-client/__tests/WABClient.man.test.ts +59 -0
  183. package/src/wab-client/auth-method-interactors/AuthMethodInteractor.ts +47 -0
  184. package/src/wab-client/auth-method-interactors/PersonaIDInteractor.ts +35 -0
  185. package/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.ts +72 -0
  186. package/test/Wallet/action/internalizeAction.a.test.ts +1 -1
  187. package/test/Wallet/certificate/acquireCertificate.test.ts +89 -30
  188. package/test/Wallet/local/localWallet.man.test.ts +20 -4
  189. package/test/storage/KnexMigrations.test.ts +1 -1
  190. package/test/storage/update.test.ts +1 -1
  191. package/test/utils/TestUtilsWalletStorage.ts +24 -13
  192. package/test/wallet/action/internalizeAction.test.ts +1 -1
  193. package/test/wallet/list/listActions2.test.ts +1 -1
  194. package/test/wallet/sync/Wallet.sync.test.ts +1 -1
  195. package/out/src/sdk/CertOps.d.ts +0 -66
  196. package/out/src/sdk/CertOps.d.ts.map +0 -1
  197. package/out/src/sdk/CertOps.js +0 -190
  198. package/out/src/sdk/CertOps.js.map +0 -1
  199. package/src/sdk/CertOps.ts +0 -274
@@ -0,0 +1,546 @@
1
+ import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals'
2
+ import { mockUnderlyingWallet, MockedBSV_SDK } from './WalletPermissionsManager.fixtures'
3
+ import { WalletPermissionsManager, PermissionRequest, PermissionToken } from '../WalletPermissionsManager'
4
+
5
+ // Re-mock @bsv/sdk with our fixture classes (MockTransaction, MockLockingScript, etc.)
6
+ jest.mock('@bsv/sdk', () => MockedBSV_SDK)
7
+
8
+ describe('WalletPermissionsManager - On-Chain Token Creation, Renewal & Revocation', () => {
9
+ let underlying: ReturnType<typeof mockUnderlyingWallet>
10
+ let manager: WalletPermissionsManager
11
+
12
+ beforeEach(() => {
13
+ // Fresh mock wallet before each test
14
+ underlying = mockUnderlyingWallet()
15
+ manager = new WalletPermissionsManager(underlying, 'admin.domain.com')
16
+ })
17
+
18
+ afterEach(() => {
19
+ jest.clearAllMocks()
20
+ })
21
+
22
+ /* ------------------------------------------------------------------------
23
+ * 1) UNIT TESTS: buildPushdropFields() correctness
24
+ * ------------------------------------------------------------------------
25
+ * We directly call the manager’s internal buildPushdropFields(...) via
26
+ * a cast to "any" so we can test each permission type’s field ordering,
27
+ * encryption calls, and final arrays.
28
+ * ------------------------------------------------------------------------
29
+ */
30
+
31
+ describe('buildPushdropFields() - unit tests for each permission type', () => {
32
+ // We’ll cast the manager to `any` to access the private method.
33
+ const privateManager = () => manager as any
34
+
35
+ it('should build correct fields for a protocol token (DPACP)', async () => {
36
+ const request: PermissionRequest = {
37
+ type: 'protocol',
38
+ originator: 'some-app.com',
39
+ privileged: true,
40
+ protocolID: [2, 'myProto'],
41
+ counterparty: 'some-other-pubkey',
42
+ reason: 'test-protocol-creation'
43
+ }
44
+ const expiry = 1234567890
45
+
46
+ // Because manager.encryptPermissionTokenField calls underlying.encrypt,
47
+ // we can observe how many times it's called & with what plaintext.
48
+ underlying.encrypt.mockClear()
49
+
50
+ const fields: number[][] = await privateManager().buildPushdropFields(request, expiry)
51
+
52
+ // We expect 6 encryption calls (domain, expiry, privileged, secLevel, protoName, cpty).
53
+ expect(underlying.encrypt).toHaveBeenCalledTimes(6)
54
+
55
+ // The final array must have length=6
56
+ expect(fields).toHaveLength(6)
57
+
58
+ // Confirm the 1st call was the domain
59
+ expect(underlying.encrypt.mock.calls[0][0].plaintext).toEqual(
60
+ expect.arrayContaining([...'some-app.com'].map(c => c.charCodeAt(0)))
61
+ )
62
+
63
+ // Confirm the 2nd call was the expiry, as a string
64
+ expect(underlying.encrypt.mock.calls[1][0].plaintext).toEqual(
65
+ expect.arrayContaining([...'1234567890'].map(c => c.charCodeAt(0)))
66
+ )
67
+
68
+ // 3rd => privileged? 'true'
69
+ expect(underlying.encrypt.mock.calls[2][0].plaintext).toEqual(
70
+ expect.arrayContaining([...'true'].map(c => c.charCodeAt(0)))
71
+ )
72
+
73
+ // 4th => security level => '2'
74
+ expect(underlying.encrypt.mock.calls[3][0].plaintext).toEqual(
75
+ expect.arrayContaining([...'2'].map(c => c.charCodeAt(0)))
76
+ )
77
+
78
+ // 5th => protoName => 'myProto'
79
+ expect(underlying.encrypt.mock.calls[4][0].plaintext).toEqual(
80
+ expect.arrayContaining([...'myProto'].map(c => c.charCodeAt(0)))
81
+ )
82
+
83
+ // 6th => counterparty => 'some-other-pubkey'
84
+ expect(underlying.encrypt.mock.calls[5][0].plaintext).toEqual(
85
+ expect.arrayContaining([...'some-other-pubkey'].map(c => c.charCodeAt(0)))
86
+ )
87
+ })
88
+
89
+ it('should build correct fields for a basket token (DBAP)', async () => {
90
+ const request: PermissionRequest = {
91
+ type: 'basket',
92
+ originator: 'origin.example',
93
+ basket: 'someBasket',
94
+ reason: 'basket usage'
95
+ }
96
+ const expiry = 999999999
97
+
98
+ underlying.encrypt.mockClear()
99
+
100
+ const fields: number[][] = await privateManager().buildPushdropFields(request, expiry)
101
+
102
+ // We expect 3 encryption calls: domain, expiry, basket
103
+ expect(underlying.encrypt).toHaveBeenCalledTimes(3)
104
+ expect(fields).toHaveLength(3)
105
+ })
106
+
107
+ it('should build correct fields for a certificate token (DCAP)', async () => {
108
+ const request: PermissionRequest = {
109
+ type: 'certificate',
110
+ originator: 'cert-user.org',
111
+ privileged: false,
112
+ certificate: {
113
+ verifier: '02abcdef...',
114
+ certType: 'KYC',
115
+ fields: ['name', 'dob']
116
+ },
117
+ reason: 'certificate usage'
118
+ }
119
+ const expiry = 2222222222
120
+
121
+ underlying.encrypt.mockClear()
122
+
123
+ const fields: number[][] = await privateManager().buildPushdropFields(request, expiry)
124
+
125
+ // DP = domain, expiry, privileged, certType, fieldsJson, verifier
126
+ expect(underlying.encrypt).toHaveBeenCalledTimes(6)
127
+ expect(fields).toHaveLength(6)
128
+
129
+ // 5th encryption call is the fields JSON => ["name","dob"]
130
+ const fifthCallPlaintext = underlying.encrypt.mock.calls[4][0].plaintext
131
+ const str = String.fromCharCode(...fifthCallPlaintext)
132
+ expect(str).toContain('"name"')
133
+ expect(str).toContain('"dob"')
134
+ })
135
+
136
+ it('should build correct fields for a spending token (DSAP)', async () => {
137
+ const request: PermissionRequest = {
138
+ type: 'spending',
139
+ originator: 'money-spender.com',
140
+ spending: { satoshis: 5000 },
141
+ reason: 'monthly spending'
142
+ }
143
+ const expiry = 0 // DSAP typically not time-limited, but manager can pass 0.
144
+
145
+ underlying.encrypt.mockClear()
146
+
147
+ const fields: number[][] = await privateManager().buildPushdropFields(request, expiry, /*amount=*/ 10000)
148
+
149
+ // For DSAP: domain + authorizedAmount (2 fields)
150
+ expect(underlying.encrypt).toHaveBeenCalledTimes(2)
151
+ expect(fields).toHaveLength(2)
152
+
153
+ // The second encryption call is '10000'
154
+ const secondPlaintext = underlying.encrypt.mock.calls[1][0].plaintext
155
+ const asString = String.fromCharCode(...secondPlaintext)
156
+ expect(asString).toBe('10000')
157
+ })
158
+ })
159
+
160
+ /* ------------------------------------------------------------------------
161
+ * 2) INTEGRATION TESTS: Token Creation
162
+ * ------------------------------------------------------------------------
163
+ * We'll simulate a user request flow, then call `grantPermission` with
164
+ * ephemeral=false to see if createAction is called with the correct script,
165
+ * basket name, tags, etc. We also decode the script to confirm it has the
166
+ * correct (encrypted) fields.
167
+ * ------------------------------------------------------------------------
168
+ */
169
+
170
+ describe('Token Creation - integration tests', () => {
171
+ it('should create a new protocol token with the correct basket, script, and tags', async () => {
172
+ // 1) Simulate the manager having an active request for a protocol token.
173
+ const request: PermissionRequest = {
174
+ type: 'protocol',
175
+ originator: 'app.example',
176
+ privileged: false,
177
+ protocolID: [1, 'testProto'],
178
+ counterparty: 'self',
179
+ reason: 'Need protocol usage'
180
+ }
181
+
182
+ // We'll emulate that the manager queued it:
183
+ const key = (manager as any).buildRequestKey(request)
184
+ ;(manager as any).activeRequests.set(key, {
185
+ request,
186
+ pending: [{ resolve: () => {}, reject: () => {} }]
187
+ })
188
+
189
+ // 2) Grant the permission with ephemeral=false => must create the token
190
+ underlying.createAction.mockClear()
191
+ await manager.grantPermission({
192
+ requestID: key,
193
+ expiry: 999999, // set some expiry
194
+ ephemeral: false
195
+ })
196
+
197
+ // 3) Expect createAction to have been called once with a single output
198
+ expect(underlying.createAction).toHaveBeenCalledTimes(1)
199
+ const actionArgs = underlying.createAction.mock.calls[0][0]
200
+ expect(actionArgs.outputs).toHaveLength(1)
201
+
202
+ // The basket name must be "admin protocol-permission" as per BASKET_MAP
203
+ expect(actionArgs.outputs[0].basket).toBe('admin protocol-permission')
204
+
205
+ // The tags must contain e.g. "originator app.example", "protocolName testProto", etc.
206
+ const outputTags = actionArgs.outputs[0].tags
207
+ expect(outputTags).toEqual(
208
+ expect.arrayContaining([
209
+ 'originator app.example',
210
+ 'privileged false',
211
+ 'protocolName testProto',
212
+ 'protocolSecurityLevel 1',
213
+ 'counterparty self'
214
+ ])
215
+ )
216
+
217
+ // The lockingScript is built by "PushDrop.lock(...)" with 6 fields
218
+ const lockingScriptHex = actionArgs.outputs[0].lockingScript
219
+ expect(lockingScriptHex).toBeTruthy()
220
+
221
+ // Because we’re using our mock pushdrop, we might see an empty decode.
222
+ // In a real environment, you would decode and confirm the fields. Here we just confirm
223
+ // that the manager called the underlying encrypt 6 times, plus the script creation.
224
+ // Two more encrypt calls should have been made within createAction (metadata encryption
225
+ // of the top-level Action description, and the output's description) for a total of 8.
226
+ expect(underlying.encrypt).toHaveBeenCalledTimes(8)
227
+ })
228
+
229
+ it('should create a new basket token (DBAP)', async () => {
230
+ const request: PermissionRequest = {
231
+ type: 'basket',
232
+ originator: 'shopper.com',
233
+ basket: 'myBasket',
234
+ reason: 'I want to store items'
235
+ }
236
+ const key = (manager as any).buildRequestKey(request)
237
+ ;(manager as any).activeRequests.set(key, {
238
+ request,
239
+ pending: [{ resolve() {}, reject() {} }]
240
+ })
241
+
242
+ underlying.createAction.mockClear()
243
+
244
+ await manager.grantPermission({
245
+ requestID: key,
246
+ ephemeral: false,
247
+ expiry: 123456789
248
+ })
249
+ expect(underlying.createAction).toHaveBeenCalledTimes(1)
250
+
251
+ const { outputs } = underlying.createAction.mock.calls[0][0]
252
+ expect(outputs).toHaveLength(1)
253
+ // "admin basket-access"
254
+ expect(outputs[0].basket).toBe('admin basket-access')
255
+ expect(outputs[0].tags).toEqual(expect.arrayContaining(['originator shopper.com', 'basket myBasket']))
256
+ // 3 fields => domain, expiry, basket, plus two metadata calls (description, outputDescription)
257
+ expect(underlying.encrypt).toHaveBeenCalledTimes(5)
258
+ })
259
+
260
+ it('should create a new certificate token (DCAP)', async () => {
261
+ const request: PermissionRequest = {
262
+ type: 'certificate',
263
+ originator: 'org.certs',
264
+ privileged: true,
265
+ certificate: {
266
+ verifier: '02cccccc',
267
+ certType: 'KYC',
268
+ fields: ['name', 'id', 'photo']
269
+ },
270
+ reason: 'Present KYC docs'
271
+ }
272
+ const key = (manager as any).buildRequestKey(request)
273
+ ;(manager as any).activeRequests.set(key, {
274
+ request,
275
+ pending: [{ resolve() {}, reject() {} }]
276
+ })
277
+
278
+ underlying.createAction.mockClear()
279
+
280
+ await manager.grantPermission({
281
+ requestID: key,
282
+ ephemeral: false,
283
+ expiry: 44444444
284
+ })
285
+
286
+ expect(underlying.createAction).toHaveBeenCalledTimes(1)
287
+ const { outputs } = underlying.createAction.mock.calls[0][0]
288
+ expect(outputs[0].basket).toBe('admin certificate-access')
289
+ expect(outputs[0].tags).toEqual(
290
+ expect.arrayContaining(['originator org.certs', 'privileged true', 'type KYC', 'verifier 02cccccc'])
291
+ )
292
+ // DP = domain, expiry, privileged, certType, fieldsJson, verifier => 6 encryption calls
293
+ // Two additional ones for metadata encryption (action description, output description) for 8 total.
294
+ expect(underlying.encrypt).toHaveBeenCalledTimes(8)
295
+ })
296
+
297
+ it('should create a new spending authorization token (DSAP)', async () => {
298
+ const request: PermissionRequest = {
299
+ type: 'spending',
300
+ originator: 'spender.com',
301
+ spending: {
302
+ satoshis: 9999
303
+ }
304
+ }
305
+ const key = (manager as any).buildRequestKey(request)
306
+ ;(manager as any).activeRequests.set(key, {
307
+ request,
308
+ pending: [{ resolve() {}, reject() {} }]
309
+ })
310
+
311
+ underlying.createAction.mockClear()
312
+
313
+ // We'll set "amount=20000" as the monthly limit
314
+ await manager.grantPermission({
315
+ requestID: key,
316
+ ephemeral: false,
317
+ amount: 20000
318
+ })
319
+
320
+ expect(underlying.createAction).toHaveBeenCalledTimes(1)
321
+ const { outputs } = underlying.createAction.mock.calls[0][0]
322
+ // "admin spending-authorization"
323
+ expect(outputs[0].basket).toBe('admin spending-authorization')
324
+ expect(outputs[0].tags).toEqual(expect.arrayContaining(['originator spender.com']))
325
+ // domain, amount => 2 calls, plus two metadata encryption calls (description, outputDescription)
326
+ expect(underlying.encrypt).toHaveBeenCalledTimes(4)
327
+ })
328
+ })
329
+
330
+ /* ------------------------------------------------------------------------
331
+ * 3) INTEGRATION TESTS: Token Renewal
332
+ * ------------------------------------------------------------------------
333
+ * We test that renewing a token:
334
+ * - Spends the old token with createAction input referencing oldToken.txid/index
335
+ * - Produces a new token output in the same transaction with updated fields
336
+ * ------------------------------------------------------------------------
337
+ */
338
+
339
+ describe('Token Renewal - integration tests', () => {
340
+ it('should spend the old token input and create a new protocol token output with updated expiry', async () => {
341
+ // Suppose the user has an old protocol token:
342
+ const oldToken: PermissionToken = {
343
+ txid: 'oldTokenTX',
344
+ outputIndex: 2,
345
+ outputScript: '76a914...ac', // not used by the mock
346
+ satoshis: 1,
347
+ originator: 'some-site.io',
348
+ expiry: 222222,
349
+ privileged: false,
350
+ securityLevel: 1,
351
+ protocol: 'coolProto',
352
+ counterparty: 'self'
353
+ }
354
+
355
+ // The user’s request to renew:
356
+ const request: PermissionRequest = {
357
+ type: 'protocol',
358
+ originator: 'some-site.io',
359
+ privileged: false,
360
+ protocolID: [1, 'coolProto'],
361
+ counterparty: 'self',
362
+ renewal: true,
363
+ previousToken: oldToken
364
+ }
365
+
366
+ // Manager normally calls requestPermissionFlow, but let's skip ahead:
367
+ // We'll place the request in activeRequests:
368
+ const key = (manager as any).buildRequestKey(request)
369
+ ;(manager as any).activeRequests.set(key, {
370
+ request,
371
+ pending: [{ resolve() {}, reject() {} }]
372
+ })
373
+
374
+ // Clear the mock calls, then renew with ephemeral=false
375
+ underlying.createAction.mockClear()
376
+
377
+ await manager.grantPermission({
378
+ requestID: key,
379
+ ephemeral: false,
380
+ expiry: 999999 // new expiry
381
+ })
382
+
383
+ // We expect createAction with:
384
+ // - 1 input referencing oldToken "oldTokenTX.2"
385
+ // - 1 output with the new script
386
+ expect(underlying.createAction).toHaveBeenCalledTimes(1)
387
+ const createArgs = underlying.createAction.mock.calls[0][0]
388
+ expect(createArgs.inputs).toHaveLength(1)
389
+ expect(createArgs.inputs[0].outpoint).toBe('oldTokenTX.2')
390
+ expect(createArgs.outputs).toHaveLength(1)
391
+ // The new basket is still "admin protocol-permission"
392
+ expect(createArgs.outputs[0].basket).toBe('admin protocol-permission')
393
+
394
+ // And we must confirm "renew" means 6 encryption calls again
395
+ // Metadata encryption means three extra calls (inputDescription, outputDescription, and Action description)
396
+ // this means a total of 9.
397
+ expect(underlying.encrypt).toHaveBeenCalledTimes(9)
398
+ })
399
+
400
+ it('should allow updating the authorizedAmount in DSAP renewal', async () => {
401
+ const oldToken: PermissionToken = {
402
+ txid: 'dsap-old-tx',
403
+ outputIndex: 0,
404
+ outputScript: 'sample script',
405
+ satoshis: 1,
406
+ originator: 'spenderX.com',
407
+ authorizedAmount: 10000,
408
+ expiry: 0
409
+ }
410
+ const request: PermissionRequest = {
411
+ type: 'spending',
412
+ originator: 'spenderX.com',
413
+ spending: { satoshis: 3000 },
414
+ renewal: true,
415
+ previousToken: oldToken
416
+ }
417
+ const key = (manager as any).buildRequestKey(request)
418
+ ;(manager as any).activeRequests.set(key, {
419
+ request,
420
+ pending: [{ resolve() {}, reject() {} }]
421
+ })
422
+
423
+ underlying.createAction.mockClear()
424
+
425
+ // Renew with new monthly limit 50000
426
+ await manager.grantPermission({
427
+ requestID: key,
428
+ amount: 50000,
429
+ ephemeral: false
430
+ })
431
+
432
+ // check
433
+ const { inputs, outputs } = underlying.createAction.mock.calls[0][0]
434
+ expect(inputs).toHaveLength(1)
435
+ expect(inputs[0].outpoint).toBe('dsap-old-tx.0')
436
+
437
+ expect(outputs).toHaveLength(1)
438
+ expect(outputs[0].basket).toBe('admin spending-authorization')
439
+
440
+ // domain + new authorizedAmount => 2 encryption calls
441
+ // For metadata encryption, we have an input description, an output description, and a top-level description.
442
+ // This makes for a total of 5 calls.
443
+ expect(underlying.encrypt).toHaveBeenCalledTimes(5)
444
+ // The second call’s plaintext should be "50000"
445
+ const secondPlaintext = underlying.encrypt.mock.calls[1][0].plaintext
446
+ const asStr = String.fromCharCode(...secondPlaintext)
447
+ expect(asStr).toBe('50000')
448
+ })
449
+ })
450
+
451
+ /* ------------------------------------------------------------------------
452
+ * 4) INTEGRATION TESTS: Token Revocation
453
+ * ------------------------------------------------------------------------
454
+ * - Revoking a token means we build a transaction that consumes the old
455
+ * token UTXO with no replacement output.
456
+ * - Then we typically call signAction to finalize. The old token is no
457
+ * longer listed as an unspent output.
458
+ * ------------------------------------------------------------------------
459
+ */
460
+
461
+ describe('Token Revocation - integration tests', () => {
462
+ it('should create a transaction that consumes (spends) the old token with no new outputs', async () => {
463
+ // A sample old token
464
+ const oldToken: PermissionToken = {
465
+ txid: 'revocableToken.txid',
466
+ outputIndex: 1,
467
+ outputScript: 'fakePushdropScript',
468
+ satoshis: 1,
469
+ originator: 'shopper.com',
470
+ basketName: 'myBasket',
471
+ expiry: 1111111111
472
+ }
473
+
474
+ underlying.createAction.mockClear()
475
+ underlying.signAction.mockClear()
476
+
477
+ await manager.revokePermission(oldToken)
478
+
479
+ // 1) The manager calls createAction with an input referencing oldToken
480
+ expect(underlying.createAction).toHaveBeenCalledTimes(1)
481
+ const createArgs = underlying.createAction.mock.calls[0][0]
482
+ expect(createArgs.inputs).toHaveLength(1)
483
+ expect(createArgs.inputs[0].outpoint).toBe('revocableToken.txid.1')
484
+
485
+ // No new outputs => final array is empty
486
+ expect(createArgs.outputs || []).toHaveLength(0)
487
+
488
+ // 2) The manager then calls signAction to finalize the spending
489
+ expect(underlying.signAction).toHaveBeenCalledTimes(1)
490
+ const signArgs = underlying.signAction.mock.calls[0][0]
491
+ // signArgs.reference should be the same from createAction’s result
492
+ expect(signArgs.reference).toBe('mockReference')
493
+
494
+ // The “spends” object should have an unlockingScript at index 0.
495
+ expect(signArgs.spends).toHaveProperty('0.unlockingScript')
496
+ // The content can be a mock, we just check it’s not empty
497
+ expect(signArgs.spends[0].unlockingScript).toBeDefined()
498
+ })
499
+
500
+ it('should remove the old token from listing after revocation', async () => {
501
+ // 1) Setup the underlying wallet to initially return the old token in listOutputs
502
+ const oldToken: PermissionToken = {
503
+ txid: 'aaaa1111',
504
+ outputIndex: 0,
505
+ outputScript: 'some script',
506
+ satoshis: 1,
507
+ originator: 'example.com',
508
+ expiry: 999999,
509
+ basketName: 'myBasket'
510
+ }
511
+ // We mock listOutputs so that it returns the old token before revocation
512
+ underlying.listOutputs.mockResolvedValueOnce({
513
+ totalOutputs: 1,
514
+ outputs: [
515
+ {
516
+ outpoint: 'aaaa1111.0',
517
+ lockingScript: 'some script',
518
+ satoshis: 1,
519
+ tags: ['originator example.com', 'basket myBasket']
520
+ }
521
+ ]
522
+ })
523
+
524
+ // Confirm the manager sees it in listBasketAccess
525
+ const tokensBefore = await manager.listBasketAccess({
526
+ originator: 'example.com'
527
+ })
528
+ expect(tokensBefore).toHaveLength(1)
529
+ expect(tokensBefore[0].txid).toBe('aaaa1111')
530
+
531
+ // 2) Revoke the token
532
+ await manager.revokePermission(oldToken)
533
+
534
+ // 3) After revocation, mock the underlying wallet to show zero outputs
535
+ underlying.listOutputs.mockResolvedValue({
536
+ totalOutputs: 0,
537
+ outputs: []
538
+ })
539
+
540
+ const tokensAfter = await manager.listBasketAccess({
541
+ originator: 'example.com'
542
+ })
543
+ expect(tokensAfter).toHaveLength(0)
544
+ })
545
+ })
546
+ })
package/src/index.all.ts CHANGED
@@ -7,3 +7,12 @@ export * from './storage/index.all'
7
7
  export * from './services/Services'
8
8
  export * from './monitor/Monitor'
9
9
  export * from './monitor/MonitorDaemon'
10
+ export * from './WalletPermissionsManager'
11
+ export * from './CWIStyleWalletManager'
12
+ export * from './WalletAuthenticationManager'
13
+ export * from './sdk/PrivilegedKeyManager'
14
+ export * from './SimpleWalletManager'
15
+ export * from './wab-client/WABClient'
16
+ export * from './wab-client/auth-method-interactors/TwilioPhoneInteractor'
17
+ export * from './wab-client/auth-method-interactors/PersonaIDInteractor'
18
+ export * from './wab-client/auth-method-interactors/AuthMethodInteractor'
@@ -5,3 +5,12 @@ export * from './signer/WalletSigner'
5
5
  export * from './storage/index.client'
6
6
  export * from './services/Services'
7
7
  export * from './monitor/Monitor'
8
+ export * from './WalletPermissionsManager'
9
+ export * from './CWIStyleWalletManager'
10
+ export * from './WalletAuthenticationManager'
11
+ export * from './sdk/PrivilegedKeyManager'
12
+ export * from './SimpleWalletManager'
13
+ export * from './wab-client/WABClient'
14
+ export * from './wab-client/auth-method-interactors/TwilioPhoneInteractor'
15
+ export * from './wab-client/auth-method-interactors/PersonaIDInteractor'
16
+ export * from './wab-client/auth-method-interactors/AuthMethodInteractor'
@@ -0,0 +1,18 @@
1
+ import {
2
+ GetPublicKeyArgs,
3
+ GetPublicKeyResult,
4
+ OriginatorDomainNameStringUnder250Bytes,
5
+ WalletDecryptArgs,
6
+ WalletDecryptResult,
7
+ WalletEncryptArgs,
8
+ WalletEncryptResult
9
+ } from '@bsv/sdk'
10
+
11
+ export interface CertOpsWallet {
12
+ getPublicKey(
13
+ args: GetPublicKeyArgs,
14
+ originator?: OriginatorDomainNameStringUnder250Bytes
15
+ ): Promise<GetPublicKeyResult>
16
+ encrypt(args: WalletEncryptArgs, originator?: OriginatorDomainNameStringUnder250Bytes): Promise<WalletEncryptResult>
17
+ decrypt(args: WalletDecryptArgs, originator?: OriginatorDomainNameStringUnder250Bytes): Promise<WalletDecryptResult>
18
+ }