mainnet-js 3.1.2 → 3.1.5

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.
@@ -16,7 +16,7 @@ import {
16
16
  sha256,
17
17
  utf8ToBin,
18
18
  } from "@bitauth/libauth";
19
- import { WalletCache, WalletCacheI } from "../cache/index.js";
19
+ import { PersistentWalletCache, WalletCacheI } from "../cache/index.js";
20
20
  import { Config } from "../config.js";
21
21
  import { NetworkType, prefixFromNetworkMap, UnitEnum } from "../enum.js";
22
22
  import { getHistory } from "../history/getHistory.js";
@@ -240,7 +240,7 @@ export class HDWallet extends BaseWallet {
240
240
  )
241
241
  );
242
242
  // @ts-ignore
243
- this.walletCache = new WalletCache(
243
+ this.walletCache = new PersistentWalletCache(
244
244
  this.walletId,
245
245
  this.xPrivNode ?? this.xPubNode,
246
246
  this.networkPrefix
@@ -0,0 +1,45 @@
1
+ import { RegTestWallet, RegTestWifWallet } from "./Wif";
2
+ import { RegTestWatchWallet } from "./Watch";
3
+ import { RegTestHDWallet } from "./HDWallet";
4
+
5
+ describe("Wallet cache initialization", () => {
6
+ test("Seed wallet should have walletCache with at least one entry", async () => {
7
+ const wallet = await RegTestWallet.newRandom();
8
+ expect(wallet.walletCache).toBeDefined();
9
+ expect(wallet.walletCache!.get(wallet.cashaddr)).toBeDefined();
10
+ expect(wallet.walletCache!.get(wallet.cashaddr)!.privateKey).toBeDefined();
11
+ });
12
+
13
+ test("Wif wallet should have walletCache with at least one entry", async () => {
14
+ const wallet = await RegTestWifWallet.newRandom();
15
+ expect(wallet.walletCache).toBeDefined();
16
+ expect(wallet.walletCache!.get(wallet.cashaddr)).toBeDefined();
17
+ expect(wallet.walletCache!.get(wallet.cashaddr)!.privateKey).toBeDefined();
18
+ });
19
+
20
+ test("Wif wallet from ID should have walletCache with at least one entry", async () => {
21
+ const wallet = await RegTestWallet.fromId(
22
+ "wif:regtest:cNfsPtqN2bMRS7vH5qd8tR8GMvgXyL5BjnGAKgZ8DYEiCrCCQcP6"
23
+ );
24
+ expect(wallet.walletCache).toBeDefined();
25
+ expect(wallet.walletCache!.get(wallet.cashaddr)).toBeDefined();
26
+ expect(wallet.walletCache!.get(wallet.cashaddr)!.privateKey).toBeDefined();
27
+ });
28
+
29
+ test("HD wallet should have walletCache with at least one entry", async () => {
30
+ const wallet = await RegTestHDWallet.newRandom();
31
+ expect(wallet.walletCache).toBeDefined();
32
+ const depositAddr = wallet.getDepositAddress();
33
+ expect(wallet.walletCache!.get(depositAddr)).toBeDefined();
34
+ expect(wallet.walletCache!.get(depositAddr)!.privateKey).toBeDefined();
35
+ await wallet.stop();
36
+ });
37
+
38
+ test("Watch wallet should have empty walletCache", async () => {
39
+ const wallet = await RegTestWatchWallet.watchOnly(
40
+ "bchreg:qpttdv3qg2usm4nm7talhxhl05mlhms3ys43u76rn0"
41
+ );
42
+ expect(wallet.walletCache).toBeDefined();
43
+ expect(wallet.walletCache!.get(wallet.cashaddr)).toBeUndefined();
44
+ });
45
+ });
@@ -1187,3 +1187,5 @@ describe(`Wallet extrema behavior regression testing`, () => {
1187
1187
  ).resolves.not.toThrow();
1188
1188
  });
1189
1189
  });
1190
+
1191
+
package/src/wallet/Wif.ts CHANGED
@@ -215,13 +215,20 @@ export class Wallet extends WatchWallet {
215
215
  }
216
216
 
217
217
  // rest cases are for watch wallets
218
- return super.initialize({
218
+ await super.initialize({
219
219
  name,
220
220
  publicKey,
221
221
  publicKeyCompressed,
222
222
  publicKeyHash,
223
223
  address,
224
224
  });
225
+
226
+ if (this.privateKey) {
227
+ // @ts-ignore
228
+ this.walletCache = new Map<string, { privateKey: Uint8Array }>().set(this.cashaddr, { privateKey: this.privateKey });
229
+ }
230
+
231
+ return this;
225
232
  }
226
233
  //#endregion Constructors and Statics
227
234