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.
Files changed (267) hide show
  1. package/dist/module/cache/IndexedDbCache.d.ts +1 -1
  2. package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
  3. package/dist/module/cache/MemoryCache.d.ts +1 -1
  4. package/dist/module/cache/MemoryCache.d.ts.map +1 -1
  5. package/dist/module/cache/WebStorageCache.d.ts +1 -1
  6. package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
  7. package/dist/module/cache/walletCache.d.ts +16 -6
  8. package/dist/module/cache/walletCache.d.ts.map +1 -1
  9. package/dist/module/cache/walletCache.js +92 -34
  10. package/dist/module/cache/walletCache.js.map +1 -1
  11. package/dist/module/cli.js +1 -2
  12. package/dist/module/cli.js.map +1 -1
  13. package/dist/module/db/index.d.ts +1 -1
  14. package/dist/module/db/index.d.ts.map +1 -1
  15. package/dist/module/db/index.js +1 -1
  16. package/dist/module/db/index.js.map +1 -1
  17. package/dist/module/enum.d.ts +1 -1
  18. package/dist/module/enum.d.ts.map +1 -1
  19. package/dist/module/history/getHistory.d.ts +1 -1
  20. package/dist/module/history/getHistory.d.ts.map +1 -1
  21. package/dist/module/history/getHistory.js +3 -3
  22. package/dist/module/history/getHistory.js.map +1 -1
  23. package/dist/module/index.d.ts +25 -22
  24. package/dist/module/index.d.ts.map +1 -1
  25. package/dist/module/index.js +33 -26
  26. package/dist/module/index.js.map +1 -1
  27. package/dist/module/interface.d.ts +12 -2
  28. package/dist/module/interface.d.ts.map +1 -1
  29. package/dist/module/interface.js.map +1 -1
  30. package/dist/module/libauth.d.ts +1 -1
  31. package/dist/module/libauth.d.ts.map +1 -1
  32. package/dist/module/libauth.js +1 -1
  33. package/dist/module/libauth.js.map +1 -1
  34. package/dist/module/mine/mine.d.ts +2 -7
  35. package/dist/module/mine/mine.d.ts.map +1 -1
  36. package/dist/module/mine/mine.js +6 -27
  37. package/dist/module/mine/mine.js.map +1 -1
  38. package/dist/module/network/Connection.d.ts +1 -12
  39. package/dist/module/network/Connection.d.ts.map +1 -1
  40. package/dist/module/network/Connection.js +24 -33
  41. package/dist/module/network/Connection.js.map +1 -1
  42. package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
  43. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  44. package/dist/module/network/ElectrumNetworkProvider.js +91 -93
  45. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  46. package/dist/module/network/MockNetworkProvider.d.ts +28 -0
  47. package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
  48. package/dist/module/network/MockNetworkProvider.js +74 -0
  49. package/dist/module/network/MockNetworkProvider.js.map +1 -0
  50. package/dist/module/network/NetworkProvider.d.ts +9 -2
  51. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  52. package/dist/module/network/configuration.d.ts +2 -4
  53. package/dist/module/network/configuration.d.ts.map +1 -1
  54. package/dist/module/network/configuration.js +25 -50
  55. package/dist/module/network/configuration.js.map +1 -1
  56. package/dist/module/network/constant.d.ts +7 -7
  57. package/dist/module/network/constant.d.ts.map +1 -1
  58. package/dist/module/network/constant.js +21 -24
  59. package/dist/module/network/constant.js.map +1 -1
  60. package/dist/module/network/default.d.ts +5 -3
  61. package/dist/module/network/default.d.ts.map +1 -1
  62. package/dist/module/network/default.js +29 -51
  63. package/dist/module/network/default.js.map +1 -1
  64. package/dist/module/network/index.d.ts +4 -4
  65. package/dist/module/network/index.d.ts.map +1 -1
  66. package/dist/module/network/index.js +2 -2
  67. package/dist/module/network/index.js.map +1 -1
  68. package/dist/module/network/interface.d.ts +0 -6
  69. package/dist/module/network/interface.d.ts.map +1 -1
  70. package/dist/module/rate/ExchangeRate.d.ts +1 -1
  71. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  72. package/dist/module/rate/ExchangeRate.js +13 -7
  73. package/dist/module/rate/ExchangeRate.js.map +1 -1
  74. package/dist/module/transaction/Wif.d.ts +3 -3
  75. package/dist/module/transaction/Wif.d.ts.map +1 -1
  76. package/dist/module/transaction/Wif.js +4 -4
  77. package/dist/module/transaction/Wif.js.map +1 -1
  78. package/dist/module/transaction/allocateFee.d.ts +1 -1
  79. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  80. package/dist/module/transaction/allocateFee.js +2 -2
  81. package/dist/module/transaction/allocateFee.js.map +1 -1
  82. package/dist/module/util/deriveCashaddr.js.map +1 -1
  83. package/dist/module/util/deriveNetwork.js +1 -1
  84. package/dist/module/util/deriveNetwork.js.map +1 -1
  85. package/dist/module/util/getAddrsByXpubKey.js +1 -1
  86. package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
  87. package/dist/module/util/getXPubKey.js +1 -1
  88. package/dist/module/util/getXPubKey.js.map +1 -1
  89. package/dist/module/util/hd.d.ts +2 -0
  90. package/dist/module/util/hd.d.ts.map +1 -1
  91. package/dist/module/util/hd.js +41 -0
  92. package/dist/module/util/hd.js.map +1 -1
  93. package/dist/module/util/index.d.ts +7 -9
  94. package/dist/module/util/index.d.ts.map +1 -1
  95. package/dist/module/util/index.js +7 -9
  96. package/dist/module/util/index.js.map +1 -1
  97. package/dist/module/util/transaction.js +1 -1
  98. package/dist/module/util/transaction.js.map +1 -1
  99. package/dist/module/wallet/Base.d.ts +13 -9
  100. package/dist/module/wallet/Base.d.ts.map +1 -1
  101. package/dist/module/wallet/Base.js +54 -58
  102. package/dist/module/wallet/Base.js.map +1 -1
  103. package/dist/module/wallet/HDWallet.d.ts.map +1 -1
  104. package/dist/module/wallet/HDWallet.js +19 -38
  105. package/dist/module/wallet/HDWallet.js.map +1 -1
  106. package/dist/module/wallet/Util.d.ts +4 -22
  107. package/dist/module/wallet/Util.d.ts.map +1 -1
  108. package/dist/module/wallet/Util.js +75 -102
  109. package/dist/module/wallet/Util.js.map +1 -1
  110. package/dist/module/wallet/Watch.d.ts +24 -5
  111. package/dist/module/wallet/Watch.d.ts.map +1 -1
  112. package/dist/module/wallet/Watch.js +141 -17
  113. package/dist/module/wallet/Watch.js.map +1 -1
  114. package/dist/module/wallet/Wif.d.ts +2 -6
  115. package/dist/module/wallet/Wif.d.ts.map +1 -1
  116. package/dist/module/wallet/Wif.js +3 -69
  117. package/dist/module/wallet/Wif.js.map +1 -1
  118. package/dist/module/wallet/createWallet.d.ts +1 -1
  119. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  120. package/dist/module/wallet/createWallet.js +2 -1
  121. package/dist/module/wallet/createWallet.js.map +1 -1
  122. package/dist/module/wallet/interface.d.ts +3 -2
  123. package/dist/module/wallet/interface.d.ts.map +1 -1
  124. package/dist/module/wallet/model.d.ts +2 -1
  125. package/dist/module/wallet/model.d.ts.map +1 -1
  126. package/dist/module/wallet/model.js +1 -1
  127. package/dist/module/wallet/model.js.map +1 -1
  128. package/package.json +17 -29
  129. package/dist/index.html +0 -9
  130. package/dist/mainnet-3.1.7.js +0 -2066
  131. package/dist/module/network/util.d.ts +0 -3
  132. package/dist/module/network/util.d.ts.map +0 -1
  133. package/dist/module/network/util.js +0 -27
  134. package/dist/module/network/util.js.map +0 -1
  135. package/dist/module/test/expect.d.ts +0 -12
  136. package/dist/module/test/expect.d.ts.map +0 -1
  137. package/dist/module/test/expect.js +0 -47
  138. package/dist/module/test/expect.js.map +0 -1
  139. package/dist/module/test/fetch.d.ts +0 -3
  140. package/dist/module/test/fetch.d.ts.map +0 -1
  141. package/dist/module/test/fetch.js +0 -32
  142. package/dist/module/test/fetch.js.map +0 -1
  143. package/dist/module/util/randomBytes.d.ts +0 -2
  144. package/dist/module/util/randomBytes.d.ts.map +0 -1
  145. package/dist/module/util/randomBytes.js +0 -13
  146. package/dist/module/util/randomBytes.js.map +0 -1
  147. package/dist/tsconfig.tsbuildinfo +0 -1
  148. package/src/cache/IndexedDbCache.test.ts +0 -15
  149. package/src/cache/IndexedDbCache.ts +0 -172
  150. package/src/cache/MemoryCache.test.ts +0 -15
  151. package/src/cache/MemoryCache.ts +0 -32
  152. package/src/cache/WebStorageCache.test.ts +0 -15
  153. package/src/cache/WebStorageCache.ts +0 -38
  154. package/src/cache/index.ts +0 -2
  155. package/src/cache/interface.ts +0 -9
  156. package/src/cache/walletCache.ts +0 -254
  157. package/src/chain.ts +0 -3
  158. package/src/cli.ts +0 -32
  159. package/src/config.ts +0 -23
  160. package/src/constant.ts +0 -27
  161. package/src/db/ExchangeRateProvider.ts +0 -28
  162. package/src/db/StorageProvider.ts +0 -64
  163. package/src/db/index.ts +0 -2
  164. package/src/db/interface.ts +0 -11
  165. package/src/enum.ts +0 -34
  166. package/src/history/getHistory.test.ts +0 -290
  167. package/src/history/getHistory.ts +0 -411
  168. package/src/history/interface.ts +0 -24
  169. package/src/index.ts +0 -48
  170. package/src/interface.ts +0 -72
  171. package/src/libauth.ts +0 -11
  172. package/src/message/index.ts +0 -2
  173. package/src/message/interface.ts +0 -40
  174. package/src/message/signed.test.ts +0 -309
  175. package/src/message/signed.ts +0 -201
  176. package/src/mine/index.ts +0 -1
  177. package/src/mine/mine.test.ts +0 -10
  178. package/src/mine/mine.ts +0 -42
  179. package/src/network/Connection.test.ts +0 -51
  180. package/src/network/Connection.ts +0 -73
  181. package/src/network/ElectrumNetworkProvider.ts +0 -657
  182. package/src/network/NetworkProvider.ts +0 -180
  183. package/src/network/Rpc.test.ts +0 -130
  184. package/src/network/configuration.test.ts +0 -59
  185. package/src/network/configuration.ts +0 -72
  186. package/src/network/constant.ts +0 -43
  187. package/src/network/default.ts +0 -120
  188. package/src/network/electrum.test.ts +0 -28
  189. package/src/network/getRelayFeeCache.test.ts +0 -15
  190. package/src/network/getRelayFeeCache.ts +0 -23
  191. package/src/network/index.ts +0 -14
  192. package/src/network/interface.ts +0 -80
  193. package/src/network/util.test.ts +0 -24
  194. package/src/network/util.ts +0 -30
  195. package/src/rate/ExchangeRate.test.headless.js +0 -35
  196. package/src/rate/ExchangeRate.test.ts +0 -51
  197. package/src/rate/ExchangeRate.ts +0 -142
  198. package/src/test/expect.ts +0 -59
  199. package/src/test/fetch.ts +0 -39
  200. package/src/test/json.test.ts +0 -13
  201. package/src/transaction/Wif.ts +0 -680
  202. package/src/transaction/allocateFee.test.ts +0 -298
  203. package/src/transaction/allocateFee.ts +0 -149
  204. package/src/util/amountInSatoshi.test.ts +0 -27
  205. package/src/util/amountInSatoshi.ts +0 -33
  206. package/src/util/asSendRequestObject.ts +0 -81
  207. package/src/util/base64.test.ts +0 -39
  208. package/src/util/base64.ts +0 -12
  209. package/src/util/browserNotSupported.ts +0 -7
  210. package/src/util/checkForEmptySeed.ts +0 -9
  211. package/src/util/checkUtxos.ts +0 -29
  212. package/src/util/checkWifNetwork.ts +0 -24
  213. package/src/util/convert.test.ts +0 -46
  214. package/src/util/convert.ts +0 -50
  215. package/src/util/delay.ts +0 -3
  216. package/src/util/deriveCashaddr.test.ts +0 -164
  217. package/src/util/deriveCashaddr.ts +0 -143
  218. package/src/util/deriveLockscript.ts +0 -16
  219. package/src/util/deriveNetwork.ts +0 -19
  220. package/src/util/derivePublicKeyHash.test.ts +0 -55
  221. package/src/util/derivePublicKeyHash.ts +0 -64
  222. package/src/util/floor.test.ts +0 -21
  223. package/src/util/floor.ts +0 -4
  224. package/src/util/getAddrsByXpubKey.test.ts +0 -115
  225. package/src/util/getAddrsByXpubKey.ts +0 -86
  226. package/src/util/getRuntimePlatform.test.headless.js +0 -40
  227. package/src/util/getRuntimePlatform.test.ts +0 -5
  228. package/src/util/getRuntimePlatform.ts +0 -31
  229. package/src/util/getUsdRate.ts +0 -5
  230. package/src/util/getXPubKey.ts +0 -39
  231. package/src/util/hash160.test.ts +0 -18
  232. package/src/util/hash160.ts +0 -12
  233. package/src/util/hd.ts +0 -16
  234. package/src/util/header.test.ts +0 -34
  235. package/src/util/header.ts +0 -26
  236. package/src/util/index.ts +0 -33
  237. package/src/util/randomBytes.ts +0 -13
  238. package/src/util/randomInt.test.ts +0 -15
  239. package/src/util/randomInt.ts +0 -4
  240. package/src/util/sanitizeAddress.ts +0 -10
  241. package/src/util/sanitizeUnit.ts +0 -11
  242. package/src/util/satoshiToAmount.test.ts +0 -6
  243. package/src/util/satoshiToAmount.ts +0 -33
  244. package/src/util/sumSendRequestAmounts.ts +0 -34
  245. package/src/util/sumUtxoValue.ts +0 -27
  246. package/src/util/transaction.ts +0 -10
  247. package/src/wallet/Base.ts +0 -1563
  248. package/src/wallet/Cashtokens.test.headless.js +0 -730
  249. package/src/wallet/Cashtokens.test.ts +0 -1411
  250. package/src/wallet/HDWallet.test.ts +0 -1086
  251. package/src/wallet/HDWallet.ts +0 -992
  252. package/src/wallet/Util.test.ts +0 -134
  253. package/src/wallet/Util.ts +0 -191
  254. package/src/wallet/WalletCache.test.ts +0 -45
  255. package/src/wallet/Watch.ts +0 -441
  256. package/src/wallet/Wif.bip39.test.ts +0 -48
  257. package/src/wallet/Wif.test.ts +0 -1189
  258. package/src/wallet/Wif.ts +0 -687
  259. package/src/wallet/Wif.watchOnly.test.ts +0 -58
  260. package/src/wallet/createWallet.ts +0 -238
  261. package/src/wallet/enum.ts +0 -18
  262. package/src/wallet/interface.ts +0 -102
  263. package/src/wallet/model.test.ts +0 -24
  264. package/src/wallet/model.ts +0 -352
  265. package/tsconfig.browser.json +0 -11
  266. package/tsconfig.json +0 -33
  267. package/webpack.config.cjs +0 -132
@@ -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
- }
@@ -1,2 +0,0 @@
1
- export * from "./interface.js";
2
- export * from "./walletCache.js";
@@ -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
- }
@@ -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
@@ -1,3 +0,0 @@
1
- export const bchParam = {
2
- subUnits: 100000000n,
3
- };
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
@@ -1,2 +0,0 @@
1
- export { default as StorageProvider } from "./StorageProvider.js";
2
- export * from "./interface.js";
@@ -1,11 +0,0 @@
1
- export interface WalletDbEntryI {
2
- id?: number;
3
- name: string;
4
- wallet: string;
5
- }
6
-
7
- export interface ExchangeRateI {
8
- symbol: string;
9
- rate: number;
10
- ttl: number;
11
- }
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];