mainnet-js 3.1.7 → 4.0.0-next.10
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/module/cache/IndexedDbCache.d.ts +1 -1
- package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
- package/dist/module/cache/MemoryCache.d.ts +1 -1
- package/dist/module/cache/MemoryCache.d.ts.map +1 -1
- package/dist/module/cache/WebStorageCache.d.ts +1 -1
- package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.d.ts +16 -6
- package/dist/module/cache/walletCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.js +92 -34
- package/dist/module/cache/walletCache.js.map +1 -1
- package/dist/module/cli.js +1 -2
- package/dist/module/cli.js.map +1 -1
- package/dist/module/db/index.d.ts +1 -1
- package/dist/module/db/index.d.ts.map +1 -1
- package/dist/module/db/index.js +1 -1
- package/dist/module/db/index.js.map +1 -1
- package/dist/module/enum.d.ts +1 -1
- package/dist/module/enum.d.ts.map +1 -1
- package/dist/module/history/getHistory.d.ts +1 -1
- package/dist/module/history/getHistory.d.ts.map +1 -1
- package/dist/module/history/getHistory.js +3 -3
- package/dist/module/history/getHistory.js.map +1 -1
- package/dist/module/index.d.ts +25 -22
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +33 -26
- package/dist/module/index.js.map +1 -1
- package/dist/module/interface.d.ts +12 -2
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/libauth.d.ts +1 -1
- package/dist/module/libauth.d.ts.map +1 -1
- package/dist/module/libauth.js +1 -1
- package/dist/module/libauth.js.map +1 -1
- package/dist/module/mine/mine.d.ts +2 -7
- package/dist/module/mine/mine.d.ts.map +1 -1
- package/dist/module/mine/mine.js +6 -27
- package/dist/module/mine/mine.js.map +1 -1
- package/dist/module/network/Connection.d.ts +1 -12
- package/dist/module/network/Connection.d.ts.map +1 -1
- package/dist/module/network/Connection.js +24 -33
- package/dist/module/network/Connection.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +91 -93
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/network/MockNetworkProvider.d.ts +28 -0
- package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
- package/dist/module/network/MockNetworkProvider.js +74 -0
- package/dist/module/network/MockNetworkProvider.js.map +1 -0
- package/dist/module/network/NetworkProvider.d.ts +9 -2
- package/dist/module/network/NetworkProvider.d.ts.map +1 -1
- package/dist/module/network/configuration.d.ts +2 -4
- package/dist/module/network/configuration.d.ts.map +1 -1
- package/dist/module/network/configuration.js +25 -50
- package/dist/module/network/configuration.js.map +1 -1
- package/dist/module/network/constant.d.ts +7 -7
- package/dist/module/network/constant.d.ts.map +1 -1
- package/dist/module/network/constant.js +21 -24
- package/dist/module/network/constant.js.map +1 -1
- package/dist/module/network/default.d.ts +5 -3
- package/dist/module/network/default.d.ts.map +1 -1
- package/dist/module/network/default.js +29 -51
- package/dist/module/network/default.js.map +1 -1
- package/dist/module/network/index.d.ts +4 -4
- package/dist/module/network/index.d.ts.map +1 -1
- package/dist/module/network/index.js +2 -2
- package/dist/module/network/index.js.map +1 -1
- package/dist/module/network/interface.d.ts +0 -6
- package/dist/module/network/interface.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.d.ts +1 -1
- package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.js +13 -7
- package/dist/module/rate/ExchangeRate.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +3 -3
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +4 -4
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/transaction/allocateFee.d.ts +1 -1
- package/dist/module/transaction/allocateFee.d.ts.map +1 -1
- package/dist/module/transaction/allocateFee.js +2 -2
- package/dist/module/transaction/allocateFee.js.map +1 -1
- package/dist/module/util/deriveCashaddr.js.map +1 -1
- package/dist/module/util/deriveNetwork.js +1 -1
- package/dist/module/util/deriveNetwork.js.map +1 -1
- package/dist/module/util/getAddrsByXpubKey.js +1 -1
- package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
- package/dist/module/util/getXPubKey.js +1 -1
- package/dist/module/util/getXPubKey.js.map +1 -1
- package/dist/module/util/hd.d.ts +2 -0
- package/dist/module/util/hd.d.ts.map +1 -1
- package/dist/module/util/hd.js +41 -0
- package/dist/module/util/hd.js.map +1 -1
- package/dist/module/util/index.d.ts +7 -9
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +7 -9
- package/dist/module/util/index.js.map +1 -1
- package/dist/module/util/transaction.js +1 -1
- package/dist/module/util/transaction.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +13 -9
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +54 -58
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/HDWallet.d.ts.map +1 -1
- package/dist/module/wallet/HDWallet.js +19 -38
- package/dist/module/wallet/HDWallet.js.map +1 -1
- package/dist/module/wallet/Util.d.ts +4 -22
- package/dist/module/wallet/Util.d.ts.map +1 -1
- package/dist/module/wallet/Util.js +75 -102
- package/dist/module/wallet/Util.js.map +1 -1
- package/dist/module/wallet/Watch.d.ts +24 -5
- package/dist/module/wallet/Watch.d.ts.map +1 -1
- package/dist/module/wallet/Watch.js +141 -17
- package/dist/module/wallet/Watch.js.map +1 -1
- package/dist/module/wallet/Wif.d.ts +2 -6
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +3 -69
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/createWallet.d.ts +1 -1
- package/dist/module/wallet/createWallet.d.ts.map +1 -1
- package/dist/module/wallet/createWallet.js +2 -1
- package/dist/module/wallet/createWallet.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +3 -2
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +2 -1
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +1 -1
- package/dist/module/wallet/model.js.map +1 -1
- package/package.json +17 -29
- package/dist/index.html +0 -9
- package/dist/mainnet-3.1.7.js +0 -2066
- package/dist/module/network/util.d.ts +0 -3
- package/dist/module/network/util.d.ts.map +0 -1
- package/dist/module/network/util.js +0 -27
- package/dist/module/network/util.js.map +0 -1
- package/dist/module/test/expect.d.ts +0 -12
- package/dist/module/test/expect.d.ts.map +0 -1
- package/dist/module/test/expect.js +0 -47
- package/dist/module/test/expect.js.map +0 -1
- package/dist/module/test/fetch.d.ts +0 -3
- package/dist/module/test/fetch.d.ts.map +0 -1
- package/dist/module/test/fetch.js +0 -32
- package/dist/module/test/fetch.js.map +0 -1
- package/dist/module/util/randomBytes.d.ts +0 -2
- package/dist/module/util/randomBytes.d.ts.map +0 -1
- package/dist/module/util/randomBytes.js +0 -13
- package/dist/module/util/randomBytes.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/cache/IndexedDbCache.test.ts +0 -15
- package/src/cache/IndexedDbCache.ts +0 -172
- package/src/cache/MemoryCache.test.ts +0 -15
- package/src/cache/MemoryCache.ts +0 -32
- package/src/cache/WebStorageCache.test.ts +0 -15
- package/src/cache/WebStorageCache.ts +0 -38
- package/src/cache/index.ts +0 -2
- package/src/cache/interface.ts +0 -9
- package/src/cache/walletCache.ts +0 -254
- package/src/chain.ts +0 -3
- package/src/cli.ts +0 -32
- package/src/config.ts +0 -23
- package/src/constant.ts +0 -27
- package/src/db/ExchangeRateProvider.ts +0 -28
- package/src/db/StorageProvider.ts +0 -64
- package/src/db/index.ts +0 -2
- package/src/db/interface.ts +0 -11
- package/src/enum.ts +0 -34
- package/src/history/getHistory.test.ts +0 -290
- package/src/history/getHistory.ts +0 -411
- package/src/history/interface.ts +0 -24
- package/src/index.ts +0 -48
- package/src/interface.ts +0 -72
- package/src/libauth.ts +0 -11
- package/src/message/index.ts +0 -2
- package/src/message/interface.ts +0 -40
- package/src/message/signed.test.ts +0 -309
- package/src/message/signed.ts +0 -201
- package/src/mine/index.ts +0 -1
- package/src/mine/mine.test.ts +0 -10
- package/src/mine/mine.ts +0 -42
- package/src/network/Connection.test.ts +0 -51
- package/src/network/Connection.ts +0 -73
- package/src/network/ElectrumNetworkProvider.ts +0 -657
- package/src/network/NetworkProvider.ts +0 -180
- package/src/network/Rpc.test.ts +0 -130
- package/src/network/configuration.test.ts +0 -59
- package/src/network/configuration.ts +0 -72
- package/src/network/constant.ts +0 -43
- package/src/network/default.ts +0 -120
- package/src/network/electrum.test.ts +0 -28
- package/src/network/getRelayFeeCache.test.ts +0 -15
- package/src/network/getRelayFeeCache.ts +0 -23
- package/src/network/index.ts +0 -14
- package/src/network/interface.ts +0 -80
- package/src/network/util.test.ts +0 -24
- package/src/network/util.ts +0 -30
- package/src/rate/ExchangeRate.test.headless.js +0 -35
- package/src/rate/ExchangeRate.test.ts +0 -51
- package/src/rate/ExchangeRate.ts +0 -142
- package/src/test/expect.ts +0 -59
- package/src/test/fetch.ts +0 -39
- package/src/test/json.test.ts +0 -13
- package/src/transaction/Wif.ts +0 -680
- package/src/transaction/allocateFee.test.ts +0 -298
- package/src/transaction/allocateFee.ts +0 -149
- package/src/util/amountInSatoshi.test.ts +0 -27
- package/src/util/amountInSatoshi.ts +0 -33
- package/src/util/asSendRequestObject.ts +0 -81
- package/src/util/base64.test.ts +0 -39
- package/src/util/base64.ts +0 -12
- package/src/util/browserNotSupported.ts +0 -7
- package/src/util/checkForEmptySeed.ts +0 -9
- package/src/util/checkUtxos.ts +0 -29
- package/src/util/checkWifNetwork.ts +0 -24
- package/src/util/convert.test.ts +0 -46
- package/src/util/convert.ts +0 -50
- package/src/util/delay.ts +0 -3
- package/src/util/deriveCashaddr.test.ts +0 -164
- package/src/util/deriveCashaddr.ts +0 -143
- package/src/util/deriveLockscript.ts +0 -16
- package/src/util/deriveNetwork.ts +0 -19
- package/src/util/derivePublicKeyHash.test.ts +0 -55
- package/src/util/derivePublicKeyHash.ts +0 -64
- package/src/util/floor.test.ts +0 -21
- package/src/util/floor.ts +0 -4
- package/src/util/getAddrsByXpubKey.test.ts +0 -115
- package/src/util/getAddrsByXpubKey.ts +0 -86
- package/src/util/getRuntimePlatform.test.headless.js +0 -40
- package/src/util/getRuntimePlatform.test.ts +0 -5
- package/src/util/getRuntimePlatform.ts +0 -31
- package/src/util/getUsdRate.ts +0 -5
- package/src/util/getXPubKey.ts +0 -39
- package/src/util/hash160.test.ts +0 -18
- package/src/util/hash160.ts +0 -12
- package/src/util/hd.ts +0 -16
- package/src/util/header.test.ts +0 -34
- package/src/util/header.ts +0 -26
- package/src/util/index.ts +0 -33
- package/src/util/randomBytes.ts +0 -13
- package/src/util/randomInt.test.ts +0 -15
- package/src/util/randomInt.ts +0 -4
- package/src/util/sanitizeAddress.ts +0 -10
- package/src/util/sanitizeUnit.ts +0 -11
- package/src/util/satoshiToAmount.test.ts +0 -6
- package/src/util/satoshiToAmount.ts +0 -33
- package/src/util/sumSendRequestAmounts.ts +0 -34
- package/src/util/sumUtxoValue.ts +0 -27
- package/src/util/transaction.ts +0 -10
- package/src/wallet/Base.ts +0 -1563
- package/src/wallet/Cashtokens.test.headless.js +0 -730
- package/src/wallet/Cashtokens.test.ts +0 -1411
- package/src/wallet/HDWallet.test.ts +0 -1086
- package/src/wallet/HDWallet.ts +0 -992
- package/src/wallet/Util.test.ts +0 -134
- package/src/wallet/Util.ts +0 -191
- package/src/wallet/WalletCache.test.ts +0 -45
- package/src/wallet/Watch.ts +0 -441
- package/src/wallet/Wif.bip39.test.ts +0 -48
- package/src/wallet/Wif.test.ts +0 -1189
- package/src/wallet/Wif.ts +0 -687
- package/src/wallet/Wif.watchOnly.test.ts +0 -58
- package/src/wallet/createWallet.ts +0 -238
- package/src/wallet/enum.ts +0 -18
- package/src/wallet/interface.ts +0 -102
- package/src/wallet/model.test.ts +0 -24
- package/src/wallet/model.ts +0 -352
- package/tsconfig.browser.json +0 -11
- package/tsconfig.json +0 -33
- package/webpack.config.cjs +0 -132
package/src/cache/MemoryCache.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { CacheProvider } from "./interface";
|
|
2
|
-
|
|
3
|
-
let cache: Record<string, string> = {};
|
|
4
|
-
export class MemoryCache implements CacheProvider {
|
|
5
|
-
async init() {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
async setItem(key: string, value: string) {
|
|
9
|
-
cache[key] = value;
|
|
10
|
-
}
|
|
11
|
-
async getItem(key: string) {
|
|
12
|
-
return cache[key] ?? null;
|
|
13
|
-
}
|
|
14
|
-
async getItems(keys: string[]): Promise<Map<string, string | null>> {
|
|
15
|
-
const results = new Map<string, string | null>();
|
|
16
|
-
for (const key of keys) {
|
|
17
|
-
results.set(key, cache[key] ?? null);
|
|
18
|
-
}
|
|
19
|
-
return results;
|
|
20
|
-
}
|
|
21
|
-
async setItems(entries: [string, string][]): Promise<void> {
|
|
22
|
-
for (const [key, value] of entries) {
|
|
23
|
-
cache[key] = value;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
async removeItem(key: string) {
|
|
27
|
-
delete cache[key];
|
|
28
|
-
}
|
|
29
|
-
async clear() {
|
|
30
|
-
cache = {};
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
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 getItems(keys: string[]): Promise<Map<string, string | null>> {
|
|
18
|
-
const results = new Map<string, string | null>();
|
|
19
|
-
for (const key of keys) {
|
|
20
|
-
results.set(key, localStorage.getItem(key));
|
|
21
|
-
}
|
|
22
|
-
return results;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async setItems(entries: [string, string][]): Promise<void> {
|
|
26
|
-
for (const [key, value] of entries) {
|
|
27
|
-
localStorage.setItem(key, value);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async removeItem(key: string): Promise<void> {
|
|
32
|
-
localStorage.removeItem(key);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async clear(): Promise<void> {
|
|
36
|
-
localStorage.clear();
|
|
37
|
-
}
|
|
38
|
-
}
|
package/src/cache/index.ts
DELETED
package/src/cache/interface.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export interface CacheProvider {
|
|
2
|
-
init(): Promise<void>;
|
|
3
|
-
setItem(key: string, value: string): Promise<void>;
|
|
4
|
-
getItem(key: string): Promise<string | null>;
|
|
5
|
-
getItems(keys: string[]): Promise<Map<string, string | null>>;
|
|
6
|
-
setItems(entries: [string, string][]): Promise<void>;
|
|
7
|
-
removeItem(key: string): Promise<void>;
|
|
8
|
-
clear(): Promise<void>;
|
|
9
|
-
}
|
package/src/cache/walletCache.ts
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assertSuccess,
|
|
3
|
-
binToHex,
|
|
4
|
-
CashAddressNetworkPrefix,
|
|
5
|
-
CashAddressType,
|
|
6
|
-
deriveHdPathRelative,
|
|
7
|
-
encodeCashAddress,
|
|
8
|
-
hash160,
|
|
9
|
-
HdPrivateNodeValid,
|
|
10
|
-
HdPublicNodeValid,
|
|
11
|
-
hexToBin,
|
|
12
|
-
secp256k1,
|
|
13
|
-
} from "@bitauth/libauth";
|
|
14
|
-
import { Config } from "../config.js";
|
|
15
|
-
import { TxI, Utxo } from "../interface.js";
|
|
16
|
-
import { IndexedDbCache } from "./IndexedDbCache.js";
|
|
17
|
-
import { CacheProvider } from "./interface.js";
|
|
18
|
-
import { MemoryCache } from "./MemoryCache.js";
|
|
19
|
-
import { WebStorageCache } from "./WebStorageCache.js";
|
|
20
|
-
|
|
21
|
-
export const stringify = (_: any) =>
|
|
22
|
-
JSON.stringify(_, (key, value) => {
|
|
23
|
-
if (key.includes("Key")) return binToHex(value);
|
|
24
|
-
return typeof value === "bigint" ? value.toString() + "n" : value;
|
|
25
|
-
});
|
|
26
|
-
export const parse = (data: string) =>
|
|
27
|
-
JSON.parse(data, (key, value) => {
|
|
28
|
-
if (key.includes("Key") && typeof value === "string") {
|
|
29
|
-
return hexToBin(value);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (typeof value === "string" && /^\d+n$/.test(value)) {
|
|
33
|
-
return BigInt(value.slice(0, -1));
|
|
34
|
-
}
|
|
35
|
-
return value;
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
export interface WalletCacheEntry {
|
|
39
|
-
address: string;
|
|
40
|
-
tokenAddress: string;
|
|
41
|
-
privateKey: Uint8Array | undefined;
|
|
42
|
-
publicKey: Uint8Array;
|
|
43
|
-
publicKeyHash: Uint8Array;
|
|
44
|
-
index: number;
|
|
45
|
-
change: boolean;
|
|
46
|
-
status: string | null;
|
|
47
|
-
utxos: Utxo[];
|
|
48
|
-
rawHistory: TxI[];
|
|
49
|
-
// Top block height of confirmed history items, used for incremental fetching
|
|
50
|
-
lastConfirmedHeight: number;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Minimal interface for use in transaction signing
|
|
54
|
-
export interface WalletCache {
|
|
55
|
-
get(address: string): { privateKey: Uint8Array | undefined } | undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Full interface for wallet cache management
|
|
59
|
-
export interface WalletCacheI extends WalletCache {
|
|
60
|
-
init(): Promise<void>;
|
|
61
|
-
persist(): Promise<void>;
|
|
62
|
-
get(address: string): WalletCacheEntry | undefined;
|
|
63
|
-
getByIndex(addressIndex: number, change: boolean): WalletCacheEntry;
|
|
64
|
-
setStatusAndUtxos(
|
|
65
|
-
address: string,
|
|
66
|
-
status: string | null,
|
|
67
|
-
utxos: Utxo[],
|
|
68
|
-
rawHistory: TxI[],
|
|
69
|
-
lastConfirmedHeight: number
|
|
70
|
-
): void;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export class PersistentWalletCache implements WalletCacheI {
|
|
74
|
-
private _storage: CacheProvider | undefined;
|
|
75
|
-
private walletCache: Record<string, WalletCacheEntry> = {};
|
|
76
|
-
private indexCache: Record<
|
|
77
|
-
string,
|
|
78
|
-
{
|
|
79
|
-
index: number;
|
|
80
|
-
change: boolean;
|
|
81
|
-
}
|
|
82
|
-
> = {};
|
|
83
|
-
private debounceTimer: ReturnType<typeof setTimeout> | undefined;
|
|
84
|
-
|
|
85
|
-
get storage(): CacheProvider | undefined {
|
|
86
|
-
if (
|
|
87
|
-
!Config.UseMemoryCache &&
|
|
88
|
-
!Config.UseLocalStorageCache &&
|
|
89
|
-
!Config.UseIndexedDBCache
|
|
90
|
-
) {
|
|
91
|
-
this._storage = undefined;
|
|
92
|
-
return this._storage;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (Config.UseMemoryCache && !(this._storage instanceof MemoryCache)) {
|
|
96
|
-
this._storage = new MemoryCache();
|
|
97
|
-
return this._storage;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
Config.UseLocalStorageCache &&
|
|
102
|
-
!(this._storage instanceof WebStorageCache)
|
|
103
|
-
) {
|
|
104
|
-
this._storage = new WebStorageCache();
|
|
105
|
-
return this._storage;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
Config.UseIndexedDBCache &&
|
|
110
|
-
!(this._storage instanceof IndexedDbCache)
|
|
111
|
-
) {
|
|
112
|
-
this._storage = new IndexedDbCache("WalletCache");
|
|
113
|
-
return this._storage;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return this._storage;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
constructor(
|
|
120
|
-
public walletId: string,
|
|
121
|
-
public hdNode: HdPublicNodeValid | HdPrivateNodeValid,
|
|
122
|
-
public networkPrefix: string,
|
|
123
|
-
public writeTimeout: number = 2000
|
|
124
|
-
) {
|
|
125
|
-
if (!this.hdNode) {
|
|
126
|
-
throw new Error("HDNode is undefined");
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public async init() {
|
|
131
|
-
await this.storage?.init();
|
|
132
|
-
const data = await this.storage?.getItem(`walletCache-${this.walletId}`);
|
|
133
|
-
if (data) {
|
|
134
|
-
try {
|
|
135
|
-
const parsed = parse(data);
|
|
136
|
-
this.walletCache = parsed.walletCache || {};
|
|
137
|
-
this.indexCache = parsed.indexCache || {};
|
|
138
|
-
} catch (e) {
|
|
139
|
-
// ignore
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private schedulePersist() {
|
|
145
|
-
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
|
146
|
-
this.debounceTimer = setTimeout(() => {
|
|
147
|
-
this.persist().catch(() => {});
|
|
148
|
-
}, this.writeTimeout);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
public async persist() {
|
|
152
|
-
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
|
153
|
-
this.debounceTimer = undefined;
|
|
154
|
-
|
|
155
|
-
this.storage?.setItem(
|
|
156
|
-
`walletCache-${this.walletId}`,
|
|
157
|
-
stringify({
|
|
158
|
-
walletCache: this.walletCache,
|
|
159
|
-
indexCache: this.indexCache,
|
|
160
|
-
})
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
public getByIndex(addressIndex: number, change: boolean) {
|
|
165
|
-
const id = `${this.walletId}-${addressIndex}-${change}`;
|
|
166
|
-
if (!this.walletCache[id]) {
|
|
167
|
-
const node = deriveHdPathRelative(
|
|
168
|
-
this.hdNode,
|
|
169
|
-
`${change ? 1 : 0}/${addressIndex}`
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
const privateKey = "privateKey" in node ? node.privateKey : undefined;
|
|
173
|
-
const publicKey =
|
|
174
|
-
"publicKey" in node
|
|
175
|
-
? node.publicKey
|
|
176
|
-
: assertSuccess(secp256k1.derivePublicKeyUncompressed(privateKey!));
|
|
177
|
-
const publicKeyCompressed = assertSuccess(
|
|
178
|
-
secp256k1.compressPublicKey(publicKey)
|
|
179
|
-
);
|
|
180
|
-
const publicKeyHash = hash160(publicKeyCompressed);
|
|
181
|
-
|
|
182
|
-
const address = encodeCashAddress({
|
|
183
|
-
throwErrors: true,
|
|
184
|
-
prefix: this.networkPrefix as CashAddressNetworkPrefix,
|
|
185
|
-
type: CashAddressType.p2pkh,
|
|
186
|
-
payload: publicKeyHash,
|
|
187
|
-
}).address;
|
|
188
|
-
|
|
189
|
-
const tokenAddress = encodeCashAddress({
|
|
190
|
-
throwErrors: true,
|
|
191
|
-
prefix: this.networkPrefix as CashAddressNetworkPrefix,
|
|
192
|
-
type: CashAddressType.p2pkhWithTokens,
|
|
193
|
-
payload: publicKeyHash,
|
|
194
|
-
}).address;
|
|
195
|
-
|
|
196
|
-
this.walletCache[id] = {
|
|
197
|
-
address,
|
|
198
|
-
tokenAddress,
|
|
199
|
-
privateKey: privateKey,
|
|
200
|
-
publicKey,
|
|
201
|
-
publicKeyHash,
|
|
202
|
-
index: addressIndex,
|
|
203
|
-
change,
|
|
204
|
-
status: null,
|
|
205
|
-
utxos: [],
|
|
206
|
-
rawHistory: [],
|
|
207
|
-
lastConfirmedHeight: 0,
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
this.indexCache[address] = {
|
|
211
|
-
index: addressIndex,
|
|
212
|
-
change,
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
this.schedulePersist();
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return this.walletCache[id];
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
public get(address: string) {
|
|
222
|
-
const { index, change } = this.indexCache[address] || {};
|
|
223
|
-
if (index === undefined || change === undefined) {
|
|
224
|
-
return undefined;
|
|
225
|
-
}
|
|
226
|
-
return this.getByIndex(index, change);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
public setStatusAndUtxos(
|
|
230
|
-
address: string,
|
|
231
|
-
status: string | null,
|
|
232
|
-
utxos: Utxo[],
|
|
233
|
-
rawHistory: TxI[],
|
|
234
|
-
lastConfirmedHeight: number
|
|
235
|
-
) {
|
|
236
|
-
const entry = this.get(address);
|
|
237
|
-
if (!entry) {
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const { index, change } = this.indexCache[address] || {};
|
|
242
|
-
if (index === undefined || change === undefined) {
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const key = `${this.walletId}-${index}-${change}`;
|
|
247
|
-
this.walletCache[key].status = status;
|
|
248
|
-
this.walletCache[key].utxos = utxos;
|
|
249
|
-
this.walletCache[key].rawHistory = rawHistory;
|
|
250
|
-
this.walletCache[key].lastConfirmedHeight = lastConfirmedHeight;
|
|
251
|
-
|
|
252
|
-
this.schedulePersist();
|
|
253
|
-
}
|
|
254
|
-
}
|
package/src/chain.ts
DELETED
package/src/cli.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { createWallet } from "../src/wallet/createWallet.js";
|
|
2
|
-
import { WalletTypeEnum } from "./wallet/enum.js";
|
|
3
|
-
import { walletFromId } from "../src/wallet/createWallet.js";
|
|
4
|
-
|
|
5
|
-
let args = process.argv.slice(); // remove ts-node
|
|
6
|
-
args.shift(); // remove ts-node
|
|
7
|
-
args.shift(); // remove cli.ts
|
|
8
|
-
|
|
9
|
-
// This is an UNSUPPORTED feature for developers
|
|
10
|
-
// Please upstream any additions you find useful.
|
|
11
|
-
// There is ZERO support for this feature
|
|
12
|
-
|
|
13
|
-
(async () => {
|
|
14
|
-
let command = args.shift();
|
|
15
|
-
let w;
|
|
16
|
-
switch (command) {
|
|
17
|
-
case "wallet/create":
|
|
18
|
-
let response = await createWallet({
|
|
19
|
-
name: args[0],
|
|
20
|
-
type: args[1] as WalletTypeEnum,
|
|
21
|
-
network: args[2],
|
|
22
|
-
});
|
|
23
|
-
console.log(response);
|
|
24
|
-
break;
|
|
25
|
-
case "wallet/deposit_address":
|
|
26
|
-
w = await walletFromId(args[0]);
|
|
27
|
-
console.log(w.depositAddress());
|
|
28
|
-
break;
|
|
29
|
-
default:
|
|
30
|
-
console.log(`${command} not implemented`);
|
|
31
|
-
}
|
|
32
|
-
})();
|
package/src/config.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export class Config {
|
|
2
|
-
// enforces all token-related methods to specify tokenaddr as recepient and change cashaddr
|
|
3
|
-
static EnforceCashTokenReceiptAddresses = false;
|
|
4
|
-
static DefaultParentDerivationPath = "m/44'/0'/0'";
|
|
5
|
-
static DefaultIpfsGateway = "https://dweb.link/ipfs/";
|
|
6
|
-
// default currency for balance and rate conversions
|
|
7
|
-
static DefaultCurrency = "usd";
|
|
8
|
-
// caches the raw transactions in browser's local storage instead of memory
|
|
9
|
-
static UseLocalStorageCache = false;
|
|
10
|
-
// caches the raw transactions in browser's indexedDB instead of memory
|
|
11
|
-
static UseIndexedDBCache = false;
|
|
12
|
-
// caches the raw transactions in browser's memory
|
|
13
|
-
static UseMemoryCache = false;
|
|
14
|
-
|
|
15
|
-
public static setIpfsGateway(ipfsGateway: string) {
|
|
16
|
-
this.DefaultIpfsGateway = ipfsGateway;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// custom exchange rate function
|
|
20
|
-
public static GetExchangeRateFn:
|
|
21
|
-
| ((symbol: string) => Promise<number>)
|
|
22
|
-
| undefined = undefined;
|
|
23
|
-
}
|
package/src/constant.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export const DELIMITER = ":";
|
|
2
|
-
|
|
3
|
-
// Min amount utxo can be to be accepted by the network
|
|
4
|
-
export const DUST_UTXO_THRESHOLD = 546n;
|
|
5
|
-
|
|
6
|
-
// Current chained tx limit
|
|
7
|
-
export const MEMPOOL_CHAIN_LIMIT = 50;
|
|
8
|
-
|
|
9
|
-
// time in milliseconds to cache the usd exchange rate
|
|
10
|
-
export const EXCHANGE_RATE_TTL = 250000;
|
|
11
|
-
|
|
12
|
-
// list of common derivation paths
|
|
13
|
-
// a la: https://github.com/Electron-Cash/Electron-Cash/blob/1de24c509992cfebc22217a2a77c862c2b02bc54/electroncash_gui/qt/installwizard.py#L624
|
|
14
|
-
export const DERIVATION_PATHS = [
|
|
15
|
-
"m/0",
|
|
16
|
-
"m/0'",
|
|
17
|
-
"m/0'/0",
|
|
18
|
-
"m/0'/0'",
|
|
19
|
-
"m/0'/0'/0'",
|
|
20
|
-
"m/44'/0'/0'",
|
|
21
|
-
"m/44'/0'/0'/0",
|
|
22
|
-
"m/44'/145'/0'",
|
|
23
|
-
"m/44'/145'/0'/0",
|
|
24
|
-
"m/44'/245'/0",
|
|
25
|
-
"m/44'/245'/0'",
|
|
26
|
-
"m/44'/245'/0'/0",
|
|
27
|
-
];
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ExchangeRateI } from "./interface.js";
|
|
2
|
-
|
|
3
|
-
export default class ExchangeRateProvider {
|
|
4
|
-
/*
|
|
5
|
-
* Exchange Rate functions
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
public async getRate(symbol: string): Promise<ExchangeRateI | undefined> {
|
|
9
|
-
const valueString = localStorage.getItem(`rate-${symbol}`);
|
|
10
|
-
if (valueString) {
|
|
11
|
-
return JSON.parse(valueString) as ExchangeRateI;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return undefined;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async setRate(
|
|
18
|
-
symbol: string,
|
|
19
|
-
rate: number,
|
|
20
|
-
ttl: number
|
|
21
|
-
): Promise<boolean> {
|
|
22
|
-
localStorage.setItem(
|
|
23
|
-
`rate-${symbol}`,
|
|
24
|
-
JSON.stringify({ symbol: symbol, rate: rate, ttl: ttl })
|
|
25
|
-
);
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { WalletDbEntryI } from "./interface.js";
|
|
2
|
-
|
|
3
|
-
export default abstract class StorageProvider {
|
|
4
|
-
public constructor(...args: any[]) {
|
|
5
|
-
throw new Error("StorageProvider is abstract and cannot be instantiated");
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Ensure the database exists and is open
|
|
10
|
-
* @throws {Error} If the wallet could not be opened.
|
|
11
|
-
* @returns the storage provider
|
|
12
|
-
*/
|
|
13
|
-
abstract init(): Promise<StorageProvider>;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Manually close the database
|
|
17
|
-
* @throws {Error} If the wallet could not be opened.
|
|
18
|
-
* @returns the storage provider
|
|
19
|
-
*/
|
|
20
|
-
abstract close(): Promise<StorageProvider>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Returns information about the storage provider
|
|
24
|
-
* @throws {Error} If there is no info
|
|
25
|
-
* @returns string
|
|
26
|
-
*/
|
|
27
|
-
abstract getInfo(): string;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Add a wallet to the database
|
|
31
|
-
* @param name A user defined name for the wallet, and the walletId.
|
|
32
|
-
* @param walletId String used to reconstruct the wallet.
|
|
33
|
-
* @throws {Error} If the wallet could not be saved.
|
|
34
|
-
* @returns if the operation was successful.
|
|
35
|
-
*/
|
|
36
|
-
abstract addWallet(name: string, walletId: string): Promise<boolean>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @returns All saved wallets.
|
|
40
|
-
*/
|
|
41
|
-
abstract getWallets(): Promise<Array<WalletDbEntryI>>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Get a named wallet from the database
|
|
45
|
-
* @param name A user defined name for the wallet.
|
|
46
|
-
* @throws {Error} If the wallet could not be saved.
|
|
47
|
-
* @returns The requested wallet.
|
|
48
|
-
*/
|
|
49
|
-
abstract getWallet(name: string): Promise<WalletDbEntryI | undefined>;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Update named wallet in the database
|
|
53
|
-
* @param name A user defined name for the wallet, and the walletId.
|
|
54
|
-
* @param walletId String used to reconstruct the wallet.
|
|
55
|
-
* @throws {Error} If the wallet could not be saved.
|
|
56
|
-
*/
|
|
57
|
-
abstract updateWallet(name: string, walletId: string): Promise<void>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Check if wallet exists in the database
|
|
61
|
-
* @param name A user defined name for the wallet, and the walletId.
|
|
62
|
-
*/
|
|
63
|
-
abstract walletExists(name: string): Promise<boolean>;
|
|
64
|
-
}
|
package/src/db/index.ts
DELETED
package/src/db/interface.ts
DELETED
package/src/enum.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { CashAddressNetworkPrefix } from "@bitauth/libauth";
|
|
2
|
-
|
|
3
|
-
export enum NetworkType {
|
|
4
|
-
Mainnet = "mainnet",
|
|
5
|
-
Testnet = "testnet",
|
|
6
|
-
Regtest = "regtest",
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export enum NetworkEnum {
|
|
10
|
-
Mainnet = <any>"mainnet",
|
|
11
|
-
Testnet = <any>"testnet",
|
|
12
|
-
Regtest = <any>"regtest",
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const networkPrefixMap = {
|
|
16
|
-
bitcoincash: "mainnet",
|
|
17
|
-
bchtest: "testnet",
|
|
18
|
-
bchreg: "regtest",
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const prefixFromNetworkMap = {
|
|
22
|
-
mainnet: CashAddressNetworkPrefix.mainnet,
|
|
23
|
-
testnet: CashAddressNetworkPrefix.testnet,
|
|
24
|
-
regtest: CashAddressNetworkPrefix.regtest,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const literal = <L extends string>(l: L): L => l;
|
|
28
|
-
|
|
29
|
-
export const UnitEnum = {
|
|
30
|
-
BCH: literal("bch"),
|
|
31
|
-
USD: literal("usd"),
|
|
32
|
-
SAT: literal("sat"),
|
|
33
|
-
};
|
|
34
|
-
export type UnitEnum = typeof UnitEnum[keyof typeof UnitEnum];
|