@bsv/wallet-toolbox 1.1.62 → 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 (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 +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/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 +1738 -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,150 @@
1
+ import { CWIStyleWalletManager, UMPTokenInteractor, OverlayUMPTokenInteractor } from './CWIStyleWalletManager'
2
+ import { PrivilegedKeyManager } from './sdk/PrivilegedKeyManager'
3
+ import { WalletInterface, Random, Utils, Transaction, RPuzzle, PrivateKey, BigNumber } from '@bsv/sdk'
4
+ import { WABClient } from './wab-client/WABClient'
5
+ import { AuthMethodInteractor } from './wab-client/auth-method-interactors/AuthMethodInteractor'
6
+
7
+ /**
8
+ * WalletAuthenticationManager
9
+ *
10
+ * A wallet manager that integrates
11
+ * with a WABClient for user authentication flows (e.g. Twilio phone).
12
+ */
13
+ export class WalletAuthenticationManager extends CWIStyleWalletManager {
14
+ private wabClient: WABClient // instance of WABClient
15
+ private authMethod?: AuthMethodInteractor // chosen AuthMethod interactor
16
+ private tempPresentationKey?: string // for temporary persistence between auth steps
17
+
18
+ constructor(
19
+ adminOriginator: string,
20
+ walletBuilder: (primaryKey: number[], privilegedKeyManager: PrivilegedKeyManager) => Promise<WalletInterface>,
21
+ interactor: UMPTokenInteractor = new OverlayUMPTokenInteractor(),
22
+ recoveryKeySaver: (key: number[]) => Promise<true>,
23
+ passwordRetriever: (reason: string, test: (passwordCandidate: string) => boolean) => Promise<string>,
24
+ wabClient: WABClient,
25
+ authMethod?: AuthMethodInteractor,
26
+ stateSnapshot?: number[]
27
+ ) {
28
+ super(
29
+ adminOriginator,
30
+ walletBuilder,
31
+ interactor,
32
+ recoveryKeySaver,
33
+ passwordRetriever,
34
+ // Here, we provide a custom new wallet funder that uses the Secret Server
35
+ async (presentationKey: number[], wallet: WalletInterface, adminOriginator: string) => {
36
+ debugger
37
+ const { paymentData } = await this.wabClient.requestFaucet(Utils.toHex(presentationKey))
38
+ if (!paymentData.k || !paymentData.tx) {
39
+ throw new Error('Invalid')
40
+ }
41
+ const tx = Transaction.fromAtomicBEEF(paymentData.tx as number[])
42
+ console.log(paymentData)
43
+ const faucetRedeemTXCreationResult = await wallet.createAction(
44
+ {
45
+ inputBEEF: tx.toBEEF(),
46
+ inputs: [
47
+ {
48
+ outpoint: `${paymentData.txid}.0`,
49
+ unlockingScriptLength: 108,
50
+ inputDescription: 'Fund from faucet'
51
+ }
52
+ ],
53
+ description: 'Fund wallet'
54
+ },
55
+ adminOriginator
56
+ )
57
+ const faucetRedeemTX = Transaction.fromAtomicBEEF(faucetRedeemTXCreationResult.signableTransaction!.tx)
58
+ const faucetRedemptionPuzzle = new RPuzzle()
59
+ const randomRedemptionPrivateKey = PrivateKey.fromRandom()
60
+ const faucetRedeemUnlocker = faucetRedemptionPuzzle.unlock(
61
+ new BigNumber(paymentData.k, 16),
62
+ randomRedemptionPrivateKey
63
+ )
64
+ const faucetRedeemUnlockingScript = await faucetRedeemUnlocker.sign(faucetRedeemTX, 0)
65
+ const signActionResult = await wallet.signAction({
66
+ reference: faucetRedeemTXCreationResult.signableTransaction!.reference,
67
+ spends: {
68
+ 0: {
69
+ unlockingScript: faucetRedeemUnlockingScript.toHex()
70
+ }
71
+ }
72
+ })
73
+ console.log('Sign action result:', signActionResult)
74
+ },
75
+ stateSnapshot
76
+ )
77
+
78
+ this.wabClient = wabClient
79
+ this.authMethod = authMethod
80
+ }
81
+
82
+ /**
83
+ * Sets (or switches) the chosen AuthMethodInteractor at runtime,
84
+ * in case the user changes their mind or picks a new method in the UI.
85
+ */
86
+ public setAuthMethod(method: AuthMethodInteractor) {
87
+ this.authMethod = method
88
+ }
89
+
90
+ /**
91
+ * Initiate the WAB-based flow, e.g. sending an SMS code or starting an ID check,
92
+ * using the chosen AuthMethodInteractor.
93
+ */
94
+ public async startAuth(payload: any): Promise<void> {
95
+ if (!this.authMethod) {
96
+ throw new Error('No AuthMethod selected in WalletAuthenticationManager')
97
+ }
98
+ this.tempPresentationKey = this.generateTemporaryPresentationKey()
99
+
100
+ // For example, if this.authMethod is Twilio, `payload` might contain { phoneNumber: "+1..." }
101
+ const startRes = await this.wabClient.startAuthMethod(
102
+ this.authMethod,
103
+ // The user might not have a presentationKey yet, so we generate one locally to pass to the server.
104
+ // If it’s an existing user on the WAB side, the WAB will give us the stored key later.
105
+ // But we do need some placeholder key for the 'startAuth' call:
106
+ this.tempPresentationKey,
107
+ payload
108
+ )
109
+
110
+ if (!startRes.success) {
111
+ throw new Error(startRes.message || 'Failed to start WAB auth method')
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Completes the WAB-based flow, retrieving the final presentationKey from WAB if successful.
117
+ */
118
+ public async completeAuth(payload: any): Promise<void> {
119
+ if (!this.authMethod || !this.tempPresentationKey) {
120
+ throw new Error('No AuthMethod selected in WalletAuthenticationManager or startAuth has yet to be called.')
121
+ }
122
+
123
+ // Unser the temp presentation key early (for security)
124
+ const tempKey = this.tempPresentationKey
125
+ this.tempPresentationKey = undefined
126
+
127
+ const result = await this.wabClient.completeAuthMethod(this.authMethod, tempKey, payload)
128
+
129
+ if (!result.success || !result.presentationKey) {
130
+ throw new Error(result.message || 'Failed to complete WAB auth')
131
+ }
132
+
133
+ // We now have the final 256-bit key in hex from the WAB
134
+ const presentationKeyHex = result.presentationKey
135
+ const presentationKeyBytes = Utils.toArray(presentationKeyHex, 'hex')
136
+
137
+ // Provide this presentation key to the underlying CWI logic:
138
+ // This sets 'this.presentationKey' and determines if it's a new or existing user
139
+ // based on whether a UMP token exists on chain. We'll do "presentation-key-and-password"
140
+ // for new user flows, for example, or user might choose "presentation-key-and-recovery-key" if we wanted.
141
+ // Either way, WAB has now done its job and we are off to base-layer CWI logic at this point!
142
+ await this.providePresentationKey(presentationKeyBytes)
143
+ }
144
+
145
+ private generateTemporaryPresentationKey(): string {
146
+ // For the 'startAuth' call, we can generate a random 32 bytes → 64 hex chars.
147
+ const randomBytes = Random(32) // array of length 32
148
+ return Utils.toHex(randomBytes)
149
+ }
150
+ }