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,201 +0,0 @@
1
- import {
2
- assertSuccess,
3
- base64ToBin,
4
- bigIntToCompactUint,
5
- binToBase64,
6
- decodeCashAddress,
7
- encodeCashAddress,
8
- RecoveryId,
9
- secp256k1,
10
- sha256,
11
- utf8ToBin,
12
- } from "@bitauth/libauth";
13
-
14
- import { hash160 } from "../util/hash160.js";
15
- import {
16
- SignedMessageI,
17
- SignedMessageResponseI,
18
- VerifyMessageResponseI,
19
- } from "./interface.js";
20
-
21
- /**
22
- * message_magic - Add "Magic", per standard bitcoin message signing.
23
- *
24
- * In this case, the magic is simply adding the number 24 as binary "\x18", and "Bitcoin Signed Message\n" followed
25
- * by the size of the message in Bitcoin's "varint" format followed by the message encoded as binary.
26
- *
27
- * @param {str} string The string to add the magic syntax to.
28
- *
29
- * @returns a promise to the string as binary array with magic syntax
30
- */
31
- // see https://github.com/Electron-Cash/Electron-Cash/blob/49f9f672364f50053a026e4a5cb30e92db2d195d/electroncash/bitcoin.py#L524
32
- function message_magic(str: string) {
33
- const msgBytes = utf8ToBin(str);
34
- const payload = `\x18Bitcoin Signed Message:\n`;
35
- return new Uint8Array([
36
- ...utf8ToBin(payload),
37
- ...bigIntToCompactUint(BigInt(msgBytes.length)),
38
- ...msgBytes,
39
- ]);
40
- }
41
-
42
- /**
43
- * hash_magic - Return the hash of the string that has undergone standard formatting
44
- *
45
- * @param {str} string The string to hash
46
- *
47
- * @returns a promise to the hash of the string.
48
- */
49
- export function hash_message(str: string) {
50
- const h = sha256.hash;
51
- return h(h(message_magic(str)));
52
- }
53
-
54
- export class SignedMessage implements SignedMessageI {
55
- /**
56
- * sign - Calculate the recoverable signed checksum of a string message.
57
- *
58
- * @param {message} string The
59
- * @param {privateKey} Uint8Array The private key to sign the message with
60
- *
61
- * @returns a promise to signature as a string
62
- */
63
- public sign(message: string, privateKey: Uint8Array): SignedMessageResponseI {
64
- const messageHash = hash_message(message);
65
- const rs = secp256k1.signMessageHashRecoverableCompact(
66
- privateKey,
67
- messageHash
68
- );
69
- if (typeof rs === "string") {
70
- throw new Error(rs);
71
- }
72
- const sigDer = secp256k1.signMessageHashDER(
73
- privateKey,
74
- messageHash
75
- ) as Uint8Array;
76
- const sigSchnorr = secp256k1.signMessageHashSchnorr(
77
- privateKey,
78
- messageHash
79
- ) as Uint8Array;
80
- const electronEncoding = new Uint8Array([
81
- ...[31 + rs.recoveryId],
82
- ...rs.signature,
83
- ]);
84
- return {
85
- raw: {
86
- ecdsa: binToBase64(rs.signature),
87
- schnorr: binToBase64(sigSchnorr),
88
- der: binToBase64(sigDer),
89
- },
90
- details: {
91
- recoveryId: rs.recoveryId,
92
- compressed: true,
93
- messageHash: binToBase64(messageHash),
94
- },
95
- signature: binToBase64(electronEncoding),
96
- };
97
- }
98
-
99
- public static sign(message: string, privateKey: Uint8Array) {
100
- return new this().sign(message, privateKey);
101
- }
102
- /**
103
- * verify - Validate that the message is valid against a given signature
104
- *
105
- * @param {message} string The message to verify as a utf8 string
106
- * @param {signature} string The signature as a base64 encoded string
107
- * @param {cashaddr} string The cashaddr to validate the signature against a recoverable signature.
108
- * @param {publicKey} string If publicKey is not recoverable from the signature type, the publicKey must be passed.
109
- *
110
- * @returns a promise to signature as a string
111
- */
112
- public verify(
113
- message: string,
114
- signature: string,
115
- cashaddr?: string,
116
- publicKey?: Uint8Array
117
- ): VerifyMessageResponseI {
118
- // Check that the signature is valid for the given message.
119
- const messageHash = hash_message(message);
120
- const sig = base64ToBin(signature);
121
-
122
- let signatureValid = false;
123
- let keyMatch = false;
124
- let pkhMatch = false;
125
- let pkh, signatureType;
126
-
127
- if (sig.length === 65) {
128
- const rawSig = sig.length === 65 ? sig.slice(1) : sig;
129
- const recoveryId = sig.slice(0, 1)[0] - 31;
130
- const recoveredPk = secp256k1.recoverPublicKeyCompressed(
131
- rawSig,
132
- recoveryId as RecoveryId,
133
- messageHash
134
- );
135
- if (typeof recoveredPk === "string") {
136
- throw new Error(recoveredPk);
137
- }
138
-
139
- pkh = hash160(recoveredPk);
140
- signatureType = "recoverable";
141
- signatureValid = secp256k1.verifySignatureCompact(
142
- rawSig,
143
- recoveredPk,
144
- messageHash
145
- );
146
- if (cashaddr) {
147
- // Validate that the signature actually matches the provided cashaddr
148
- const decoded = assertSuccess(decodeCashAddress(cashaddr));
149
-
150
- const resultingCashaddr = encodeCashAddress({
151
- prefix: decoded.prefix,
152
- type: decoded.type,
153
- payload: pkh,
154
- }).address;
155
- if (resultingCashaddr === cashaddr) {
156
- pkhMatch = true;
157
- }
158
- }
159
- } else if (publicKey) {
160
- if (secp256k1.verifySignatureDER(sig, publicKey, messageHash)) {
161
- signatureType = "der";
162
- signatureValid = true;
163
- keyMatch = true;
164
- } else if (
165
- secp256k1.verifySignatureSchnorr(sig, publicKey, messageHash)
166
- ) {
167
- signatureType = "schnorr";
168
- signatureValid = true;
169
- keyMatch = true;
170
- } else if (
171
- secp256k1.verifySignatureCompact(sig, publicKey, messageHash)
172
- ) {
173
- signatureType = "ecdsa";
174
- signatureValid = true;
175
- keyMatch = true;
176
- } else {
177
- signatureType = "na";
178
- }
179
- }
180
-
181
- return {
182
- valid: signatureValid && (keyMatch || pkhMatch),
183
- details: {
184
- signatureValid: signatureValid,
185
- signatureType: signatureType,
186
- messageHash: binToBase64(messageHash),
187
- publicKeyHashMatch: pkhMatch,
188
- publicKeyMatch: keyMatch,
189
- },
190
- };
191
- }
192
-
193
- public static verify(
194
- message: string,
195
- signature: string,
196
- cashaddr?: string,
197
- publicKey?: Uint8Array
198
- ) {
199
- return new this().verify(message, signature, cashaddr, publicKey);
200
- }
201
- }
package/src/mine/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./mine.js";
@@ -1,10 +0,0 @@
1
- import { mine } from "./mine";
2
- import { RegTestWallet } from "../wallet/Wif";
3
-
4
- describe(`Test Mining on Regtest`, () => {
5
- test("Should mine two blocks", async () => {
6
- const minerWallet = await RegTestWallet.newRandom();
7
- const response = await mine({ cashaddr: minerWallet.cashaddr!, blocks: 2 });
8
- expect(response.length).toBe(2);
9
- });
10
- });
package/src/mine/mine.ts DELETED
@@ -1,42 +0,0 @@
1
- import { binToBase64, utf8ToBin } from "@bitauth/libauth";
2
-
3
- /**
4
- * Mine blocks to a regtest address
5
- *
6
- * @param cashaddr - the address to mine to
7
- * @param blocks - the number of blocks to mine
8
- *
9
- * @remarks
10
- * This function assumes a local regtest bitcoin node with RPC_* matching the docker configuration
11
- */
12
- export const mine = async ({
13
- cashaddr,
14
- blocks,
15
- }: {
16
- cashaddr: string;
17
- blocks: number;
18
- }): Promise<any> => {
19
- const response = await fetch(`http://127.0.0.1:${process.env.RPC_PORT}/`, {
20
- method: "POST",
21
- headers: {
22
- "Content-Type": "application/json",
23
- Authorization:
24
- "Basic " +
25
- binToBase64(
26
- utf8ToBin(`${process.env.RPC_USER}:${process.env.RPC_PASS}`)
27
- ),
28
- },
29
- body: JSON.stringify({
30
- jsonrpc: "2.0",
31
- id: "0",
32
- method: "generatetoaddress",
33
- params: {
34
- nblocks: blocks,
35
- address: cashaddr,
36
- },
37
- }),
38
- });
39
- const json = await response.json();
40
-
41
- return json.result;
42
- };
@@ -1,51 +0,0 @@
1
- import { initProviders, disconnectProviders, Connection } from "./Connection";
2
- import { RegTestWallet, TestNetWallet, Wallet } from "../wallet/Wif";
3
-
4
- beforeAll(async () => {
5
- await initProviders();
6
- });
7
-
8
- afterAll(async () => {
9
- await disconnectProviders();
10
- });
11
-
12
- test("Should connect to mainnet", async () => {
13
- process.setMaxListeners(0);
14
-
15
- let wallet = await Wallet.newRandom();
16
- expect(wallet.provider == globalThis.BCH).toBeTruthy();
17
- expect(await wallet.getBalance()).toBe(0n);
18
-
19
- let height = await globalThis.BCH.getBlockHeight();
20
- expect(height).toBeGreaterThan(5000);
21
- });
22
-
23
- test("Should use global provider when creating testnet wallet", async () => {
24
- let wallet = await TestNetWallet.newRandom();
25
- expect(wallet.provider == globalThis.tBCH).toBeTruthy();
26
- expect(await wallet.getBalance()).toBe(0n);
27
-
28
- let height = await globalThis.tBCH.getBlockHeight();
29
- expect(height).toBeGreaterThan(114);
30
- });
31
-
32
- test.skip("Should lower overhead in creating wallets", async () => {
33
- process.setMaxListeners(0);
34
- for (let i = 0; i < 100; i++) {
35
- let wallet = await RegTestWallet.newRandom();
36
- expect(wallet.provider == globalThis.rBCH).toBeTruthy();
37
- expect(await wallet.getBalance()).toBe(0n);
38
- }
39
-
40
- let height = await globalThis.rBCH.getBlockHeight();
41
- expect(height).toBeGreaterThan(114);
42
- });
43
-
44
- test("Should create a new Connection", async () => {
45
- let conn = new Connection("mainnet", "wss://bch.imaginary.cash:50004");
46
- await conn.ready();
47
- expect(conn.networkProvider == globalThis.BCH).toBeFalsy();
48
- let blockheight = await conn.networkProvider.getBlockHeight();
49
- expect(blockheight).toBeGreaterThan(10000);
50
- expect(10001).toBeGreaterThan(10000);
51
- });
@@ -1,73 +0,0 @@
1
- import { default as NetworkProvider } from "./NetworkProvider.js";
2
- import {
3
- getNetworkProvider,
4
- setGlobalProvider,
5
- getGlobalProvider,
6
- removeGlobalProvider,
7
- } from "./default.js";
8
- import { Network } from "../interface.js";
9
- import { networkTickerMap } from "./constant.js";
10
- import { prefixFromNetworkMap } from "../enum.js";
11
- import { CashAddressNetworkPrefix } from "@bitauth/libauth";
12
-
13
- export async function initProvider(network: Network) {
14
- if (!getGlobalProvider(network)) {
15
- const conn = new Connection(network);
16
- const provider = (await conn.ready()).networkProvider;
17
- setGlobalProvider(network, provider);
18
- return provider;
19
- }
20
- return getGlobalProvider(network);
21
- }
22
-
23
- export async function initProviders(networks?: Network[]) {
24
- networks = networks ? networks : (Object.keys(networkTickerMap) as Network[]);
25
- let initPromises = networks.map((n) => initProvider(n));
26
- await Promise.all(initPromises).catch((e) => {
27
- console.warn(`Warning, couldn't establish a connection for ${e}`);
28
- });
29
- }
30
-
31
- async function disconnectProvider(network: Network) {
32
- const provider = getGlobalProvider(network);
33
- if (provider) {
34
- await provider.disconnect();
35
- removeGlobalProvider(network);
36
- return;
37
- } else {
38
- // console.warn(
39
- // `Ignoring attempt to disconnect non-existent ${network} provider`
40
- // );
41
- return true;
42
- }
43
- }
44
-
45
- export async function disconnectProviders(networks?: Network[]) {
46
- networks = networks ? networks : (Object.keys(networkTickerMap) as Network[]);
47
- let disconnectPromises = networks.map((n) => disconnectProvider(n));
48
- await Promise.all(disconnectPromises);
49
- }
50
-
51
- export class Connection {
52
- public network: Network;
53
- public servers?: string[];
54
- public networkPrefix: CashAddressNetworkPrefix;
55
- public networkProvider: NetworkProvider;
56
-
57
- constructor(network?: Network, servers?: string[] | string) {
58
- this.network = network ? network : "mainnet";
59
- this.networkPrefix = prefixFromNetworkMap[this.network];
60
- this.networkProvider = getNetworkProvider(this.network, servers, true);
61
- }
62
-
63
- public async ready() {
64
- await this.networkProvider.connect();
65
- await this.networkProvider.ready();
66
- return this;
67
- }
68
-
69
- public async disconnect() {
70
- await this.networkProvider.disconnect();
71
- return this;
72
- }
73
- }