@bsv/wallet-toolbox 1.1.46 → 1.1.48

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 (87) hide show
  1. package/docs/client.md +112 -72
  2. package/docs/services.md +4 -3
  3. package/docs/storage.md +11 -24
  4. package/docs/wallet.md +112 -72
  5. package/out/src/Wallet.d.ts +3 -7
  6. package/out/src/Wallet.d.ts.map +1 -1
  7. package/out/src/Wallet.js +6 -5
  8. package/out/src/Wallet.js.map +1 -1
  9. package/out/src/monitor/tasks/TaskCheckForProofs.d.ts.map +1 -1
  10. package/out/src/monitor/tasks/TaskCheckForProofs.js +4 -2
  11. package/out/src/monitor/tasks/TaskCheckForProofs.js.map +1 -1
  12. package/out/src/monitor/tasks/TaskNewHeader.js +1 -2
  13. package/out/src/monitor/tasks/TaskNewHeader.js.map +1 -1
  14. package/out/src/sdk/WalletServices.interfaces.d.ts +3 -0
  15. package/out/src/sdk/WalletServices.interfaces.d.ts.map +1 -1
  16. package/out/src/sdk/types.d.ts +12 -0
  17. package/out/src/sdk/types.d.ts.map +1 -1
  18. package/out/src/services/Services.d.ts.map +1 -1
  19. package/out/src/services/Services.js +7 -2
  20. package/out/src/services/Services.js.map +1 -1
  21. package/out/src/services/providers/ARC.d.ts.map +1 -1
  22. package/out/src/services/providers/ARC.js +83 -14
  23. package/out/src/services/providers/ARC.js.map +1 -1
  24. package/out/src/services/providers/WhatsOnChain.d.ts +1 -1
  25. package/out/src/services/providers/WhatsOnChain.d.ts.map +1 -1
  26. package/out/src/services/providers/WhatsOnChain.js +119 -32
  27. package/out/src/services/providers/WhatsOnChain.js.map +1 -1
  28. package/out/src/services/providers/__tests/WhatsOnChain.test.js +7 -4
  29. package/out/src/services/providers/__tests/WhatsOnChain.test.js.map +1 -1
  30. package/out/src/signer/methods/buildSignableTransaction.d.ts +10 -0
  31. package/out/src/signer/methods/buildSignableTransaction.d.ts.map +1 -0
  32. package/out/src/signer/methods/buildSignableTransaction.js +125 -0
  33. package/out/src/signer/methods/buildSignableTransaction.js.map +1 -0
  34. package/out/src/signer/methods/createAction.d.ts +5 -1
  35. package/out/src/signer/methods/createAction.d.ts.map +1 -1
  36. package/out/src/signer/methods/createAction.js +8 -121
  37. package/out/src/signer/methods/createAction.js.map +1 -1
  38. package/out/src/storage/WalletStorageManager.d.ts +1 -0
  39. package/out/src/storage/WalletStorageManager.d.ts.map +1 -1
  40. package/out/src/storage/WalletStorageManager.js +3 -0
  41. package/out/src/storage/WalletStorageManager.js.map +1 -1
  42. package/out/src/storage/methods/attemptToPostReqsToNetwork.d.ts.map +1 -1
  43. package/out/src/storage/methods/attemptToPostReqsToNetwork.js +18 -1
  44. package/out/src/storage/methods/attemptToPostReqsToNetwork.js.map +1 -1
  45. package/out/src/storage/methods/processAction.d.ts.map +1 -1
  46. package/out/src/storage/methods/processAction.js +10 -0
  47. package/out/src/storage/methods/processAction.js.map +1 -1
  48. package/out/src/storage/schema/entities/ProvenTx.d.ts.map +1 -1
  49. package/out/src/storage/schema/entities/ProvenTx.js +9 -20
  50. package/out/src/storage/schema/entities/ProvenTx.js.map +1 -1
  51. package/out/src/storage/schema/entities/ProvenTxReq.d.ts +4 -9
  52. package/out/src/storage/schema/entities/ProvenTxReq.d.ts.map +1 -1
  53. package/out/src/storage/schema/entities/ProvenTxReq.js +8 -5
  54. package/out/src/storage/schema/entities/ProvenTxReq.js.map +1 -1
  55. package/out/test/Wallet/local/localWallet.man.test.d.ts +2 -0
  56. package/out/test/Wallet/local/localWallet.man.test.d.ts.map +1 -0
  57. package/out/test/Wallet/local/localWallet.man.test.js +169 -0
  58. package/out/test/Wallet/local/localWallet.man.test.js.map +1 -0
  59. package/out/test/utils/TestUtilsWalletStorage.d.ts +5 -0
  60. package/out/test/utils/TestUtilsWalletStorage.d.ts.map +1 -1
  61. package/out/test/utils/TestUtilsWalletStorage.js +29 -18
  62. package/out/test/utils/TestUtilsWalletStorage.js.map +1 -1
  63. package/out/tsconfig.all.tsbuildinfo +1 -1
  64. package/package.json +1 -1
  65. package/src/Wallet.ts +7 -9
  66. package/src/monitor/tasks/TaskCheckForProofs.ts +4 -2
  67. package/src/monitor/tasks/TaskNewHeader.ts +1 -1
  68. package/src/sdk/WalletServices.interfaces.ts +6 -0
  69. package/src/sdk/types.ts +11 -0
  70. package/src/services/Services.ts +5 -2
  71. package/src/services/providers/ARC.ts +84 -15
  72. package/src/services/providers/WhatsOnChain.ts +129 -41
  73. package/src/services/providers/__tests/WhatsOnChain.test.ts +11 -4
  74. package/src/signer/methods/buildSignableTransaction.ts +160 -0
  75. package/src/signer/methods/createAction.ts +8 -161
  76. package/src/storage/WalletStorageManager.ts +4 -0
  77. package/src/storage/methods/attemptToPostReqsToNetwork.ts +17 -1
  78. package/src/storage/methods/processAction.ts +14 -0
  79. package/src/storage/schema/entities/ProvenTx.ts +10 -30
  80. package/src/storage/schema/entities/ProvenTxReq.ts +15 -17
  81. package/test/Wallet/local/localWallet.man.test.ts +225 -0
  82. package/test/utils/TestUtilsWalletStorage.ts +43 -19
  83. package/out/test/Wallet/local/localWallet.test.d.ts +0 -2
  84. package/out/test/Wallet/local/localWallet.test.d.ts.map +0 -1
  85. package/out/test/Wallet/local/localWallet.test.js +0 -14
  86. package/out/test/Wallet/local/localWallet.test.js.map +0 -1
  87. package/test/Wallet/local/localWallet.test.ts +0 -15
@@ -0,0 +1,225 @@
1
+ import {
2
+ Beef,
3
+ CreateActionArgs,
4
+ CreateActionOptions,
5
+ CreateActionResult,
6
+ P2PKH,
7
+ PrivateKey,
8
+ PublicKey,
9
+ Script,
10
+ SignActionArgs
11
+ } from '@bsv/sdk'
12
+ import { EntityProvenTxReq, Monitor, sdk, Setup, wait } from '../../../src'
13
+ import { _tu, TestWalletNoSetup } from '../../utils/TestUtilsWalletStorage'
14
+ import { monitorEventLoopDelay } from 'perf_hooks'
15
+
16
+ describe('localWallet tests', () => {
17
+ jest.setTimeout(99999999)
18
+
19
+ test('00', () => {})
20
+ if (_tu.noTestEnv('test')) return
21
+ if (_tu.noTestEnv('main')) return
22
+
23
+ test('0 create 1 sat delayed', async () => {
24
+ const setup = await createSetup('test')
25
+
26
+ const car = await createOneSatTestOutput(setup, {}, 1)
27
+
28
+ await trackReqByTxid(setup, car.txid!)
29
+
30
+ await setup.wallet.destroy()
31
+ })
32
+
33
+ test('0a create 1 sat immediate', async () => {
34
+ const setup = await createSetup('test')
35
+
36
+ const car = await createOneSatTestOutput(
37
+ setup,
38
+ { acceptDelayedBroadcast: false },
39
+ 1
40
+ )
41
+
42
+ await trackReqByTxid(setup, car.txid!)
43
+
44
+ await setup.wallet.destroy()
45
+ })
46
+
47
+ test('1 recover 1 sat outputs', async () => {
48
+ const setup = await createSetup('test')
49
+
50
+ await recoverOneSatTestOutputs(setup)
51
+
52
+ await setup.wallet.destroy()
53
+ })
54
+
55
+ test('2 monitor runOnce', async () => {
56
+ const setup = await createSetup('test')
57
+
58
+ await setup.monitor.runOnce()
59
+
60
+ await setup.wallet.destroy()
61
+ })
62
+
63
+ test('3 return active to cloud client', async () => {
64
+ const setup = await createSetup('test')
65
+
66
+ const localBalance = await setup.wallet.balance()
67
+
68
+ const log = await setup.storage.setActive(setup.clientStorageIdentityKey!)
69
+ console.log(log)
70
+
71
+ console.log(`ACTIVE STORAGE: ${setup.storage.getActiveStoreName()}`)
72
+
73
+ const clientBalance = await setup.wallet.balance()
74
+
75
+ expect(localBalance.total).toBe(clientBalance.total)
76
+
77
+ await setup.wallet.destroy()
78
+ })
79
+ })
80
+
81
+ async function createSetup(chain: sdk.Chain): Promise<TestWalletNoSetup> {
82
+ const env = _tu.getEnv(chain)
83
+ if (!env.testIdentityKey)
84
+ throw new sdk.WERR_INVALID_PARAMETER('env.testIdentityKey', 'valid')
85
+ if (!env.testFilePath)
86
+ throw new sdk.WERR_INVALID_PARAMETER('env.testFilePath', 'valid')
87
+
88
+ const setup = await _tu.createTestWallet({
89
+ chain,
90
+ rootKeyHex: env.devKeys[env.testIdentityKey],
91
+ filePath: env.testFilePath,
92
+ setActiveClient: false,
93
+ addLocalBackup: false
94
+ })
95
+
96
+ console.log(`ACTIVE STORAGE: ${setup.storage.getActiveStoreName()}`)
97
+
98
+ return setup
99
+ }
100
+
101
+ async function createOneSatTestOutput(
102
+ setup: TestWalletNoSetup,
103
+ options: CreateActionOptions = {},
104
+ howMany: number = 1
105
+ ): Promise<CreateActionResult> {
106
+ let car: CreateActionResult = {}
107
+
108
+ for (let i = 0; i < howMany; i++) {
109
+ const args: CreateActionArgs = {
110
+ outputs: [
111
+ {
112
+ lockingScript: new P2PKH()
113
+ .lock(PublicKey.fromString(setup.identityKey).toAddress())
114
+ .toHex(),
115
+ satoshis: 1,
116
+ outputDescription: 'test output',
117
+ customInstructions: JSON.stringify({
118
+ type: 'P2PKH',
119
+ key: 'identity'
120
+ }),
121
+ basket: 'test-output'
122
+ }
123
+ ],
124
+ description: 'create test output',
125
+ options: {
126
+ ...options
127
+ }
128
+ }
129
+ car = await setup.wallet.createAction(args)
130
+ expect(car.txid)
131
+
132
+ const req = await EntityProvenTxReq.fromStorageTxid(
133
+ setup.activeStorage,
134
+ car.txid!
135
+ )
136
+ expect(req !== undefined && req.history.notes !== undefined)
137
+ if (req && req.history.notes) {
138
+ expect(req.status === 'unsent')
139
+ expect(req.history.notes.length === 1)
140
+ const n = req.history.notes[0]
141
+ expect(n.what === 'status' && n.status_now === 'unsent')
142
+ }
143
+ }
144
+ return car
145
+ }
146
+
147
+ async function recoverOneSatTestOutputs(
148
+ setup: TestWalletNoSetup
149
+ ): Promise<void> {
150
+ const outputs = await setup.wallet.listOutputs({
151
+ basket: 'test-output',
152
+ include: 'entire transactions',
153
+ limit: 1000
154
+ })
155
+
156
+ if (outputs.outputs.length > 8) {
157
+ const args: CreateActionArgs = {
158
+ inputBEEF: outputs.BEEF!,
159
+ inputs: [],
160
+ description: 'recover test output'
161
+ }
162
+ const p2pkh = new P2PKH()
163
+ for (const o of outputs.outputs) {
164
+ args.inputs!.push({
165
+ unlockingScriptLength: 108,
166
+ outpoint: o.outpoint,
167
+ inputDescription: 'recovered test output'
168
+ })
169
+ }
170
+ const car = await setup.wallet.createAction(args)
171
+ expect(car.signableTransaction)
172
+
173
+ const st = car.signableTransaction!
174
+ const beef = Beef.fromBinary(st.tx)
175
+ const tx = beef.findAtomicTransaction(beef.txs.slice(-1)[0].txid)!
176
+ const signArgs: SignActionArgs = {
177
+ reference: st.reference,
178
+ spends: {} // 0: { unlockingScript } },
179
+ }
180
+ for (let i = 0; i < outputs.outputs.length; i++) {
181
+ const o = outputs.outputs[i]
182
+ const unlock = p2pkh.unlock(setup.keyDeriver.rootKey, 'all', false)
183
+ const unlockingScript = (await unlock.sign(tx, i)).toHex()
184
+ signArgs.spends[i] = { unlockingScript }
185
+ }
186
+ const sar = await setup.wallet.signAction(signArgs)
187
+ expect(sar.txid)
188
+ }
189
+ }
190
+
191
+ async function trackReqByTxid(
192
+ setup: TestWalletNoSetup,
193
+ txid: string
194
+ ): Promise<void> {
195
+ const req = await EntityProvenTxReq.fromStorageTxid(setup.activeStorage, txid)
196
+
197
+ expect(req !== undefined && req.history.notes !== undefined)
198
+ if (!req || !req.history.notes) throw new sdk.WERR_INTERNAL()
199
+
200
+ let newBlocks = 0
201
+ let lastHeight: number | undefined
202
+ for (; req.status !== 'completed'; ) {
203
+ let height = setup.monitor.lastNewHeader?.height
204
+ if (req.status === 'unsent') {
205
+ // send it...
206
+ }
207
+ if (req.status === 'sending') {
208
+ // send it...
209
+ }
210
+ if (req.status === 'unmined') {
211
+ if (height && lastHeight) {
212
+ if (height === lastHeight) {
213
+ await wait(1000 * 60)
214
+ } else {
215
+ newBlocks++
216
+ expect(newBlocks < 5)
217
+ }
218
+ }
219
+ }
220
+
221
+ await setup.monitor.runOnce()
222
+ await req.refreshFromStorage(setup.activeStorage)
223
+ lastHeight = height
224
+ }
225
+ }
@@ -344,6 +344,8 @@ export abstract class TestUtilsWalletStorage {
344
344
  let chain: sdk.Chain
345
345
  let rootKeyHex: string
346
346
  let filePath: string
347
+ let addLocalBackup = false
348
+ let setActiveClient = false
347
349
  if (typeof args === 'string') {
348
350
  chain = args
349
351
  const env = _tu.getEnv(chain)
@@ -359,6 +361,8 @@ export abstract class TestUtilsWalletStorage {
359
361
  chain = args.chain
360
362
  rootKeyHex = args.rootKeyHex
361
363
  filePath = args.filePath
364
+ addLocalBackup = args.addLocalBackup === true
365
+ setActiveClient = args.setActiveClient === true
362
366
  }
363
367
 
364
368
  const databaseName = path.parse(filePath).name
@@ -368,7 +372,7 @@ export abstract class TestUtilsWalletStorage {
368
372
  databaseName,
369
373
  chain
370
374
  })
371
- const localStorageIdentityKey = setup.storage.getActiveStore()
375
+ setup.localStorageIdentityKey = setup.storage.getActiveStore()
372
376
 
373
377
  const endpointUrl =
374
378
  chain === 'main'
@@ -376,29 +380,39 @@ export abstract class TestUtilsWalletStorage {
376
380
  : 'https://staging-storage.babbage.systems'
377
381
 
378
382
  const client = new StorageClient(setup.wallet, endpointUrl)
379
- const clientStorageIdentityKey = (await client.makeAvailable())
380
- .storageIdentityKey
383
+ setup.clientStorageIdentityKey = (
384
+ await client.makeAvailable()
385
+ ).storageIdentityKey
381
386
  await setup.wallet.storage.addWalletStorageProvider(client)
382
387
 
383
- const backupName = `${databaseName}_backup`
384
- const backupPath = filePath.replace(databaseName, backupName)
385
- const localBackup = new StorageKnex({
386
- ...StorageKnex.defaultOptions(),
387
- knex: _tu.createLocalSQLite(backupPath),
388
- chain
389
- })
390
- await localBackup.migrate(backupName, randomBytesHex(33))
391
- const backupSettings = await localBackup.makeAvailable()
392
- await setup.wallet.storage.addWalletStorageProvider(localBackup)
388
+ if (addLocalBackup) {
389
+ const backupName = `${databaseName}_backup`
390
+ const backupPath = filePath.replace(databaseName, backupName)
391
+ const localBackup = new StorageKnex({
392
+ ...StorageKnex.defaultOptions(),
393
+ knex: _tu.createLocalSQLite(backupPath),
394
+ chain
395
+ })
396
+ await localBackup.migrate(backupName, randomBytesHex(33))
397
+ setup.localBackupStorageIdentityKey = (
398
+ await localBackup.makeAvailable()
399
+ ).storageIdentityKey
400
+ await setup.wallet.storage.addWalletStorageProvider(localBackup)
401
+ }
393
402
 
394
403
  // SETTING ACTIVE
395
404
  // SETTING ACTIVE
396
405
  // SETTING ACTIVE
397
- const log = await setup.storage.setActive(localStorageIdentityKey)
398
- //const log = await setup.storage.setActive(clientStorageIdentityKey)
406
+ const log = await setup.storage.setActive(
407
+ setActiveClient
408
+ ? setup.clientStorageIdentityKey
409
+ : setup.localStorageIdentityKey
410
+ )
399
411
  console.log(log)
400
412
 
401
- if (setup.storage.getActiveStore() === localStorageIdentityKey) {
413
+ let needsBackup = false
414
+
415
+ if (setup.storage.getActiveStore() === setup.localStorageIdentityKey) {
402
416
  const basket = verifyOne(
403
417
  await setup.activeStorage.findOutputBaskets({
404
418
  partial: {
@@ -411,6 +425,7 @@ export abstract class TestUtilsWalletStorage {
411
425
  basket.minimumDesiredUTXOValue !== 5 ||
412
426
  basket.numberOfDesiredUTXOs < 32
413
427
  ) {
428
+ needsBackup = true
414
429
  await setup.activeStorage.updateOutputBasket(basket.basketId, {
415
430
  minimumDesiredUTXOValue: 5,
416
431
  numberOfDesiredUTXOs: 32
@@ -418,19 +433,23 @@ export abstract class TestUtilsWalletStorage {
418
433
  }
419
434
  }
420
435
 
421
- const log2 = await setup.storage.updateBackups()
422
- console.log(log2)
423
-
424
436
  const balance = await setup.wallet.balance()
425
437
 
426
438
  if (balance.total < 1000) {
427
439
  throw new sdk.WERR_INSUFFICIENT_FUNDS(1000, 1000 - balance.total)
428
440
  }
441
+
429
442
  if (balance.utxos.length <= 10) {
430
443
  const args: CreateActionArgs = {
431
444
  description: 'spread change'
432
445
  }
433
446
  await setup.wallet.createAction(args)
447
+ needsBackup = true
448
+ }
449
+
450
+ if (needsBackup) {
451
+ const log2 = await setup.storage.updateBackups()
452
+ console.log(log2)
434
453
  }
435
454
 
436
455
  return setup
@@ -1776,6 +1795,9 @@ export interface TestWallet<T> extends TestWalletOnly {
1776
1795
  services: Services
1777
1796
  monitor: Monitor
1778
1797
  wallet: Wallet
1798
+ localStorageIdentityKey?: string
1799
+ clientStorageIdentityKey?: string
1800
+ localBackupStorageIdentityKey?: string
1779
1801
  }
1780
1802
 
1781
1803
  export interface TestWalletOnly {
@@ -2485,4 +2507,6 @@ export interface CreateTestWalletArgs {
2485
2507
  chain: sdk.Chain
2486
2508
  rootKeyHex: string
2487
2509
  filePath: string
2510
+ addLocalBackup?: boolean
2511
+ setActiveClient?: boolean
2488
2512
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=localWallet.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"localWallet.test.d.ts","sourceRoot":"","sources":["../../../../test/Wallet/local/localWallet.test.ts"],"names":[],"mappings":""}
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const TestUtilsWalletStorage_1 = require("../../utils/TestUtilsWalletStorage");
4
- describe('localWallet tests', () => {
5
- jest.setTimeout(99999999);
6
- test('0', async () => {
7
- const chain = 'test';
8
- if (TestUtilsWalletStorage_1._tu.noTestEnv(chain))
9
- return;
10
- const setup = await TestUtilsWalletStorage_1._tu.createTestWallet(chain);
11
- await setup.wallet.destroy();
12
- });
13
- });
14
- //# sourceMappingURL=localWallet.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"localWallet.test.js","sourceRoot":"","sources":["../../../../test/Wallet/local/localWallet.test.ts"],"names":[],"mappings":";;AACA,+EAAwD;AAExD,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;IAEzB,IAAI,CAAC,GAAG,EAAE,KAAK,IAAI,EAAE;QACnB,MAAM,KAAK,GAAc,MAAM,CAAA;QAC/B,IAAI,4BAAG,CAAC,SAAS,CAAC,KAAK,CAAC;YAAE,OAAM;QAEhC,MAAM,KAAK,GAAG,MAAM,4BAAG,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAE/C,MAAM,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,CAAA;IAC9B,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -1,15 +0,0 @@
1
- import { sdk } from '../../../src'
2
- import { _tu } from '../../utils/TestUtilsWalletStorage'
3
-
4
- describe('localWallet tests', () => {
5
- jest.setTimeout(99999999)
6
-
7
- test('0', async () => {
8
- const chain: sdk.Chain = 'test'
9
- if (_tu.noTestEnv(chain)) return
10
-
11
- const setup = await _tu.createTestWallet(chain)
12
-
13
- await setup.wallet.destroy()
14
- })
15
- })