@bsv/wallet-toolbox 1.1.23 → 1.1.25

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 (201) hide show
  1. package/docs/client.md +2404 -870
  2. package/docs/setup.md +102 -134
  3. package/docs/wallet.md +2404 -870
  4. package/out/src/CWIStyleWalletManager.d.ts +411 -0
  5. package/out/src/CWIStyleWalletManager.d.ts.map +1 -0
  6. package/out/src/CWIStyleWalletManager.js +1131 -0
  7. package/out/src/CWIStyleWalletManager.js.map +1 -0
  8. package/out/src/Setup.d.ts +252 -8
  9. package/out/src/Setup.d.ts.map +1 -1
  10. package/out/src/Setup.js +299 -5
  11. package/out/src/Setup.js.map +1 -1
  12. package/out/src/SetupClient.d.ts +2 -16
  13. package/out/src/SetupClient.d.ts.map +1 -1
  14. package/out/src/SetupClient.js +8 -72
  15. package/out/src/SetupClient.js.map +1 -1
  16. package/out/src/SimpleWalletManager.d.ts +169 -0
  17. package/out/src/SimpleWalletManager.d.ts.map +1 -0
  18. package/out/src/SimpleWalletManager.js +315 -0
  19. package/out/src/SimpleWalletManager.js.map +1 -0
  20. package/out/src/Wallet.d.ts +6 -1
  21. package/out/src/Wallet.d.ts.map +1 -1
  22. package/out/src/Wallet.js +29 -2
  23. package/out/src/Wallet.js.map +1 -1
  24. package/out/src/WalletAuthenticationManager.d.ts +33 -0
  25. package/out/src/WalletAuthenticationManager.d.ts.map +1 -0
  26. package/out/src/WalletAuthenticationManager.js +107 -0
  27. package/out/src/WalletAuthenticationManager.js.map +1 -0
  28. package/out/src/WalletPermissionsManager.d.ts +575 -0
  29. package/out/src/WalletPermissionsManager.d.ts.map +1 -0
  30. package/out/src/WalletPermissionsManager.js +1807 -0
  31. package/out/src/WalletPermissionsManager.js.map +1 -0
  32. package/out/src/WalletSettingsManager.d.ts +59 -0
  33. package/out/src/WalletSettingsManager.d.ts.map +1 -0
  34. package/out/src/WalletSettingsManager.js +168 -0
  35. package/out/src/WalletSettingsManager.js.map +1 -0
  36. package/out/src/__tests/CWIStyleWalletManager.test.d.ts +2 -0
  37. package/out/src/__tests/CWIStyleWalletManager.test.d.ts.map +1 -0
  38. package/out/src/__tests/CWIStyleWalletManager.test.js +472 -0
  39. package/out/src/__tests/CWIStyleWalletManager.test.js.map +1 -0
  40. package/out/src/__tests/WalletPermissionsManager.callbacks.test.d.ts +2 -0
  41. package/out/src/__tests/WalletPermissionsManager.callbacks.test.d.ts.map +1 -0
  42. package/out/src/__tests/WalletPermissionsManager.callbacks.test.js +239 -0
  43. package/out/src/__tests/WalletPermissionsManager.callbacks.test.js.map +1 -0
  44. package/out/src/__tests/WalletPermissionsManager.checks.test.d.ts +2 -0
  45. package/out/src/__tests/WalletPermissionsManager.checks.test.d.ts.map +1 -0
  46. package/out/src/__tests/WalletPermissionsManager.checks.test.js +644 -0
  47. package/out/src/__tests/WalletPermissionsManager.checks.test.js.map +1 -0
  48. package/out/src/__tests/WalletPermissionsManager.encryption.test.d.ts +2 -0
  49. package/out/src/__tests/WalletPermissionsManager.encryption.test.d.ts.map +1 -0
  50. package/out/src/__tests/WalletPermissionsManager.encryption.test.js +295 -0
  51. package/out/src/__tests/WalletPermissionsManager.encryption.test.js.map +1 -0
  52. package/out/src/__tests/WalletPermissionsManager.fixtures.d.ts +82 -0
  53. package/out/src/__tests/WalletPermissionsManager.fixtures.d.ts.map +1 -0
  54. package/out/src/__tests/WalletPermissionsManager.fixtures.js +260 -0
  55. package/out/src/__tests/WalletPermissionsManager.fixtures.js.map +1 -0
  56. package/out/src/__tests/WalletPermissionsManager.flows.test.d.ts +2 -0
  57. package/out/src/__tests/WalletPermissionsManager.flows.test.d.ts.map +1 -0
  58. package/out/src/__tests/WalletPermissionsManager.flows.test.js +389 -0
  59. package/out/src/__tests/WalletPermissionsManager.flows.test.js.map +1 -0
  60. package/out/src/__tests/WalletPermissionsManager.initialization.test.d.ts +2 -0
  61. package/out/src/__tests/WalletPermissionsManager.initialization.test.d.ts.map +1 -0
  62. package/out/src/__tests/WalletPermissionsManager.initialization.test.js +227 -0
  63. package/out/src/__tests/WalletPermissionsManager.initialization.test.js.map +1 -0
  64. package/out/src/__tests/WalletPermissionsManager.proxying.test.d.ts +2 -0
  65. package/out/src/__tests/WalletPermissionsManager.proxying.test.d.ts.map +1 -0
  66. package/out/src/__tests/WalletPermissionsManager.proxying.test.js +566 -0
  67. package/out/src/__tests/WalletPermissionsManager.proxying.test.js.map +1 -0
  68. package/out/src/__tests/WalletPermissionsManager.tokens.test.d.ts +2 -0
  69. package/out/src/__tests/WalletPermissionsManager.tokens.test.d.ts.map +1 -0
  70. package/out/src/__tests/WalletPermissionsManager.tokens.test.js +460 -0
  71. package/out/src/__tests/WalletPermissionsManager.tokens.test.js.map +1 -0
  72. package/out/src/index.all.d.ts +9 -1
  73. package/out/src/index.all.d.ts.map +1 -1
  74. package/out/src/index.all.js +10 -3
  75. package/out/src/index.all.js.map +1 -1
  76. package/out/src/index.client.d.ts +9 -1
  77. package/out/src/index.client.d.ts.map +1 -1
  78. package/out/src/index.client.js +10 -3
  79. package/out/src/index.client.js.map +1 -1
  80. package/out/src/utility/identityUtils.d.ts +31 -0
  81. package/out/src/utility/identityUtils.d.ts.map +1 -0
  82. package/out/src/utility/identityUtils.js +114 -0
  83. package/out/src/utility/identityUtils.js.map +1 -0
  84. package/out/src/wab-client/WABClient.d.ts +38 -0
  85. package/out/src/wab-client/WABClient.d.ts.map +1 -0
  86. package/out/src/wab-client/WABClient.js +95 -0
  87. package/out/src/wab-client/WABClient.js.map +1 -0
  88. package/out/src/wab-client/__tests/WABClient.test.d.ts +2 -0
  89. package/out/src/wab-client/__tests/WABClient.test.d.ts.map +1 -0
  90. package/out/src/wab-client/__tests/WABClient.test.js +47 -0
  91. package/out/src/wab-client/__tests/WABClient.test.js.map +1 -0
  92. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.d.ts +34 -0
  93. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.d.ts.map +1 -0
  94. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.js +16 -0
  95. package/out/src/wab-client/auth-method-interactors/AuthMethodInteractor.js.map +1 -0
  96. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.d.ts +7 -0
  97. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.d.ts.map +1 -0
  98. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.js +40 -0
  99. package/out/src/wab-client/auth-method-interactors/PersonaIDInteractor.js.map +1 -0
  100. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.d.ts +28 -0
  101. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.d.ts.map +1 -0
  102. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.js +73 -0
  103. package/out/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.js.map +1 -0
  104. package/out/test/Wallet/action/abortAction.test.d.ts.map +1 -0
  105. package/out/test/{wallet → Wallet}/action/abortAction.test.js.map +1 -1
  106. package/out/test/Wallet/action/createAction.test.d.ts.map +1 -0
  107. package/out/test/{wallet → Wallet}/action/createAction.test.js.map +1 -1
  108. package/out/test/{wallet → Wallet}/action/createAction2.test.d.ts.map +1 -1
  109. package/out/test/{wallet → Wallet}/action/createAction2.test.js.map +1 -1
  110. package/out/test/Wallet/action/createActionToGenerateBeefs.man.test.d.ts.map +1 -0
  111. package/out/test/{wallet → Wallet}/action/createActionToGenerateBeefs.man.test.js.map +1 -1
  112. package/out/test/Wallet/action/internalizeAction.test.d.ts.map +1 -0
  113. package/out/test/{wallet → Wallet}/action/internalizeAction.test.js.map +1 -1
  114. package/out/test/Wallet/action/relinquishOutput.test.d.ts.map +1 -0
  115. package/out/test/{wallet → Wallet}/action/relinquishOutput.test.js.map +1 -1
  116. package/out/test/Wallet/construct/Wallet.constructor.test.d.ts.map +1 -0
  117. package/out/test/{wallet → Wallet}/construct/Wallet.constructor.test.js.map +1 -1
  118. package/out/test/Wallet/list/listActions.test.d.ts.map +1 -0
  119. package/out/test/{wallet → Wallet}/list/listActions.test.js.map +1 -1
  120. package/out/test/Wallet/list/listActions2.test.d.ts.map +1 -0
  121. package/out/test/{wallet → Wallet}/list/listActions2.test.js.map +1 -1
  122. package/out/test/Wallet/list/listCertificates.test.d.ts.map +1 -0
  123. package/out/test/{wallet → Wallet}/list/listCertificates.test.js.map +1 -1
  124. package/out/test/Wallet/list/listOutputs.test.d.ts.map +1 -0
  125. package/out/test/{wallet → Wallet}/list/listOutputs.test.js.map +1 -1
  126. package/out/test/Wallet/sync/Wallet.sync.test.d.ts.map +1 -0
  127. package/out/test/{wallet → Wallet}/sync/Wallet.sync.test.js.map +1 -1
  128. package/out/tsconfig.all.tsbuildinfo +1 -1
  129. package/package.json +3 -3
  130. package/src/CWIStyleWalletManager.ts +1891 -0
  131. package/src/Setup.ts +514 -8
  132. package/src/SimpleWalletManager.ts +553 -0
  133. package/src/Wallet.ts +47 -3
  134. package/src/WalletAuthenticationManager.ts +183 -0
  135. package/src/WalletPermissionsManager.ts +2639 -0
  136. package/src/WalletSettingsManager.ts +241 -0
  137. package/src/__tests/CWIStyleWalletManager.test.ts +709 -0
  138. package/src/__tests/WalletPermissionsManager.callbacks.test.ts +328 -0
  139. package/src/__tests/WalletPermissionsManager.checks.test.ts +857 -0
  140. package/src/__tests/WalletPermissionsManager.encryption.test.ts +407 -0
  141. package/src/__tests/WalletPermissionsManager.fixtures.ts +283 -0
  142. package/src/__tests/WalletPermissionsManager.flows.test.ts +490 -0
  143. package/src/__tests/WalletPermissionsManager.initialization.test.ts +333 -0
  144. package/src/__tests/WalletPermissionsManager.proxying.test.ts +753 -0
  145. package/src/__tests/WalletPermissionsManager.tokens.test.ts +584 -0
  146. package/src/index.all.ts +9 -9
  147. package/src/index.client.ts +9 -1
  148. package/src/utility/identityUtils.ts +170 -0
  149. package/src/wab-client/WABClient.ts +103 -0
  150. package/src/wab-client/__tests/WABClient.test.ts +58 -0
  151. package/src/wab-client/auth-method-interactors/AuthMethodInteractor.ts +47 -0
  152. package/src/wab-client/auth-method-interactors/PersonaIDInteractor.ts +45 -0
  153. package/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.ts +82 -0
  154. package/out/test/wallet/action/abortAction.test.d.ts.map +0 -1
  155. package/out/test/wallet/action/createAction.test.d.ts.map +0 -1
  156. package/out/test/wallet/action/createActionToGenerateBeefs.man.test.d.ts.map +0 -1
  157. package/out/test/wallet/action/internalizeAction.test.d.ts.map +0 -1
  158. package/out/test/wallet/action/relinquishOutput.test.d.ts.map +0 -1
  159. package/out/test/wallet/construct/Wallet.constructor.test.d.ts.map +0 -1
  160. package/out/test/wallet/list/listActions.test.d.ts.map +0 -1
  161. package/out/test/wallet/list/listActions2.test.d.ts.map +0 -1
  162. package/out/test/wallet/list/listCertificates.test.d.ts.map +0 -1
  163. package/out/test/wallet/list/listOutputs.test.d.ts.map +0 -1
  164. package/out/test/wallet/sync/Wallet.sync.test.d.ts.map +0 -1
  165. package/src/SetupClient.ts +0 -532
  166. /package/out/test/{wallet → Wallet}/action/abortAction.test.d.ts +0 -0
  167. /package/out/test/{wallet → Wallet}/action/abortAction.test.js +0 -0
  168. /package/out/test/{wallet → Wallet}/action/createAction.test.d.ts +0 -0
  169. /package/out/test/{wallet → Wallet}/action/createAction.test.js +0 -0
  170. /package/out/test/{wallet → Wallet}/action/createAction2.test.d.ts +0 -0
  171. /package/out/test/{wallet → Wallet}/action/createAction2.test.js +0 -0
  172. /package/out/test/{wallet → Wallet}/action/createActionToGenerateBeefs.man.test.d.ts +0 -0
  173. /package/out/test/{wallet → Wallet}/action/createActionToGenerateBeefs.man.test.js +0 -0
  174. /package/out/test/{wallet → Wallet}/action/internalizeAction.test.d.ts +0 -0
  175. /package/out/test/{wallet → Wallet}/action/internalizeAction.test.js +0 -0
  176. /package/out/test/{wallet → Wallet}/action/relinquishOutput.test.d.ts +0 -0
  177. /package/out/test/{wallet → Wallet}/action/relinquishOutput.test.js +0 -0
  178. /package/out/test/{wallet → Wallet}/construct/Wallet.constructor.test.d.ts +0 -0
  179. /package/out/test/{wallet → Wallet}/construct/Wallet.constructor.test.js +0 -0
  180. /package/out/test/{wallet → Wallet}/list/listActions.test.d.ts +0 -0
  181. /package/out/test/{wallet → Wallet}/list/listActions.test.js +0 -0
  182. /package/out/test/{wallet → Wallet}/list/listActions2.test.d.ts +0 -0
  183. /package/out/test/{wallet → Wallet}/list/listActions2.test.js +0 -0
  184. /package/out/test/{wallet → Wallet}/list/listCertificates.test.d.ts +0 -0
  185. /package/out/test/{wallet → Wallet}/list/listCertificates.test.js +0 -0
  186. /package/out/test/{wallet → Wallet}/list/listOutputs.test.d.ts +0 -0
  187. /package/out/test/{wallet → Wallet}/list/listOutputs.test.js +0 -0
  188. /package/out/test/{wallet → Wallet}/sync/Wallet.sync.test.d.ts +0 -0
  189. /package/out/test/{wallet → Wallet}/sync/Wallet.sync.test.js +0 -0
  190. /package/test/{wallet → Wallet}/action/abortAction.test.ts +0 -0
  191. /package/test/{wallet → Wallet}/action/createAction.test.ts +0 -0
  192. /package/test/{wallet → Wallet}/action/createAction2.test.ts +0 -0
  193. /package/test/{wallet → Wallet}/action/createActionToGenerateBeefs.man.test.ts +0 -0
  194. /package/test/{wallet → Wallet}/action/internalizeAction.test.ts +0 -0
  195. /package/test/{wallet → Wallet}/action/relinquishOutput.test.ts +0 -0
  196. /package/test/{wallet → Wallet}/construct/Wallet.constructor.test.ts +0 -0
  197. /package/test/{wallet → Wallet}/list/listActions.test.ts +0 -0
  198. /package/test/{wallet → Wallet}/list/listActions2.test.ts +0 -0
  199. /package/test/{wallet → Wallet}/list/listCertificates.test.ts +0 -0
  200. /package/test/{wallet → Wallet}/list/listOutputs.test.ts +0 -0
  201. /package/test/{wallet → Wallet}/sync/Wallet.sync.test.ts +0 -0
@@ -0,0 +1,170 @@
1
+ import {
2
+ LookupAnswer,
3
+ Transaction,
4
+ PushDrop,
5
+ VerifiableCertificate,
6
+ Utils,
7
+ ProtoWallet,
8
+ LookupResolver,
9
+ DiscoverCertificatesResult,
10
+ IdentityCertificate,
11
+ IdentityCertifier,
12
+ Base64String
13
+ } from '@bsv/sdk'
14
+ import { Certifier, TrustSettings } from '../WalletSettingsManager'
15
+
16
+ const OUTPUT_INDEX = 0
17
+
18
+ // Our extended certificate includes certifierInfo.
19
+ export interface ExtendedVerifiableCertificate extends IdentityCertificate {
20
+ certifierInfo: IdentityCertifier
21
+ publiclyRevealedKeyring: Record<string, Base64String>
22
+ }
23
+
24
+ // --- Helper Types for Grouping ---
25
+
26
+ interface IdentityGroup {
27
+ totalTrust: number
28
+ members: ExtendedVerifiableCertificate[]
29
+ }
30
+
31
+ /**
32
+ * Transforms an array of VerifiableCertificate instances according to the trust settings.
33
+ * Only certificates whose grouped total trust meets the threshold are returned,
34
+ * and each certificate is augmented with a certifierInfo property.
35
+ *
36
+ * @param trustSettings - the user's trust settings including trustLevel and trusted certifiers.
37
+ * @param certificates - an array of VerifiableCertificate objects.
38
+ * @returns a DiscoverCertificatesResult with totalCertificates and ordered certificates.
39
+ */
40
+ export const transformVerifiableCertificatesWithTrust = (
41
+ trustSettings: TrustSettings,
42
+ certificates: VerifiableCertificate[]
43
+ ): DiscoverCertificatesResult => {
44
+ // Group certificates by subject while accumulating trust.
45
+ const identityGroups: Record<string, IdentityGroup> = {}
46
+ // Cache certifier lookups.
47
+ const certifierCache: Record<string, Certifier> = {}
48
+
49
+ certificates.forEach(cert => {
50
+ const { subject, certifier } = cert
51
+ if (!subject || !certifier) return
52
+
53
+ // Lookup and cache certifier details from trustSettings.
54
+ if (!certifierCache[certifier]) {
55
+ const found = trustSettings.trustedCertifiers.find(
56
+ x => x.identityKey === certifier
57
+ )
58
+ if (!found) return // Skip this certificate if its certifier is not trusted.
59
+ certifierCache[certifier] = found
60
+ }
61
+
62
+ // Create the IdentityCertifier object that we want to attach.
63
+ const certifierInfo: IdentityCertifier = {
64
+ name: certifierCache[certifier].name,
65
+ iconUrl: certifierCache[certifier].iconUrl || '',
66
+ description: certifierCache[certifier].description,
67
+ trust: certifierCache[certifier].trust
68
+ }
69
+
70
+ // Create an extended certificate that includes certifierInfo.
71
+ // Note: We use object spread to copy over all properties from the original certificate.
72
+ const extendedCert: IdentityCertificate = {
73
+ ...cert,
74
+ signature: cert.signature!, // We know it exists at this point
75
+ decryptedFields: cert.decryptedFields as Record<string, string>,
76
+ publiclyRevealedKeyring: cert.keyring,
77
+ certifierInfo
78
+ }
79
+
80
+ // Group certificates by subject.
81
+ if (!identityGroups[subject]) {
82
+ identityGroups[subject] = { totalTrust: 0, members: [] }
83
+ }
84
+ identityGroups[subject].totalTrust += certifierInfo.trust
85
+ identityGroups[subject].members.push(extendedCert)
86
+ })
87
+
88
+ // Filter out groups that do not meet the trust threshold and flatten the results.
89
+ const finalResults: ExtendedVerifiableCertificate[] = []
90
+ Object.values(identityGroups).forEach(group => {
91
+ if (group.totalTrust >= trustSettings.trustLevel) {
92
+ finalResults.push(...group.members)
93
+ }
94
+ })
95
+
96
+ // Sort the certificates by their certifier trust in descending order.
97
+ finalResults.sort((a, b) => b.certifierInfo.trust - a.certifierInfo.trust)
98
+
99
+ return {
100
+ totalCertificates: finalResults.length,
101
+ certificates: finalResults
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Performs an identity overlay service lookup query and returns the parsed results
107
+ *
108
+ * @param query
109
+ * @returns
110
+ */
111
+ export const queryOverlay = async (
112
+ query: unknown,
113
+ resolver: LookupResolver
114
+ ): Promise<VerifiableCertificate[]> => {
115
+ const results = await resolver.query({
116
+ service: 'ls_identity',
117
+ query
118
+ })
119
+
120
+ return await parseResults(results)
121
+ }
122
+
123
+ /**
124
+ * Internal func: Parse the returned UTXOs Decrypt and verify the certificates and signatures Return the set of identity keys, certificates and decrypted certificate fields
125
+ *
126
+ * @param {Output[]} outputs
127
+ * @returns {Promise<VerifiableCertificate[]>}
128
+ */
129
+ export const parseResults = async (
130
+ lookupResult: LookupAnswer
131
+ ): Promise<VerifiableCertificate[]> => {
132
+ if (lookupResult.type === 'output-list') {
133
+ const parsedResults: VerifiableCertificate[] = []
134
+
135
+ for (const output of lookupResult.outputs) {
136
+ try {
137
+ const tx = Transaction.fromAtomicBEEF(output.beef)
138
+ // Decode the Identity token fields from the Bitcoin outputScript
139
+ const decodedOutput = PushDrop.decode(
140
+ tx.outputs[OUTPUT_INDEX].lockingScript
141
+ )
142
+
143
+ // Parse out the certificate and relevant data
144
+ const certificate: VerifiableCertificate = JSON.parse(
145
+ Utils.toUTF8(decodedOutput.fields[0])
146
+ ) // TEST
147
+ const verifiableCert = new VerifiableCertificate(
148
+ certificate.type,
149
+ certificate.serialNumber,
150
+ certificate.subject,
151
+ certificate.revocationOutpoint,
152
+ certificate.certifier,
153
+ certificate.fields,
154
+ certificate.keyring,
155
+ certificate.signature
156
+ )
157
+ const decryptedFields = await verifiableCert.decryptFields(
158
+ new ProtoWallet('anyone')
159
+ )
160
+ verifiableCert.decryptedFields = decryptedFields
161
+ parsedResults.push(verifiableCert)
162
+ } catch (error) {
163
+ console.error(error)
164
+ // do nothing
165
+ }
166
+ }
167
+ return parsedResults
168
+ }
169
+ return []
170
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * WABClient
3
+ *
4
+ * Provides high-level methods to:
5
+ * - Retrieve server info (supported auth methods, faucet info)
6
+ * - Generate a random presentation key
7
+ * - Start/Complete authentication with a chosen AuthMethodInteractor
8
+ * - Link/unlink methods
9
+ * - Request faucet
10
+ * - Delete user
11
+ */
12
+ import fetch from 'node-fetch'
13
+ import { AuthMethodInteractor } from './auth-method-interactors/AuthMethodInteractor'
14
+ import crypto from 'crypto'
15
+
16
+ export class WABClient {
17
+ constructor(private serverUrl: string) {}
18
+
19
+ /**
20
+ * Return the WAB server info
21
+ */
22
+ public async getInfo() {
23
+ const res = await fetch(`${this.serverUrl}/info`)
24
+ return res.json()
25
+ }
26
+
27
+ /**
28
+ * Generate a random 256-bit presentation key as a hex string (client side).
29
+ */
30
+ public generateRandomPresentationKey(): string {
31
+ return crypto.randomBytes(32).toString('hex')
32
+ }
33
+
34
+ /**
35
+ * Start an Auth Method flow
36
+ */
37
+ public async startAuthMethod(
38
+ authMethod: AuthMethodInteractor,
39
+ presentationKey: string,
40
+ payload: any
41
+ ) {
42
+ return authMethod.startAuth(this.serverUrl, presentationKey, payload)
43
+ }
44
+
45
+ /**
46
+ * Complete an Auth Method flow
47
+ */
48
+ public async completeAuthMethod(
49
+ authMethod: AuthMethodInteractor,
50
+ presentationKey: string,
51
+ payload: any
52
+ ) {
53
+ return authMethod.completeAuth(this.serverUrl, presentationKey, payload)
54
+ }
55
+
56
+ /**
57
+ * List user-linked methods
58
+ */
59
+ public async listLinkedMethods(presentationKey: string) {
60
+ const res = await fetch(`${this.serverUrl}/user/linkedMethods`, {
61
+ method: 'POST',
62
+ headers: { 'Content-Type': 'application/json' },
63
+ body: JSON.stringify({ presentationKey })
64
+ })
65
+ return res.json()
66
+ }
67
+
68
+ /**
69
+ * Unlink a given Auth Method by ID
70
+ */
71
+ public async unlinkMethod(presentationKey: string, authMethodId: number) {
72
+ const res = await fetch(`${this.serverUrl}/user/unlinkMethod`, {
73
+ method: 'POST',
74
+ headers: { 'Content-Type': 'application/json' },
75
+ body: JSON.stringify({ presentationKey, authMethodId })
76
+ })
77
+ return res.json()
78
+ }
79
+
80
+ /**
81
+ * Request faucet
82
+ */
83
+ public async requestFaucet(presentationKey: string) {
84
+ const res = await fetch(`${this.serverUrl}/faucet/request`, {
85
+ method: 'POST',
86
+ headers: { 'Content-Type': 'application/json' },
87
+ body: JSON.stringify({ presentationKey })
88
+ })
89
+ return res.json()
90
+ }
91
+
92
+ /**
93
+ * Delete user
94
+ */
95
+ public async deleteUser(presentationKey: string) {
96
+ const res = await fetch(`${this.serverUrl}/user/delete`, {
97
+ method: 'POST',
98
+ headers: { 'Content-Type': 'application/json' },
99
+ body: JSON.stringify({ presentationKey })
100
+ })
101
+ return res.json()
102
+ }
103
+ }
@@ -0,0 +1,58 @@
1
+ import { WABClient } from '../WABClient'
2
+ import { TwilioPhoneInteractor } from '../auth-method-interactors/TwilioPhoneInteractor'
3
+
4
+ // This test suite requires the WAB server to be running on localhost:3000 or you can
5
+ // spin up a test environment or mock server. For demonstration, we'll keep it simple.
6
+
7
+ describe('WABClient', () => {
8
+ let client: WABClient
9
+ const serverUrl = 'http://localhost:3000' // Adjust if your server is different
10
+ const testPresentationKey = 'clientTestKey' + Date.now()
11
+
12
+ beforeAll(() => {
13
+ client = new WABClient(serverUrl)
14
+ })
15
+
16
+ it('should get server info', async () => {
17
+ const info = await client.getInfo()
18
+ expect(info.supportedAuthMethods).toContain('TwilioPhone')
19
+ })
20
+
21
+ it('should do Twilio phone flow', async () => {
22
+ const twilio = new TwilioPhoneInteractor()
23
+
24
+ const startRes = await client.startAuthMethod(twilio, testPresentationKey, {
25
+ phoneNumber: '+12223334444'
26
+ })
27
+ expect(startRes.success).toBe(true)
28
+
29
+ const completeRes = await client.completeAuthMethod(
30
+ twilio,
31
+ testPresentationKey,
32
+ {
33
+ otp: '123456',
34
+ phoneNumber: '+12223334444'
35
+ }
36
+ )
37
+ expect(completeRes.success).toBe(true)
38
+ expect(completeRes.presentationKey).toBe(testPresentationKey)
39
+ })
40
+
41
+ it('should request faucet', async () => {
42
+ const faucetRes = await client.requestFaucet(testPresentationKey)
43
+ expect(faucetRes.success).toBe(true)
44
+ expect(faucetRes.paymentData).toBeDefined()
45
+ expect(faucetRes.paymentData.amount).toBe(1000)
46
+ })
47
+
48
+ it('should list linked methods', async () => {
49
+ const linked = await client.listLinkedMethods(testPresentationKey)
50
+ expect(linked.authMethods).toHaveLength(1)
51
+ expect(linked.authMethods[0].methodType).toBe('TwilioPhone')
52
+ })
53
+
54
+ it('can delete user', async () => {
55
+ const del = await client.deleteUser(testPresentationKey)
56
+ expect(del.success).toBe(true)
57
+ })
58
+ })
@@ -0,0 +1,47 @@
1
+ /**
2
+ * AuthMethodInteractor
3
+ *
4
+ * A base interface/class for client-side logic to interact with a server
5
+ * for a specific Auth Method's flow (start, complete).
6
+ */
7
+
8
+ export interface AuthPayload {
9
+ [key: string]: any
10
+ }
11
+
12
+ export interface StartAuthResponse {
13
+ success: boolean
14
+ message?: string
15
+ data?: any
16
+ }
17
+
18
+ export interface CompleteAuthResponse {
19
+ success: boolean
20
+ message?: string
21
+ presentationKey?: string
22
+ }
23
+
24
+ /**
25
+ * Abstract client-side interactor for an Auth Method
26
+ */
27
+ export abstract class AuthMethodInteractor {
28
+ public abstract methodType: string
29
+
30
+ /**
31
+ * Start the flow (e.g. request an OTP or create a session).
32
+ */
33
+ public abstract startAuth(
34
+ serverUrl: string,
35
+ presentationKey: string,
36
+ payload: AuthPayload
37
+ ): Promise<StartAuthResponse>
38
+
39
+ /**
40
+ * Complete the flow (e.g. confirm OTP).
41
+ */
42
+ public abstract completeAuth(
43
+ serverUrl: string,
44
+ presentationKey: string,
45
+ payload: AuthPayload
46
+ ): Promise<CompleteAuthResponse>
47
+ }
@@ -0,0 +1,45 @@
1
+ import {
2
+ AuthMethodInteractor,
3
+ AuthPayload,
4
+ StartAuthResponse,
5
+ CompleteAuthResponse
6
+ } from './AuthMethodInteractor'
7
+ import fetch from 'node-fetch'
8
+
9
+ export class PersonaIDInteractor extends AuthMethodInteractor {
10
+ public methodType = 'PersonaID'
11
+
12
+ public async startAuth(
13
+ serverUrl: string,
14
+ presentationKey: string,
15
+ payload: AuthPayload
16
+ ): Promise<StartAuthResponse> {
17
+ const res = await fetch(`${serverUrl}/auth/start`, {
18
+ method: 'POST',
19
+ headers: { 'Content-Type': 'application/json' },
20
+ body: JSON.stringify({
21
+ methodType: this.methodType,
22
+ presentationKey,
23
+ payload
24
+ })
25
+ })
26
+ return res.json()
27
+ }
28
+
29
+ public async completeAuth(
30
+ serverUrl: string,
31
+ presentationKey: string,
32
+ payload: AuthPayload
33
+ ): Promise<CompleteAuthResponse> {
34
+ const res = await fetch(`${serverUrl}/auth/complete`, {
35
+ method: 'POST',
36
+ headers: { 'Content-Type': 'application/json' },
37
+ body: JSON.stringify({
38
+ methodType: this.methodType,
39
+ presentationKey,
40
+ payload
41
+ })
42
+ })
43
+ return res.json()
44
+ }
45
+ }
@@ -0,0 +1,82 @@
1
+ import {
2
+ AuthMethodInteractor,
3
+ AuthPayload,
4
+ StartAuthResponse,
5
+ CompleteAuthResponse
6
+ } from './AuthMethodInteractor'
7
+ import fetch from 'node-fetch'
8
+
9
+ /**
10
+ * TwilioPhoneInteractor
11
+ *
12
+ * A client-side class that knows how to call the WAB server for Twilio-based phone verification.
13
+ */
14
+ export class TwilioPhoneInteractor extends AuthMethodInteractor {
15
+ public methodType = 'TwilioPhone'
16
+
17
+ /**
18
+ * Start the Twilio phone verification on the server.
19
+ * - The server will send an SMS code to the user’s phone, using Twilio Verify.
20
+ * @param serverUrl - The base URL of the WAB server (e.g. http://localhost:3000)
21
+ * @param presentationKey - The 256-bit key the client is attempting to authenticate with
22
+ * @param payload - { phoneNumber: string } (the phone number to verify)
23
+ * @returns - { success, message, data }
24
+ */
25
+ public async startAuth(
26
+ serverUrl: string,
27
+ presentationKey: string,
28
+ payload: AuthPayload
29
+ ): Promise<StartAuthResponse> {
30
+ const res = await fetch(`${serverUrl}/auth/start`, {
31
+ method: 'POST',
32
+ headers: { 'Content-Type': 'application/json' },
33
+ body: JSON.stringify({
34
+ methodType: this.methodType,
35
+ presentationKey,
36
+ payload
37
+ })
38
+ })
39
+
40
+ if (!res.ok) {
41
+ return {
42
+ success: false,
43
+ message: `HTTP error ${res.status}`
44
+ }
45
+ }
46
+
47
+ return res.json()
48
+ }
49
+
50
+ /**
51
+ * Complete the Twilio phone verification on the server.
52
+ * - The server will verify the code with Twilio Verify’s verificationChecks endpoint.
53
+ * @param serverUrl - The base URL of the WAB server
54
+ * @param presentationKey - The 256-bit key
55
+ * @param payload - { phoneNumber: string, otp: string } (the code that was received via SMS)
56
+ * @returns - { success, message, presentationKey }
57
+ */
58
+ public async completeAuth(
59
+ serverUrl: string,
60
+ presentationKey: string,
61
+ payload: AuthPayload
62
+ ): Promise<CompleteAuthResponse> {
63
+ const res = await fetch(`${serverUrl}/auth/complete`, {
64
+ method: 'POST',
65
+ headers: { 'Content-Type': 'application/json' },
66
+ body: JSON.stringify({
67
+ methodType: this.methodType,
68
+ presentationKey,
69
+ payload
70
+ })
71
+ })
72
+
73
+ if (!res.ok) {
74
+ return {
75
+ success: false,
76
+ message: `HTTP error ${res.status}`
77
+ }
78
+ }
79
+
80
+ return res.json()
81
+ }
82
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"abortAction.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/action/abortAction.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"createAction.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/action/createAction.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"createActionToGenerateBeefs.man.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/action/createActionToGenerateBeefs.man.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"internalizeAction.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/action/internalizeAction.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"relinquishOutput.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/action/relinquishOutput.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"Wallet.constructor.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/construct/Wallet.constructor.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"listActions.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/list/listActions.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"listActions2.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/list/listActions2.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"listCertificates.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/list/listCertificates.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"listOutputs.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/list/listOutputs.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"Wallet.sync.test.d.ts","sourceRoot":"","sources":["../../../../test/wallet/sync/Wallet.sync.test.ts"],"names":[],"mappings":""}