@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.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/accumulative.js +38 -0
- package/blackjack.js +35 -0
- package/break.js +34 -0
- package/index.js +21 -0
- package/package.json +82 -0
- package/split.js +45 -0
- package/third-party/utils/addDashesToSpaces.d.ts +2 -0
- package/third-party/utils/addDashesToSpaces.js +6 -0
- package/third-party/utils/arrayDistinct.d.ts +2 -0
- package/third-party/utils/arrayDistinct.js +6 -0
- package/third-party/utils/arrayPartition.d.ts +7 -0
- package/third-party/utils/arrayPartition.js +6 -0
- package/third-party/utils/arrayShuffle.d.ts +4 -0
- package/third-party/utils/arrayShuffle.js +13 -0
- package/third-party/utils/arrayToDictionary.d.ts +10 -0
- package/third-party/utils/arrayToDictionary.js +27 -0
- package/third-party/utils/asciiUtils.d.ts +3 -0
- package/third-party/utils/asciiUtils.js +16 -0
- package/third-party/utils/bigNumber.d.ts +5 -0
- package/third-party/utils/bigNumber.js +9 -0
- package/third-party/utils/bufferUtils.d.ts +3 -0
- package/third-party/utils/bufferUtils.js +23 -0
- package/third-party/utils/bytesToHumanReadable.d.ts +2 -0
- package/third-party/utils/bytesToHumanReadable.js +15 -0
- package/third-party/utils/capitalizeFirstLetter.d.ts +2 -0
- package/third-party/utils/capitalizeFirstLetter.js +6 -0
- package/third-party/utils/cloneObject.d.ts +2 -0
- package/third-party/utils/cloneObject.js +32 -0
- package/third-party/utils/countBytesInString.d.ts +2 -0
- package/third-party/utils/countBytesInString.js +6 -0
- package/third-party/utils/createCooldown.d.ts +2 -0
- package/third-party/utils/createCooldown.js +16 -0
- package/third-party/utils/createDeferred.d.ts +14 -0
- package/third-party/utils/createDeferred.js +19 -0
- package/third-party/utils/createDeferredManager.d.ts +19 -0
- package/third-party/utils/createDeferredManager.js +67 -0
- package/third-party/utils/createLazy.d.ts +7 -0
- package/third-party/utils/createLazy.js +42 -0
- package/third-party/utils/createTimeoutPromise.d.ts +2 -0
- package/third-party/utils/createTimeoutPromise.js +6 -0
- package/third-party/utils/enumUtils.d.ts +6 -0
- package/third-party/utils/enumUtils.js +12 -0
- package/third-party/utils/extractUrlsFromText.d.ts +5 -0
- package/third-party/utils/extractUrlsFromText.js +28 -0
- package/third-party/utils/getLocaleSeparators.d.ts +5 -0
- package/third-party/utils/getLocaleSeparators.js +13 -0
- package/third-party/utils/getLocaleSeparators.native.d.ts +5 -0
- package/third-party/utils/getLocaleSeparators.native.js +17 -0
- package/third-party/utils/getMutex.d.ts +2 -0
- package/third-party/utils/getMutex.js +22 -0
- package/third-party/utils/getNumberFromPixelString.d.ts +2 -0
- package/third-party/utils/getNumberFromPixelString.js +6 -0
- package/third-party/utils/getRandomInt.d.ts +2 -0
- package/third-party/utils/getRandomInt.js +56 -0
- package/third-party/utils/getSynchronize.d.ts +4 -0
- package/third-party/utils/getSynchronize.js +10 -0
- package/third-party/utils/getWeakRandomId.d.ts +2 -0
- package/third-party/utils/getWeakRandomId.js +13 -0
- package/third-party/utils/getWeakRandomInt.d.ts +2 -0
- package/third-party/utils/getWeakRandomInt.js +11 -0
- package/third-party/utils/getWeakRandomNumberInRange.d.ts +2 -0
- package/third-party/utils/getWeakRandomNumberInRange.js +6 -0
- package/third-party/utils/hasUppercaseLetter.d.ts +2 -0
- package/third-party/utils/hasUppercaseLetter.js +7 -0
- package/third-party/utils/index.d.ts +52 -0
- package/third-party/utils/index.js +56 -0
- package/third-party/utils/isArrayMember.d.ts +2 -0
- package/third-party/utils/isArrayMember.js +6 -0
- package/third-party/utils/isFullPath.d.ts +2 -0
- package/third-party/utils/isFullPath.js +9 -0
- package/third-party/utils/isHex.d.ts +2 -0
- package/third-party/utils/isHex.js +9 -0
- package/third-party/utils/isNotUndefined.d.ts +2 -0
- package/third-party/utils/isNotUndefined.js +6 -0
- package/third-party/utils/isUrl.d.ts +2 -0
- package/third-party/utils/isUrl.js +7 -0
- package/third-party/utils/logs.d.ts +32 -0
- package/third-party/utils/logs.js +82 -0
- package/third-party/utils/logsManager.d.ts +17 -0
- package/third-party/utils/logsManager.js +53 -0
- package/third-party/utils/mergeDeepObject.d.ts +26 -0
- package/third-party/utils/mergeDeepObject.js +68 -0
- package/third-party/utils/objectPartition.d.ts +6 -0
- package/third-party/utils/objectPartition.js +12 -0
- package/third-party/utils/parseElectrumUrl.d.ts +6 -0
- package/third-party/utils/parseElectrumUrl.js +17 -0
- package/third-party/utils/parseHostname.d.ts +2 -0
- package/third-party/utils/parseHostname.js +7 -0
- package/third-party/utils/promiseAllSequence.d.ts +2 -0
- package/third-party/utils/promiseAllSequence.js +14 -0
- package/third-party/utils/redactUserPath.d.ts +3 -0
- package/third-party/utils/redactUserPath.js +7 -0
- package/third-party/utils/scheduleAction.d.ts +15 -0
- package/third-party/utils/scheduleAction.js +125 -0
- package/third-party/utils/splitStringEveryNCharacters.d.ts +2 -0
- package/third-party/utils/splitStringEveryNCharacters.js +12 -0
- package/third-party/utils/throttler.d.ts +11 -0
- package/third-party/utils/throttler.js +38 -0
- package/third-party/utils/throwError.d.ts +2 -0
- package/third-party/utils/throwError.js +8 -0
- package/third-party/utils/topologicalSort.d.ts +2 -0
- package/third-party/utils/topologicalSort.js +19 -0
- package/third-party/utils/truncateMiddle.d.ts +2 -0
- package/third-party/utils/truncateMiddle.js +12 -0
- package/third-party/utils/typedEventEmitter.d.ts +23 -0
- package/third-party/utils/typedEventEmitter.js +11 -0
- package/third-party/utils/urlToOnion.d.ts +4 -0
- package/third-party/utils/urlToOnion.js +12 -0
- package/third-party/utils/versionUtils.d.ts +9 -0
- package/third-party/utils/versionUtils.js +28 -0
- package/third-party/utils/xssFilters.d.ts +4 -0
- package/third-party/utils/xssFilters.js +13 -0
- package/third-party/utxo-lib/address.d.ts +17 -0
- package/third-party/utxo-lib/address.js +174 -0
- package/third-party/utxo-lib/bip32.d.ts +28 -0
- package/third-party/utxo-lib/bip32.js +253 -0
- package/third-party/utxo-lib/bs58check.d.ts +12 -0
- package/third-party/utxo-lib/bs58check.js +92 -0
- package/third-party/utxo-lib/bufferutils.d.ts +59 -0
- package/third-party/utxo-lib/bufferutils.js +219 -0
- package/third-party/utxo-lib/coinselect/coinselectUtils.d.ts +53 -0
- package/third-party/utxo-lib/coinselect/coinselectUtils.js +232 -0
- package/third-party/utxo-lib/coinselect/index.d.ts +3 -0
- package/third-party/utxo-lib/coinselect/index.js +19 -0
- package/third-party/utxo-lib/coinselect/inputs/accumulative.d.ts +3 -0
- package/third-party/utxo-lib/coinselect/inputs/accumulative.js +55 -0
- package/third-party/utxo-lib/coinselect/inputs/branchAndBound.d.ts +3 -0
- package/third-party/utxo-lib/coinselect/inputs/branchAndBound.js +123 -0
- package/third-party/utxo-lib/coinselect/outputs/split.d.ts +3 -0
- package/third-party/utxo-lib/coinselect/outputs/split.js +35 -0
- package/third-party/utxo-lib/coinselect/tryconfirmed.d.ts +3 -0
- package/third-party/utxo-lib/coinselect/tryconfirmed.js +65 -0
- package/third-party/utxo-lib/compose/index.d.ts +3 -0
- package/third-party/utxo-lib/compose/index.js +20 -0
- package/third-party/utxo-lib/compose/request.d.ts +5 -0
- package/third-party/utxo-lib/compose/request.js +168 -0
- package/third-party/utxo-lib/compose/result.d.ts +4 -0
- package/third-party/utxo-lib/compose/result.js +64 -0
- package/third-party/utxo-lib/compose/sorting/bip69SortingStrategy.d.ts +3 -0
- package/third-party/utxo-lib/compose/sorting/bip69SortingStrategy.js +32 -0
- package/third-party/utxo-lib/compose/sorting/convertOutput.d.ts +13 -0
- package/third-party/utxo-lib/compose/sorting/convertOutput.js +14 -0
- package/third-party/utxo-lib/compose/sorting/noneSortingStrategy.d.ts +3 -0
- package/third-party/utxo-lib/compose/sorting/noneSortingStrategy.js +19 -0
- package/third-party/utxo-lib/compose/sorting/randomSortingStrategy.d.ts +3 -0
- package/third-party/utxo-lib/compose/sorting/randomSortingStrategy.js +28 -0
- package/third-party/utxo-lib/compose/sorting/sortingStrategy.d.ts +9 -0
- package/third-party/utxo-lib/compose/sorting/sortingStrategy.js +3 -0
- package/third-party/utxo-lib/compose/transaction.d.ts +3 -0
- package/third-party/utxo-lib/compose/transaction.js +16 -0
- package/third-party/utxo-lib/crypto.d.ts +9 -0
- package/third-party/utxo-lib/crypto.js +44 -0
- package/third-party/utxo-lib/derivation.d.ts +24 -0
- package/third-party/utxo-lib/derivation.js +121 -0
- package/third-party/utxo-lib/discovery.d.ts +9 -0
- package/third-party/utxo-lib/discovery.js +30 -0
- package/third-party/utxo-lib/global.d.ts +62 -0
- package/third-party/utxo-lib/global.js +2 -0
- package/third-party/utxo-lib/index.d.ts +18 -0
- package/third-party/utxo-lib/index.js +28 -0
- package/third-party/utxo-lib/networks.d.ts +45 -0
- package/third-party/utxo-lib/networks.js +250 -0
- package/third-party/utxo-lib/payments/embed.d.ts +3 -0
- package/third-party/utxo-lib/payments/embed.js +49 -0
- package/third-party/utxo-lib/payments/index.d.ts +13 -0
- package/third-party/utxo-lib/payments/index.js +28 -0
- package/third-party/utxo-lib/payments/lazy.d.ts +3 -0
- package/third-party/utxo-lib/payments/lazy.js +33 -0
- package/third-party/utxo-lib/payments/p2ms.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2ms.js +139 -0
- package/third-party/utxo-lib/payments/p2pk.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2pk.js +72 -0
- package/third-party/utxo-lib/payments/p2pkh.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2pkh.js +132 -0
- package/third-party/utxo-lib/payments/p2sh.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2sh.js +171 -0
- package/third-party/utxo-lib/payments/p2tr.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2tr.js +123 -0
- package/third-party/utxo-lib/payments/p2wpkh.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2wpkh.js +137 -0
- package/third-party/utxo-lib/payments/p2wsh.d.ts +3 -0
- package/third-party/utxo-lib/payments/p2wsh.js +183 -0
- package/third-party/utxo-lib/payments/sstxchange.d.ts +3 -0
- package/third-party/utxo-lib/payments/sstxchange.js +79 -0
- package/third-party/utxo-lib/payments/sstxcommitment.d.ts +3 -0
- package/third-party/utxo-lib/payments/sstxcommitment.js +74 -0
- package/third-party/utxo-lib/payments/sstxpkh.d.ts +3 -0
- package/third-party/utxo-lib/payments/sstxpkh.js +79 -0
- package/third-party/utxo-lib/payments/sstxsh.d.ts +3 -0
- package/third-party/utxo-lib/payments/sstxsh.js +70 -0
- package/third-party/utxo-lib/script/index.d.ts +17 -0
- package/third-party/utxo-lib/script/index.js +167 -0
- package/third-party/utxo-lib/script/ops.d.ts +4 -0
- package/third-party/utxo-lib/script/ops.js +13 -0
- package/third-party/utxo-lib/script/scriptNumber.d.ts +3 -0
- package/third-party/utxo-lib/script/scriptNumber.js +62 -0
- package/third-party/utxo-lib/script/scriptSignature.d.ts +8 -0
- package/third-party/utxo-lib/script/scriptSignature.js +55 -0
- package/third-party/utxo-lib/transaction/base.d.ts +56 -0
- package/third-party/utxo-lib/transaction/base.js +108 -0
- package/third-party/utxo-lib/transaction/bitcoin.d.ts +4 -0
- package/third-party/utxo-lib/transaction/bitcoin.js +109 -0
- package/third-party/utxo-lib/transaction/dash.d.ts +8 -0
- package/third-party/utxo-lib/transaction/dash.js +105 -0
- package/third-party/utxo-lib/transaction/decred.d.ts +4 -0
- package/third-party/utxo-lib/transaction/decred.js +149 -0
- package/third-party/utxo-lib/transaction/index.d.ts +15 -0
- package/third-party/utxo-lib/transaction/index.js +44 -0
- package/third-party/utxo-lib/transaction/peercoin.d.ts +4 -0
- package/third-party/utxo-lib/transaction/peercoin.js +64 -0
- package/third-party/utxo-lib/transaction/zcash.d.ts +66 -0
- package/third-party/utxo-lib/transaction/zcash.js +451 -0
- package/third-party/utxo-lib/types/coinselect.d.ts +61 -0
- package/third-party/utxo-lib/types/coinselect.js +3 -0
- package/third-party/utxo-lib/types/compose.d.ts +98 -0
- package/third-party/utxo-lib/types/compose.js +10 -0
- package/third-party/utxo-lib/types/index.d.ts +5 -0
- package/third-party/utxo-lib/types/index.js +8 -0
- package/third-party/utxo-lib/types/payments.d.ts +30 -0
- package/third-party/utxo-lib/types/payments.js +3 -0
- package/third-party/utxo-lib/types/typeforce.d.ts +8 -0
- package/third-party/utxo-lib/types/typeforce.js +16 -0
- package/third-party/utxo-lib/vsize.d.ts +13 -0
- package/third-party/utxo-lib/vsize.js +50 -0
- package/utils.js +78 -0
- package/witness.d.ts +70 -0
- package/witness.js +30 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.typeforce = exports.anyOf = exports.Nil = exports.BufferN = exports.Function = exports.UInt32 = exports.UInt16 = exports.UInt8 = exports.tuple = exports.maybe = exports.Hex = exports.Buffer = exports.String = exports.Boolean = exports.Array = exports.Number = exports.Hash256bit = exports.Hash160bit = exports.Buffer256bit = void 0;
|
|
4
|
+
exports.Satoshi = Satoshi;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const typeforce_1 = tslib_1.__importDefault(require("typeforce"));
|
|
7
|
+
exports.typeforce = typeforce_1.default;
|
|
8
|
+
const SATOSHI_MAX = 21 * 1e14;
|
|
9
|
+
function Satoshi(value) {
|
|
10
|
+
return typeforce_1.default.UInt53(value) && value <= SATOSHI_MAX;
|
|
11
|
+
}
|
|
12
|
+
exports.Buffer256bit = typeforce_1.default.BufferN(32);
|
|
13
|
+
exports.Hash160bit = typeforce_1.default.BufferN(20);
|
|
14
|
+
exports.Hash256bit = typeforce_1.default.BufferN(32);
|
|
15
|
+
exports.Number = typeforce_1.default.Number, exports.Array = typeforce_1.default.Array, exports.Boolean = typeforce_1.default.Boolean, exports.String = typeforce_1.default.String, exports.Buffer = typeforce_1.default.Buffer, exports.Hex = typeforce_1.default.Hex, exports.maybe = typeforce_1.default.maybe, exports.tuple = typeforce_1.default.tuple, exports.UInt8 = typeforce_1.default.UInt8, exports.UInt16 = typeforce_1.default.UInt16, exports.UInt32 = typeforce_1.default.UInt32, exports.Function = typeforce_1.default.Function, exports.BufferN = typeforce_1.default.BufferN, exports.Nil = typeforce_1.default.Nil, exports.anyOf = typeforce_1.default.anyOf;
|
|
16
|
+
//# sourceMappingURL=typeforce.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Network } from './networks';
|
|
2
|
+
export declare const getTransactionVbytesFromAddresses: (inputs: string[], outputs: string[], network: Network) => number;
|
|
3
|
+
type GetTransactionVbytesParam = {
|
|
4
|
+
vin: {
|
|
5
|
+
addresses?: string[];
|
|
6
|
+
}[];
|
|
7
|
+
vout: {
|
|
8
|
+
addresses?: string[];
|
|
9
|
+
}[];
|
|
10
|
+
};
|
|
11
|
+
export declare const getTransactionVbytes: ({ vin, vout }: GetTransactionVbytesParam, network: Network) => number;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=vsize.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTransactionVbytes = exports.getTransactionVbytesFromAddresses = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const BitcoinJsAddress = tslib_1.__importStar(require("./address"));
|
|
6
|
+
const coinselectUtils_1 = require("./coinselect/coinselectUtils");
|
|
7
|
+
const address_1 = require("./address");
|
|
8
|
+
const isKnownInputAddress = (type) => type in coinselectUtils_1.INPUT_SCRIPT_LENGTH;
|
|
9
|
+
const toVin = (network) => (address) => {
|
|
10
|
+
const type = (0, address_1.getAddressType)(address, network);
|
|
11
|
+
if (isKnownInputAddress(type)) {
|
|
12
|
+
return {
|
|
13
|
+
type,
|
|
14
|
+
script: { length: coinselectUtils_1.INPUT_SCRIPT_LENGTH[type] },
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`Unknown input address '${address}'`);
|
|
18
|
+
};
|
|
19
|
+
const toVout = (network) => (address) => {
|
|
20
|
+
var _a;
|
|
21
|
+
let length;
|
|
22
|
+
try {
|
|
23
|
+
length = BitcoinJsAddress.toOutputScript(address, network).length;
|
|
24
|
+
}
|
|
25
|
+
catch (_b) {
|
|
26
|
+
const msg = (_a = address.match(/^OP_RETURN (.*)$/)) === null || _a === void 0 ? void 0 : _a.pop();
|
|
27
|
+
if (msg) {
|
|
28
|
+
length = msg.match(/^\(.*\)$/)
|
|
29
|
+
? msg.length
|
|
30
|
+
: 2 + msg.length / 2;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
length = 0;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { script: { length } };
|
|
37
|
+
};
|
|
38
|
+
const getTransactionVbytesFromAddresses = (inputs, outputs, network) => {
|
|
39
|
+
const ins = inputs.map(toVin(network));
|
|
40
|
+
const outs = outputs.map(toVout(network));
|
|
41
|
+
return (0, coinselectUtils_1.transactionBytes)(ins, outs);
|
|
42
|
+
};
|
|
43
|
+
exports.getTransactionVbytesFromAddresses = getTransactionVbytesFromAddresses;
|
|
44
|
+
const getTransactionVbytes = ({ vin, vout }, network) => {
|
|
45
|
+
const ins = vin.map(({ addresses = [] }) => { var _a; return (_a = addresses[0]) !== null && _a !== void 0 ? _a : ''; });
|
|
46
|
+
const outs = vout.map(({ addresses = [] }) => { var _a; return (_a = addresses[0]) !== null && _a !== void 0 ? _a : ''; });
|
|
47
|
+
return (0, exports.getTransactionVbytesFromAddresses)(ins, outs, network);
|
|
48
|
+
};
|
|
49
|
+
exports.getTransactionVbytes = getTransactionVbytes;
|
|
50
|
+
//# sourceMappingURL=vsize.js.map
|
package/utils.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// baseline estimates, used to improve performance
|
|
2
|
+
var TX_EMPTY_SIZE = 4 + 1 + 1 + 4
|
|
3
|
+
var TX_INPUT_BASE = 32 + 4 + 1 + 4
|
|
4
|
+
var TX_INPUT_PUBKEYHASH = 107
|
|
5
|
+
var TX_OUTPUT_BASE = 8 + 1
|
|
6
|
+
var TX_OUTPUT_PUBKEYHASH = 25
|
|
7
|
+
var OP_RETURN_OVERHEAD = 1 + 8 + 1
|
|
8
|
+
|
|
9
|
+
function inputBytes (input) {
|
|
10
|
+
return TX_INPUT_BASE + (input.script ? input.script.length : TX_INPUT_PUBKEYHASH)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function outputBytes (output) {
|
|
14
|
+
if (output.script) {
|
|
15
|
+
return OP_RETURN_OVERHEAD + output.script.length + (output.script.length >= 74 ? 2 : 1)
|
|
16
|
+
}
|
|
17
|
+
return TX_OUTPUT_BASE + (output.script ? output.script.length : TX_OUTPUT_PUBKEYHASH)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function dustThreshold (output, feeRate) {
|
|
21
|
+
/* ... classify the output for input estimate */
|
|
22
|
+
return inputBytes({}) * feeRate
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function transactionBytes (inputs, outputs) {
|
|
26
|
+
return TX_EMPTY_SIZE +
|
|
27
|
+
inputs.reduce(function (a, x) { return a + inputBytes(x) }, 0) +
|
|
28
|
+
outputs.reduce(function (a, x) { return a + outputBytes(x) }, 0)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function uintOrNaN (v) {
|
|
32
|
+
if (typeof v !== 'number') return NaN
|
|
33
|
+
if (!isFinite(v)) return NaN
|
|
34
|
+
if (Math.floor(v) !== v) return NaN
|
|
35
|
+
if (v < 0) return NaN
|
|
36
|
+
return v
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function sumForgiving (range) {
|
|
40
|
+
return range.reduce(function (a, x) { return a + (isFinite(x.value) ? x.value : 0) }, 0)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sumOrNaN (range) {
|
|
44
|
+
return range.reduce(function (a, x) { return a + uintOrNaN(x.value) }, 0)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
var BLANK_OUTPUT = outputBytes({})
|
|
48
|
+
|
|
49
|
+
function finalize (inputs, outputs, feeRate) {
|
|
50
|
+
var bytesAccum = transactionBytes(inputs, outputs)
|
|
51
|
+
var feeAfterExtraOutput = feeRate * (bytesAccum + BLANK_OUTPUT)
|
|
52
|
+
var remainderAfterExtraOutput = sumOrNaN(inputs) - (sumOrNaN(outputs) + feeAfterExtraOutput)
|
|
53
|
+
|
|
54
|
+
// is it worth a change output?
|
|
55
|
+
if (remainderAfterExtraOutput > dustThreshold({}, feeRate)) {
|
|
56
|
+
outputs = outputs.concat({ value: remainderAfterExtraOutput })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var fee = sumOrNaN(inputs) - sumOrNaN(outputs)
|
|
60
|
+
if (!isFinite(fee)) return { fee: feeRate * bytesAccum }
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
inputs: inputs,
|
|
64
|
+
outputs: outputs,
|
|
65
|
+
fee: fee
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = {
|
|
70
|
+
dustThreshold: dustThreshold,
|
|
71
|
+
finalize: finalize,
|
|
72
|
+
inputBytes: inputBytes,
|
|
73
|
+
outputBytes: outputBytes,
|
|
74
|
+
sumOrNaN: sumOrNaN,
|
|
75
|
+
sumForgiving: sumForgiving,
|
|
76
|
+
transactionBytes: transactionBytes,
|
|
77
|
+
uintOrNaN: uintOrNaN
|
|
78
|
+
}
|
package/witness.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export type IPaymentType = 'p2pkh' | 'p2sh' | 'p2tr' | 'p2wpkh' | 'p2wsh';
|
|
2
|
+
export interface IUtxo {
|
|
3
|
+
vout: number;
|
|
4
|
+
txid: string;
|
|
5
|
+
amount: string;
|
|
6
|
+
coinbase: boolean;
|
|
7
|
+
own: boolean;
|
|
8
|
+
confirmations: number;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface IOutputPayment {
|
|
13
|
+
type: 'payment';
|
|
14
|
+
address: string;
|
|
15
|
+
amount: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IOutputSendMax {
|
|
19
|
+
type: 'send-max'; // only one in TX request
|
|
20
|
+
address: string;
|
|
21
|
+
amount?: typeof undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface IOutputOpreturn {
|
|
25
|
+
type: 'opreturn';
|
|
26
|
+
dataHex: string;
|
|
27
|
+
amount?: typeof undefined;
|
|
28
|
+
address?: typeof undefined;
|
|
29
|
+
}
|
|
30
|
+
export interface IOutputChange {
|
|
31
|
+
type: 'change';
|
|
32
|
+
amount: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type IFinalOutput =
|
|
36
|
+
| ComposeOutputPayment
|
|
37
|
+
| ComposeOutputSendMax
|
|
38
|
+
| ComposeOutputOpreturn;
|
|
39
|
+
|
|
40
|
+
export interface IChangeAddress {
|
|
41
|
+
address: string;
|
|
42
|
+
path: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ICoinSelectParams {
|
|
46
|
+
utxos: IUtxo[];
|
|
47
|
+
outputs: IFinalOutput[];
|
|
48
|
+
feeRate: string | number;
|
|
49
|
+
changeAddress: IChangeAddress;
|
|
50
|
+
network: any;
|
|
51
|
+
txType: IPaymentType
|
|
52
|
+
baseFee?: number;
|
|
53
|
+
dustThreshold?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ICoinSelectResult {
|
|
57
|
+
type: 'final';
|
|
58
|
+
max?: string;
|
|
59
|
+
totalSpent: string;
|
|
60
|
+
fee: string;
|
|
61
|
+
feePerByte: string;
|
|
62
|
+
bytes: number;
|
|
63
|
+
inputs: IUtxo[];
|
|
64
|
+
outputs: (IFinalOutput | IOutputChange)[];
|
|
65
|
+
outputsPermutation: number[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare function coinSelect(params: ICoinSelectParams): ICoinSelectResult;
|
|
69
|
+
|
|
70
|
+
export = coinSelect;
|
package/witness.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const { composeTx } = require('./third-party/utxo-lib')
|
|
2
|
+
|
|
3
|
+
module.exports = function coinSelect ({
|
|
4
|
+
utxos,
|
|
5
|
+
outputs,
|
|
6
|
+
feeRate,
|
|
7
|
+
changeAddress,
|
|
8
|
+
network,
|
|
9
|
+
txType,
|
|
10
|
+
baseFee = 0,
|
|
11
|
+
dustThreshold = 546
|
|
12
|
+
}) {
|
|
13
|
+
const result = composeTx({
|
|
14
|
+
utxos,
|
|
15
|
+
outputs,
|
|
16
|
+
feeRate,
|
|
17
|
+
sortingStrategy: 'random',
|
|
18
|
+
txType,
|
|
19
|
+
dustThreshold,
|
|
20
|
+
changeAddress,
|
|
21
|
+
network,
|
|
22
|
+
baseFee
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
if (result.type === 'error') {
|
|
26
|
+
throw new Error(result.message || result.error)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return result
|
|
30
|
+
}
|