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,134 +0,0 @@
1
- import { initProviders, disconnectProviders } from "../network";
2
- import { RegTestWallet, Wallet } from "./Wif";
3
- import { mine } from "../mine";
4
- import { Config } from "../config";
5
- import { delay } from "../util/delay";
6
-
7
- beforeAll(async () => {
8
- await initProviders();
9
- });
10
- afterAll(async () => {
11
- await disconnectProviders();
12
- });
13
-
14
- describe("Utility tests", () => {
15
- test("Should compute raw transaction hash", async () => {
16
- const wallet = await RegTestWallet.newRandom();
17
- expect(
18
- await wallet.util.getTransactionHash(
19
- "01000000015bb9142c960a838329694d3fe9ba08c2a6421c5158d8f7044cb7c48006c1b484000000006a4730440220229ea5359a63c2b83a713fcc20d8c41b20d48fe639a639d2a8246a137f29d0fc02201de12de9c056912a4e581a62d12fb5f43ee6c08ed0238c32a1ee769213ca8b8b412103bcf9a004f1f7a9a8d8acce7b51c983233d107329ff7c4fb53e44c855dbe1f6a4feffffff02c6b68200000000001976a9141041fb024bd7a1338ef1959026bbba860064fe5f88ac50a8cf00000000001976a91445dac110239a7a3814535c15858b939211f8529888ac61ee0700"
20
- )
21
- ).toBe("36a3692a41a8ac60b73f7f41ee23f5c917413e5b2fad9e44b34865bd0d601a3d");
22
-
23
- // test static accessor
24
- expect(
25
- await RegTestWallet.util.getTransactionHash(
26
- "01000000015bb9142c960a838329694d3fe9ba08c2a6421c5158d8f7044cb7c48006c1b484000000006a4730440220229ea5359a63c2b83a713fcc20d8c41b20d48fe639a639d2a8246a137f29d0fc02201de12de9c056912a4e581a62d12fb5f43ee6c08ed0238c32a1ee769213ca8b8b412103bcf9a004f1f7a9a8d8acce7b51c983233d107329ff7c4fb53e44c855dbe1f6a4feffffff02c6b68200000000001976a9141041fb024bd7a1338ef1959026bbba860064fe5f88ac50a8cf00000000001976a91445dac110239a7a3814535c15858b939211f8529888ac61ee0700"
27
- )
28
- ).toBe("36a3692a41a8ac60b73f7f41ee23f5c917413e5b2fad9e44b34865bd0d601a3d");
29
- });
30
-
31
- test("Should throw on non-existent transaction and invalid hash", async () => {
32
- const wallet = await RegTestWallet.newRandom();
33
- await expect(
34
- wallet.util.decodeTransaction(
35
- "36a3692a41a8ac60b73f7f41ee23f5c917413e5b2fad9e44b34865bd0d601a3d"
36
- )
37
- ).rejects.toThrowError("might not exist");
38
- await expect(wallet.util.decodeTransaction("test")).rejects.toThrowError(
39
- "Invalid tx hash"
40
- );
41
- });
42
-
43
- test("Should get raw transaction", async () => {
44
- let wallet = await RegTestWallet.fromId(process.env.ALICE_ID!);
45
- const utxo = (await wallet.getUtxos())[0];
46
- const transaction = await wallet.provider!.getRawTransactionObject(
47
- utxo.txid
48
- );
49
- expect((await wallet.util.decodeTransaction(transaction.hash)).hash).toBe(
50
- utxo.txid
51
- );
52
- expect((await wallet.util.decodeTransaction(transaction.hex)).txid).toBe(
53
- utxo.txid
54
- );
55
-
56
- // test static accessor
57
- expect(
58
- (await RegTestWallet.util.decodeTransaction(transaction.hex)).txid
59
- ).toBe(utxo.txid);
60
- });
61
-
62
- test("Should decode a transaction from fist block", async () => {
63
- let wallet = await Wallet.newRandom();
64
- const decoded = await wallet.util.decodeTransaction(
65
- "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
66
- );
67
-
68
- expect(decoded.txid).toBe(
69
- "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
70
- );
71
-
72
- // non-enriched vin entries don't carry vout fields (enforced by types)
73
- expect((decoded.vin[0] as any).address).toBeUndefined();
74
- });
75
-
76
- test("Should decode a transaction and fetch input values and addresses", async () => {
77
- const txHash =
78
- "dc8f059900807c36941313f10b43ec049e23dfede4e09f8fbccc3871ed359fbe";
79
- const decoded = await Wallet.util.decodeTransaction(txHash, true);
80
- expect(decoded.vin[0].scriptPubKey.addresses[0]).toBeDefined();
81
- expect(decoded.vin[0].value).toBeDefined();
82
-
83
- // uncomment next line
84
- // expect(await Wallet.util.decodeTransaction(txHash)).toBe(await new Wallet().provider!.getRawTransactionObject(txHash));
85
- });
86
- });
87
-
88
- describe("Dynamic confirmations via fetchHeight", () => {
89
- test("confirmations defaults to 0 for decoded mempool transactions", async () => {
90
- const wallet = await RegTestWallet.newRandom();
91
- const decoded = await wallet.util.decodeTransaction(
92
- "01000000015bb9142c960a838329694d3fe9ba08c2a6421c5158d8f7044cb7c48006c1b484000000006a4730440220229ea5359a63c2b83a713fcc20d8c41b20d48fe639a639d2a8246a137f29d0fc02201de12de9c056912a4e581a62d12fb5f43ee6c08ed0238c32a1ee769213ca8b8b412103bcf9a004f1f7a9a8d8acce7b51c983233d107329ff7c4fb53e44c855dbe1f6a4feffffff02c6b68200000000001976a9141041fb024bd7a1338ef1959026bbba860064fe5f88ac50a8cf00000000001976a91445dac110239a7a3814535c15858b939211f8529888ac61ee0700"
93
- );
94
- expect(decoded.confirmations).toBe(0);
95
- });
96
-
97
- test("confirmations is present on verbose server response", async () => {
98
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
99
- const utxo = (await alice.getUtxos())[0];
100
- const transaction = await alice.provider.getRawTransactionObject(utxo.txid);
101
- expect(transaction.confirmations).toBeGreaterThan(0);
102
- });
103
-
104
- test("fetchHeight is not exposed in returned transaction", async () => {
105
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
106
- const utxo = (await alice.getUtxos())[0];
107
- const transaction = await alice.provider.getRawTransactionObject(utxo.txid);
108
- expect((transaction as any).fetchHeight).toBeUndefined();
109
- });
110
-
111
- test("cached transaction has up-to-date confirmations", async () => {
112
- const memoryCacheValue = Config.UseMemoryCache;
113
- Config.UseMemoryCache = true;
114
- try {
115
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
116
- const utxo = (await alice.getUtxos())[0];
117
-
118
- // first fetch primes the cache
119
- const tx1 = await alice.provider.getRawTransactionObject(utxo.txid);
120
- const confirmations1 = tx1.confirmations;
121
-
122
- // mine a block and wait for header subscription to propagate
123
- await mine({ cashaddr: alice.cashaddr!, blocks: 1 });
124
- await delay(1000);
125
-
126
- // second fetch should hit cache but with updated confirmations
127
- const tx2 = await alice.provider.getRawTransactionObject(utxo.txid);
128
- expect(tx2.confirmations).toBe(confirmations1 + 1);
129
- expect((tx2 as any).fetchHeight).toBeUndefined();
130
- } finally {
131
- Config.UseMemoryCache = memoryCacheValue;
132
- }
133
- });
134
- });
@@ -1,191 +0,0 @@
1
- import {
2
- Transaction as LibAuthTransaction,
3
- assertSuccess,
4
- binToHex,
5
- decodeTransaction as decodeTransactionLibAuth,
6
- hexToBin,
7
- isPayToPublicKey,
8
- lockingBytecodeToAddressContents,
9
- lockingBytecodeToCashAddress,
10
- publicKeyToP2pkhCashAddress,
11
- } from "@bitauth/libauth";
12
- import { bchParam } from "../chain.js";
13
- import { NetworkType, prefixFromNetworkMap } from "../enum.js";
14
- import { getNetworkProvider } from "../network/default.js";
15
- import {
16
- ElectrumRawTransaction,
17
- ElectrumRawTransactionVin,
18
- ElectrumRawTransactionVinWithValues,
19
- ElectrumRawTransactionVout,
20
- ElectrumRawTransactionWithInputValues,
21
- } from "../network/interface.js";
22
- import NetworkProvider from "../network/NetworkProvider.js";
23
- import { getTransactionHash } from "../util/transaction.js";
24
-
25
- /**
26
- * Class with various wallet utilities.
27
- */
28
- export class Util {
29
- readonly network: NetworkType;
30
- provider: NetworkProvider;
31
-
32
- /**
33
- * Initializes a wallet Util class.
34
- *
35
- * @param network The network type to use. Defaults to mainnet.
36
- */
37
- constructor(network = NetworkType.Mainnet) {
38
- this.network = network;
39
- this.provider = getNetworkProvider(network);
40
- }
41
-
42
- public async getTransactionHash(rawTransactionHex: string): Promise<string> {
43
- return getTransactionHash(rawTransactionHex);
44
- }
45
-
46
- public static async getTransactionHash(
47
- rawTransactionHex: string
48
- ): Promise<string> {
49
- return getTransactionHash(rawTransactionHex);
50
- }
51
-
52
- public async decodeTransaction(
53
- transactionHashOrHex: string,
54
- loadInputValues: true
55
- ): Promise<ElectrumRawTransactionWithInputValues>;
56
- public async decodeTransaction(
57
- transactionHashOrHex: string,
58
- loadInputValues?: false
59
- ): Promise<ElectrumRawTransaction>;
60
- public async decodeTransaction(
61
- transactionHashOrHex: string,
62
- loadInputValues: boolean = false
63
- ): Promise<ElectrumRawTransaction | ElectrumRawTransactionWithInputValues> {
64
- let transactionHex: string;
65
- let transactionBin: Uint8Array;
66
- let txHash: string;
67
-
68
- // raw transaction
69
- if (transactionHashOrHex.length > 64) {
70
- txHash = await this.getTransactionHash(transactionHashOrHex);
71
- transactionBin = hexToBin(transactionHashOrHex);
72
- transactionHex = transactionHashOrHex;
73
- } else {
74
- // tx hash, look up the raw transaction
75
- txHash = transactionHashOrHex;
76
- transactionHex = await this.provider.getRawTransaction(txHash);
77
- transactionBin = hexToBin(transactionHex);
78
- }
79
-
80
- const result = decodeTransactionLibAuth(transactionBin);
81
- if (typeof result === "string") {
82
- throw Error(result);
83
- }
84
-
85
- const transaction = this.mapToElectrumRawTransaction(
86
- result,
87
- txHash,
88
- transactionHex
89
- );
90
-
91
- if (loadInputValues) {
92
- // get unique transaction hashes
93
- const hashes = [...new Set(transaction.vin.map((val) => val.txid))];
94
- const transactions = await Promise.all(
95
- hashes.map((hash) => this.decodeTransaction(hash, false))
96
- );
97
- const transactionMap = new Map<string, ElectrumRawTransaction>();
98
- transactions.forEach((val) => transactionMap.set(val.hash, val));
99
-
100
- const enrichedVin: ElectrumRawTransactionVinWithValues[] =
101
- transaction.vin.map((input) => {
102
- const output = transactionMap
103
- .get(input.txid)!
104
- .vout.find((val) => val.n === input.vout)!;
105
- return { ...input, ...output };
106
- });
107
-
108
- return { ...transaction, vin: enrichedVin };
109
- }
110
-
111
- return transaction;
112
- }
113
-
114
- public mapToElectrumRawTransaction(
115
- transaction: LibAuthTransaction,
116
- txHash: string,
117
- txHex: string
118
- ): ElectrumRawTransaction {
119
- return {
120
- blockhash: "",
121
- blocktime: 0,
122
- confirmations: 0,
123
- time: 0,
124
- hash: txHash,
125
- hex: txHex,
126
- txid: txHash,
127
- locktime: transaction.locktime,
128
- version: transaction.version,
129
- size: txHex.length / 2,
130
- vin: transaction.inputs.map(
131
- (input): ElectrumRawTransactionVin => ({
132
- scriptSig: {
133
- asm: "",
134
- hex: binToHex(input.unlockingBytecode),
135
- },
136
- sequence: input.sequenceNumber,
137
- txid: binToHex(input.outpointTransactionHash),
138
- vout: input.outpointIndex,
139
- })
140
- ),
141
- vout: transaction.outputs.map(
142
- (output, index): ElectrumRawTransactionVout => ({
143
- n: index,
144
- scriptPubKey: {
145
- addresses: [
146
- isPayToPublicKey(output.lockingBytecode)
147
- ? publicKeyToP2pkhCashAddress({
148
- publicKey: lockingBytecodeToAddressContents(
149
- output.lockingBytecode
150
- ).payload,
151
- prefix: prefixFromNetworkMap[this.network],
152
- }).address
153
- : assertSuccess(
154
- lockingBytecodeToCashAddress({
155
- bytecode: output.lockingBytecode,
156
- prefix: prefixFromNetworkMap[this.network],
157
- })
158
- ).address,
159
- ],
160
- asm: "",
161
- hex: binToHex(output.lockingBytecode),
162
- reqSigs: 1,
163
- type: "",
164
- },
165
- value: Number(output.valueSatoshis) / Number(bchParam.subUnits),
166
- })
167
- ),
168
- };
169
- }
170
-
171
- public static async decodeTransaction(
172
- transactionHashOrHex: string,
173
- loadInputValues: true,
174
- network?: NetworkType
175
- ): Promise<ElectrumRawTransactionWithInputValues>;
176
- public static async decodeTransaction(
177
- transactionHashOrHex: string,
178
- loadInputValues?: false,
179
- network?: NetworkType
180
- ): Promise<ElectrumRawTransaction>;
181
- public static async decodeTransaction(
182
- transactionHashOrHex: string,
183
- loadInputValues: boolean = false,
184
- network?: NetworkType
185
- ): Promise<ElectrumRawTransaction | ElectrumRawTransactionWithInputValues> {
186
- if (loadInputValues) {
187
- return new this(network).decodeTransaction(transactionHashOrHex, true);
188
- }
189
- return new this(network).decodeTransaction(transactionHashOrHex);
190
- }
191
- }
@@ -1,45 +0,0 @@
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
- });