mainnet-js 2.6.2 → 2.6.4
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/dist/index.html +1 -1
- package/dist/{mainnet-2.6.2.js → mainnet-2.6.4.js} +32 -2
- package/dist/module/cache/IndexedDbCache.d.ts +14 -0
- package/dist/module/cache/IndexedDbCache.d.ts.map +1 -0
- package/dist/module/cache/IndexedDbCache.js +86 -0
- package/dist/module/cache/IndexedDbCache.js.map +1 -0
- package/dist/module/cache/KeyValueCache.d.ts +9 -0
- package/dist/module/cache/KeyValueCache.d.ts.map +1 -0
- package/dist/module/cache/KeyValueCache.js +3 -0
- package/dist/module/cache/KeyValueCache.js.map +1 -0
- package/dist/module/cache/MemoryCache.d.ts +9 -0
- package/dist/module/cache/MemoryCache.d.ts.map +1 -0
- package/dist/module/cache/MemoryCache.js +21 -0
- package/dist/module/cache/MemoryCache.js.map +1 -0
- package/dist/module/cache/WebStorageCache.d.ts +9 -0
- package/dist/module/cache/WebStorageCache.d.ts.map +1 -0
- package/dist/module/cache/WebStorageCache.js +19 -0
- package/dist/module/cache/WebStorageCache.js.map +1 -0
- package/dist/module/cache/index.d.ts +2 -0
- package/dist/module/cache/index.d.ts.map +1 -0
- package/dist/module/cache/index.js +2 -0
- package/dist/module/cache/index.js.map +1 -0
- package/dist/module/cache/interface.d.ts +8 -0
- package/dist/module/cache/interface.d.ts.map +1 -0
- package/dist/module/cache/interface.js +2 -0
- package/dist/module/cache/interface.js.map +1 -0
- package/dist/module/config.d.ts +2 -0
- package/dist/module/config.d.ts.map +1 -1
- package/dist/module/config.js +4 -0
- package/dist/module/config.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts +3 -3
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +34 -23
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/cache/IndexedDbCache.test.ts +15 -0
- package/src/cache/IndexedDbCache.ts +127 -0
- package/src/cache/KeyValueCache.ts +9 -0
- package/src/cache/MemoryCache.test.ts +15 -0
- package/src/cache/MemoryCache.ts +18 -0
- package/src/cache/WebStorageCache.test.ts +15 -0
- package/src/cache/WebStorageCache.ts +24 -0
- package/src/cache/index.ts +1 -0
- package/src/cache/interface.ts +7 -0
- package/src/config.ts +4 -0
- package/src/network/ElectrumNetworkProvider.ts +46 -19
- 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";
|
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) {
|
|
@@ -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
|
-
|
|
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 (
|
|
123
|
-
const cached =
|
|
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 (
|
|
136
|
-
|
|
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 (
|
|
158
|
-
const cached =
|
|
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 (
|
|
174
|
-
|
|
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
|
|
package/src/wallet/Wif.test.ts
CHANGED
|
@@ -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 () => {
|