mainnet-js 2.6.1 → 2.6.3

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 (54) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.6.1.js → mainnet-2.6.3.js} +34 -4
  3. package/dist/module/cache/IndexedDbCache.d.ts +14 -0
  4. package/dist/module/cache/IndexedDbCache.d.ts.map +1 -0
  5. package/dist/module/cache/IndexedDbCache.js +86 -0
  6. package/dist/module/cache/IndexedDbCache.js.map +1 -0
  7. package/dist/module/cache/KeyValueCache.d.ts +9 -0
  8. package/dist/module/cache/KeyValueCache.d.ts.map +1 -0
  9. package/dist/module/cache/KeyValueCache.js +3 -0
  10. package/dist/module/cache/KeyValueCache.js.map +1 -0
  11. package/dist/module/cache/MemoryCache.d.ts +9 -0
  12. package/dist/module/cache/MemoryCache.d.ts.map +1 -0
  13. package/dist/module/cache/MemoryCache.js +21 -0
  14. package/dist/module/cache/MemoryCache.js.map +1 -0
  15. package/dist/module/cache/WebStorageCache.d.ts +9 -0
  16. package/dist/module/cache/WebStorageCache.d.ts.map +1 -0
  17. package/dist/module/cache/WebStorageCache.js +19 -0
  18. package/dist/module/cache/WebStorageCache.js.map +1 -0
  19. package/dist/module/cache/index.d.ts +2 -0
  20. package/dist/module/cache/index.d.ts.map +1 -0
  21. package/dist/module/cache/index.js +2 -0
  22. package/dist/module/cache/index.js.map +1 -0
  23. package/dist/module/cache/interface.d.ts +8 -0
  24. package/dist/module/cache/interface.d.ts.map +1 -0
  25. package/dist/module/cache/interface.js +2 -0
  26. package/dist/module/cache/interface.js.map +1 -0
  27. package/dist/module/config.d.ts +2 -0
  28. package/dist/module/config.d.ts.map +1 -1
  29. package/dist/module/config.js +4 -0
  30. package/dist/module/config.js.map +1 -1
  31. package/dist/module/history/electrumTransformer.d.ts.map +1 -1
  32. package/dist/module/history/electrumTransformer.js +2 -2
  33. package/dist/module/history/electrumTransformer.js.map +1 -1
  34. package/dist/module/network/ElectrumNetworkProvider.d.ts +3 -3
  35. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  36. package/dist/module/network/ElectrumNetworkProvider.js +34 -23
  37. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  38. package/dist/module/network/constant.js +1 -1
  39. package/dist/tsconfig.tsbuildinfo +1 -1
  40. package/package.json +1 -1
  41. package/src/cache/IndexedDbCache.test.ts +15 -0
  42. package/src/cache/IndexedDbCache.ts +127 -0
  43. package/src/cache/KeyValueCache.ts +9 -0
  44. package/src/cache/MemoryCache.test.ts +15 -0
  45. package/src/cache/MemoryCache.ts +18 -0
  46. package/src/cache/WebStorageCache.test.ts +15 -0
  47. package/src/cache/WebStorageCache.ts +24 -0
  48. package/src/cache/index.ts +1 -0
  49. package/src/cache/interface.ts +7 -0
  50. package/src/config.ts +4 -0
  51. package/src/history/electrumTransformer.ts +3 -1
  52. package/src/network/ElectrumNetworkProvider.ts +46 -19
  53. package/src/network/constant.ts +1 -1
  54. package/src/wallet/Wif.test.ts +0 -12
@@ -0,0 +1,18 @@
1
+ export class MemoryCache {
2
+ public cache: Record<string, string> = {};
3
+ async init() {
4
+ return;
5
+ }
6
+ async setItem(key: string, value: string) {
7
+ this.cache[key] = value;
8
+ }
9
+ async getItem(key: string) {
10
+ return this.cache[key] ?? null;
11
+ }
12
+ async removeItem(key: string) {
13
+ delete this.cache[key];
14
+ }
15
+ async clear() {
16
+ this.cache = {};
17
+ }
18
+ }
@@ -0,0 +1,15 @@
1
+ import { WebStorageCache } from "./WebStorageCache";
2
+
3
+ describe("WebStorageCache Tests", () => {
4
+ test("test", async () => {
5
+ const cache = new WebStorageCache();
6
+ await cache.init();
7
+ await cache.setItem("key", "value");
8
+ const value = await cache.getItem("key");
9
+ expect(value).toBe("value");
10
+
11
+ await cache.removeItem("key");
12
+ const value2 = await cache.getItem("key");
13
+ expect(value2).toBeNull();
14
+ });
15
+ });
@@ -0,0 +1,24 @@
1
+ import { CacheProvider } from "./interface";
2
+
3
+ // super thin wrapper around localStorage
4
+ export class WebStorageCache implements CacheProvider {
5
+ async init() {
6
+ return;
7
+ }
8
+
9
+ async setItem(key: string, value: string): Promise<void> {
10
+ localStorage.setItem(key, value);
11
+ }
12
+
13
+ async getItem(key: string): Promise<string | null> {
14
+ return localStorage.getItem(key);
15
+ }
16
+
17
+ async removeItem(key: string): Promise<void> {
18
+ localStorage.removeItem(key);
19
+ }
20
+
21
+ async clear(): Promise<void> {
22
+ localStorage.clear();
23
+ }
24
+ }
@@ -0,0 +1 @@
1
+ export * from "./interface.js";
@@ -0,0 +1,7 @@
1
+ export interface CacheProvider {
2
+ init(): Promise<void>;
3
+ setItem(key: string, value: string): Promise<void>;
4
+ getItem(key: string): Promise<string | null>;
5
+ removeItem(key: string): Promise<void>;
6
+ clear(): Promise<void>;
7
+ }
package/src/config.ts CHANGED
@@ -11,6 +11,10 @@ export class Config {
11
11
  static DefaultCurrency = "usd";
12
12
  // caches the raw transactions in browser's local storage instead of memory
13
13
  static UseLocalStorageCache = false;
14
+ // caches the raw transactions in browser's indexedDB instead of memory
15
+ static UseIndexedDBCache = false;
16
+ // caches the raw transactions in browser's memory
17
+ static UseMemoryCache = false;
14
18
  private static DefaultWordlist = english;
15
19
 
16
20
  public static setIpfsGateway(ipfsGateway: string) {
@@ -7,6 +7,8 @@ import {
7
7
  decodeCashAddress,
8
8
  TransactionCommon,
9
9
  assertSuccess,
10
+ OpcodesBCH,
11
+ Opcodes,
10
12
  } from "@bitauth/libauth";
11
13
  import { UnitEnum } from "../enum.js";
12
14
  import NetworkProvider from "../network/NetworkProvider.js";
@@ -174,7 +176,7 @@ export const getAddressHistory = async ({
174
176
  const cached = addressCache[output.lockingBytecode as any];
175
177
  let address: string;
176
178
  if (!cached) {
177
- if (output.valueSatoshis === 0n) {
179
+ if (output.lockingBytecode[0] === Opcodes.OP_RETURN) {
178
180
  address = `OP_RETURN: ${binToHex(output.lockingBytecode)}`;
179
181
  } else {
180
182
  address = assertSuccess(
@@ -20,13 +20,49 @@ import { CancelWatchFn } from "../wallet/interface.js";
20
20
  import { getTransactionHash } from "../util/transaction.js";
21
21
  import { Config } from "../config.js";
22
22
  import { decodeHeader } from "../util/header.js";
23
+ import { CacheProvider } from "../cache/interface.js";
24
+ import { IndexedDbCache } from "../cache/IndexedDbCache.js";
25
+ import { WebStorageCache } from "../cache/WebStorageCache.js";
26
+ import { MemoryCache } from "../cache/MemoryCache.js";
23
27
 
24
28
  export default class ElectrumNetworkProvider implements NetworkProvider {
25
29
  public electrum: ElectrumCluster | ElectrumClient;
26
30
  public subscriptions: number = 0;
27
31
  public version;
28
32
  private connectPromise;
29
- private blockHeight = 0;
33
+
34
+ private _cache: CacheProvider | undefined;
35
+
36
+ get cache(): CacheProvider | undefined {
37
+ if (
38
+ !Config.UseMemoryCache &&
39
+ !Config.UseLocalStorageCache &&
40
+ !Config.UseIndexedDBCache
41
+ ) {
42
+ this._cache = undefined;
43
+ return this._cache;
44
+ }
45
+
46
+ if (Config.UseMemoryCache && !(this._cache instanceof MemoryCache)) {
47
+ this._cache = new IndexedDbCache();
48
+ return this._cache;
49
+ }
50
+
51
+ if (
52
+ Config.UseLocalStorageCache &&
53
+ !(this._cache instanceof WebStorageCache)
54
+ ) {
55
+ this._cache = new WebStorageCache();
56
+ return this._cache;
57
+ }
58
+
59
+ if (Config.UseIndexedDBCache && !(this._cache instanceof IndexedDbCache)) {
60
+ this._cache = new IndexedDbCache();
61
+ return this._cache;
62
+ }
63
+
64
+ return this._cache;
65
+ }
30
66
 
31
67
  constructor(
32
68
  electrum: ElectrumCluster | ElectrumClient,
@@ -112,30 +148,25 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
112
148
  return result.confirmed + result.unconfirmed;
113
149
  }
114
150
 
115
- static rawHeaderCache = {};
116
151
  async getHeader(
117
152
  height: number,
118
153
  verbose: boolean = false
119
154
  ): Promise<HeaderI | HexHeaderI> {
120
155
  const key = `header-${this.network}-${height}-${verbose}`;
121
156
 
122
- if (Config.UseLocalStorageCache) {
123
- const cached = localStorage.getItem(key);
157
+ if (this.cache) {
158
+ const cached = await this.cache.getItem(key);
124
159
  if (cached) {
125
160
  return verbose ? decodeHeader(JSON.parse(cached)) : JSON.parse(cached);
126
161
  }
127
- } else {
128
- ElectrumNetworkProvider.rawTransactionCache[key];
129
162
  }
130
163
 
131
164
  const result = (await this.performRequest(
132
165
  "blockchain.header.get",
133
166
  height
134
167
  )) as HexHeaderI;
135
- if (Config.UseLocalStorageCache) {
136
- localStorage.setItem(key, JSON.stringify(result));
137
- } else {
138
- ElectrumNetworkProvider.rawTransactionCache[key] = result;
168
+ if (this.cache) {
169
+ await this.cache.setItem(key, JSON.stringify(result));
139
170
  }
140
171
 
141
172
  return verbose ? decodeHeader(result) : result;
@@ -146,7 +177,6 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
146
177
  .height;
147
178
  }
148
179
 
149
- static rawTransactionCache = {};
150
180
  async getRawTransaction(
151
181
  txHash: string,
152
182
  verbose: boolean = false,
@@ -154,13 +184,11 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
154
184
  ): Promise<string> {
155
185
  const key = `tx-${this.network}-${txHash}-${verbose}-${loadInputValues}`;
156
186
 
157
- if (Config.UseLocalStorageCache) {
158
- const cached = localStorage.getItem(key);
187
+ if (this.cache) {
188
+ const cached = await this.cache.getItem(key);
159
189
  if (cached) {
160
190
  return verbose ? JSON.parse(cached) : cached;
161
191
  }
162
- } else {
163
- ElectrumNetworkProvider.rawTransactionCache[key];
164
192
  }
165
193
 
166
194
  try {
@@ -170,15 +198,13 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
170
198
  verbose
171
199
  )) as ElectrumRawTransaction;
172
200
 
173
- if (Config.UseLocalStorageCache) {
174
- localStorage.setItem(
201
+ if (this.cache) {
202
+ await this.cache.setItem(
175
203
  key,
176
204
  verbose
177
205
  ? JSON.stringify(transaction)
178
206
  : (transaction as unknown as string)
179
207
  );
180
- } else {
181
- ElectrumNetworkProvider.rawTransactionCache[key] = transaction;
182
208
  }
183
209
 
184
210
  if (verbose && loadInputValues) {
@@ -551,6 +577,7 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
551
577
  }
552
578
 
553
579
  async connect(): Promise<void[]> {
580
+ await this.cache?.init();
554
581
  return await this.connectPromise;
555
582
  }
556
583
 
@@ -33,7 +33,7 @@ export const mainnetServers = [
33
33
  export const testnetServers = [
34
34
  // "wss://chipnet.imaginary.cash:50004",
35
35
  //"wss://blackie.c3-soft.com:64004", // chipnet with protocol 1.5.0
36
- "wss://chipnet.bch.ninja:50004"
36
+ "wss://chipnet.bch.ninja:50004",
37
37
  ];
38
38
 
39
39
  export const regtestServers = ["ws://127.0.0.1:60003"];
@@ -543,18 +543,6 @@ describe(`Watch only Wallets`, () => {
543
543
 
544
544
  expect(await bobWallet.getLastTransaction()).not.toBeNull();
545
545
  });
546
-
547
- test("Should fail localStorage cache", async () => {
548
- const aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
549
- const aliceWallet = await RegTestWallet.fromId(aliceWif);
550
-
551
- expect(await aliceWallet.getLastTransaction()).not.toBeNull();
552
- Config.UseLocalStorageCache = true;
553
- await expect(aliceWallet.getLastTransaction()).rejects.toThrow(
554
- "localStorage is not defined"
555
- );
556
- Config.UseLocalStorageCache = false;
557
- });
558
546
  });
559
547
  describe(`Wallet subscriptions`, () => {
560
548
  test("Should wait for transaction", async () => {