@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
@@ -1,532 +0,0 @@
1
- import {
2
- BEEF,
3
- CreateActionArgs,
4
- CreateActionOptions,
5
- CreateActionOutput,
6
- CreateActionResult,
7
- KeyDeriver,
8
- LockingScript,
9
- P2PKH,
10
- PrivateKey,
11
- PublicKey,
12
- WalletInterface
13
- } from '@bsv/sdk'
14
- import {
15
- Monitor,
16
- sdk,
17
- Services,
18
- StorageClient,
19
- verifyTruthy,
20
- Wallet,
21
- WalletStorageManager
22
- } from './index.client'
23
-
24
- import * as dotenv from 'dotenv'
25
- dotenv.config()
26
-
27
- /**
28
- * The `SetupClient` class provides static setup functions to construct BRC-100 compatible
29
- * wallets in a variety of configurations.
30
- *
31
- * It serves as a starting point for experimentation and customization.
32
- *
33
- * `SetupClient` references only browser compatible code including storage via `StorageClient`.
34
- * `Setup` extends `SetupClient` adding database storage via `Knex` and `StorageKnex`.
35
- *
36
- */
37
- export abstract class SetupClient {
38
- /**
39
- * @param chain
40
- * @returns true if .env is not valid for chain
41
- */
42
- static noEnv(chain: sdk.Chain): boolean {
43
- try {
44
- SetupClient.getEnv(chain)
45
- return false
46
- } catch {
47
- return true
48
- }
49
- }
50
-
51
- /**
52
- * Creates content for .env file with some private keys, identity keys, sample API keys, and sample MySQL connection string.
53
- *
54
- * Two new, random private keys are generated each time, with their associated public identity keys.
55
- *
56
- * Loading secrets from a .env file is intended only for experimentation and getting started.
57
- * Private keys should never be included directly in your source code.
58
- *
59
- * @publicbody
60
- */
61
- static makeEnv(): string {
62
- const testPrivKey1 = PrivateKey.fromRandom()
63
- const testIdentityKey1 = testPrivKey1.toPublicKey().toString()
64
- const testPrivKey2 = PrivateKey.fromRandom()
65
- const testIdentityKey2 = testPrivKey2.toPublicKey().toString()
66
- const mainPrivKey1 = PrivateKey.fromRandom()
67
- const mainIdentityKey1 = mainPrivKey1.toPublicKey().toString()
68
- const mainPrivKey2 = PrivateKey.fromRandom()
69
- const mainIdentityKey2 = mainPrivKey2.toPublicKey().toString()
70
-
71
- const log = `
72
- # .env file template for working with wallet-toolbox Setup functions.
73
- MY_TEST_IDENTITY = '${testIdentityKey1}'
74
- MY_TEST_IDENTITY2 = '${testIdentityKey2}'
75
- MY_MAIN_IDENTITY = '${mainIdentityKey1}'
76
- MY_MAIN_IDENTITY2 = '${mainIdentityKey2}'
77
- MAIN_TAAL_API_KEY='mainnet_9596de07e92300c6287e4393594ae39c'
78
- TEST_TAAL_API_KEY='testnet_0e6cf72133b43ea2d7861da2a38684e3'
79
- MYSQL_CONNECTION='{"port":3306,"host":"127.0.0.1","user":"root","password":"your_password","database":"your_database", "timezone": "Z"}'
80
- DEV_KEYS = '{
81
- "${testIdentityKey1}": "${testPrivKey1.toString()}",
82
- "${testIdentityKey2}": "${testPrivKey2.toString()}",
83
- "${mainIdentityKey1}": "${mainPrivKey1.toString()}",
84
- "${mainIdentityKey2}": "${mainPrivKey2.toString()}"
85
- }'
86
- `
87
- console.log(log)
88
-
89
- return log
90
- }
91
-
92
- /**
93
- * Reads a .env file of the format created by `makeEnv`.
94
- *
95
- * Returns values for designated `chain`.
96
- *
97
- * Access private keys through the `devKeys` object: `devKeys[identityKey]`
98
- *
99
- * @param chain Which chain to use: 'test' or 'main'
100
- * @returns {SetupEnv} with configuration environment secrets used by `Setup` functions.
101
- *
102
- * @publicbody
103
- */
104
- static getEnv(chain: sdk.Chain): SetupEnv {
105
- // Identity keys of the lead maintainer of this repo...
106
- const identityKey =
107
- chain === 'main'
108
- ? process.env.MY_MAIN_IDENTITY
109
- : process.env.MY_TEST_IDENTITY
110
- const identityKey2 =
111
- chain === 'main'
112
- ? process.env.MY_MAIN_IDENTITY2
113
- : process.env.MY_TEST_IDENTITY2
114
- const filePath =
115
- chain === 'main'
116
- ? process.env.MY_MAIN_FILEPATH
117
- : process.env.MY_TEST_FILEPATH
118
- const DEV_KEYS = process.env.DEV_KEYS || '{}'
119
- const mySQLConnection = process.env.MYSQL_CONNECTION || '{}'
120
- const taalApiKey = verifyTruthy(
121
- chain === 'main'
122
- ? process.env.MAIN_TAAL_API_KEY
123
- : process.env.TEST_TAAL_API_KEY,
124
- `.env value for '${chain.toUpperCase()}_TAAL_API_KEY' is required.`
125
- )
126
-
127
- if (!identityKey || !identityKey2)
128
- throw new sdk.WERR_INVALID_OPERATION(
129
- '.env is not a valid SetupEnv configuration.'
130
- )
131
-
132
- return {
133
- chain,
134
- identityKey,
135
- identityKey2,
136
- filePath,
137
- taalApiKey,
138
- devKeys: JSON.parse(DEV_KEYS) as Record<string, string>,
139
- mySQLConnection
140
- }
141
- }
142
-
143
- /**
144
- * Create a `Wallet`. Storage can optionally be provided or configured later.
145
- *
146
- * The following components are configured: KeyDeriver, WalletStorageManager, WalletService, WalletStorage.
147
- * Optionally, PrivilegedKeyManager is also configured.
148
- *
149
- * @publicbody
150
- */
151
- static async createWallet(args: SetupWalletArgs): Promise<SetupWallet> {
152
- const chain = args.env.chain
153
- args.rootKeyHex ||= args.env.devKeys[args.env.identityKey]
154
- const rootKey = PrivateKey.fromHex(args.rootKeyHex)
155
- const identityKey = rootKey.toPublicKey().toString()
156
- const keyDeriver = new KeyDeriver(rootKey)
157
- const storage = new WalletStorageManager(
158
- identityKey,
159
- args.active,
160
- args.backups
161
- )
162
- if (storage.stores.length > 0) await storage.makeAvailable()
163
- const serviceOptions = Services.createDefaultOptions(chain)
164
- serviceOptions.taalApiKey = args.env.taalApiKey
165
- const services = new Services(serviceOptions)
166
- const monopts = Monitor.createDefaultWalletMonitorOptions(
167
- chain,
168
- storage,
169
- services
170
- )
171
- const monitor = new Monitor(monopts)
172
- monitor.addDefaultTasks()
173
- const privilegedKeyManager = args.privilegedKeyGetter
174
- ? new sdk.PrivilegedKeyManager(args.privilegedKeyGetter)
175
- : undefined
176
- const wallet = new Wallet({
177
- chain,
178
- keyDeriver,
179
- storage,
180
- services,
181
- monitor,
182
- privilegedKeyManager
183
- })
184
- const r: SetupWallet = {
185
- rootKey,
186
- identityKey,
187
- keyDeriver,
188
- chain,
189
- storage,
190
- services,
191
- monitor,
192
- wallet
193
- }
194
- return r
195
- }
196
-
197
- /**
198
- * Setup a new `Wallet` without requiring a .env file.
199
- *
200
- * @param args.chain - 'main' or 'test'
201
- * @param args.rootKeyHex - Root private key for wallet's key deriver.
202
- * @param args.storageUrl - Optional. `StorageClient` and `chain` compatible endpoint URL.
203
- * @param args.privilegedKeyGetter - Optional. Method that will return the privileged `PrivateKey`, on demand.
204
- */
205
- static async createWalletClientNoEnv(args: {
206
- chain: sdk.Chain
207
- rootKeyHex: string
208
- storageUrl?: string
209
- privilegedKeyGetter?: () => Promise<PrivateKey>
210
- }): Promise<Wallet> {
211
- const chain = args.chain
212
- const endpointUrl =
213
- args.storageUrl ||
214
- `https://${args.chain !== 'main' ? 'staging-' : ''}storage.babbage.systems`
215
- const rootKey = PrivateKey.fromHex(args.rootKeyHex)
216
- const keyDeriver = new KeyDeriver(rootKey)
217
- const storage = new WalletStorageManager(keyDeriver.identityKey)
218
- const services = new Services(chain)
219
- const privilegedKeyManager = args.privilegedKeyGetter
220
- ? new sdk.PrivilegedKeyManager(args.privilegedKeyGetter)
221
- : undefined
222
- const wallet = new Wallet({
223
- chain,
224
- keyDeriver,
225
- storage,
226
- services,
227
- privilegedKeyManager
228
- })
229
- const client = new StorageClient(wallet, endpointUrl)
230
- await storage.addWalletStorageProvider(client)
231
- await storage.makeAvailable()
232
- return wallet
233
- }
234
-
235
- /**
236
- * @publicbody
237
- */
238
- static async createWalletClient(
239
- args: SetupWalletClientArgs
240
- ): Promise<SetupWalletClient> {
241
- const wo = await SetupClient.createWallet(args)
242
-
243
- const endpointUrl =
244
- args.endpointUrl ||
245
- `https://${args.env.chain !== 'main' ? 'staging-' : ''}storage.babbage.systems`
246
-
247
- const client = new StorageClient(wo.wallet, endpointUrl)
248
- await wo.storage.addWalletStorageProvider(client)
249
- await wo.storage.makeAvailable()
250
- return {
251
- ...wo,
252
- endpointUrl
253
- }
254
- }
255
-
256
- /**
257
- * @publicbody
258
- */
259
- static getKeyPair(priv?: string | PrivateKey): KeyPairAddress {
260
- if (priv === undefined) priv = PrivateKey.fromRandom()
261
- else if (typeof priv === 'string') priv = new PrivateKey(priv, 'hex')
262
-
263
- const pub = PublicKey.fromPrivateKey(priv)
264
- const address = pub.toAddress()
265
- return { privateKey: priv, publicKey: pub, address }
266
- }
267
-
268
- /**
269
- * @publicbody
270
- */
271
- static getLockP2PKH(address: string): LockingScript {
272
- const p2pkh = new P2PKH()
273
- const lock = p2pkh.lock(address)
274
- return lock
275
- }
276
-
277
- /**
278
- * @publicbody
279
- */
280
- static getUnlockP2PKH(
281
- priv: PrivateKey,
282
- satoshis: number
283
- ): sdk.ScriptTemplateUnlock {
284
- const p2pkh = new P2PKH()
285
- const lock = SetupClient.getLockP2PKH(SetupClient.getKeyPair(priv).address)
286
- // Prepare to pay with SIGHASH_ALL and without ANYONE_CAN_PAY.
287
- // In otherwords:
288
- // - all outputs must remain in the current order, amount and locking scripts.
289
- // - all inputs must remain from the current outpoints and sequence numbers.
290
- // (unlock scripts are never signed)
291
- const unlock = p2pkh.unlock(priv, 'all', false, satoshis, lock)
292
- return unlock
293
- }
294
-
295
- /**
296
- * @publicbody
297
- */
298
- static createP2PKHOutputs(
299
- outputs: {
300
- address: string
301
- satoshis: number
302
- outputDescription?: string
303
- basket?: string
304
- tags?: string[]
305
- }[]
306
- ): CreateActionOutput[] {
307
- const os: CreateActionOutput[] = []
308
- const count = outputs.length
309
- for (let i = 0; i < count; i++) {
310
- const o = outputs[i]
311
- os.push({
312
- basket: o.basket,
313
- tags: o.tags,
314
- satoshis: o.satoshis,
315
- lockingScript: SetupClient.getLockP2PKH(o.address).toHex(),
316
- outputDescription: o.outputDescription || `p2pkh ${i}`
317
- })
318
- }
319
- return os
320
- }
321
-
322
- /**
323
- * @publicbody
324
- */
325
- static async createP2PKHOutputsAction(
326
- wallet: WalletInterface,
327
- outputs: {
328
- address: string
329
- satoshis: number
330
- outputDescription?: string
331
- basket?: string
332
- tags?: string[]
333
- }[],
334
- options?: CreateActionOptions
335
- ): Promise<{
336
- cr: CreateActionResult
337
- outpoints: string[] | undefined
338
- }> {
339
- const os = SetupClient.createP2PKHOutputs(outputs)
340
-
341
- const createArgs: CreateActionArgs = {
342
- description: `createP2PKHOutputs`,
343
- outputs: os,
344
- options: {
345
- ...options,
346
- // Don't randomize so we can simplify outpoint creation
347
- randomizeOutputs: false
348
- }
349
- }
350
-
351
- const cr = await wallet.createAction(createArgs)
352
-
353
- let outpoints: string[] | undefined
354
-
355
- if (cr.txid) {
356
- outpoints = os.map((o, i) => `${cr.txid}.${i}`)
357
- }
358
-
359
- return { cr, outpoints }
360
- }
361
-
362
- /**
363
- * @publicbody
364
- */
365
- static async fundWalletFromP2PKHOutpoints(
366
- wallet: WalletInterface,
367
- outpoints: string[],
368
- p2pkhKey: KeyPairAddress,
369
- inputBEEF?: BEEF
370
- ) {
371
- // TODO
372
- }
373
- }
374
-
375
- /**
376
- * A private key and associated public key and address.
377
- */
378
- export interface KeyPairAddress {
379
- privateKey: PrivateKey
380
- publicKey: PublicKey
381
- address: string
382
- }
383
-
384
- /**
385
- * `SetupEnv` provides a starting point for managing secrets that
386
- * must not appear in source code.
387
- *
388
- * The `makeEnv` and `getEnv` functions of the `Setup` and `SetupClient` classes
389
- * provide an easy way to create and import these secrets and related properties.
390
- */
391
- export interface SetupEnv {
392
- /**
393
- * The chan being accessed: 'main' for mainnet, 'test' for 'testnet'.
394
- */
395
- chain: sdk.Chain
396
- /**
397
- * The user's primary identity key (public key).
398
- */
399
- identityKey: string
400
- /**
401
- * A secondary identity key (public key), used to test exchanges with other users.
402
- */
403
- identityKey2: string
404
- /**
405
- * Filepath to sqlite file to be used for identityKey wallet.
406
- */
407
- filePath: string | undefined
408
- /**
409
- * A vaild TAAL API key for use by `Services`
410
- */
411
- taalApiKey: string
412
- /**
413
- * A map of public keys (identity keys, hex strings) to private keys (hex strings).
414
- */
415
- devKeys: Record<string, string>
416
- /**
417
- * A MySQL connection string including user and password properties.
418
- * Must be valid to make use of MySQL `Setup` class support.
419
- */
420
- mySQLConnection: string
421
- }
422
-
423
- /**
424
- * Arguments used by `createWallet` to construct a `SetupWallet`.
425
- *
426
- * Extension `SetupWalletClientArgs` used by `createWalletClient` to construct a `SetupWalletClient`.
427
- *
428
- * Extension `SetupWalletKnexArgs` used by `createWalletKnex` to construct a `SetupWalletKnex`.
429
- *
430
- * Extension `SetupWalletMySQLArgs` used by `createWalletMySQL` to construct a `SetupWalletKnex`.
431
- *
432
- * Extension `SetupWalletSQLiteArgs` used by `createWalletSQLite` to construct a `SetupWalletKnex`.
433
- */
434
- export interface SetupWalletArgs {
435
- /**
436
- * Configuration "secrets" typically obtained by `Setup.makeEnv` and `Setup.getEnv` functions.
437
- */
438
- env: SetupEnv
439
- /**
440
- * Optional. The non-privileged private key used to initialize the `KeyDeriver` and determine the `identityKey`.
441
- * Defaults to `env.devKeys[env.identityKey]
442
- */
443
- rootKeyHex?: string
444
- /**
445
- * Optional. The privileged private key getter used to initialize the `PrivilegedKeyManager`.
446
- * Defaults to undefined.
447
- */
448
- privilegedKeyGetter?: () => Promise<PrivateKey>
449
- /**
450
- * Optional. Active wallet storage. Can be added later.
451
- */
452
- active?: sdk.WalletStorageProvider
453
- /**
454
- * Optional. One or more storage providers managed as backup destinations. Can be added later.
455
- */
456
- backups?: sdk.WalletStorageProvider[]
457
- }
458
-
459
- /**
460
- * When creating a BRC-100 compatible `Wallet`, many components come into play.
461
- *
462
- * All of the `createWallet` functions in the `Setup` and `SetupClient` classes return
463
- * an object with direct access to each component to facilitate experimentation, testing
464
- * and customization.
465
- */
466
- export interface SetupWallet {
467
- /**
468
- * The rootKey of the `KeyDeriver`. The private key from which other keys are derived.
469
- */
470
- rootKey: PrivateKey
471
- /**
472
- * The pubilc key associated with the `rootKey` which also serves as the wallet's identity.
473
- */
474
- identityKey: string
475
- /**
476
- * The `KeyDeriver` component used by the wallet for key derivation and cryptographic functions.
477
- */
478
- keyDeriver: KeyDeriver
479
- /**
480
- * The chain ('main' or 'test') which the wallet accesses.
481
- */
482
- chain: sdk.Chain
483
- /**
484
- * The `WalletStorageManager` that manages all the configured storage providers (active and backups)
485
- * accessed by the wallet.
486
- */
487
- storage: WalletStorageManager
488
- /**
489
- * The network `Services` component which provides the wallet with access to external services hosted
490
- * on the public network.
491
- */
492
- services: Services
493
- /**
494
- * The background task `Monitor` component available to the wallet to offload tasks
495
- * that speed up wallet operations and maintain data integrity.
496
- */
497
- monitor: Monitor
498
- /**
499
- * The actual BRC-100 `Wallet` to which all the other properties and components contribute.
500
- *
501
- * Note that internally, the wallet is itself linked to all these properties and components.
502
- * They are included in this interface to facilitate access after wallet construction for
503
- * experimentation, testing and customization. Any changes made to the configuration of these
504
- * components after construction may disrupt the normal operation of the wallet.
505
- */
506
- wallet: Wallet
507
- }
508
-
509
- /**
510
- * Extension `SetupWalletClientArgs` of `SetupWalletArgs` is used by `createWalletClient`
511
- * to construct a `SetupWalletClient`.
512
- */
513
- export interface SetupWalletClientArgs extends SetupWalletArgs {
514
- /**
515
- * The endpoint URL of a service hosting the `StorageServer` JSON-RPC service to
516
- * which a `StorageClient` instance should connect to function as
517
- * the active storage provider of the newly created wallet.
518
- */
519
- endpointUrl?: string
520
- }
521
-
522
- /**
523
- * Extension `SetupWalletClient` of `SetupWallet` is returned by `createWalletClient`
524
- */
525
- export interface SetupWalletClient extends SetupWallet {
526
- /**
527
- * The endpoint URL of the service hosting the `StorageServer` JSON-RPC service to
528
- * which a `StorageClient` instance is connected to function as
529
- * the active storage provider of the wallet.
530
- */
531
- endpointUrl: string
532
- }