@bsv/wallet-toolbox 1.1.25 → 1.1.27

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