@onetokenfe/coinselect 3.1.16

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 (229) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/accumulative.js +38 -0
  4. package/blackjack.js +35 -0
  5. package/break.js +34 -0
  6. package/index.js +21 -0
  7. package/package.json +82 -0
  8. package/split.js +45 -0
  9. package/third-party/utils/addDashesToSpaces.d.ts +2 -0
  10. package/third-party/utils/addDashesToSpaces.js +6 -0
  11. package/third-party/utils/arrayDistinct.d.ts +2 -0
  12. package/third-party/utils/arrayDistinct.js +6 -0
  13. package/third-party/utils/arrayPartition.d.ts +7 -0
  14. package/third-party/utils/arrayPartition.js +6 -0
  15. package/third-party/utils/arrayShuffle.d.ts +4 -0
  16. package/third-party/utils/arrayShuffle.js +13 -0
  17. package/third-party/utils/arrayToDictionary.d.ts +10 -0
  18. package/third-party/utils/arrayToDictionary.js +27 -0
  19. package/third-party/utils/asciiUtils.d.ts +3 -0
  20. package/third-party/utils/asciiUtils.js +16 -0
  21. package/third-party/utils/bigNumber.d.ts +5 -0
  22. package/third-party/utils/bigNumber.js +9 -0
  23. package/third-party/utils/bufferUtils.d.ts +3 -0
  24. package/third-party/utils/bufferUtils.js +23 -0
  25. package/third-party/utils/bytesToHumanReadable.d.ts +2 -0
  26. package/third-party/utils/bytesToHumanReadable.js +15 -0
  27. package/third-party/utils/capitalizeFirstLetter.d.ts +2 -0
  28. package/third-party/utils/capitalizeFirstLetter.js +6 -0
  29. package/third-party/utils/cloneObject.d.ts +2 -0
  30. package/third-party/utils/cloneObject.js +32 -0
  31. package/third-party/utils/countBytesInString.d.ts +2 -0
  32. package/third-party/utils/countBytesInString.js +6 -0
  33. package/third-party/utils/createCooldown.d.ts +2 -0
  34. package/third-party/utils/createCooldown.js +16 -0
  35. package/third-party/utils/createDeferred.d.ts +14 -0
  36. package/third-party/utils/createDeferred.js +19 -0
  37. package/third-party/utils/createDeferredManager.d.ts +19 -0
  38. package/third-party/utils/createDeferredManager.js +67 -0
  39. package/third-party/utils/createLazy.d.ts +7 -0
  40. package/third-party/utils/createLazy.js +42 -0
  41. package/third-party/utils/createTimeoutPromise.d.ts +2 -0
  42. package/third-party/utils/createTimeoutPromise.js +6 -0
  43. package/third-party/utils/enumUtils.d.ts +6 -0
  44. package/third-party/utils/enumUtils.js +12 -0
  45. package/third-party/utils/extractUrlsFromText.d.ts +5 -0
  46. package/third-party/utils/extractUrlsFromText.js +28 -0
  47. package/third-party/utils/getLocaleSeparators.d.ts +5 -0
  48. package/third-party/utils/getLocaleSeparators.js +13 -0
  49. package/third-party/utils/getLocaleSeparators.native.d.ts +5 -0
  50. package/third-party/utils/getLocaleSeparators.native.js +17 -0
  51. package/third-party/utils/getMutex.d.ts +2 -0
  52. package/third-party/utils/getMutex.js +22 -0
  53. package/third-party/utils/getNumberFromPixelString.d.ts +2 -0
  54. package/third-party/utils/getNumberFromPixelString.js +6 -0
  55. package/third-party/utils/getRandomInt.d.ts +2 -0
  56. package/third-party/utils/getRandomInt.js +56 -0
  57. package/third-party/utils/getSynchronize.d.ts +4 -0
  58. package/third-party/utils/getSynchronize.js +10 -0
  59. package/third-party/utils/getWeakRandomId.d.ts +2 -0
  60. package/third-party/utils/getWeakRandomId.js +13 -0
  61. package/third-party/utils/getWeakRandomInt.d.ts +2 -0
  62. package/third-party/utils/getWeakRandomInt.js +11 -0
  63. package/third-party/utils/getWeakRandomNumberInRange.d.ts +2 -0
  64. package/third-party/utils/getWeakRandomNumberInRange.js +6 -0
  65. package/third-party/utils/hasUppercaseLetter.d.ts +2 -0
  66. package/third-party/utils/hasUppercaseLetter.js +7 -0
  67. package/third-party/utils/index.d.ts +52 -0
  68. package/third-party/utils/index.js +56 -0
  69. package/third-party/utils/isArrayMember.d.ts +2 -0
  70. package/third-party/utils/isArrayMember.js +6 -0
  71. package/third-party/utils/isFullPath.d.ts +2 -0
  72. package/third-party/utils/isFullPath.js +9 -0
  73. package/third-party/utils/isHex.d.ts +2 -0
  74. package/third-party/utils/isHex.js +9 -0
  75. package/third-party/utils/isNotUndefined.d.ts +2 -0
  76. package/third-party/utils/isNotUndefined.js +6 -0
  77. package/third-party/utils/isUrl.d.ts +2 -0
  78. package/third-party/utils/isUrl.js +7 -0
  79. package/third-party/utils/logs.d.ts +32 -0
  80. package/third-party/utils/logs.js +82 -0
  81. package/third-party/utils/logsManager.d.ts +17 -0
  82. package/third-party/utils/logsManager.js +53 -0
  83. package/third-party/utils/mergeDeepObject.d.ts +26 -0
  84. package/third-party/utils/mergeDeepObject.js +68 -0
  85. package/third-party/utils/objectPartition.d.ts +6 -0
  86. package/third-party/utils/objectPartition.js +12 -0
  87. package/third-party/utils/parseElectrumUrl.d.ts +6 -0
  88. package/third-party/utils/parseElectrumUrl.js +17 -0
  89. package/third-party/utils/parseHostname.d.ts +2 -0
  90. package/third-party/utils/parseHostname.js +7 -0
  91. package/third-party/utils/promiseAllSequence.d.ts +2 -0
  92. package/third-party/utils/promiseAllSequence.js +14 -0
  93. package/third-party/utils/redactUserPath.d.ts +3 -0
  94. package/third-party/utils/redactUserPath.js +7 -0
  95. package/third-party/utils/scheduleAction.d.ts +15 -0
  96. package/third-party/utils/scheduleAction.js +125 -0
  97. package/third-party/utils/splitStringEveryNCharacters.d.ts +2 -0
  98. package/third-party/utils/splitStringEveryNCharacters.js +12 -0
  99. package/third-party/utils/throttler.d.ts +11 -0
  100. package/third-party/utils/throttler.js +38 -0
  101. package/third-party/utils/throwError.d.ts +2 -0
  102. package/third-party/utils/throwError.js +8 -0
  103. package/third-party/utils/topologicalSort.d.ts +2 -0
  104. package/third-party/utils/topologicalSort.js +19 -0
  105. package/third-party/utils/truncateMiddle.d.ts +2 -0
  106. package/third-party/utils/truncateMiddle.js +12 -0
  107. package/third-party/utils/typedEventEmitter.d.ts +23 -0
  108. package/third-party/utils/typedEventEmitter.js +11 -0
  109. package/third-party/utils/urlToOnion.d.ts +4 -0
  110. package/third-party/utils/urlToOnion.js +12 -0
  111. package/third-party/utils/versionUtils.d.ts +9 -0
  112. package/third-party/utils/versionUtils.js +28 -0
  113. package/third-party/utils/xssFilters.d.ts +4 -0
  114. package/third-party/utils/xssFilters.js +13 -0
  115. package/third-party/utxo-lib/address.d.ts +17 -0
  116. package/third-party/utxo-lib/address.js +174 -0
  117. package/third-party/utxo-lib/bip32.d.ts +28 -0
  118. package/third-party/utxo-lib/bip32.js +253 -0
  119. package/third-party/utxo-lib/bs58check.d.ts +12 -0
  120. package/third-party/utxo-lib/bs58check.js +92 -0
  121. package/third-party/utxo-lib/bufferutils.d.ts +59 -0
  122. package/third-party/utxo-lib/bufferutils.js +219 -0
  123. package/third-party/utxo-lib/coinselect/coinselectUtils.d.ts +53 -0
  124. package/third-party/utxo-lib/coinselect/coinselectUtils.js +232 -0
  125. package/third-party/utxo-lib/coinselect/index.d.ts +3 -0
  126. package/third-party/utxo-lib/coinselect/index.js +19 -0
  127. package/third-party/utxo-lib/coinselect/inputs/accumulative.d.ts +3 -0
  128. package/third-party/utxo-lib/coinselect/inputs/accumulative.js +55 -0
  129. package/third-party/utxo-lib/coinselect/inputs/branchAndBound.d.ts +3 -0
  130. package/third-party/utxo-lib/coinselect/inputs/branchAndBound.js +123 -0
  131. package/third-party/utxo-lib/coinselect/outputs/split.d.ts +3 -0
  132. package/third-party/utxo-lib/coinselect/outputs/split.js +35 -0
  133. package/third-party/utxo-lib/coinselect/tryconfirmed.d.ts +3 -0
  134. package/third-party/utxo-lib/coinselect/tryconfirmed.js +65 -0
  135. package/third-party/utxo-lib/compose/index.d.ts +3 -0
  136. package/third-party/utxo-lib/compose/index.js +20 -0
  137. package/third-party/utxo-lib/compose/request.d.ts +5 -0
  138. package/third-party/utxo-lib/compose/request.js +168 -0
  139. package/third-party/utxo-lib/compose/result.d.ts +4 -0
  140. package/third-party/utxo-lib/compose/result.js +64 -0
  141. package/third-party/utxo-lib/compose/sorting/bip69SortingStrategy.d.ts +3 -0
  142. package/third-party/utxo-lib/compose/sorting/bip69SortingStrategy.js +32 -0
  143. package/third-party/utxo-lib/compose/sorting/convertOutput.d.ts +13 -0
  144. package/third-party/utxo-lib/compose/sorting/convertOutput.js +14 -0
  145. package/third-party/utxo-lib/compose/sorting/noneSortingStrategy.d.ts +3 -0
  146. package/third-party/utxo-lib/compose/sorting/noneSortingStrategy.js +19 -0
  147. package/third-party/utxo-lib/compose/sorting/randomSortingStrategy.d.ts +3 -0
  148. package/third-party/utxo-lib/compose/sorting/randomSortingStrategy.js +28 -0
  149. package/third-party/utxo-lib/compose/sorting/sortingStrategy.d.ts +9 -0
  150. package/third-party/utxo-lib/compose/sorting/sortingStrategy.js +3 -0
  151. package/third-party/utxo-lib/compose/transaction.d.ts +3 -0
  152. package/third-party/utxo-lib/compose/transaction.js +16 -0
  153. package/third-party/utxo-lib/crypto.d.ts +9 -0
  154. package/third-party/utxo-lib/crypto.js +44 -0
  155. package/third-party/utxo-lib/derivation.d.ts +24 -0
  156. package/third-party/utxo-lib/derivation.js +121 -0
  157. package/third-party/utxo-lib/discovery.d.ts +9 -0
  158. package/third-party/utxo-lib/discovery.js +30 -0
  159. package/third-party/utxo-lib/global.d.ts +62 -0
  160. package/third-party/utxo-lib/global.js +2 -0
  161. package/third-party/utxo-lib/index.d.ts +18 -0
  162. package/third-party/utxo-lib/index.js +28 -0
  163. package/third-party/utxo-lib/networks.d.ts +45 -0
  164. package/third-party/utxo-lib/networks.js +250 -0
  165. package/third-party/utxo-lib/payments/embed.d.ts +3 -0
  166. package/third-party/utxo-lib/payments/embed.js +49 -0
  167. package/third-party/utxo-lib/payments/index.d.ts +13 -0
  168. package/third-party/utxo-lib/payments/index.js +28 -0
  169. package/third-party/utxo-lib/payments/lazy.d.ts +3 -0
  170. package/third-party/utxo-lib/payments/lazy.js +33 -0
  171. package/third-party/utxo-lib/payments/p2ms.d.ts +3 -0
  172. package/third-party/utxo-lib/payments/p2ms.js +139 -0
  173. package/third-party/utxo-lib/payments/p2pk.d.ts +3 -0
  174. package/third-party/utxo-lib/payments/p2pk.js +72 -0
  175. package/third-party/utxo-lib/payments/p2pkh.d.ts +3 -0
  176. package/third-party/utxo-lib/payments/p2pkh.js +132 -0
  177. package/third-party/utxo-lib/payments/p2sh.d.ts +3 -0
  178. package/third-party/utxo-lib/payments/p2sh.js +171 -0
  179. package/third-party/utxo-lib/payments/p2tr.d.ts +3 -0
  180. package/third-party/utxo-lib/payments/p2tr.js +123 -0
  181. package/third-party/utxo-lib/payments/p2wpkh.d.ts +3 -0
  182. package/third-party/utxo-lib/payments/p2wpkh.js +137 -0
  183. package/third-party/utxo-lib/payments/p2wsh.d.ts +3 -0
  184. package/third-party/utxo-lib/payments/p2wsh.js +183 -0
  185. package/third-party/utxo-lib/payments/sstxchange.d.ts +3 -0
  186. package/third-party/utxo-lib/payments/sstxchange.js +79 -0
  187. package/third-party/utxo-lib/payments/sstxcommitment.d.ts +3 -0
  188. package/third-party/utxo-lib/payments/sstxcommitment.js +74 -0
  189. package/third-party/utxo-lib/payments/sstxpkh.d.ts +3 -0
  190. package/third-party/utxo-lib/payments/sstxpkh.js +79 -0
  191. package/third-party/utxo-lib/payments/sstxsh.d.ts +3 -0
  192. package/third-party/utxo-lib/payments/sstxsh.js +70 -0
  193. package/third-party/utxo-lib/script/index.d.ts +17 -0
  194. package/third-party/utxo-lib/script/index.js +167 -0
  195. package/third-party/utxo-lib/script/ops.d.ts +4 -0
  196. package/third-party/utxo-lib/script/ops.js +13 -0
  197. package/third-party/utxo-lib/script/scriptNumber.d.ts +3 -0
  198. package/third-party/utxo-lib/script/scriptNumber.js +62 -0
  199. package/third-party/utxo-lib/script/scriptSignature.d.ts +8 -0
  200. package/third-party/utxo-lib/script/scriptSignature.js +55 -0
  201. package/third-party/utxo-lib/transaction/base.d.ts +56 -0
  202. package/third-party/utxo-lib/transaction/base.js +108 -0
  203. package/third-party/utxo-lib/transaction/bitcoin.d.ts +4 -0
  204. package/third-party/utxo-lib/transaction/bitcoin.js +109 -0
  205. package/third-party/utxo-lib/transaction/dash.d.ts +8 -0
  206. package/third-party/utxo-lib/transaction/dash.js +105 -0
  207. package/third-party/utxo-lib/transaction/decred.d.ts +4 -0
  208. package/third-party/utxo-lib/transaction/decred.js +149 -0
  209. package/third-party/utxo-lib/transaction/index.d.ts +15 -0
  210. package/third-party/utxo-lib/transaction/index.js +44 -0
  211. package/third-party/utxo-lib/transaction/peercoin.d.ts +4 -0
  212. package/third-party/utxo-lib/transaction/peercoin.js +64 -0
  213. package/third-party/utxo-lib/transaction/zcash.d.ts +66 -0
  214. package/third-party/utxo-lib/transaction/zcash.js +451 -0
  215. package/third-party/utxo-lib/types/coinselect.d.ts +61 -0
  216. package/third-party/utxo-lib/types/coinselect.js +3 -0
  217. package/third-party/utxo-lib/types/compose.d.ts +98 -0
  218. package/third-party/utxo-lib/types/compose.js +10 -0
  219. package/third-party/utxo-lib/types/index.d.ts +5 -0
  220. package/third-party/utxo-lib/types/index.js +8 -0
  221. package/third-party/utxo-lib/types/payments.d.ts +30 -0
  222. package/third-party/utxo-lib/types/payments.js +3 -0
  223. package/third-party/utxo-lib/types/typeforce.d.ts +8 -0
  224. package/third-party/utxo-lib/types/typeforce.js +16 -0
  225. package/third-party/utxo-lib/vsize.d.ts +13 -0
  226. package/third-party/utxo-lib/vsize.js +50 -0
  227. package/utils.js +78 -0
  228. package/witness.d.ts +70 -0
  229. package/witness.js +30 -0
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.accumulative = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
6
+ const coinselectUtils_1 = require("../coinselectUtils");
7
+ const accumulative = (utxos0, outputs, feeRate, options) => {
8
+ let inAccum = coinselectUtils_1.ZERO;
9
+ const inputs = [];
10
+ const outAccum = (0, coinselectUtils_1.sumOrNaN)(outputs);
11
+ const requiredUtxos = [];
12
+ const utxos = [];
13
+ utxos0.forEach(u => {
14
+ if (u.required) {
15
+ requiredUtxos.push(u);
16
+ const utxoValue = (0, coinselectUtils_1.bignumberOrNaN)(u.value, true);
17
+ inAccum = inAccum.add(utxoValue);
18
+ inputs.push(u);
19
+ }
20
+ else {
21
+ utxos.push(u);
22
+ }
23
+ });
24
+ if (requiredUtxos.length > 0) {
25
+ const requiredIsEnough = (0, coinselectUtils_1.finalize)(requiredUtxos, outputs, feeRate, options);
26
+ if (requiredIsEnough.inputs) {
27
+ return requiredIsEnough;
28
+ }
29
+ }
30
+ for (let i = 0; i < utxos.length; ++i) {
31
+ const utxo = utxos[i];
32
+ const utxoBytes = (0, coinselectUtils_1.inputBytes)(utxo);
33
+ const utxoFee = (0, coinselectUtils_1.getFeeForBytes)(feeRate, utxoBytes);
34
+ const utxoValue = (0, coinselectUtils_1.bignumberOrNaN)(utxo.value);
35
+ if (!utxoValue || utxoValue.lt(new bn_js_1.default(utxoFee))) {
36
+ if (i === utxos.length - 1) {
37
+ const fee = (0, coinselectUtils_1.getFee)([...inputs, utxo], outputs, feeRate, options);
38
+ return { fee };
39
+ }
40
+ }
41
+ else {
42
+ inAccum = inAccum.add(utxoValue);
43
+ inputs.push(utxo);
44
+ const fee = (0, coinselectUtils_1.getFee)(inputs, outputs, feeRate, options);
45
+ const outAccumWithFee = outAccum ? outAccum.add(new bn_js_1.default(fee)) : coinselectUtils_1.ZERO;
46
+ if (inAccum.gte(outAccumWithFee)) {
47
+ return (0, coinselectUtils_1.finalize)(inputs, outputs, feeRate, options);
48
+ }
49
+ }
50
+ }
51
+ const fee = (0, coinselectUtils_1.getFee)(inputs, outputs, feeRate, options);
52
+ return { fee };
53
+ };
54
+ exports.accumulative = accumulative;
55
+ //# sourceMappingURL=accumulative.js.map
@@ -0,0 +1,3 @@
1
+ import { CoinSelectAlgorithm } from '../../types';
2
+ export declare const branchAndBound: CoinSelectAlgorithm;
3
+ //# sourceMappingURL=branchAndBound.d.ts.map
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.branchAndBound = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
6
+ const coinselectUtils_1 = require("../coinselectUtils");
7
+ const MAX_TRIES = 1000000;
8
+ function calculateEffectiveValues(utxos, feeRate) {
9
+ return utxos.map(utxo => {
10
+ const value = (0, coinselectUtils_1.bignumberOrNaN)(utxo.value);
11
+ if (!value) {
12
+ return {
13
+ utxo,
14
+ effectiveValue: coinselectUtils_1.ZERO,
15
+ };
16
+ }
17
+ const effectiveFee = (0, coinselectUtils_1.getFeeForBytes)(feeRate, (0, coinselectUtils_1.inputBytes)(utxo));
18
+ const effectiveValue = value.sub(new bn_js_1.default(effectiveFee));
19
+ return {
20
+ utxo,
21
+ effectiveValue,
22
+ };
23
+ });
24
+ }
25
+ function search(effectiveUtxos, target, costRange) {
26
+ if (effectiveUtxos.length === 0) {
27
+ return null;
28
+ }
29
+ let tries = MAX_TRIES;
30
+ const selected = [];
31
+ let selectedAccum = coinselectUtils_1.ZERO;
32
+ let done = false;
33
+ let backtrack = false;
34
+ let remaining = effectiveUtxos.reduce((a, x) => x.effectiveValue.add(a), coinselectUtils_1.ZERO);
35
+ let depth = 0;
36
+ while (!done) {
37
+ if (tries <= 0) {
38
+ return null;
39
+ }
40
+ if (selectedAccum.gt(costRange)) {
41
+ backtrack = true;
42
+ }
43
+ else if (selectedAccum.gte(target)) {
44
+ done = true;
45
+ }
46
+ else if (depth >= effectiveUtxos.length) {
47
+ backtrack = true;
48
+ }
49
+ else if (selectedAccum.add(remaining).lt(target)) {
50
+ if (depth === 0) {
51
+ return null;
52
+ }
53
+ backtrack = true;
54
+ }
55
+ else {
56
+ remaining = remaining.sub(effectiveUtxos[depth].effectiveValue);
57
+ selected[depth] = true;
58
+ selectedAccum = selectedAccum.add(effectiveUtxos[depth].effectiveValue);
59
+ depth++;
60
+ }
61
+ if (backtrack) {
62
+ backtrack = false;
63
+ depth--;
64
+ while (!selected[depth]) {
65
+ remaining = remaining.add(effectiveUtxos[depth].effectiveValue);
66
+ depth--;
67
+ if (depth < 0) {
68
+ return null;
69
+ }
70
+ }
71
+ selected[depth] = false;
72
+ selectedAccum = selectedAccum.sub(effectiveUtxos[depth].effectiveValue);
73
+ depth++;
74
+ }
75
+ tries--;
76
+ }
77
+ return selected;
78
+ }
79
+ const branchAndBound = (utxos, outputs, feeRate, options) => {
80
+ if (options.baseFee)
81
+ return { fee: 0 };
82
+ if (utxos.find(u => u.required))
83
+ return { fee: 0 };
84
+ const changeOutputFee = (0, coinselectUtils_1.getFeeForBytes)(feeRate, (0, coinselectUtils_1.outputBytes)({
85
+ script: {
86
+ length: coinselectUtils_1.OUTPUT_SCRIPT_LENGTH[options.txType],
87
+ },
88
+ }));
89
+ const costOfChange = changeOutputFee + (0, coinselectUtils_1.getDustAmount)(feeRate, options);
90
+ const outputsBytes = (0, coinselectUtils_1.transactionBytes)([], outputs);
91
+ const outputsFee = (0, coinselectUtils_1.getFeeForBytes)(feeRate, outputsBytes);
92
+ const outputsTotalValue = (0, coinselectUtils_1.sumOrNaN)(outputs);
93
+ if (!outputsTotalValue)
94
+ return { fee: 0 };
95
+ const target = outputsTotalValue.add(new bn_js_1.default(outputsFee));
96
+ const targetRange = target.add(new bn_js_1.default(costOfChange));
97
+ const effectiveUtxos = calculateEffectiveValues(utxos, feeRate)
98
+ .filter(({ effectiveValue }) => effectiveValue.gt(coinselectUtils_1.ZERO) && effectiveValue.lte(targetRange))
99
+ .sort((a, b) => {
100
+ const subtract = b.effectiveValue.sub(a.effectiveValue).toNumber();
101
+ if (subtract !== 0) {
102
+ return subtract;
103
+ }
104
+ return a.utxo.i - b.utxo.i;
105
+ });
106
+ const utxosTotalEffectiveValue = effectiveUtxos.reduce((total, { effectiveValue }) => total.add(effectiveValue), coinselectUtils_1.ZERO);
107
+ if (utxosTotalEffectiveValue.lt(target)) {
108
+ return { fee: 0 };
109
+ }
110
+ const selected = search(effectiveUtxos, target, targetRange);
111
+ if (selected !== null) {
112
+ const inputs = [];
113
+ for (let i = 0; i < effectiveUtxos.length; i++) {
114
+ if (selected[i]) {
115
+ inputs.push(effectiveUtxos[i].utxo);
116
+ }
117
+ }
118
+ return (0, coinselectUtils_1.finalize)(inputs, outputs, feeRate, options);
119
+ }
120
+ return { fee: 0 };
121
+ };
122
+ exports.branchAndBound = branchAndBound;
123
+ //# sourceMappingURL=branchAndBound.js.map
@@ -0,0 +1,3 @@
1
+ import { CoinSelectInput, CoinSelectOutput, CoinSelectOptions, CoinSelectResult } from '../../types';
2
+ export declare function split(utxosOrig: CoinSelectInput[], outputs: CoinSelectOutput[], feeRate: number, options: CoinSelectOptions): CoinSelectResult;
3
+ //# sourceMappingURL=split.d.ts.map
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.split = split;
4
+ const tslib_1 = require("tslib");
5
+ const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
6
+ const coinselectUtils_1 = require("../coinselectUtils");
7
+ function split(utxosOrig, outputs, feeRate, options) {
8
+ const coinbase = options.coinbase || 100;
9
+ const utxos = (0, coinselectUtils_1.filterCoinbase)(utxosOrig, coinbase);
10
+ const fee = (0, coinselectUtils_1.getFee)(utxos, outputs, feeRate, options);
11
+ if (outputs.length === 0)
12
+ return { fee };
13
+ const inAccum = (0, coinselectUtils_1.sumOrNaN)(utxos);
14
+ const outAccum = (0, coinselectUtils_1.sumOrNaN)(outputs, true);
15
+ if (!inAccum)
16
+ return { fee };
17
+ const remaining = inAccum.sub(outAccum).sub(new bn_js_1.default(fee));
18
+ if (remaining.lt(coinselectUtils_1.ZERO))
19
+ return { fee };
20
+ const unspecified = outputs.reduce((a, x) => a + (!(0, coinselectUtils_1.bignumberOrNaN)(x.value) ? 1 : 0), 0);
21
+ if (remaining.isZero() || unspecified === 0) {
22
+ return (0, coinselectUtils_1.finalize)(utxos, outputs, feeRate, options);
23
+ }
24
+ const splitValue = remaining.div(new bn_js_1.default(unspecified));
25
+ const dustAmount = (0, coinselectUtils_1.getDustAmount)(feeRate, options);
26
+ if (unspecified && splitValue.lt(new bn_js_1.default(dustAmount)))
27
+ return { fee };
28
+ const outputsSplit = outputs.map(output => {
29
+ if (output.value)
30
+ return output;
31
+ return Object.assign(Object.assign({}, output), { value: splitValue });
32
+ });
33
+ return (0, coinselectUtils_1.finalize)(utxos, outputsSplit, feeRate, options);
34
+ }
35
+ //# sourceMappingURL=split.js.map
@@ -0,0 +1,3 @@
1
+ import { CoinSelectAlgorithm, CoinSelectOptions } from '../types';
2
+ export declare function tryConfirmed(algorithm: CoinSelectAlgorithm, options: CoinSelectOptions): CoinSelectAlgorithm;
3
+ //# sourceMappingURL=tryconfirmed.d.ts.map
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tryConfirmed = tryConfirmed;
4
+ const coinselectUtils_1 = require("./coinselectUtils");
5
+ function filterUtxos(utxos, minConfOwn, minConfOther) {
6
+ const usable = [];
7
+ const unusable = [];
8
+ for (let i = 0; i < utxos.length; i++) {
9
+ const utxo = utxos[i];
10
+ const isUsed = utxo.own
11
+ ? utxo.confirmations >= minConfOwn
12
+ : utxo.confirmations >= minConfOther;
13
+ if (isUsed || utxo.required) {
14
+ usable.push(utxo);
15
+ }
16
+ else {
17
+ unusable.push(utxo);
18
+ }
19
+ }
20
+ return {
21
+ usable,
22
+ unusable,
23
+ };
24
+ }
25
+ function tryConfirmed(algorithm, options) {
26
+ const own = options.own || 1;
27
+ const other = options.other || 6;
28
+ const coinbase = options.coinbase || 100;
29
+ return (utxosO, outputs, feeRate, optionsIn) => {
30
+ const utxos = (0, coinselectUtils_1.filterCoinbase)(utxosO, coinbase);
31
+ if (utxos.length === 0) {
32
+ return { fee: 0 };
33
+ }
34
+ const trials = [];
35
+ let i;
36
+ for (i = own; i > 0; i--) {
37
+ trials.push({ other, own: i });
38
+ }
39
+ for (i = other - 1; i > 0; i--) {
40
+ trials.push({ other: i, own: 1 });
41
+ }
42
+ trials.push({ other: 1, own: 0 });
43
+ trials.push({ other: 0, own: 0 });
44
+ let unusable = utxos;
45
+ let usable = [];
46
+ for (i = 0; i < trials.length; i++) {
47
+ const trial = trials[i];
48
+ const filterResult = filterUtxos(unusable, trial.own, trial.other);
49
+ if (filterResult.usable.length > 0) {
50
+ usable = usable.concat(filterResult.usable);
51
+ const unusableH = filterResult.unusable;
52
+ unusable = unusableH;
53
+ const result = algorithm(usable, outputs, feeRate, optionsIn);
54
+ if (result.inputs) {
55
+ return result;
56
+ }
57
+ if (unusable.length === 0) {
58
+ return result;
59
+ }
60
+ }
61
+ }
62
+ throw new Error('Unexpected unreturned result');
63
+ };
64
+ }
65
+ //# sourceMappingURL=tryconfirmed.js.map
@@ -0,0 +1,3 @@
1
+ import { ComposeRequest, ComposeInput, ComposeOutput, ComposeChangeAddress, ComposeResult } from '../types';
2
+ export declare function composeTx<Input extends ComposeInput, Output extends ComposeOutput, Change extends ComposeChangeAddress>(request: ComposeRequest<Input, Output, Change>): ComposeResult<Input, Output, Change>;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.composeTx = composeTx;
4
+ const request_1 = require("./request");
5
+ const result_1 = require("./result");
6
+ const coinselect_1 = require("../coinselect");
7
+ function composeTx(request) {
8
+ const coinselectRequest = (0, request_1.validateAndParseRequest)(request);
9
+ if ('error' in coinselectRequest) {
10
+ return coinselectRequest;
11
+ }
12
+ try {
13
+ const result = (0, coinselect_1.coinselect)(coinselectRequest);
14
+ return (0, result_1.getResult)(request, coinselectRequest, result);
15
+ }
16
+ catch (error) {
17
+ return (0, result_1.getErrorResult)(error);
18
+ }
19
+ }
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ import type { ComposeInput, ComposeOutput, ComposeChangeAddress, ComposeRequest, ComposeResultError, CoinSelectRequest } from '../types';
2
+ type Request = ComposeRequest<ComposeInput, ComposeOutput, ComposeChangeAddress>;
3
+ export declare function validateAndParseRequest(request: Request): CoinSelectRequest | ComposeResultError;
4
+ export {};
5
+ //# sourceMappingURL=request.d.ts.map
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateAndParseRequest = validateAndParseRequest;
4
+ const address_1 = require("../address");
5
+ const embed_1 = require("../payments/embed");
6
+ const coinselectUtils_1 = require("../coinselect/coinselectUtils");
7
+ function validateAndParseFeeRate(rate) {
8
+ const feeRate = typeof rate === 'string' ? Number(rate) : rate;
9
+ if (typeof feeRate !== 'number' ||
10
+ Number.isNaN(feeRate) ||
11
+ !Number.isFinite(feeRate) ||
12
+ feeRate > Number.MAX_SAFE_INTEGER ||
13
+ feeRate <= 0) {
14
+ return;
15
+ }
16
+ return feeRate;
17
+ }
18
+ function transformInput(i, utxo, txType) {
19
+ if (typeof utxo.coinbase !== 'boolean') {
20
+ throw new Error('Missing coinbase');
21
+ }
22
+ if (typeof utxo.own !== 'boolean') {
23
+ throw new Error('Missing own');
24
+ }
25
+ if (typeof utxo.confirmations !== 'number') {
26
+ throw new Error('Missing confirmations');
27
+ }
28
+ const value = (0, coinselectUtils_1.bignumberOrNaN)(utxo.amount);
29
+ if (!value) {
30
+ throw new Error('Invalid amount');
31
+ }
32
+ return Object.assign(Object.assign({}, utxo), { type: txType, i, script: { length: coinselectUtils_1.INPUT_SCRIPT_LENGTH[txType] }, value });
33
+ }
34
+ function validateAndParseUtxos(txType, { utxos }) {
35
+ if (utxos.length === 0) {
36
+ return { type: 'error', error: 'MISSING-UTXOS' };
37
+ }
38
+ const incorrectUtxoError = (index, message) => ({
39
+ type: 'error',
40
+ error: 'INCORRECT-UTXO',
41
+ message: `${message} at index ${index}`,
42
+ });
43
+ const result = [];
44
+ for (let i = 0; i < utxos.length; i++) {
45
+ try {
46
+ const csInput = transformInput(i, utxos[i], txType);
47
+ csInput.weight = (0, coinselectUtils_1.inputWeight)(csInput);
48
+ result.push(csInput);
49
+ }
50
+ catch (error) {
51
+ return incorrectUtxoError(i, error.message);
52
+ }
53
+ }
54
+ return result;
55
+ }
56
+ function transformOutput(output, txType, network) {
57
+ const script = { length: coinselectUtils_1.OUTPUT_SCRIPT_LENGTH[txType] };
58
+ if (output.type === 'payment') {
59
+ const value = (0, coinselectUtils_1.bignumberOrNaN)(output.amount);
60
+ if (!value)
61
+ throw new Error('Invalid amount');
62
+ return {
63
+ value,
64
+ script: (0, address_1.toOutputScript)(output.address, network),
65
+ };
66
+ }
67
+ if (output.type === 'payment-noaddress') {
68
+ const value = (0, coinselectUtils_1.bignumberOrNaN)(output.amount);
69
+ if (!value)
70
+ throw new Error('Invalid amount');
71
+ return {
72
+ value,
73
+ script,
74
+ };
75
+ }
76
+ if (output.type === 'opreturn') {
77
+ return {
78
+ value: (0, coinselectUtils_1.bignumberOrNaN)('0', true),
79
+ script: (0, embed_1.p2data)({ data: [Buffer.from(output.dataHex, 'hex')] }).output,
80
+ };
81
+ }
82
+ if (output.type === 'send-max') {
83
+ return {
84
+ script: (0, address_1.toOutputScript)(output.address, network),
85
+ };
86
+ }
87
+ if (output.type === 'send-max-noaddress') {
88
+ return {
89
+ script,
90
+ };
91
+ }
92
+ throw new Error('Unknown output type');
93
+ }
94
+ function validateAndParseOutputs(txType, { outputs, network }) {
95
+ if (outputs.length === 0) {
96
+ return { type: 'error', error: 'MISSING-OUTPUTS' };
97
+ }
98
+ const incorrectOutputError = (index, message) => ({
99
+ type: 'error',
100
+ error: 'INCORRECT-OUTPUT',
101
+ message: `${message} at index ${index}`,
102
+ });
103
+ let sendMaxOutputIndex = -1;
104
+ const result = [];
105
+ for (let i = 0; i < outputs.length; i++) {
106
+ const output = outputs[i];
107
+ if (output.type === 'send-max-noaddress' || output.type === 'send-max') {
108
+ if (sendMaxOutputIndex >= 0) {
109
+ return incorrectOutputError(i, 'Multiple send-max');
110
+ }
111
+ sendMaxOutputIndex = i;
112
+ }
113
+ try {
114
+ const csOutput = transformOutput(output, txType, network);
115
+ csOutput.weight = (0, coinselectUtils_1.outputWeight)(csOutput);
116
+ result.push(csOutput);
117
+ }
118
+ catch (error) {
119
+ return incorrectOutputError(i, error.message);
120
+ }
121
+ }
122
+ return {
123
+ outputs: result,
124
+ sendMaxOutputIndex,
125
+ };
126
+ }
127
+ function validateAndParseChangeOutput(txType, { changeAddress, network }) {
128
+ try {
129
+ return transformOutput(Object.assign({ type: 'send-max' }, changeAddress), txType, network);
130
+ }
131
+ catch (error) {
132
+ return {
133
+ type: 'error',
134
+ error: 'INCORRECT-OUTPUT',
135
+ message: error.message,
136
+ };
137
+ }
138
+ }
139
+ function validateAndParseRequest(request) {
140
+ const feeRate = validateAndParseFeeRate(request.feeRate);
141
+ if (!feeRate) {
142
+ return { type: 'error', error: 'INCORRECT-FEE-RATE' };
143
+ }
144
+ const longTermFeeRate = validateAndParseFeeRate(request.longTermFeeRate);
145
+ if (request.longTermFeeRate != null && !longTermFeeRate) {
146
+ return { type: 'error', error: 'INCORRECT-FEE-RATE' };
147
+ }
148
+ const txType = request.txType || 'p2pkh';
149
+ const inputs = validateAndParseUtxos(txType, request);
150
+ if ('error' in inputs) {
151
+ return inputs;
152
+ }
153
+ const outputs = validateAndParseOutputs(txType, request);
154
+ if ('error' in outputs) {
155
+ return outputs;
156
+ }
157
+ const changeOutput = validateAndParseChangeOutput(txType, request);
158
+ if ('error' in changeOutput) {
159
+ return changeOutput;
160
+ }
161
+ const feePolicy = (0, coinselectUtils_1.getFeePolicy)(request.network);
162
+ return Object.assign(Object.assign({ txType,
163
+ inputs }, outputs), { changeOutput,
164
+ feeRate,
165
+ feePolicy,
166
+ longTermFeeRate, dustThreshold: request.dustThreshold, baseFee: request.baseFee, floorBaseFee: request.floorBaseFee, sortingStrategy: request.sortingStrategy });
167
+ }
168
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1,4 @@
1
+ import { CoinSelectRequest, CoinSelectResult, ComposeRequest, ComposeInput, ComposeOutput, ComposeChangeAddress, ComposeResult, ComposeResultError } from '../types';
2
+ export declare function getErrorResult(error: unknown): ComposeResultError;
3
+ export declare function getResult<Input extends ComposeInput, Output extends ComposeOutput, Change extends ComposeChangeAddress>(request: ComposeRequest<Input, Output, Change>, { sendMaxOutputIndex }: CoinSelectRequest, result: CoinSelectResult): ComposeResult<Input, Output, Change>;
4
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getErrorResult = getErrorResult;
4
+ exports.getResult = getResult;
5
+ const tslib_1 = require("tslib");
6
+ const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
7
+ const coinselectUtils_1 = require("../coinselect/coinselectUtils");
8
+ const transaction_1 = require("./transaction");
9
+ const types_1 = require("../types");
10
+ function getErrorResult(error) {
11
+ const message = error instanceof Error ? error.message : `${error}`;
12
+ const known = types_1.COMPOSE_ERROR_TYPES.find(e => e === message);
13
+ if (known) {
14
+ return { type: 'error', error: known };
15
+ }
16
+ return { type: 'error', error: 'COINSELECT', message };
17
+ }
18
+ function splitByCompleteness(outputs) {
19
+ const complete = [];
20
+ const incomplete = [];
21
+ outputs.forEach(output => {
22
+ if (output.type === 'payment' || output.type === 'send-max' || output.type === 'opreturn') {
23
+ complete.push(output);
24
+ }
25
+ else {
26
+ incomplete.push(output);
27
+ }
28
+ });
29
+ return {
30
+ complete,
31
+ incomplete,
32
+ };
33
+ }
34
+ function getResult(request, { sendMaxOutputIndex }, result) {
35
+ if (!result.inputs || !result.outputs) {
36
+ return { type: 'error', error: 'NOT-ENOUGH-FUNDS' };
37
+ }
38
+ const totalSpent = result.outputs.reduce((total, output, index) => {
39
+ if (request.outputs[index]) {
40
+ return total.add(output.value);
41
+ }
42
+ return total;
43
+ }, new bn_js_1.default(result.fee));
44
+ const max = sendMaxOutputIndex >= 0 ? result.outputs[sendMaxOutputIndex].value.toString() : undefined;
45
+ const bytes = (0, coinselectUtils_1.transactionBytes)(result.inputs, result.outputs);
46
+ const feePerByte = result.fee / bytes;
47
+ const { complete, incomplete } = splitByCompleteness(request.outputs);
48
+ if (incomplete.length > 0) {
49
+ const inputs = result.inputs.map(input => request.utxos[input.i]);
50
+ return {
51
+ type: 'nonfinal',
52
+ fee: result.fee.toString(),
53
+ feePerByte: feePerByte.toString(),
54
+ bytes,
55
+ max,
56
+ totalSpent: totalSpent.toString(),
57
+ inputs,
58
+ };
59
+ }
60
+ const transaction = (0, transaction_1.createTransaction)(Object.assign(Object.assign({}, request), { outputs: complete }), result);
61
+ return Object.assign({ type: 'final', fee: result.fee.toString(), feePerByte: feePerByte.toString(), bytes,
62
+ max, totalSpent: totalSpent.toString() }, transaction);
63
+ }
64
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1,3 @@
1
+ import { SortingStrategy } from './sortingStrategy';
2
+ export declare const bip69SortingStrategy: SortingStrategy;
3
+ //# sourceMappingURL=bip69SortingStrategy.d.ts.map
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bip69SortingStrategy = void 0;
4
+ const convertOutput_1 = require("./convertOutput");
5
+ function inputComparator(a, b) {
6
+ return Buffer.from(a.txid, 'hex').compare(Buffer.from(b.txid, 'hex')) || a.vout - b.vout;
7
+ }
8
+ function outputComparator(a, b) {
9
+ return (a.value.cmp(b.value) ||
10
+ (Buffer.isBuffer(a.script) && Buffer.isBuffer(b.script)
11
+ ? a.script.compare(b.script)
12
+ : a.script.length - b.script.length));
13
+ }
14
+ const bip69SortingStrategy = ({ result, request, convertedInputs }) => {
15
+ const defaultPermutation = [];
16
+ const convertedOutputs = result.outputs.map((output, index) => {
17
+ defaultPermutation.push(index);
18
+ if (request.outputs[index]) {
19
+ return (0, convertOutput_1.convertOutput)(output, request.outputs[index]);
20
+ }
21
+ return (0, convertOutput_1.convertOutput)(output, Object.assign({ type: 'change' }, request.changeAddress));
22
+ });
23
+ const permutation = defaultPermutation.sort((a, b) => outputComparator(result.outputs[a], result.outputs[b]));
24
+ const sortedOutputs = permutation.map(index => convertedOutputs[index]);
25
+ return {
26
+ inputs: convertedInputs.sort(inputComparator),
27
+ outputs: sortedOutputs,
28
+ outputsPermutation: permutation,
29
+ };
30
+ };
31
+ exports.bip69SortingStrategy = bip69SortingStrategy;
32
+ //# sourceMappingURL=bip69SortingStrategy.js.map
@@ -0,0 +1,13 @@
1
+ import { CoinSelectOutputFinal, ComposeChangeAddress, ComposeFinalOutput } from '../../types';
2
+ export declare const convertOutput: (selectedOutput: CoinSelectOutputFinal, composeOutput: ComposeFinalOutput | ({
3
+ type: "change";
4
+ } & ComposeChangeAddress)) => import("../../types").ComposeOutputOpreturn | {
5
+ amount: string;
6
+ type: "change";
7
+ address: string;
8
+ } | {
9
+ type: "payment";
10
+ amount: string;
11
+ address: string;
12
+ };
13
+ //# sourceMappingURL=convertOutput.d.ts.map
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertOutput = void 0;
4
+ const convertOutput = (selectedOutput, composeOutput) => {
5
+ if (composeOutput.type === 'change') {
6
+ return Object.assign(Object.assign({}, composeOutput), { amount: selectedOutput.value.toString() });
7
+ }
8
+ if (composeOutput.type === 'opreturn') {
9
+ return composeOutput;
10
+ }
11
+ return Object.assign(Object.assign({}, composeOutput), { type: 'payment', amount: selectedOutput.value.toString() });
12
+ };
13
+ exports.convertOutput = convertOutput;
14
+ //# sourceMappingURL=convertOutput.js.map
@@ -0,0 +1,3 @@
1
+ import { SortingStrategy } from './sortingStrategy';
2
+ export declare const noneSortingStrategy: SortingStrategy;
3
+ //# sourceMappingURL=noneSortingStrategy.d.ts.map
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noneSortingStrategy = void 0;
4
+ const convertOutput_1 = require("./convertOutput");
5
+ const noneSortingStrategy = ({ result, request, convertedInputs }) => {
6
+ const convertedOutputs = result.outputs.map((output, index) => {
7
+ if (request.outputs[index]) {
8
+ return (0, convertOutput_1.convertOutput)(output, request.outputs[index]);
9
+ }
10
+ return (0, convertOutput_1.convertOutput)(output, Object.assign({ type: 'change' }, request.changeAddress));
11
+ });
12
+ return {
13
+ inputs: convertedInputs,
14
+ outputs: convertedOutputs,
15
+ outputsPermutation: Array.from(convertedOutputs.keys()),
16
+ };
17
+ };
18
+ exports.noneSortingStrategy = noneSortingStrategy;
19
+ //# sourceMappingURL=noneSortingStrategy.js.map
@@ -0,0 +1,3 @@
1
+ import { SortingStrategy } from './sortingStrategy';
2
+ export declare const randomSortingStrategy: SortingStrategy;
3
+ //# sourceMappingURL=randomSortingStrategy.d.ts.map