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,123 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _asyncmutex = require('async-mutex');
2
+
3
+
4
+
5
+
6
+ var _baselet = require('baselet');
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+ var _baselet3 = require('./Models/baselet');
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
+
63
+
64
+
65
+
66
+ const makeBaselets = async (
67
+ config
68
+ ) => {
69
+ /* Tables */
70
+ const addressBases = {
71
+ addressByScriptPubkey: await _baselet.createOrOpenHashBase.call(void 0,
72
+ config.disklet,
73
+ _baselet3.addressByScriptPubkeyOptions
74
+ ),
75
+ scriptPubkeyByPath: await _baselet.createOrOpenCountBase.call(void 0,
76
+ config.disklet,
77
+ _baselet3.scriptPubkeyByPathOptions
78
+ ),
79
+ lastUsedByFormatPath: await _baselet.createOrOpenHashBase.call(void 0,
80
+ config.disklet,
81
+ _baselet3.lastUsedByFormatPathOptions
82
+ )
83
+ }
84
+ const txBases = {
85
+ txById: await _baselet.createOrOpenHashBase.call(void 0, config.disklet, _baselet3.txByIdOptions),
86
+ txIdsByBlockHeight: await _baselet.createOrOpenRangeBase.call(void 0,
87
+ config.disklet,
88
+ _baselet3.txIdsByBlockHeightOptions
89
+ ),
90
+ txIdsByDate: await _baselet.createOrOpenRangeBase.call(void 0, config.disklet, _baselet3.txIdsByDateOptions)
91
+ }
92
+ const utxoBases = {
93
+ utxoById: await _baselet.createOrOpenHashBase.call(void 0, config.disklet, _baselet3.utxoByIdOptions),
94
+ utxoIdsByScriptPubkey: await _baselet.createOrOpenHashBase.call(void 0,
95
+ config.disklet,
96
+ _baselet3.utxoIdsByScriptPubkeyOptions
97
+ )
98
+ }
99
+
100
+ const addressMutex = new (0, _asyncmutex.Mutex)()
101
+ const txMutex = new (0, _asyncmutex.Mutex)()
102
+ const utxoMutex = new (0, _asyncmutex.Mutex)()
103
+
104
+ return {
105
+ async address(fn) {
106
+ return await addressMutex.runExclusive(async () => await fn(addressBases))
107
+ },
108
+
109
+ async tx(fn) {
110
+ return await txMutex.runExclusive(async () => await fn(txBases))
111
+ },
112
+
113
+ async utxo(fn) {
114
+ return await utxoMutex.runExclusive(async () => await fn(utxoBases))
115
+ },
116
+
117
+ all: {
118
+ ...addressBases,
119
+ ...txBases,
120
+ ...utxoBases
121
+ }
122
+ }
123
+ }; exports.makeBaselets = makeBaselets
@@ -0,0 +1,17 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ function makeMutex() {
4
+ let busy = false
5
+ const queue = []
6
+ return async function lock(callback) {
7
+ if (busy) await new Promise(resolve => queue.push(resolve))
8
+ try {
9
+ busy = true
10
+ return callback()
11
+ } finally {
12
+ busy = false
13
+ const resolve = queue.shift()
14
+ if (resolve != null) resolve({})
15
+ }
16
+ }
17
+ } exports.makeMutex = makeMutex;
@@ -0,0 +1,554 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _baselet = require('baselet');
2
+ var _biggystring = require('biggystring'); var bs = _interopRequireWildcard(_biggystring);
3
+ var _disklet = require('disklet');
4
+
5
+
6
+ var _unixTime = require('../../../util/unixTime');
7
+
8
+ var _makeBaselets = require('./makeBaselets');
9
+ var _baselet3 = require('./Models/baselet');
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+ const BASELET_DIR = 'tables'
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
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+ async function makeProcessor(
111
+ config
112
+ ) {
113
+ const disklet = _disklet.navigateDisklet.call(void 0, config.disklet, BASELET_DIR)
114
+ let baselets = await _makeBaselets.makeBaselets.call(void 0, { disklet })
115
+
116
+ /**
117
+ * Calculates the transaction value supplied (negative) or received (positive). In order to calculate
118
+ * a value, the `ourIns` and `ourOuts` of the object must be populated with indices.
119
+ * @param tx {IProcessorTransaction} A transaction object with `ourIns` and `ourOuts` populated
120
+ */
121
+ const calculateTxAmount = (tx) => {
122
+
123
+
124
+
125
+ let ourAmount = '0'
126
+ let txIndexMap = {}
127
+ for (const input of tx.inputs) {
128
+ txIndexMap[input.n.toString()] = input
129
+ }
130
+ for (const i of tx.ourIns) {
131
+ const input = txIndexMap[i]
132
+ ourAmount = bs.sub(ourAmount, input.amount)
133
+ }
134
+ txIndexMap = {}
135
+ for (const output of tx.outputs) {
136
+ txIndexMap[output.n.toString()] = output
137
+ }
138
+ for (const i of tx.ourOuts) {
139
+ const output = txIndexMap[i]
140
+ ourAmount = bs.add(ourAmount, output.amount)
141
+ }
142
+ return ourAmount
143
+ }
144
+
145
+ const processor = {
146
+ async clearAll() {
147
+ await _baselet.clearMemletCache.call(void 0, )
148
+ // why is this delay needed?
149
+ await new Promise(resolve => setTimeout(resolve, 0))
150
+ await disklet.delete('.')
151
+ baselets = await _makeBaselets.makeBaselets.call(void 0, { disklet })
152
+ },
153
+
154
+ async dumpData() {
155
+ const allBases = Object.values(baselets.all)
156
+ return await Promise.all(
157
+ allBases.map(async base => ({
158
+ databaseName: base.databaseName,
159
+ data: (await base.dumpData(''))
160
+ }))
161
+ )
162
+ },
163
+
164
+ async saveUtxo(utxo) {
165
+ return await baselets.utxo(async tables => {
166
+ const [utxoIds = []] = await tables.utxoIdsByScriptPubkey.query('', [
167
+ utxo.scriptPubkey
168
+ ])
169
+ if (!utxoIds.includes(utxo.id)) {
170
+ utxoIds.push(utxo.id)
171
+ await tables.utxoIdsByScriptPubkey.insert(
172
+ '',
173
+ utxo.scriptPubkey,
174
+ utxoIds
175
+ )
176
+ }
177
+
178
+ await tables.utxoById.insert('', utxo.id, utxo)
179
+ })
180
+ },
181
+
182
+ async removeUtxos(utxoIds) {
183
+ return await baselets.utxo(async tables => {
184
+ if (utxoIds.length === 0) return
185
+
186
+ // To remove utxo from utxoIdsByScriptPubkey table:
187
+ // 1. Create a map of scriptPubkeys to utxoIds
188
+ // 2. Use the map to remove utxoIds from the utxoIdsByScriptPubkey table
189
+ const utxos = (await tables.utxoById.query('', utxoIds)).filter(
190
+ utxo => utxo != null
191
+ )
192
+ const utxoIdsMap = {}
193
+ for (const utxo of utxos) {
194
+ utxoIdsMap[utxo.scriptPubkey] = [
195
+ ...(_nullishCoalesce(utxoIdsMap[utxo.scriptPubkey], () => ( []))),
196
+ utxo.id
197
+ ]
198
+ }
199
+ for (const scriptPubkey of Object.keys(utxoIdsMap)) {
200
+ const utxoIdsToRemove = utxoIdsMap[scriptPubkey]
201
+ const [utxoIds = []] = await tables.utxoIdsByScriptPubkey.query('', [
202
+ scriptPubkey
203
+ ])
204
+
205
+ for (const utxoIdToRemove of utxoIdsToRemove) {
206
+ utxoIds.splice(utxoIds.indexOf(utxoIdToRemove), 1)
207
+ }
208
+
209
+ // Update utxoIds for entry in table, otherwise delete entry
210
+ if (utxoIds.length > 0) {
211
+ await tables.utxoIdsByScriptPubkey.insert('', scriptPubkey, utxoIds)
212
+ } else {
213
+ await tables.utxoIdsByScriptPubkey.delete('', [scriptPubkey])
214
+ }
215
+ }
216
+
217
+ // Remove utxo utxoById table
218
+ await tables.utxoById.delete('', utxoIds)
219
+ })
220
+ },
221
+
222
+ async fetchUtxos(args) {
223
+ const { scriptPubkey, utxoIds = [] } = args
224
+ return await baselets.utxo(async tables => {
225
+ if (scriptPubkey != null) {
226
+ const [
227
+ utxoIdsByScriptPubkey
228
+ ] = await tables.utxoIdsByScriptPubkey.query('', [scriptPubkey])
229
+
230
+ if (utxoIdsByScriptPubkey != null)
231
+ utxoIds.push(...utxoIdsByScriptPubkey)
232
+
233
+ // Exit early if no utxoIds are found by scriptPubkey
234
+ if (utxoIds.length === 0) {
235
+ return []
236
+ }
237
+ }
238
+
239
+ // Return all UTXOs if no UTXO ids are specified
240
+ if (utxoIds.length === 0) {
241
+ const { data } = await tables.utxoById.dumpData('')
242
+ return Object.values(_nullishCoalesce(data[''], () => ( {})))
243
+ }
244
+
245
+ return await tables.utxoById.query('', utxoIds)
246
+ })
247
+ },
248
+
249
+ async saveTransaction(
250
+ args
251
+ ) {
252
+ const { scriptPubkeys = [], tx } = args
253
+ return await baselets.tx(async tables => {
254
+ // Check if the transaction already exists
255
+ const processorTx = await tables.txById
256
+ .query('', [tx.txid])
257
+ .then(transactions => transactions[0])
258
+ .catch(_ => undefined)
259
+
260
+ // Use the existing transaction if it does exist.
261
+ const transaction = _nullishCoalesce(processorTx, () => ( tx))
262
+
263
+ // Mark the used inputs with the provided script pubkey
264
+ for (const scriptPubkey of scriptPubkeys) {
265
+ for (const input of transaction.inputs) {
266
+ if (input.scriptPubkey === scriptPubkey) {
267
+ if (!transaction.ourIns.includes(input.n.toString())) {
268
+ transaction.ourIns.push(input.n.toString())
269
+ }
270
+ }
271
+ }
272
+ for (const output of transaction.outputs) {
273
+ if (output.scriptPubkey === scriptPubkey) {
274
+ if (!transaction.ourOuts.includes(output.n.toString())) {
275
+ transaction.ourOuts.push(output.n.toString())
276
+ }
277
+ }
278
+ }
279
+ transaction.ourAmount = calculateTxAmount(transaction)
280
+ }
281
+
282
+ if (transaction.blockHeight !== tx.blockHeight) {
283
+ // the transaction already exists, so delete it and re-insert at a different blockHeight
284
+ await tables.txIdsByBlockHeight.delete(
285
+ '',
286
+ transaction.blockHeight,
287
+ transaction.txid
288
+ )
289
+ transaction.blockHeight = tx.blockHeight
290
+ await tables.txIdsByBlockHeight.insert('', {
291
+ txid: transaction.txid,
292
+ blockHeight: transaction.blockHeight
293
+ })
294
+ } else {
295
+ // Save tx by blockheight
296
+ const txIdsByTransactionBlockHeight = await (
297
+ await tables.txIdsByBlockHeight.query('', transaction.blockHeight)
298
+ )
299
+ .reverse()
300
+ .map(({ txid: id }) => id)
301
+ if (!txIdsByTransactionBlockHeight.includes(transaction.txid)) {
302
+ await tables.txIdsByBlockHeight.insert('', {
303
+ txid: transaction.txid,
304
+ blockHeight: transaction.blockHeight
305
+ })
306
+ }
307
+ }
308
+
309
+ // Save transaction
310
+ await tables.txById.insert('', transaction.txid, transaction)
311
+
312
+ // Save index entry only for first transaction insert
313
+ if (processorTx == null) {
314
+ await tables.txIdsByDate.insert('', {
315
+ txid: tx.txid,
316
+ date: tx.date
317
+ })
318
+ }
319
+
320
+ return transaction
321
+ })
322
+ },
323
+
324
+ numTransactions() {
325
+ return baselets.all.txIdsByDate.size('')
326
+ },
327
+
328
+ async removeTransaction(_txId) {
329
+ return
330
+ },
331
+
332
+ async fetchTransactions(
333
+ args
334
+ ) {
335
+ const { blockHeightMax, txId, options } = args
336
+ let { blockHeight } = args
337
+ const txs = []
338
+ await baselets.tx(async tables => {
339
+ // Fetch transactions by id
340
+ if (txId != null) {
341
+ const txById = await tables.txById.query('', [txId])
342
+ txs.push(...txById)
343
+ }
344
+ // Fetch transactions by min block height
345
+ if (blockHeightMax != null && blockHeight == null) blockHeight = 0
346
+ if (blockHeight != null) {
347
+ const txIdsByMinBlockHeight = await (
348
+ await tables.txIdsByBlockHeight.query(
349
+ '',
350
+ blockHeight,
351
+ blockHeightMax
352
+ )
353
+ )
354
+ .reverse()
355
+ .map(({ txid: id }) => id)
356
+
357
+ const txsById = await tables.txById.query('', txIdsByMinBlockHeight)
358
+ txs.push(...txsById)
359
+ }
360
+ if (options != null) {
361
+ const {
362
+ startEntries,
363
+ startIndex,
364
+ startDate = new Date(0),
365
+ endDate = new Date()
366
+ } = options
367
+
368
+ // Fetch transaction IDs ordered by date
369
+ let txData
370
+ if (startEntries != null && startIndex != null) {
371
+ txData = await tables.txIdsByDate.queryByCount(
372
+ '',
373
+ startEntries,
374
+ startIndex
375
+ )
376
+ } else {
377
+ txData = await tables.txIdsByDate.query(
378
+ '',
379
+ _unixTime.unixTime.call(void 0, startDate.getTime()),
380
+ _unixTime.unixTime.call(void 0, endDate.getTime())
381
+ )
382
+ }
383
+ const txIdsByOptions = await txData
384
+ .reverse()
385
+ .map(({ txid: id }) => id)
386
+
387
+ const txsByOptions = await tables.txById.query('', txIdsByOptions)
388
+ // Make sure only existing transactions are included
389
+ txs.push(...txsByOptions.filter(tx => tx != null))
390
+ }
391
+ })
392
+ return txs
393
+ },
394
+
395
+ async saveAddress(address) {
396
+ await baselets.address(async tables => {
397
+ const [existingAddress] = await tables.addressByScriptPubkey.query('', [
398
+ address.scriptPubkey
399
+ ])
400
+
401
+ // save to the path index if available
402
+ if (address.path != null) {
403
+ // check if the path already exists with a different script pubkey
404
+ const [scriptPubkey] = await tables.scriptPubkeyByPath.query(
405
+ _baselet3.addressPathToPrefix.call(void 0, address.path),
406
+ address.path.addressIndex
407
+ )
408
+ if (scriptPubkey != null && scriptPubkey !== address.scriptPubkey)
409
+ throw new Error(
410
+ 'Attempted to save address with an existing path, but different script pubkey'
411
+ )
412
+
413
+ await tables.scriptPubkeyByPath.insert(
414
+ _baselet3.addressPathToPrefix.call(void 0, address.path),
415
+ address.path.addressIndex,
416
+ address.scriptPubkey
417
+ )
418
+
419
+ // check if this address is used and if so, whether it has a higher last used index
420
+ if (address.used || (_nullishCoalesce(_optionalChain([existingAddress, 'optionalAccess', _2 => _2.used]), () => ( false)))) {
421
+ let [lastUsed] = await tables.lastUsedByFormatPath.query('', [
422
+ _baselet3.addressPathToPrefix.call(void 0, address.path)
423
+ ])
424
+ if (lastUsed == null) lastUsed = -1
425
+
426
+ if (lastUsed < address.path.addressIndex) {
427
+ await tables.lastUsedByFormatPath.insert(
428
+ '',
429
+ _baselet3.addressPathToPrefix.call(void 0, address.path),
430
+ address.path.addressIndex
431
+ )
432
+ }
433
+ }
434
+ }
435
+
436
+ if (existingAddress != null) {
437
+ // Only update the lastQueriedBlockHeight on the address if one was given and is greater than the existing value
438
+ if (
439
+ address.lastQueriedBlockHeight >
440
+ existingAddress.lastQueriedBlockHeight
441
+ ) {
442
+ existingAddress.lastQueriedBlockHeight =
443
+ address.lastQueriedBlockHeight
444
+ }
445
+
446
+ // Only update the lastQuery value if one was given and is greater than the existing value
447
+ if (address.lastQuery > existingAddress.lastQuery) {
448
+ existingAddress.lastQuery = address.lastQuery
449
+ }
450
+
451
+ // Only update the lastTouched value if one was given and is greater than the existing value
452
+ if (address.lastTouched > existingAddress.lastTouched) {
453
+ existingAddress.lastTouched = address.lastTouched
454
+ }
455
+
456
+ // Only update the balance field if one was given and does not equal the existing value
457
+ if (address.balance !== existingAddress.balance) {
458
+ existingAddress.balance = address.balance
459
+ }
460
+
461
+ // Only update the path field if one was given and currently does not have one
462
+ // NOTE: Addresses can be stored in the db without a path due to the `EdgeCurrencyEngine.addGapLimitAddresses` function
463
+ // Once an address path is known, it should never be updated
464
+ if (address.path != null && existingAddress.path == null) {
465
+ existingAddress.path = address.path
466
+ await tables.scriptPubkeyByPath.insert(
467
+ _baselet3.addressPathToPrefix.call(void 0, address.path),
468
+ address.path.addressIndex,
469
+ address.scriptPubkey
470
+ )
471
+ }
472
+
473
+ // Only update the used flag if one was given and is true
474
+ if (address.used && !existingAddress.used) {
475
+ existingAddress.used = true
476
+ }
477
+
478
+ // check if the lastUsed changed by the update
479
+ if (existingAddress.path != null && existingAddress.used) {
480
+ let [lastUsed] = await tables.lastUsedByFormatPath.query('', [
481
+ _baselet3.addressPathToPrefix.call(void 0, existingAddress.path)
482
+ ])
483
+ if (lastUsed == null) lastUsed = -1
484
+
485
+ if (lastUsed < existingAddress.path.addressIndex) {
486
+ await tables.lastUsedByFormatPath.insert(
487
+ '',
488
+ _baselet3.addressPathToPrefix.call(void 0, existingAddress.path),
489
+ existingAddress.path.addressIndex
490
+ )
491
+ }
492
+ }
493
+ await tables.addressByScriptPubkey.insert(
494
+ '',
495
+ existingAddress.scriptPubkey,
496
+ existingAddress
497
+ )
498
+ return
499
+ }
500
+ await tables.addressByScriptPubkey.insert(
501
+ '',
502
+ address.scriptPubkey,
503
+ address
504
+ )
505
+ })
506
+ },
507
+
508
+ numAddressesByFormatPath(path) {
509
+ return baselets.all.scriptPubkeyByPath.length(_baselet3.addressPathToPrefix.call(void 0, path))
510
+ },
511
+
512
+ async lastUsedIndexByFormatPath(path) {
513
+ const [addressIndex] = await baselets.address(async tables => {
514
+ return await tables.lastUsedByFormatPath.query('', [
515
+ _baselet3.addressPathToPrefix.call(void 0, path)
516
+ ])
517
+ })
518
+ return _nullishCoalesce(addressIndex, () => ( -1))
519
+ },
520
+
521
+ async fetchAddress(
522
+ fetchAddressArg
523
+ ) {
524
+ return await baselets.address(async tables => {
525
+ if (typeof fetchAddressArg === 'string') {
526
+ // if it is a string, it is a scriptPubkey
527
+ const scriptPubkey = fetchAddressArg
528
+ const [address] = await tables.addressByScriptPubkey.query('', [
529
+ scriptPubkey
530
+ ])
531
+ return address
532
+ }
533
+
534
+ // since it is not a string, it has to be an AddressPath
535
+ const path = fetchAddressArg
536
+ // fetch addresses by provided path
537
+ const [scriptPubkeyFromPath] = await tables.scriptPubkeyByPath.query(
538
+ _baselet3.addressPathToPrefix.call(void 0, path),
539
+ path.addressIndex
540
+ )
541
+
542
+ // return if the address by path was not found
543
+ if (scriptPubkeyFromPath == null) return
544
+
545
+ const [address] = await tables.addressByScriptPubkey.query('', [
546
+ scriptPubkeyFromPath
547
+ ])
548
+
549
+ return address
550
+ })
551
+ }
552
+ }
553
+ return processor
554
+ } exports.makeProcessor = makeProcessor;