edge-currency-plugins 1.0.0-rc.1

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 (233) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/lib/bch.js +15 -0
  4. package/lib/bsv.js +15 -0
  5. package/lib/btc.js +15 -0
  6. package/lib/common/constants.js +11 -0
  7. package/lib/common/fees/calcMinerFeePerByte.js +62 -0
  8. package/lib/common/fees/makeFees.js +182 -0
  9. package/lib/common/fees/processInfoServerFees.js +30 -0
  10. package/lib/common/fees/processMempoolSpaceFees.js +38 -0
  11. package/lib/common/plugin/makeCurrencyPlugin.js +66 -0
  12. package/lib/common/plugin/makeCurrencyTools.js +211 -0
  13. package/lib/common/plugin/makeEngineEmitter.js +102 -0
  14. package/lib/common/plugin/makeMetadata.js +99 -0
  15. package/lib/common/plugin/pluginState.js +253 -0
  16. package/lib/common/plugin/serverScores.js +328 -0
  17. package/lib/common/plugin/types.js +173 -0
  18. package/lib/common/plugin/utils.js +18 -0
  19. package/lib/common/utxobased/db/Models/ProcessorTransaction.js +122 -0
  20. package/lib/common/utxobased/db/Models/baselet.js +81 -0
  21. package/lib/common/utxobased/db/makeBaselets.js +123 -0
  22. package/lib/common/utxobased/db/makeMutex.js +17 -0
  23. package/lib/common/utxobased/db/makeProcessor.js +554 -0
  24. package/lib/common/utxobased/db/types.js +67 -0
  25. package/lib/common/utxobased/engine/await-lock.js +65 -0
  26. package/lib/common/utxobased/engine/constants.js +7 -0
  27. package/lib/common/utxobased/engine/makeServerStates.js +394 -0
  28. package/lib/common/utxobased/engine/makeUtxoEngine.js +690 -0
  29. package/lib/common/utxobased/engine/makeUtxoEngineState.js +1443 -0
  30. package/lib/common/utxobased/engine/makeUtxoWalletTools.js +216 -0
  31. package/lib/common/utxobased/engine/paymentRequest.js +102 -0
  32. package/lib/common/utxobased/engine/types.js +21 -0
  33. package/lib/common/utxobased/engine/util/defaultValidateConfirmations.js +19 -0
  34. package/lib/common/utxobased/engine/util/getOwnUtxosFromTx.js +54 -0
  35. package/lib/common/utxobased/engine/utils.js +239 -0
  36. package/lib/common/utxobased/info/all.js +67 -0
  37. package/lib/common/utxobased/info/badcoin.js +73 -0
  38. package/lib/common/utxobased/info/bitcoin.js +91 -0
  39. package/lib/common/utxobased/info/bitcoincash.js +93 -0
  40. package/lib/common/utxobased/info/bitcoincashtestnet.js +72 -0
  41. package/lib/common/utxobased/info/bitcoingold.js +85 -0
  42. package/lib/common/utxobased/info/bitcoingoldtestnet.js +71 -0
  43. package/lib/common/utxobased/info/bitcoinsv.js +76 -0
  44. package/lib/common/utxobased/info/bitcointestnet.js +81 -0
  45. package/lib/common/utxobased/info/dash.js +76 -0
  46. package/lib/common/utxobased/info/digibyte.js +76 -0
  47. package/lib/common/utxobased/info/dogecoin.js +76 -0
  48. package/lib/common/utxobased/info/eboost.js +70 -0
  49. package/lib/common/utxobased/info/feathercoin.js +76 -0
  50. package/lib/common/utxobased/info/groestlcoin.js +88 -0
  51. package/lib/common/utxobased/info/litecoin.js +82 -0
  52. package/lib/common/utxobased/info/qtum.js +67 -0
  53. package/lib/common/utxobased/info/ravencoin.js +70 -0
  54. package/lib/common/utxobased/info/scriptTemplates/bitcoincashScriptTemplates.js +56 -0
  55. package/lib/common/utxobased/info/scriptTemplates/types.js +14 -0
  56. package/lib/common/utxobased/info/smartcash.js +78 -0
  57. package/lib/common/utxobased/info/ufo.js +75 -0
  58. package/lib/common/utxobased/info/utxoPickers/dogeUtxoPicker.js +34 -0
  59. package/lib/common/utxobased/info/vertcoin.js +82 -0
  60. package/lib/common/utxobased/info/zcoin.js +72 -0
  61. package/lib/common/utxobased/keymanager/base.js +60 -0
  62. package/lib/common/utxobased/keymanager/bip69.js +31 -0
  63. package/lib/common/utxobased/keymanager/bitcoincashUtils/base32.js +88 -0
  64. package/lib/common/utxobased/keymanager/bitcoincashUtils/bn.js +210 -0
  65. package/lib/common/utxobased/keymanager/bitcoincashUtils/cashAddress.js +282 -0
  66. package/lib/common/utxobased/keymanager/cleaners.js +198 -0
  67. package/lib/common/utxobased/keymanager/coinmapper.js +12 -0
  68. package/lib/common/utxobased/keymanager/declarations/base58grscheck.d.js +1 -0
  69. package/lib/common/utxobased/keymanager/declarations/blake-hash.d.js +1 -0
  70. package/lib/common/utxobased/keymanager/declarations/bn.d.js +1 -0
  71. package/lib/common/utxobased/keymanager/declarations/bs58smartcheck.d.js +1 -0
  72. package/lib/common/utxobased/keymanager/declarations/wif-smart.d.js +1 -0
  73. package/lib/common/utxobased/keymanager/declarations/wifgrs.d.js +1 -0
  74. package/lib/common/utxobased/keymanager/keymanager.js +1214 -0
  75. package/lib/common/utxobased/keymanager/utxopicker/accumulative.js +52 -0
  76. package/lib/common/utxobased/keymanager/utxopicker/forceUseUtxo.js +59 -0
  77. package/lib/common/utxobased/keymanager/utxopicker/subtractFee.js +17 -0
  78. package/lib/common/utxobased/keymanager/utxopicker/types.js +42 -0
  79. package/lib/common/utxobased/keymanager/utxopicker/utils.js +159 -0
  80. package/lib/common/utxobased/keymanager/utxopicker.js +17 -0
  81. package/lib/common/utxobased/network/BlockBook.js +200 -0
  82. package/lib/common/utxobased/network/BlockBookAPI.js +335 -0
  83. package/lib/common/utxobased/network/Deferred.js +29 -0
  84. package/lib/common/utxobased/network/MakeSocketEmitter.js +35 -0
  85. package/lib/common/utxobased/network/Socket.js +362 -0
  86. package/lib/common/utxobased/network/nodejsWS.js +59 -0
  87. package/lib/common/utxobased/network/socketQueue.js +60 -0
  88. package/lib/common/utxobased/network/types.js +19 -0
  89. package/lib/common/utxobased/network/windowWS.js +54 -0
  90. package/lib/firo.js +15 -0
  91. package/lib/ftc.js +15 -0
  92. package/lib/index.js +19 -0
  93. package/lib/ltc.js +15 -0
  94. package/lib/src/bch.d.ts +3 -0
  95. package/lib/src/bsv.d.ts +3 -0
  96. package/lib/src/btc.d.ts +3 -0
  97. package/lib/src/common/constants.d.ts +9 -0
  98. package/lib/src/common/fees/calcMinerFeePerByte.d.ts +13 -0
  99. package/lib/src/common/fees/makeFees.d.ts +20 -0
  100. package/lib/src/common/fees/processInfoServerFees.d.ts +7 -0
  101. package/lib/src/common/fees/processMempoolSpaceFees.d.ts +13 -0
  102. package/lib/src/common/plugin/makeCurrencyPlugin.d.ts +3 -0
  103. package/lib/src/common/plugin/makeCurrencyTools.d.ts +8 -0
  104. package/lib/src/common/plugin/makeEngineEmitter.d.ts +23 -0
  105. package/lib/src/common/plugin/makeMetadata.d.ts +14 -0
  106. package/lib/src/common/plugin/pluginState.d.ts +34 -0
  107. package/lib/src/common/plugin/serverScores.d.ts +61 -0
  108. package/lib/src/common/plugin/types.d.ts +135 -0
  109. package/lib/src/common/plugin/utils.d.ts +6 -0
  110. package/lib/src/common/utxobased/db/Models/ProcessorTransaction.d.ts +16 -0
  111. package/lib/src/common/utxobased/db/Models/baselet.d.ts +28 -0
  112. package/lib/src/common/utxobased/db/makeBaselets.d.ts +33 -0
  113. package/lib/src/common/utxobased/db/makeMutex.d.ts +3 -0
  114. package/lib/src/common/utxobased/db/makeProcessor.d.ts +42 -0
  115. package/lib/src/common/utxobased/db/types.d.ts +47 -0
  116. package/lib/src/common/utxobased/engine/await-lock.d.ts +29 -0
  117. package/lib/src/common/utxobased/engine/constants.d.ts +4 -0
  118. package/lib/src/common/utxobased/engine/makeServerStates.d.ts +37 -0
  119. package/lib/src/common/utxobased/engine/makeUtxoEngine.d.ts +3 -0
  120. package/lib/src/common/utxobased/engine/makeUtxoEngineState.d.ts +108 -0
  121. package/lib/src/common/utxobased/engine/makeUtxoWalletTools.d.ts +52 -0
  122. package/lib/src/common/utxobased/engine/paymentRequest.d.ts +18 -0
  123. package/lib/src/common/utxobased/engine/types.d.ts +19 -0
  124. package/lib/src/common/utxobased/engine/util/defaultValidateConfirmations.d.ts +3 -0
  125. package/lib/src/common/utxobased/engine/util/getOwnUtxosFromTx.d.ts +4 -0
  126. package/lib/src/common/utxobased/engine/utils.d.ts +43 -0
  127. package/lib/src/common/utxobased/info/all.d.ts +22 -0
  128. package/lib/src/common/utxobased/info/badcoin.d.ts +3 -0
  129. package/lib/src/common/utxobased/info/bitcoin.d.ts +3 -0
  130. package/lib/src/common/utxobased/info/bitcoincash.d.ts +3 -0
  131. package/lib/src/common/utxobased/info/bitcoincashtestnet.d.ts +3 -0
  132. package/lib/src/common/utxobased/info/bitcoingold.d.ts +3 -0
  133. package/lib/src/common/utxobased/info/bitcoingoldtestnet.d.ts +3 -0
  134. package/lib/src/common/utxobased/info/bitcoinsv.d.ts +3 -0
  135. package/lib/src/common/utxobased/info/bitcointestnet.d.ts +6 -0
  136. package/lib/src/common/utxobased/info/dash.d.ts +3 -0
  137. package/lib/src/common/utxobased/info/digibyte.d.ts +3 -0
  138. package/lib/src/common/utxobased/info/dogecoin.d.ts +3 -0
  139. package/lib/src/common/utxobased/info/eboost.d.ts +3 -0
  140. package/lib/src/common/utxobased/info/feathercoin.d.ts +6 -0
  141. package/lib/src/common/utxobased/info/groestlcoin.d.ts +3 -0
  142. package/lib/src/common/utxobased/info/litecoin.d.ts +6 -0
  143. package/lib/src/common/utxobased/info/qtum.d.ts +3 -0
  144. package/lib/src/common/utxobased/info/ravencoin.d.ts +3 -0
  145. package/lib/src/common/utxobased/info/scriptTemplates/bitcoincashScriptTemplates.d.ts +2 -0
  146. package/lib/src/common/utxobased/info/scriptTemplates/types.d.ts +9 -0
  147. package/lib/src/common/utxobased/info/smartcash.d.ts +3 -0
  148. package/lib/src/common/utxobased/info/ufo.d.ts +3 -0
  149. package/lib/src/common/utxobased/info/utxoPickers/dogeUtxoPicker.d.ts +2 -0
  150. package/lib/src/common/utxobased/info/vertcoin.d.ts +3 -0
  151. package/lib/src/common/utxobased/info/zcoin.d.ts +6 -0
  152. package/lib/src/common/utxobased/keymanager/base.d.ts +9 -0
  153. package/lib/src/common/utxobased/keymanager/bip69.d.ts +19 -0
  154. package/lib/src/common/utxobased/keymanager/bitcoincashUtils/base32.d.ts +19 -0
  155. package/lib/src/common/utxobased/keymanager/bitcoincashUtils/bn.d.ts +2 -0
  156. package/lib/src/common/utxobased/keymanager/bitcoincashUtils/cashAddress.d.ts +12 -0
  157. package/lib/src/common/utxobased/keymanager/cleaners.d.ts +55 -0
  158. package/lib/src/common/utxobased/keymanager/coinmapper.d.ts +2 -0
  159. package/lib/src/common/utxobased/keymanager/keymanager.d.ts +198 -0
  160. package/lib/src/common/utxobased/keymanager/utxopicker/accumulative.d.ts +2 -0
  161. package/lib/src/common/utxobased/keymanager/utxopicker/forceUseUtxo.d.ts +2 -0
  162. package/lib/src/common/utxobased/keymanager/utxopicker/subtractFee.d.ts +2 -0
  163. package/lib/src/common/utxobased/keymanager/utxopicker/types.d.ts +36 -0
  164. package/lib/src/common/utxobased/keymanager/utxopicker/utils.d.ts +15 -0
  165. package/lib/src/common/utxobased/keymanager/utxopicker.d.ts +8 -0
  166. package/lib/src/common/utxobased/network/BlockBook.d.ts +33 -0
  167. package/lib/src/common/utxobased/network/BlockBookAPI.d.ts +163 -0
  168. package/lib/src/common/utxobased/network/Deferred.d.ts +8 -0
  169. package/lib/src/common/utxobased/network/MakeSocketEmitter.d.ts +13 -0
  170. package/lib/src/common/utxobased/network/Socket.d.ts +41 -0
  171. package/lib/src/common/utxobased/network/nodejsWS.d.ts +2 -0
  172. package/lib/src/common/utxobased/network/socketQueue.d.ts +8 -0
  173. package/lib/src/common/utxobased/network/types.d.ts +17 -0
  174. package/lib/src/common/utxobased/network/windowWS.d.ts +2 -0
  175. package/lib/src/firo.d.ts +3 -0
  176. package/lib/src/ftc.d.ts +3 -0
  177. package/lib/src/index.d.ts +4 -0
  178. package/lib/src/ltc.d.ts +3 -0
  179. package/lib/src/tbch.d.ts +3 -0
  180. package/lib/src/tbtc.d.ts +3 -0
  181. package/lib/src/tbtg.d.ts +3 -0
  182. package/lib/src/types.d.ts +6 -0
  183. package/lib/src/util/delay.d.ts +1 -0
  184. package/lib/src/util/filterUndefined.d.ts +1 -0
  185. package/lib/src/util/indexAtProtected.d.ts +5 -0
  186. package/lib/src/util/typeUtil.d.ts +8 -0
  187. package/lib/src/util/undefinedIfEmptyString.d.ts +1 -0
  188. package/lib/src/util/unixTime.d.ts +1 -0
  189. package/lib/tbch.js +15 -0
  190. package/lib/tbtc.js +15 -0
  191. package/lib/tbtg.js +15 -0
  192. package/lib/test/common/fees/calcMinerFeePerByte.spec.d.ts +1 -0
  193. package/lib/test/common/fees/fees.spec.d.ts +1 -0
  194. package/lib/test/common/plugin/currencyPlugin.fixtures/common.d.ts +29 -0
  195. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/bitcoin.d.ts +2 -0
  196. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/bitcoincash.d.ts +2 -0
  197. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/bitcoinsv.d.ts +2 -0
  198. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/digibyte.d.ts +2 -0
  199. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/feathercoin.d.ts +2 -0
  200. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/groestlcoin.d.ts +2 -0
  201. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/litecoin.d.ts +2 -0
  202. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/smartcash.d.ts +2 -0
  203. package/lib/test/common/plugin/currencyPlugin.fixtures/currencies/zcoin.d.ts +2 -0
  204. package/lib/test/common/plugin/currencyPlugin.fixtures/index.d.ts +2 -0
  205. package/lib/test/common/plugin/currencyPlugin.spec.d.ts +1 -0
  206. package/lib/test/common/plugin/makeMetadata.spec.d.ts +1 -0
  207. package/lib/test/common/plugin/serverScores.spec.d.ts +1 -0
  208. package/lib/test/common/utxobased/addressFormat/addressFormatIndex/addressFormat.spec.d.ts +1 -0
  209. package/lib/test/common/utxobased/db/Processor.spec.d.ts +1 -0
  210. package/lib/test/common/utxobased/engine/PaymentRequest.spec.d.ts +1 -0
  211. package/lib/test/common/utxobased/engine/UtxoWalletTools.spec.d.ts +1 -0
  212. package/lib/test/common/utxobased/engine/engine.fixtures/bitcoinTestnet/tests.d.ts +70 -0
  213. package/lib/test/common/utxobased/engine/engine.fixtures/index.d.ts +65 -0
  214. package/lib/test/common/utxobased/engine/engine.spec.d.ts +1 -0
  215. package/lib/test/common/utxobased/keymanager/coins/altcointestfixtures.d.ts +54 -0
  216. package/lib/test/common/utxobased/keymanager/coins/bitcoincashtransactiontest.spec.d.ts +1 -0
  217. package/lib/test/common/utxobased/keymanager/coins/bitcointransactiontest.spec.d.ts +1 -0
  218. package/lib/test/common/utxobased/keymanager/coins/cashaddr.spec.d.ts +1 -0
  219. package/lib/test/common/utxobased/keymanager/coins/checkdatasig.spec.d.ts +1 -0
  220. package/lib/test/common/utxobased/keymanager/coins/groestlcointransactiontest.spec.d.ts +1 -0
  221. package/lib/test/common/utxobased/keymanager/coins/keymanagertest.spec.d.ts +1 -0
  222. package/lib/test/common/utxobased/network/BlockBook.spec.d.ts +1 -0
  223. package/lib/test/util/objectKeys.d.ts +8 -0
  224. package/lib/test/util/testLog.d.ts +3 -0
  225. package/lib/test/utils.d.ts +10 -0
  226. package/lib/types.js +7 -0
  227. package/lib/util/delay.js +4 -0
  228. package/lib/util/filterUndefined.js +4 -0
  229. package/lib/util/indexAtProtected.js +9 -0
  230. package/lib/util/typeUtil.js +9 -0
  231. package/lib/util/undefinedIfEmptyString.js +2 -0
  232. package/lib/util/unixTime.js +2 -0
  233. package/package.json +101 -0
@@ -0,0 +1,67 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+ const makeIAddress = (
17
+ addressFields
18
+ ) => {
19
+ const { scriptPubkey, used = false, ...rest } = addressFields
20
+
21
+ return {
22
+ scriptPubkey,
23
+ used,
24
+ lastQueriedBlockHeight: 0,
25
+ lastQuery: 0,
26
+ lastTouched: 0,
27
+ balance: '0',
28
+ ...rest
29
+ }
30
+ }; exports.makeIAddress = makeIAddress
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
@@ -0,0 +1,65 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});/**
2
+ * A mutex lock for coordination across async functions
3
+ */
4
+ class AwaitLock {constructor() { AwaitLock.prototype.__init.call(this);AwaitLock.prototype.__init2.call(this); }
5
+ __init() {this._acquired = false}
6
+ __init2() {this._waitingResolvers = []}
7
+
8
+ /**
9
+ * Whether the lock is currently acquired or not. Accessing this property does not affect the
10
+ * status of the lock.
11
+ */
12
+ get acquired() {
13
+ return this._acquired
14
+ }
15
+
16
+ /**
17
+ * Acquires the lock, waiting if necessary for it to become free if it is already locked. The
18
+ * returned promise is fulfilled once the lock is acquired.
19
+ *
20
+ * After acquiring the lock, you **must** call `release` when you are done with it.
21
+ */
22
+ async acquireAsync() {
23
+ if (!this._acquired) {
24
+ this._acquired = true
25
+ return await Promise.resolve()
26
+ }
27
+
28
+ return await new Promise(resolve => {
29
+ this._waitingResolvers.push(resolve)
30
+ })
31
+ }
32
+
33
+ /**
34
+ * Acquires the lock if it is free and otherwise returns immediately without waiting. Returns
35
+ * `true` if the lock was free and is now acquired, and `false` otherwise,
36
+ */
37
+ tryAcquire() {
38
+ if (!this._acquired) {
39
+ this._acquired = true
40
+ return true
41
+ }
42
+
43
+ return false
44
+ }
45
+
46
+ /**
47
+ * Releases the lock and gives it to the next waiting acquirer, if there is one. Each acquirer
48
+ * must release the lock exactly once.
49
+ */
50
+ release() {
51
+ if (!this._acquired) {
52
+ throw new Error(`Cannot release an unacquired lock`)
53
+ }
54
+
55
+ if (this._waitingResolvers.length > 0) {
56
+ const resolve = this._waitingResolvers.shift()
57
+ if (resolve == null) {
58
+ throw new Error('Cannot release null lock')
59
+ }
60
+ resolve()
61
+ } else {
62
+ this._acquired = false
63
+ }
64
+ }
65
+ } exports.default = AwaitLock;
@@ -0,0 +1,7 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); const BLOCKBOOK_TXS_PER_PAGE = 100; exports.BLOCKBOOK_TXS_PER_PAGE = BLOCKBOOK_TXS_PER_PAGE
2
+
3
+ const CACHE_THROTTLE = 0.05; exports.CACHE_THROTTLE = CACHE_THROTTLE
4
+
5
+ const MAX_CONNECTIONS = 2; exports.MAX_CONNECTIONS = MAX_CONNECTIONS
6
+
7
+ const NEW_CONNECTIONS = 8; exports.NEW_CONNECTIONS = NEW_CONNECTIONS
@@ -0,0 +1,394 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+ var _urijs = require('uri-js');
3
+
4
+ var _makeEngineEmitter = require('../../plugin/makeEngineEmitter');
5
+
6
+
7
+ var _utils = require('../../plugin/utils');
8
+
9
+ var _BlockBook = require('../network/BlockBook');
10
+
11
+ var _Deferred = require('../network/Deferred'); var _Deferred2 = _interopRequireDefault(_Deferred);
12
+ var _MakeSocketEmitter = require('../network/MakeSocketEmitter');
13
+
14
+ var _socketQueue = require('../network/socketQueue');
15
+ var _constants = require('./constants');
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+ function makeServerStates(config) {
63
+ const { engineEmitter, log, pluginInfo, pluginState, walletInfo } = config
64
+ log('Making server states')
65
+
66
+ let serverStates = {}
67
+ let isEngineOn = true
68
+ let connections = {}
69
+ let serverList = []
70
+ let reconnectCounter = 0
71
+ let reconnectTimer = setTimeout(() => {
72
+ return
73
+ }, 0)
74
+ const socketEmitter = new (0, _MakeSocketEmitter.SocketEmitter)()
75
+
76
+ // set server specific event emitters
77
+ socketEmitter.on(_MakeSocketEmitter.SocketEvent.CONNECTION_OPEN, (uri) => {
78
+ reconnectCounter = 0
79
+ log(`${uri} ** Connected **`)
80
+ })
81
+ socketEmitter.on(
82
+ _MakeSocketEmitter.SocketEvent.CONNECTION_CLOSE,
83
+ (uri, error) => {
84
+ _utils.removeItem.call(void 0, connections, uri)
85
+ _utils.removeItem.call(void 0, serverStates, uri)
86
+
87
+ const msg =
88
+ error != null ? ` !! Connection ERROR !! ${error.message}` : ''
89
+ log(`${uri} onClose ${msg}`)
90
+ if (error != null) {
91
+ pluginState.serverScoreDown(uri)
92
+ }
93
+ reconnect()
94
+ }
95
+ )
96
+ socketEmitter.on(
97
+ _MakeSocketEmitter.SocketEvent.CONNECTION_TIMER,
98
+ (uri, queryDate) => {
99
+ const queryTime = Date.now() - queryDate
100
+ log(`${uri} returned healthCheck in ${queryTime}ms`)
101
+ pluginState.serverScoreUp(uri, queryTime)
102
+ }
103
+ )
104
+ engineEmitter.on(
105
+ _makeEngineEmitter.EngineEvent.NEW_ADDRESS_TRANSACTION,
106
+ (uri, newTx) => {
107
+ log(
108
+ `${uri} received received new transaction with id ${newTx.tx.txid} to address ${newTx.address}`
109
+ )
110
+ const serverState = serverStates[uri]
111
+ if (serverState != null) {
112
+ serverState.txids.add(newTx.tx.txid)
113
+ }
114
+ }
115
+ )
116
+ engineEmitter.on(
117
+ _makeEngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED,
118
+ (uri, blockHeight) => {
119
+ log(`${uri} block height changed to ${blockHeight}`)
120
+ serverStates[uri].blockHeight = blockHeight
121
+ }
122
+ )
123
+
124
+ let pickNextTaskCB
125
+
126
+
127
+
128
+
129
+ const makeServerState = () => ({
130
+ blockSubscriptionStatus: 'unsubscribed',
131
+ txids: new Set(),
132
+ addresses: new Set(),
133
+ blockHeight: 0
134
+ })
135
+ const setPickNextTaskCB = (
136
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
137
+ callback
138
+ ) => {
139
+ pickNextTaskCB = callback
140
+ }
141
+
142
+ const stop = async () => {
143
+ isEngineOn = false
144
+ log(`stopping server states`)
145
+ _socketQueue.removeIdFromQueue.call(void 0, walletInfo.id)
146
+ clearTimeout(reconnectTimer)
147
+ for (const uri of Object.keys(connections)) {
148
+ const blockBook = connections[uri]
149
+ if (blockBook == null) continue
150
+ await blockBook.disconnect()
151
+ }
152
+ connections = {}
153
+ serverStates = {}
154
+ }
155
+
156
+ const reconnect = () => {
157
+ if (isEngineOn) {
158
+ log(`attempting server reconnect number ${reconnectCounter}`)
159
+ const reconnectionDelay = Math.max(5, reconnectCounter++) * 1000
160
+ reconnectTimer = setTimeout(() => {
161
+ clearTimeout(reconnectTimer)
162
+ refillServers()
163
+ }, reconnectionDelay)
164
+ }
165
+ }
166
+
167
+ const refillServers = () => {
168
+ log(`refilling servers...`)
169
+ _socketQueue.pushUpdate.call(void 0, {
170
+ id: walletInfo.id,
171
+ updateFunc: () => {
172
+ doRefillServers()
173
+ }
174
+ })
175
+ }
176
+
177
+ const doRefillServers = () => {
178
+ const includePatterns = ['wss:', 'ws:']
179
+ if (serverList.length === 0) {
180
+ serverList = pluginState.getLocalServers(_constants.NEW_CONNECTIONS, includePatterns)
181
+ }
182
+ log(`refillServers: Top ${_constants.NEW_CONNECTIONS} servers:`, serverList)
183
+ let chanceToBePicked = 1.25
184
+ while (Object.keys(connections).length < _constants.MAX_CONNECTIONS) {
185
+ if (serverList.length === 0) break
186
+ const uri = serverList.shift()
187
+ if (uri == null) {
188
+ reconnect()
189
+ break
190
+ }
191
+ if (connections[uri] != null) {
192
+ continue
193
+ }
194
+ // Validate the URI of server to make sure it is valid
195
+ const parsed = _urijs.parse.call(void 0, uri)
196
+ if (
197
+ parsed.scheme == null ||
198
+ parsed.scheme.length < 2 ||
199
+ parsed.host == null
200
+ ) {
201
+ continue
202
+ }
203
+ chanceToBePicked -= chanceToBePicked > 0.5 ? 0.25 : 0
204
+ if (Math.random() > chanceToBePicked) {
205
+ serverList.push(uri)
206
+ continue
207
+ }
208
+
209
+ serverStates[uri] = makeServerState()
210
+
211
+ const onQueueSpaceCB = async (
212
+
213
+
214
+ ) => {
215
+ const blockBook = connections[uri]
216
+ if (blockBook == null) {
217
+ return
218
+ }
219
+ const task = await pickNextTaskCB(uri)
220
+ if (task != null && typeof task !== 'boolean') {
221
+ const taskMessage = `${task.method} params: ${JSON.stringify(
222
+ task.params
223
+ )}`
224
+ log(`${uri} nextTask: ${taskMessage}`)
225
+ }
226
+ return task
227
+ }
228
+
229
+ connections[uri] = _BlockBook.makeBlockBook.call(void 0, {
230
+ wsAddress: uri,
231
+ socketEmitter,
232
+ engineEmitter,
233
+ log,
234
+ onQueueSpaceCB,
235
+ walletId: walletInfo.id,
236
+ asAddress: pluginInfo.engineInfo.asBlockbookAddress
237
+ })
238
+
239
+ const blockBook = connections[uri]
240
+ if (blockBook == null) continue
241
+
242
+ // Initialize blockbook connection for server
243
+ blockBook
244
+ .connect()
245
+ .then(async () => {
246
+ // Fetch block height from blockbook server
247
+ const startTime = Date.now()
248
+ const { bestHeight: blockHeight } = await blockBook.fetchInfo()
249
+ log('height:', blockHeight)
250
+
251
+ // Update server state
252
+ serverStates[uri].blockHeight = blockHeight
253
+
254
+ // Emit initial BLOCK_HEIGHT_CHANGED event
255
+ engineEmitter.emit(_makeEngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED, uri, blockHeight)
256
+
257
+ // Increment server score using response time
258
+ const responseTime = Date.now() - startTime
259
+ pluginState.serverScoreUp(uri, responseTime)
260
+ })
261
+ .catch(e => {
262
+ log.error(`${JSON.stringify(e.message)}`)
263
+ })
264
+ }
265
+ }
266
+
267
+ const serverCanGetTx = (uri, txid) => {
268
+ const serverState = serverStates[uri]
269
+ if (serverState == null) return false
270
+ if (serverState.txids.has(txid)) return true
271
+
272
+ for (const state of Object.values(serverStates)) {
273
+ if (state.txids.has(txid)) return false
274
+ }
275
+ return true
276
+ }
277
+
278
+ const serverCanGetAddress = (uri, address) => {
279
+ const serverState = serverStates[uri]
280
+ if (serverState == null) return false
281
+ if (serverState.addresses.has(address)) return true
282
+
283
+ for (const state of Object.values(serverStates)) {
284
+ if (state.addresses.has(address)) return false
285
+ }
286
+ return true
287
+ }
288
+
289
+ const serverScoreUp = (uri, score) => {
290
+ pluginState.serverScoreUp(uri, score)
291
+ }
292
+
293
+ const getServerState = (uri) => {
294
+ return serverStates[uri]
295
+ }
296
+
297
+ const getServerList = () => {
298
+ return serverList
299
+ }
300
+
301
+ const setServerList = (updatedServerList) => {
302
+ serverList = updatedServerList
303
+ }
304
+
305
+ const broadcastTx = async (transaction) => {
306
+ return await new Promise((resolve, reject) => {
307
+ const uris = Object.keys(connections).filter(uri => {
308
+ const blockBook = connections[uri]
309
+ if (blockBook == null) return false
310
+ return blockBook.isConnected
311
+ })
312
+ if (uris == null || uris.length < 1) {
313
+ reject(
314
+ new Error('No available connections\nCheck your internet signal')
315
+ )
316
+ }
317
+ let resolved = false
318
+ let bad = 0
319
+ for (const uri of uris) {
320
+ const blockBook = connections[uri]
321
+ if (blockBook == null) continue
322
+ blockBook
323
+ .broadcastTx(transaction)
324
+ .then(response => {
325
+ if (!resolved) {
326
+ resolved = true
327
+ resolve(response.result)
328
+ }
329
+ })
330
+ .catch((e) => {
331
+ if (++bad === uris.length) {
332
+ const msg = e != null ? `With error ${e.message}` : ''
333
+ log.error(
334
+ `broadcastTx fail: ${JSON.stringify(transaction)}\n${msg}`
335
+ )
336
+ reject(e)
337
+ }
338
+ })
339
+ }
340
+ })
341
+ }
342
+
343
+ const watchAddresses = (
344
+ uri,
345
+ addresses,
346
+ deferredAddressSub
347
+ ) => {
348
+ const blockbook = connections[uri]
349
+ if (blockbook == null)
350
+ throw new Error(`No blockbook connection with ${uri}`)
351
+ blockbook.watchAddresses(addresses, deferredAddressSub)
352
+ }
353
+
354
+ const watchBlocks = (uri) => {
355
+ const blockbook = connections[uri]
356
+ if (blockbook == null)
357
+ throw new Error(`No blockbook connection with ${uri}`)
358
+
359
+ const serverState = _nullishCoalesce(serverStates[uri], () => ( makeServerState()))
360
+ serverState.blockSubscriptionStatus = 'subscribing'
361
+
362
+ const queryTime = Date.now()
363
+ const deferred = new (0, _Deferred2.default)()
364
+ deferred.promise
365
+ .then(() => {
366
+ serverState.blockSubscriptionStatus = 'subscribed'
367
+ serverScoreUp(uri, Date.now() - queryTime)
368
+ })
369
+ .catch(() => {
370
+ serverState.blockSubscriptionStatus = 'unsubscribed'
371
+ })
372
+ blockbook.watchBlocks(deferred)
373
+ }
374
+
375
+ const getBlockHeight = (uri) => {
376
+ return serverStates[uri].blockHeight
377
+ }
378
+
379
+ return {
380
+ setPickNextTaskCB,
381
+ stop,
382
+ serverCanGetTx,
383
+ serverCanGetAddress,
384
+ serverScoreUp,
385
+ getServerState,
386
+ refillServers,
387
+ getServerList,
388
+ setServerList,
389
+ broadcastTx,
390
+ watchAddresses,
391
+ watchBlocks,
392
+ getBlockHeight
393
+ }
394
+ } exports.makeServerStates = makeServerStates;