@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
package/src/Setup.ts CHANGED
@@ -1,26 +1,373 @@
1
- import { KeyDeriver, PrivateKey } from '@bsv/sdk'
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'
2
14
  import {
3
15
  Monitor,
4
16
  sdk,
5
17
  Services,
6
- SetupClient,
18
+ StorageClient,
19
+ verifyTruthy,
7
20
  Wallet,
8
21
  WalletStorageManager
9
22
  } from './index.client'
10
23
  import { Knex, knex as makeKnex } from 'knex'
11
- import { SetupWallet, SetupWalletArgs, StorageKnex } from './index.all'
24
+ import { StorageKnex } from './index.all'
25
+
26
+ import * as dotenv from 'dotenv'
27
+ dotenv.config()
12
28
 
13
29
  /**
14
30
  * The 'Setup` class provides static setup functions to construct BRC-100 compatible
15
31
  * wallets in a variety of configurations.
16
32
  *
17
33
  * It serves as a starting point for experimentation and customization.
18
- *
19
- * `SetupClient` references only browser compatible code including storage via `StorageClient`.
20
- * `Setup` extends `SetupClient` adding database storage via `Knex` and `StorageKnex`.
21
- *
22
34
  */
23
- export abstract class Setup extends SetupClient {
35
+ export abstract class Setup {
36
+ /**
37
+ * @param chain
38
+ * @returns true if .env is not valid for chain
39
+ */
40
+ static noEnv(chain: sdk.Chain): boolean {
41
+ try {
42
+ Setup.getEnv(chain)
43
+ return false
44
+ } catch {
45
+ return true
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Creates content for .env file with some private keys, identity keys, sample API keys, and sample MySQL connection string.
51
+ *
52
+ * Two new, random private keys are generated each time, with their associated public identity keys.
53
+ *
54
+ * Loading secrets from a .env file is intended only for experimentation and getting started.
55
+ * Private keys should never be included directly in your source code.
56
+ *
57
+ * @publicbody
58
+ */
59
+ static makeEnv(): string {
60
+ const testPrivKey1 = PrivateKey.fromRandom()
61
+ const testIdentityKey1 = testPrivKey1.toPublicKey().toString()
62
+ const testPrivKey2 = PrivateKey.fromRandom()
63
+ const testIdentityKey2 = testPrivKey2.toPublicKey().toString()
64
+ const mainPrivKey1 = PrivateKey.fromRandom()
65
+ const mainIdentityKey1 = mainPrivKey1.toPublicKey().toString()
66
+ const mainPrivKey2 = PrivateKey.fromRandom()
67
+ const mainIdentityKey2 = mainPrivKey2.toPublicKey().toString()
68
+
69
+ const log = `
70
+ # .env file template for working with wallet-toolbox Setup functions.
71
+ MY_TEST_IDENTITY = '${testIdentityKey1}'
72
+ MY_TEST_IDENTITY2 = '${testIdentityKey2}'
73
+ MY_MAIN_IDENTITY = '${mainIdentityKey1}'
74
+ MY_MAIN_IDENTITY2 = '${mainIdentityKey2}'
75
+ MAIN_TAAL_API_KEY='mainnet_9596de07e92300c6287e4393594ae39c'
76
+ TEST_TAAL_API_KEY='testnet_0e6cf72133b43ea2d7861da2a38684e3'
77
+ MYSQL_CONNECTION='{"port":3306,"host":"127.0.0.1","user":"root","password":"your_password","database":"your_database", "timezone": "Z"}'
78
+ DEV_KEYS = '{
79
+ "${testIdentityKey1}": "${testPrivKey1.toString()}",
80
+ "${testIdentityKey2}": "${testPrivKey2.toString()}",
81
+ "${mainIdentityKey1}": "${mainPrivKey1.toString()}",
82
+ "${mainIdentityKey2}": "${mainPrivKey2.toString()}"
83
+ }'
84
+ `
85
+ console.log(log)
86
+
87
+ return log
88
+ }
89
+
90
+ /**
91
+ * Reads a .env file of the format created by `makeEnv`.
92
+ *
93
+ * Returns values for designated `chain`.
94
+ *
95
+ * Access private keys through the `devKeys` object: `devKeys[identityKey]`
96
+ *
97
+ * @param chain Which chain to use: 'test' or 'main'
98
+ * @returns {SetupEnv} with configuration environment secrets used by `Setup` functions.
99
+ *
100
+ * @publicbody
101
+ */
102
+ static getEnv(chain: sdk.Chain): SetupEnv {
103
+ // Identity keys of the lead maintainer of this repo...
104
+ const identityKey =
105
+ chain === 'main'
106
+ ? process.env.MY_MAIN_IDENTITY
107
+ : process.env.MY_TEST_IDENTITY
108
+ const identityKey2 =
109
+ chain === 'main'
110
+ ? process.env.MY_MAIN_IDENTITY2
111
+ : process.env.MY_TEST_IDENTITY2
112
+ const filePath =
113
+ chain === 'main'
114
+ ? process.env.MY_MAIN_FILEPATH
115
+ : process.env.MY_TEST_FILEPATH
116
+ const DEV_KEYS = process.env.DEV_KEYS || '{}'
117
+ const mySQLConnection = process.env.MYSQL_CONNECTION || '{}'
118
+ const taalApiKey = verifyTruthy(
119
+ chain === 'main'
120
+ ? process.env.MAIN_TAAL_API_KEY
121
+ : process.env.TEST_TAAL_API_KEY,
122
+ `.env value for '${chain.toUpperCase()}_TAAL_API_KEY' is required.`
123
+ )
124
+
125
+ if (!identityKey || !identityKey2)
126
+ throw new sdk.WERR_INVALID_OPERATION(
127
+ '.env is not a valid SetupEnv configuration.'
128
+ )
129
+
130
+ return {
131
+ chain,
132
+ identityKey,
133
+ identityKey2,
134
+ filePath,
135
+ taalApiKey,
136
+ devKeys: JSON.parse(DEV_KEYS) as Record<string, string>,
137
+ mySQLConnection
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Create a `Wallet`. Storage can optionally be provided or configured later.
143
+ *
144
+ * The following components are configured: KeyDeriver, WalletStorageManager, WalletService, WalletStorage.
145
+ * Optionally, PrivilegedKeyManager is also configured.
146
+ *
147
+ * @publicbody
148
+ */
149
+ static async createWallet(args: SetupWalletArgs): Promise<SetupWallet> {
150
+ const chain = args.env.chain
151
+ args.rootKeyHex ||= args.env.devKeys[args.env.identityKey]
152
+ const rootKey = PrivateKey.fromHex(args.rootKeyHex)
153
+ const identityKey = rootKey.toPublicKey().toString()
154
+ const keyDeriver = new KeyDeriver(rootKey)
155
+ const storage = new WalletStorageManager(
156
+ identityKey,
157
+ args.active,
158
+ args.backups
159
+ )
160
+ if (storage.stores.length > 0) await storage.makeAvailable()
161
+ const serviceOptions = Services.createDefaultOptions(chain)
162
+ serviceOptions.taalApiKey = args.env.taalApiKey
163
+ const services = new Services(serviceOptions)
164
+ const monopts = Monitor.createDefaultWalletMonitorOptions(
165
+ chain,
166
+ storage,
167
+ services
168
+ )
169
+ const monitor = new Monitor(monopts)
170
+ monitor.addDefaultTasks()
171
+ const privilegedKeyManager = args.privilegedKeyGetter
172
+ ? new sdk.PrivilegedKeyManager(args.privilegedKeyGetter)
173
+ : undefined
174
+ const wallet = new Wallet({
175
+ chain,
176
+ keyDeriver,
177
+ storage,
178
+ services,
179
+ monitor,
180
+ privilegedKeyManager
181
+ })
182
+ const r: SetupWallet = {
183
+ rootKey,
184
+ identityKey,
185
+ keyDeriver,
186
+ chain,
187
+ storage,
188
+ services,
189
+ monitor,
190
+ wallet
191
+ }
192
+ return r
193
+ }
194
+
195
+ /**
196
+ * Setup a new `Wallet` without requiring a .env file.
197
+ *
198
+ * @param args.chain - 'main' or 'test'
199
+ * @param args.rootKeyHex - Root private key for wallet's key deriver.
200
+ * @param args.storageUrl - Optional. `StorageClient` and `chain` compatible endpoint URL.
201
+ * @param args.privilegedKeyGetter - Optional. Method that will return the privileged `PrivateKey`, on demand.
202
+ */
203
+ static async createWalletClientNoEnv(args: {
204
+ chain: sdk.Chain
205
+ rootKeyHex: string
206
+ storageUrl?: string
207
+ privilegedKeyGetter?: () => Promise<PrivateKey>
208
+ }): Promise<Wallet> {
209
+ const chain = args.chain
210
+ const endpointUrl =
211
+ args.storageUrl ||
212
+ `https://${args.chain !== 'main' ? 'staging-' : ''}storage.babbage.systems`
213
+ const rootKey = PrivateKey.fromHex(args.rootKeyHex)
214
+ const keyDeriver = new KeyDeriver(rootKey)
215
+ const storage = new WalletStorageManager(keyDeriver.identityKey)
216
+ const services = new Services(chain)
217
+ const privilegedKeyManager = args.privilegedKeyGetter
218
+ ? new sdk.PrivilegedKeyManager(args.privilegedKeyGetter)
219
+ : undefined
220
+ const wallet = new Wallet({
221
+ chain,
222
+ keyDeriver,
223
+ storage,
224
+ services,
225
+ privilegedKeyManager
226
+ })
227
+ const client = new StorageClient(wallet, endpointUrl)
228
+ await storage.addWalletStorageProvider(client)
229
+ await storage.makeAvailable()
230
+ return wallet
231
+ }
232
+
233
+ /**
234
+ * @publicbody
235
+ */
236
+ static async createWalletClient(
237
+ args: SetupWalletClientArgs
238
+ ): Promise<SetupWalletClient> {
239
+ const wo = await Setup.createWallet(args)
240
+
241
+ const endpointUrl =
242
+ args.endpointUrl ||
243
+ `https://${args.env.chain !== 'main' ? 'staging-' : ''}storage.babbage.systems`
244
+
245
+ const client = new StorageClient(wo.wallet, endpointUrl)
246
+ await wo.storage.addWalletStorageProvider(client)
247
+ await wo.storage.makeAvailable()
248
+ return {
249
+ ...wo,
250
+ endpointUrl
251
+ }
252
+ }
253
+
254
+ /**
255
+ * @publicbody
256
+ */
257
+ static getKeyPair(priv?: string | PrivateKey): KeyPairAddress {
258
+ if (priv === undefined) priv = PrivateKey.fromRandom()
259
+ else if (typeof priv === 'string') priv = new PrivateKey(priv, 'hex')
260
+
261
+ const pub = PublicKey.fromPrivateKey(priv)
262
+ const address = pub.toAddress()
263
+ return { privateKey: priv, publicKey: pub, address }
264
+ }
265
+
266
+ /**
267
+ * @publicbody
268
+ */
269
+ static getLockP2PKH(address: string): LockingScript {
270
+ const p2pkh = new P2PKH()
271
+ const lock = p2pkh.lock(address)
272
+ return lock
273
+ }
274
+
275
+ /**
276
+ * @publicbody
277
+ */
278
+ static getUnlockP2PKH(
279
+ priv: PrivateKey,
280
+ satoshis: number
281
+ ): sdk.ScriptTemplateUnlock {
282
+ const p2pkh = new P2PKH()
283
+ const lock = Setup.getLockP2PKH(Setup.getKeyPair(priv).address)
284
+ // Prepare to pay with SIGHASH_ALL and without ANYONE_CAN_PAY.
285
+ // In otherwords:
286
+ // - all outputs must remain in the current order, amount and locking scripts.
287
+ // - all inputs must remain from the current outpoints and sequence numbers.
288
+ // (unlock scripts are never signed)
289
+ const unlock = p2pkh.unlock(priv, 'all', false, satoshis, lock)
290
+ return unlock
291
+ }
292
+
293
+ /**
294
+ * @publicbody
295
+ */
296
+ static createP2PKHOutputs(
297
+ outputs: {
298
+ address: string
299
+ satoshis: number
300
+ outputDescription?: string
301
+ basket?: string
302
+ tags?: string[]
303
+ }[]
304
+ ): CreateActionOutput[] {
305
+ const os: CreateActionOutput[] = []
306
+ const count = outputs.length
307
+ for (let i = 0; i < count; i++) {
308
+ const o = outputs[i]
309
+ os.push({
310
+ basket: o.basket,
311
+ tags: o.tags,
312
+ satoshis: o.satoshis,
313
+ lockingScript: Setup.getLockP2PKH(o.address).toHex(),
314
+ outputDescription: o.outputDescription || `p2pkh ${i}`
315
+ })
316
+ }
317
+ return os
318
+ }
319
+
320
+ /**
321
+ * @publicbody
322
+ */
323
+ static async createP2PKHOutputsAction(
324
+ wallet: WalletInterface,
325
+ outputs: {
326
+ address: string
327
+ satoshis: number
328
+ outputDescription?: string
329
+ basket?: string
330
+ tags?: string[]
331
+ }[],
332
+ options?: CreateActionOptions
333
+ ): Promise<{
334
+ cr: CreateActionResult
335
+ outpoints: string[] | undefined
336
+ }> {
337
+ const os = Setup.createP2PKHOutputs(outputs)
338
+
339
+ const createArgs: CreateActionArgs = {
340
+ description: `createP2PKHOutputs`,
341
+ outputs: os,
342
+ options: {
343
+ ...options,
344
+ // Don't randomize so we can simplify outpoint creation
345
+ randomizeOutputs: false
346
+ }
347
+ }
348
+
349
+ const cr = await wallet.createAction(createArgs)
350
+
351
+ let outpoints: string[] | undefined
352
+
353
+ if (cr.txid) {
354
+ outpoints = os.map((o, i) => `${cr.txid}.${i}`)
355
+ }
356
+
357
+ return { cr, outpoints }
358
+ }
359
+
360
+ /**
361
+ * @publicbody
362
+ */
363
+ static async fundWalletFromP2PKHOutpoints(
364
+ wallet: WalletInterface,
365
+ outpoints: string[],
366
+ p2pkhKey: KeyPairAddress,
367
+ inputBEEF?: BEEF
368
+ ) {
369
+ // TODO
370
+ }
24
371
  /**
25
372
  * Adds `Knex` based storage to a `Wallet` configured by `Setup.createWalletOnly`
26
373
  *
@@ -113,6 +460,42 @@ export abstract class Setup extends SetupClient {
113
460
  }
114
461
  }
115
462
 
463
+ /**
464
+ * Arguments used by `createWallet` to construct a `SetupWallet`.
465
+ *
466
+ * Extension `SetupWalletClientArgs` used by `createWalletClient` to construct a `SetupWalletClient`.
467
+ *
468
+ * Extension `SetupWalletKnexArgs` used by `createWalletKnex` to construct a `SetupWalletKnex`.
469
+ *
470
+ * Extension `SetupWalletMySQLArgs` used by `createWalletMySQL` to construct a `SetupWalletKnex`.
471
+ *
472
+ * Extension `SetupWalletSQLiteArgs` used by `createWalletSQLite` to construct a `SetupWalletKnex`.
473
+ */
474
+ export interface SetupWalletArgs {
475
+ /**
476
+ * Configuration "secrets" typically obtained by `Setup.makeEnv` and `Setup.getEnv` functions.
477
+ */
478
+ env: SetupEnv
479
+ /**
480
+ * Optional. The non-privileged private key used to initialize the `KeyDeriver` and determine the `identityKey`.
481
+ * Defaults to `env.devKeys[env.identityKey]
482
+ */
483
+ rootKeyHex?: string
484
+ /**
485
+ * Optional. The privileged private key getter used to initialize the `PrivilegedKeyManager`.
486
+ * Defaults to undefined.
487
+ */
488
+ privilegedKeyGetter?: () => Promise<PrivateKey>
489
+ /**
490
+ * Optional. Active wallet storage. Can be added later.
491
+ */
492
+ active?: sdk.WalletStorageProvider
493
+ /**
494
+ * Optional. One or more storage providers managed as backup destinations. Can be added later.
495
+ */
496
+ backups?: sdk.WalletStorageProvider[]
497
+ }
498
+
116
499
  /**
117
500
  *
118
501
  */
@@ -152,3 +535,126 @@ export interface SetupWalletKnex extends SetupWallet {
152
535
  monitor: Monitor
153
536
  wallet: Wallet
154
537
  }
538
+
539
+ /**
540
+ * A private key and associated public key and address.
541
+ */
542
+ export interface KeyPairAddress {
543
+ privateKey: PrivateKey
544
+ publicKey: PublicKey
545
+ address: string
546
+ }
547
+
548
+ /**
549
+ * `SetupEnv` provides a starting point for managing secrets that
550
+ * must not appear in source code.
551
+ *
552
+ * The `makeEnv` and `getEnv` functions of the `Setup` and `SetupClient` classes
553
+ * provide an easy way to create and import these secrets and related properties.
554
+ */
555
+ export interface SetupEnv {
556
+ /**
557
+ * The chan being accessed: 'main' for mainnet, 'test' for 'testnet'.
558
+ */
559
+ chain: sdk.Chain
560
+ /**
561
+ * The user's primary identity key (public key).
562
+ */
563
+ identityKey: string
564
+ /**
565
+ * A secondary identity key (public key), used to test exchanges with other users.
566
+ */
567
+ identityKey2: string
568
+ /**
569
+ * Filepath to sqlite file to be used for identityKey wallet.
570
+ */
571
+ filePath: string | undefined
572
+ /**
573
+ * A vaild TAAL API key for use by `Services`
574
+ */
575
+ taalApiKey: string
576
+ /**
577
+ * A map of public keys (identity keys, hex strings) to private keys (hex strings).
578
+ */
579
+ devKeys: Record<string, string>
580
+ /**
581
+ * A MySQL connection string including user and password properties.
582
+ * Must be valid to make use of MySQL `Setup` class support.
583
+ */
584
+ mySQLConnection: string
585
+ }
586
+
587
+ /**
588
+ * When creating a BRC-100 compatible `Wallet`, many components come into play.
589
+ *
590
+ * All of the `createWallet` functions in the `Setup` and `SetupClient` classes return
591
+ * an object with direct access to each component to facilitate experimentation, testing
592
+ * and customization.
593
+ */
594
+ export interface SetupWallet {
595
+ /**
596
+ * The rootKey of the `KeyDeriver`. The private key from which other keys are derived.
597
+ */
598
+ rootKey: PrivateKey
599
+ /**
600
+ * The pubilc key associated with the `rootKey` which also serves as the wallet's identity.
601
+ */
602
+ identityKey: string
603
+ /**
604
+ * The `KeyDeriver` component used by the wallet for key derivation and cryptographic functions.
605
+ */
606
+ keyDeriver: KeyDeriver
607
+ /**
608
+ * The chain ('main' or 'test') which the wallet accesses.
609
+ */
610
+ chain: sdk.Chain
611
+ /**
612
+ * The `WalletStorageManager` that manages all the configured storage providers (active and backups)
613
+ * accessed by the wallet.
614
+ */
615
+ storage: WalletStorageManager
616
+ /**
617
+ * The network `Services` component which provides the wallet with access to external services hosted
618
+ * on the public network.
619
+ */
620
+ services: Services
621
+ /**
622
+ * The background task `Monitor` component available to the wallet to offload tasks
623
+ * that speed up wallet operations and maintain data integrity.
624
+ */
625
+ monitor: Monitor
626
+ /**
627
+ * The actual BRC-100 `Wallet` to which all the other properties and components contribute.
628
+ *
629
+ * Note that internally, the wallet is itself linked to all these properties and components.
630
+ * They are included in this interface to facilitate access after wallet construction for
631
+ * experimentation, testing and customization. Any changes made to the configuration of these
632
+ * components after construction may disrupt the normal operation of the wallet.
633
+ */
634
+ wallet: Wallet
635
+ }
636
+
637
+ /**
638
+ * Extension `SetupWalletClientArgs` of `SetupWalletArgs` is used by `createWalletClient`
639
+ * to construct a `SetupWalletClient`.
640
+ */
641
+ export interface SetupWalletClientArgs extends SetupWalletArgs {
642
+ /**
643
+ * The endpoint URL of a service hosting the `StorageServer` JSON-RPC service to
644
+ * which a `StorageClient` instance should connect to function as
645
+ * the active storage provider of the newly created wallet.
646
+ */
647
+ endpointUrl?: string
648
+ }
649
+
650
+ /**
651
+ * Extension `SetupWalletClient` of `SetupWallet` is returned by `createWalletClient`
652
+ */
653
+ export interface SetupWalletClient extends SetupWallet {
654
+ /**
655
+ * The endpoint URL of the service hosting the `StorageServer` JSON-RPC service to
656
+ * which a `StorageClient` instance is connected to function as
657
+ * the active storage provider of the wallet.
658
+ */
659
+ endpointUrl: string
660
+ }