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,52 @@
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; } }
2
+ var _utils = require('./utils'); var utils = _interopRequireWildcard(_utils);
3
+ // add inputs until we reach or surpass the target value (or deplete)
4
+ // worst-case: O(n)
5
+
6
+ function accumulative(args) {
7
+ const { utxos, targets, feeRate, changeScript } = args
8
+
9
+ if (!isFinite(utils.uintOrNaN(feeRate))) {
10
+ throw new Error('No rate provided')
11
+ }
12
+
13
+ const outputs = targets.map(target => ({
14
+ ...target,
15
+ script: Buffer.from(target.script, 'hex'),
16
+ scriptPubkey: Buffer.from(target.script, 'hex')
17
+ }))
18
+
19
+ let inValue = 0
20
+ const inputs = []
21
+ const targetValue = utils.sumOrNaN(targets)
22
+
23
+ for (let i = 0; i < utxos.length; ++i) {
24
+ const utxo = utxos[i]
25
+
26
+ // skip detrimental input
27
+ const utxoFee = feeRate * utils.inputBytes(utxo)
28
+ if (utxoFee > utxo.value) {
29
+ if (i === utxos.length - 1) {
30
+ break
31
+ } else {
32
+ continue
33
+ }
34
+ }
35
+
36
+ inputs.push(utxo)
37
+ inValue += utxo.value
38
+
39
+ const bytes = utils.transactionBytes(inputs, outputs)
40
+ const fee = bytes * feeRate
41
+
42
+ // go again?
43
+ if (inValue < targetValue + fee) continue
44
+
45
+ return utils.finalize(inputs, outputs, feeRate, changeScript)
46
+ }
47
+
48
+ return {
49
+ changeUsed: false,
50
+ fee: feeRate * utils.transactionBytes(inputs, outputs)
51
+ }
52
+ } exports.accumulative = accumulative;
@@ -0,0 +1,59 @@
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(); } }
2
+ var _utils = require('./utils'); var utils = _interopRequireWildcard(_utils);
3
+ // implementation very similar to accumulative strategy
4
+ // add inputs until we reach or surpass the target value (or deplete)
5
+ // worst-case: O(n)
6
+
7
+ function forceUseUtxo(args) {
8
+ const { utxos, useUtxos, targets, feeRate, changeScript } = args
9
+
10
+ if (!isFinite(utils.uintOrNaN(feeRate))) {
11
+ throw new Error('No rate provided')
12
+ }
13
+
14
+ const outputs = targets.map(target => ({
15
+ ...target,
16
+ script: Buffer.from(target.script, 'hex'),
17
+ scriptPubkey: Buffer.from(target.script, 'hex')
18
+ }))
19
+
20
+ const inputs = _nullishCoalesce(useUtxos, () => ( []))
21
+ let inValue = inputs.reduce((n, { value }) => n + value, 0)
22
+ const targetValue = utils.sumOrNaN(targets)
23
+ const bytes = utils.transactionBytes(inputs, outputs)
24
+ const fee = bytes * feeRate
25
+ // if the new feeRate is already covered by lowering the change amount, return
26
+ if (inValue >= targetValue + fee) {
27
+ return utils.finalize(inputs, outputs, feeRate, changeScript)
28
+ }
29
+
30
+ for (let i = 0; i < utxos.length; ++i) {
31
+ const utxo = utxos[i]
32
+
33
+ // skip detrimental input
34
+ const utxoFee = feeRate * utils.inputBytes(utxo)
35
+ if (utxoFee > utxo.value) {
36
+ if (i === utxos.length - 1) {
37
+ break
38
+ } else {
39
+ continue
40
+ }
41
+ }
42
+
43
+ inputs.push(utxo)
44
+ inValue += utxo.value
45
+
46
+ const bytes = utils.transactionBytes(inputs, outputs)
47
+ const fee = bytes * feeRate
48
+
49
+ // go again?
50
+ if (inValue < targetValue + fee) continue
51
+
52
+ return utils.finalize(inputs, outputs, feeRate, changeScript)
53
+ }
54
+
55
+ return {
56
+ changeUsed: false,
57
+ fee: feeRate * utils.transactionBytes(inputs, outputs)
58
+ }
59
+ } exports.forceUseUtxo = forceUseUtxo;
@@ -0,0 +1,17 @@
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; } }
2
+ var _utils = require('./utils'); var utils = _interopRequireWildcard(_utils);
3
+
4
+ function subtractFee(args) {
5
+ const { utxos, targets, feeRate } = args
6
+
7
+ const outputs = targets.map(target => ({
8
+ ...target,
9
+ script: Buffer.from(target.script, 'hex'),
10
+ scriptPubkey: Buffer.from(target.script, 'hex')
11
+ }))
12
+
13
+ const fee = feeRate * utils.transactionBytes(utxos, outputs)
14
+ targets[0].value -= fee
15
+ outputs[0].value -= fee
16
+ return { inputs: utxos, outputs, fee, changeUsed: false }
17
+ } exports.subtractFee = subtractFee;
@@ -0,0 +1,159 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint-disable no-fallthrough */
2
+ /* eslint-disable no-duplicate-case */
3
+ var _keymanager = require('../keymanager');
4
+
5
+
6
+ /*
7
+ Reference: https://bitcoinops.org/en/tools/calc-size/
8
+ */
9
+
10
+ const WITNESS_SCALE = 4
11
+ const OP_CODE_SIZE = 1
12
+ const SIGNATURE_SIZE = 72
13
+ const PUB_KEY_SIZE = 33
14
+ const P2SH_SCRIPT_HASH_SIZE = 22
15
+ const OUTPOINT_SIZE = 36
16
+ const N_SEQUENCE_SIZE = 4
17
+
18
+ // Measures the VarInt given a size
19
+ const compactSize = (num) =>
20
+ num < 0xfd ? 1 : num <= 0xffff ? 3 : num <= 0xffffffff ? 5 : 9; exports.compactSize = compactSize
21
+
22
+ // Adds a VarInt to the size given
23
+ const withCompactSize = (num) => {
24
+ return exports.compactSize.call(void 0, num) + num
25
+ }; exports.withCompactSize = withCompactSize
26
+
27
+ const scriptSizeMap = (() => {
28
+ // VarInt OP_PUSH <signature> OP_PUSH <public key>
29
+ const p2pkh = exports.withCompactSize.call(void 0,
30
+ OP_CODE_SIZE + SIGNATURE_SIZE + OP_CODE_SIZE + PUB_KEY_SIZE
31
+ )
32
+ // <p2pkh> <witness count (assumed to be 1)> (segwit)
33
+ const p2wpkh = (p2pkh + exports.compactSize.call(void 0, 1)) / WITNESS_SCALE
34
+ // VarInt <script hash>
35
+ const p2shOverhead = exports.withCompactSize.call(void 0, P2SH_SCRIPT_HASH_SIZE)
36
+ // <p2wpkh> <p2sh>
37
+ const p2wpkhp2sh = p2wpkh + p2shOverhead
38
+ // (fixed size)
39
+ const replayProtection = 284
40
+
41
+ // TOOD: support other p2sh (e.g. 2-3 multisig) and other p2wsh (taproot/lightning)
42
+ return {
43
+ p2pkh: p2pkh,
44
+ p2wpkh: p2wpkh,
45
+ p2wpkhp2sh: p2wpkhp2sh,
46
+ replayprotection: replayProtection,
47
+ replayprotectionp2sh: 284
48
+ }
49
+ })()
50
+
51
+ function inputBytes(input) {
52
+ const scriptSigSize = scriptSizeMap[input.scriptType]
53
+
54
+ if (scriptSigSize == null)
55
+ throw new Error(`${input.scriptType} script type not supported, yet`)
56
+
57
+ return OUTPOINT_SIZE + scriptSigSize + N_SEQUENCE_SIZE
58
+ } exports.inputBytes = inputBytes;
59
+
60
+ function outputBytes(output) {
61
+ const nValue = 8 // output value/amount
62
+ const scriptPubkeyLength = exports.compactSize.call(void 0, output.script.length) // scriptPubKey length varint
63
+ /*
64
+ scriptPubKey bytes:
65
+ p2pkh: 25
66
+ p2wpkh: 22
67
+ p2sh: 23
68
+ p2wsh: 34
69
+ */
70
+ const scriptPubkeySize = output.script.length
71
+
72
+ return nValue + scriptPubkeyLength + scriptPubkeySize
73
+ } exports.outputBytes = outputBytes;
74
+
75
+ function dustThreshold(output, feeRate) {
76
+ return outputBytes(output) * feeRate
77
+ } exports.dustThreshold = dustThreshold;
78
+
79
+ function witnessCount(inputs) {
80
+ return inputs.reduce((sum, input) => {
81
+ if (
82
+ input.scriptType === _keymanager.ScriptTypeEnum.p2wsh ||
83
+ input.scriptType === _keymanager.ScriptTypeEnum.p2wpkhp2sh ||
84
+ input.scriptType === _keymanager.ScriptTypeEnum.p2wpkh
85
+ ) {
86
+ sum++
87
+ }
88
+
89
+ return sum
90
+ }, 0)
91
+ }
92
+
93
+ function transactionBytes(inputs, outputs) {
94
+ // Overhead:
95
+ const nVersion = 4 // fixed 4 bytes
96
+ const inputCount = exports.compactSize.call(void 0, inputs.length) // varint
97
+ const outputCount = exports.compactSize.call(void 0, outputs.length) // varint
98
+ const nLockTime = 4 // fixed 4 bytes
99
+ let overhead = nVersion + inputCount + outputCount + nLockTime
100
+ const numWitnesses = witnessCount(inputs)
101
+ if (numWitnesses > 0) {
102
+ // <segwit marker> <segwit flag> (segwit)
103
+ overhead += Math.ceil(2 / WITNESS_SCALE)
104
+ }
105
+
106
+ // Inputs/Outputs:
107
+ const inputsSize = inputs.reduce((a, x) => a + inputBytes(x), 0)
108
+ const outputsSize = outputs.reduce((a, x) => a + outputBytes(x), 0)
109
+
110
+ return overhead + inputsSize + outputsSize
111
+ } exports.transactionBytes = transactionBytes;
112
+
113
+ function uintOrNaN(v) {
114
+ if (!isFinite(v)) return NaN
115
+ if (Math.floor(v) !== v) return NaN
116
+ if (v < 0) return NaN
117
+ return v
118
+ } exports.uintOrNaN = uintOrNaN;
119
+
120
+ function sumForgiving(range) {
121
+ return range.reduce((a, x) => a + (isFinite(x.value) ? x.value : 0), 0)
122
+ } exports.sumForgiving = sumForgiving;
123
+
124
+ function sumOrNaN(range) {
125
+ return range.reduce((a, x) => a + uintOrNaN(x.value), 0)
126
+ } exports.sumOrNaN = sumOrNaN;
127
+
128
+ function finalize(
129
+ inputs,
130
+ outputs,
131
+ feeRate,
132
+ changeScript
133
+ ) {
134
+ const inValue = sumOrNaN(inputs)
135
+ const outValue = sumOrNaN(outputs)
136
+ let txSize = transactionBytes(inputs, outputs)
137
+ let fee = Math.ceil(feeRate * txSize)
138
+
139
+ const changeValue = inValue - (outValue + fee)
140
+ const changeOutput = {
141
+ script: Buffer.from(changeScript, 'hex'),
142
+ scriptPubkey: Buffer.from(changeScript, 'hex'),
143
+ value: changeValue
144
+ }
145
+ const changeOutputSize = outputBytes(changeOutput)
146
+ txSize += changeOutputSize
147
+ const changeFee = feeRate * changeOutputSize
148
+ changeOutput.value -= changeFee
149
+ let changeUsed = false
150
+ if (changeOutput.value > dustThreshold(changeOutput, feeRate)) {
151
+ outputs.push(changeOutput)
152
+ fee += changeFee
153
+ changeUsed = true
154
+ } else {
155
+ fee += changeValue
156
+ }
157
+
158
+ return { inputs, outputs, changeUsed, fee }
159
+ } exports.finalize = finalize;
@@ -0,0 +1,17 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, get: () => obj[key]}); }); }var _accumulative = require('./utxopicker/accumulative');
2
+ var _forceUseUtxo = require('./utxopicker/forceUseUtxo');
3
+ var _subtractFee = require('./utxopicker/subtractFee');
4
+ var _types = require('./utxopicker/types'); _createStarExport(_types);
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+ const utxoPicker = {
14
+ forceUseUtxo: _forceUseUtxo.forceUseUtxo,
15
+ subtractFee: _subtractFee.subtractFee,
16
+ accumulative: _accumulative.accumulative
17
+ }; exports.utxoPicker = utxoPicker
@@ -0,0 +1,200 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
+
3
+
4
+ var _makeEngineEmitter = require('../../plugin/makeEngineEmitter');
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ var _BlockBookAPI = require('./BlockBookAPI');
24
+ var _Deferred = require('./Deferred'); var _Deferred2 = _interopRequireDefault(_Deferred);
25
+
26
+ var _Socket = require('./Socket');
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
+ function makeBlockBook(config) {
74
+ const {
75
+ wsAddress,
76
+ socketEmitter,
77
+ engineEmitter,
78
+ log,
79
+ onQueueSpaceCB,
80
+ walletId,
81
+ asAddress
82
+ } = config
83
+ log(`makeBlockBook with uri ${wsAddress}`)
84
+
85
+ const instance = {
86
+ isConnected: false,
87
+ connect,
88
+ disconnect,
89
+ onQueueSpace,
90
+ fetchInfo,
91
+ fetchAddress,
92
+ watchAddresses,
93
+ watchBlocks,
94
+ fetchAddressUtxos,
95
+ fetchTransaction,
96
+ broadcastTx
97
+ }
98
+
99
+ const socket = _Socket.makeSocket.call(void 0, wsAddress, {
100
+ healthCheck: ping,
101
+ onQueueSpaceCB,
102
+ log,
103
+ emitter: socketEmitter,
104
+ walletId
105
+ })
106
+
107
+ async function connect() {
108
+ log(`connecting to blockbook socket with uri ${wsAddress}`)
109
+ if (instance.isConnected) return
110
+
111
+ await socket.connect()
112
+ instance.isConnected = socket.isConnected()
113
+ }
114
+
115
+ async function disconnect() {
116
+ log(
117
+ `disconnecting from blockbook socket with uri ${wsAddress}, currently connected: ${instance.isConnected}`
118
+ )
119
+ if (!instance.isConnected) return
120
+
121
+ socket.disconnect()
122
+ instance.isConnected = false
123
+ }
124
+
125
+ function onQueueSpace(cb) {
126
+ socket.onQueueSpace(cb)
127
+ }
128
+
129
+ async function promisifyWsMessage(message) {
130
+ const deferred = new _Deferred2.default()
131
+ socket.submitTask({ ...message, deferred })
132
+ return await deferred.promise
133
+ }
134
+
135
+ async function ping() {
136
+ await promisifyWsMessage(_BlockBookAPI.pingMessage.call(void 0, ))
137
+ }
138
+
139
+ async function fetchInfo() {
140
+ return await promisifyWsMessage(_BlockBookAPI.infoMessage.call(void 0, ))
141
+ }
142
+
143
+ async function fetchAddressUtxos(
144
+ account
145
+ ) {
146
+ return await promisifyWsMessage(_BlockBookAPI.addressUtxosMessage.call(void 0, account))
147
+ }
148
+
149
+ async function fetchTransaction(hash) {
150
+ return await promisifyWsMessage(_BlockBookAPI.transactionMessage.call(void 0, hash))
151
+ }
152
+
153
+ async function fetchAddress(
154
+ address,
155
+ params = {}
156
+ ) {
157
+ return await promisifyWsMessage(_BlockBookAPI.addressMessage.call(void 0, address, asAddress, params))
158
+ }
159
+
160
+ async function watchBlocks(
161
+ deferredBlockSub
162
+ ) {
163
+ const socketCb = async (res) => {
164
+ engineEmitter.emit(
165
+ _makeEngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED,
166
+ wsAddress,
167
+ res.height
168
+ )
169
+ }
170
+ socket.subscribe({
171
+ ..._BlockBookAPI.subscribeNewBlockMessage.call(void 0, ),
172
+ cb: socketCb,
173
+ deferred: deferredBlockSub,
174
+ subscribed: false
175
+ })
176
+ }
177
+
178
+ function watchAddresses(
179
+ addresses,
180
+ deferredAddressSub
181
+ ) {
182
+ const socketCb = async (res) => {
183
+ engineEmitter.emit(_makeEngineEmitter.EngineEvent.NEW_ADDRESS_TRANSACTION, wsAddress, res)
184
+ }
185
+ socket.subscribe({
186
+ ..._BlockBookAPI.subscribeAddressesMessage.call(void 0, addresses, asAddress),
187
+ cb: socketCb,
188
+ deferred: deferredAddressSub,
189
+ subscribed: false
190
+ })
191
+ }
192
+
193
+ async function broadcastTx(
194
+ transaction
195
+ ) {
196
+ return await promisifyWsMessage(_BlockBookAPI.broadcastTxMessage.call(void 0, transaction))
197
+ }
198
+
199
+ return instance
200
+ } exports.makeBlockBook = makeBlockBook;