@bsv/wallet-toolbox 1.3.11 → 1.3.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/wallet-toolbox",
3
- "version": "1.3.11",
3
+ "version": "1.3.13",
4
4
  "description": "BRC100 conforming wallet, wallet storage and wallet signer components",
5
5
  "main": "./out/src/index.js",
6
6
  "types": "./out/src/index.d.ts",
@@ -564,8 +564,10 @@ export class WalletStorageManager implements sdk.WalletStorage {
564
564
  auth: sdk.AuthId,
565
565
  writer: sdk.WalletStorageProvider,
566
566
  activeSync?: sdk.WalletStorageSync,
567
- log: string = ''
567
+ log: string = '',
568
+ progLog?: (s: string) => string
568
569
  ): Promise<{ inserts: number; updates: number; log: string }> {
570
+ progLog ||= s => s
569
571
  const identityKey = auth.identityKey
570
572
 
571
573
  const writerSettings = await writer.makeAvailable()
@@ -577,7 +579,7 @@ export class WalletStorageManager implements sdk.WalletStorage {
577
579
  const reader = sync
578
580
  const readerSettings = reader.getSettings()
579
581
 
580
- log += `syncToWriter from ${readerSettings.storageName} to ${writerSettings.storageName}\n`
582
+ log += progLog(`syncToWriter from ${readerSettings.storageName} to ${writerSettings.storageName}\n`)
581
583
 
582
584
  let i = -1
583
585
  for (;;) {
@@ -589,22 +591,23 @@ export class WalletStorageManager implements sdk.WalletStorage {
589
591
  const r = await writer.processSyncChunk(args, chunk)
590
592
  inserts += r.inserts
591
593
  updates += r.updates
592
- log += `chunk ${i} inserted ${r.inserts} updated ${r.updates} ${r.maxUpdated_at}\n`
594
+ log += progLog(`chunk ${i} inserted ${r.inserts} updated ${r.updates} ${r.maxUpdated_at}\n`)
593
595
  if (r.done) break
594
596
  }
595
- log += `syncToWriter complete: ${inserts} inserts, ${updates} updates\n`
597
+ log += progLog(`syncToWriter complete: ${inserts} inserts, ${updates} updates\n`)
596
598
  return log
597
599
  }, activeSync)
598
600
 
599
601
  return { inserts, updates, log }
600
602
  }
601
603
 
602
- async updateBackups(activeSync?: sdk.WalletStorageSync): Promise<string> {
604
+ async updateBackups(activeSync?: sdk.WalletStorageSync, progLog?: (s: string) => string): Promise<string> {
605
+ progLog ||= s => s
603
606
  const auth = await this.getAuth(true)
604
607
  return await this.runAsSync(async sync => {
605
- let log = `BACKUP CURRENT ACTIVE TO ${this._backups!.length} STORES\n`
608
+ let log = progLog(`BACKUP CURRENT ACTIVE TO ${this._backups!.length} STORES\n`)
606
609
  for (const backup of this._backups!) {
607
- const stwr = await this.syncToWriter(auth, backup.storage, sync)
610
+ const stwr = await this.syncToWriter(auth, backup.storage, sync, undefined, progLog)
608
611
  log += stwr.log
609
612
  }
610
613
  return log
@@ -618,7 +621,8 @@ export class WalletStorageManager implements sdk.WalletStorage {
618
621
  *
619
622
  * @param storageIdentityKey of current backup storage provider that is to become the new active provider.
620
623
  */
621
- async setActive(storageIdentityKey: string): Promise<string> {
624
+ async setActive(storageIdentityKey: string, progLog?: (s: string) => string): Promise<string> {
625
+ progLog ||= s => s
622
626
  if (!this.isAvailable()) await this.makeAvailable()
623
627
 
624
628
  // Confirm a valid storageIdentityKey: must match one of the _stores.
@@ -632,13 +636,13 @@ export class WalletStorageManager implements sdk.WalletStorage {
632
636
  const identityKey = (await this.getAuth()).identityKey
633
637
  const newActive = this._stores[newActiveIndex]
634
638
 
635
- let log = `setActive to ${newActive.settings!.storageName}`
639
+ let log = progLog(`setActive to ${newActive.settings!.storageName}`)
636
640
 
637
641
  if (storageIdentityKey === this.getActiveStore() && this.isActiveEnabled)
638
642
  /** Setting the current active as the new active is a permitted no-op. */
639
- return log + ` unchanged\n`
643
+ return log + progLog(` unchanged\n`)
640
644
 
641
- log += '\n'
645
+ log += progLog('\n')
642
646
 
643
647
  log += await this.runAsSync(async sync => {
644
648
  let log = ''
@@ -658,17 +662,19 @@ export class WalletStorageManager implements sdk.WalletStorage {
658
662
 
659
663
  // Merge state from conflicting actives into `newActive`.
660
664
  for (const conflict of this._conflictingActives) {
661
- log += 'MERGING STATE FROM CONFLICTING ACTIVES:\n'
665
+ log += progLog('MERGING STATE FROM CONFLICTING ACTIVES:\n')
662
666
  const sfr = await this.syncToWriter(
663
667
  { identityKey, userId: newActive.user!.userId, isActive: false },
664
668
  newActive.storage,
665
- conflict.storage
669
+ conflict.storage,
670
+ undefined,
671
+ progLog
666
672
  )
667
673
  log += sfr.log
668
674
  }
669
- log += 'PROPAGATE MERGED ACTIVE STATE TO NON-ACTIVES\n'
675
+ log += progLog('PROPAGATE MERGED ACTIVE STATE TO NON-ACTIVES\n')
670
676
  } else {
671
- log += 'BACKUP CURRENT ACTIVE STATE THEN SET NEW ACTIVE\n'
677
+ log += progLog('BACKUP CURRENT ACTIVE STATE THEN SET NEW ACTIVE\n')
672
678
  }
673
679
 
674
680
  // If there were conflicting actives,
@@ -690,7 +696,9 @@ export class WalletStorageManager implements sdk.WalletStorage {
690
696
  const stwr = await this.syncToWriter(
691
697
  { identityKey, userId: store.user!.userId, isActive: false },
692
698
  store.storage,
693
- backupSource.storage
699
+ backupSource.storage,
700
+ undefined,
701
+ progLog
694
702
  )
695
703
  log += stwr.log
696
704
  }
@@ -1,3 +1,6 @@
1
+ import { PrivateKey } from '@bsv/sdk'
2
+ import { Setup } from '../../Setup'
3
+ import { SetupClient } from '../../SetupClient'
1
4
  import { StorageIdb } from '../StorageIdb'
2
5
  import { StorageProvider, StorageProviderOptions } from '../StorageProvider'
3
6
  import 'fake-indexeddb/auto'
@@ -12,4 +15,21 @@ describe('StorageIdb tests', () => {
12
15
  const db = storage.db!
13
16
  expect(db).toBeTruthy()
14
17
  })
18
+
19
+ test('1', async () => {
20
+ if (Setup.noEnv('test')) return
21
+ const env = Setup.getEnv('test')
22
+ const wallet = await SetupClient.createWalletClientNoEnv({
23
+ chain: env.chain,
24
+ rootKeyHex: env.devKeys[env.identityKey]
25
+ })
26
+ const stores = wallet.storage.getStores()
27
+ const options = StorageIdb.createStorageBaseOptions(wallet.chain)
28
+ const store = new StorageIdb(options)
29
+ await store.migrate(store.dbName, PrivateKey.fromRandom().toHex())
30
+ await store.makeAvailable()
31
+ await wallet.storage.addWalletStorageProvider(store)
32
+ await wallet.storage.setActive(stores[0].storageIdentityKey, (s) => { console.log(s); return s })
33
+ await wallet.storage.updateBackups(undefined, (s) => { console.log(s); return s })
34
+ })
15
35
  })