@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,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeBlake = decodeBlake;
|
|
4
|
+
exports.decodeBlake256Key = decodeBlake256Key;
|
|
5
|
+
exports.decodeBlake256 = decodeBlake256;
|
|
6
|
+
exports.encodeBlake256 = encodeBlake256;
|
|
7
|
+
exports.encode = encode;
|
|
8
|
+
exports.decode = decode;
|
|
9
|
+
exports.decodeAddress = decodeAddress;
|
|
10
|
+
exports.encodeAddress = encodeAddress;
|
|
11
|
+
const tslib_1 = require("tslib");
|
|
12
|
+
const bchaddrjs_1 = tslib_1.__importDefault(require("bchaddrjs"));
|
|
13
|
+
const bs58_1 = tslib_1.__importDefault(require("bs58"));
|
|
14
|
+
const bs58check_1 = tslib_1.__importDefault(require("bs58check"));
|
|
15
|
+
const networks_1 = require("./networks");
|
|
16
|
+
const crypto_1 = require("./crypto");
|
|
17
|
+
function decodeBlake(buffer) {
|
|
18
|
+
const want = buffer.subarray(-4);
|
|
19
|
+
const payload = buffer.subarray(0, -4);
|
|
20
|
+
const got = (0, crypto_1.blake256)((0, crypto_1.blake256)(payload)).subarray(0, 4);
|
|
21
|
+
if ((want[0] ^ got[0]) | (want[1] ^ got[1]) | (want[2] ^ got[2]) | (want[3] ^ got[3]))
|
|
22
|
+
throw new Error('invalid checksum');
|
|
23
|
+
return payload;
|
|
24
|
+
}
|
|
25
|
+
function decodeBlake256Key(key) {
|
|
26
|
+
const bytes = bs58_1.default.decode(key);
|
|
27
|
+
const buffer = Buffer.from(bytes);
|
|
28
|
+
return decodeBlake(buffer);
|
|
29
|
+
}
|
|
30
|
+
function decodeBlake256(address) {
|
|
31
|
+
const bytes = bs58_1.default.decode(address);
|
|
32
|
+
const buffer = Buffer.from(bytes);
|
|
33
|
+
if (buffer.length !== 26)
|
|
34
|
+
throw new Error(`${address} invalid address length`);
|
|
35
|
+
let payload;
|
|
36
|
+
try {
|
|
37
|
+
payload = decodeBlake(buffer);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (e instanceof Error) {
|
|
41
|
+
throw new Error(`${address} ${e.message}`);
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`${address} ${e}`);
|
|
44
|
+
}
|
|
45
|
+
return payload;
|
|
46
|
+
}
|
|
47
|
+
function encodeBlake256(payload) {
|
|
48
|
+
const checksum = (0, crypto_1.blake256)((0, crypto_1.blake256)(payload)).subarray(0, 4);
|
|
49
|
+
return bs58_1.default.encode(Buffer.concat([payload, checksum]));
|
|
50
|
+
}
|
|
51
|
+
function encode(payload, network = networks_1.bitcoin) {
|
|
52
|
+
return (0, networks_1.isNetworkType)('decred', network) ? encodeBlake256(payload) : bs58check_1.default.encode(payload);
|
|
53
|
+
}
|
|
54
|
+
function decode(payload, network = networks_1.bitcoin) {
|
|
55
|
+
return (0, networks_1.isNetworkType)('decred', network) ? decodeBlake256(payload) : bs58check_1.default.decode(payload);
|
|
56
|
+
}
|
|
57
|
+
function decodeAddress(address, network = networks_1.bitcoin) {
|
|
58
|
+
let payload;
|
|
59
|
+
if ((0, networks_1.isNetworkType)('bitcoinCash', network)) {
|
|
60
|
+
if (!bchaddrjs_1.default.isCashAddress(address))
|
|
61
|
+
throw Error(`${address} is not a cash address`);
|
|
62
|
+
payload = Buffer.from(bs58check_1.default.decode(bchaddrjs_1.default.toLegacyAddress(address)));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
payload = Buffer.from(decode(address, network));
|
|
66
|
+
}
|
|
67
|
+
if (payload.length < 21)
|
|
68
|
+
throw new TypeError(`${address} is too short`);
|
|
69
|
+
if (payload.length > 22)
|
|
70
|
+
throw new TypeError(`${address} is too long`);
|
|
71
|
+
const multibyte = payload.length === 22;
|
|
72
|
+
const offset = multibyte ? 2 : 1;
|
|
73
|
+
const version = multibyte ? payload.readUInt16BE(0) : payload[0];
|
|
74
|
+
const hash = payload.subarray(offset);
|
|
75
|
+
return { version, hash };
|
|
76
|
+
}
|
|
77
|
+
function encodeAddress(hash, version, network = networks_1.bitcoin) {
|
|
78
|
+
const multibyte = version > 0xff;
|
|
79
|
+
const size = multibyte ? 22 : 21;
|
|
80
|
+
const offset = multibyte ? 2 : 1;
|
|
81
|
+
const payload = Buffer.allocUnsafe(size);
|
|
82
|
+
if (multibyte) {
|
|
83
|
+
payload.writeUInt16BE(version, 0);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
payload.writeUInt8(version, 0);
|
|
87
|
+
}
|
|
88
|
+
hash.copy(payload, offset);
|
|
89
|
+
const encoded = encode(payload, network);
|
|
90
|
+
return (0, networks_1.isNetworkType)('bitcoinCash', network) ? bchaddrjs_1.default.toCashAddress(encoded) : encoded;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=bs58check.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as varuint from 'varuint-bitcoin';
|
|
2
|
+
export declare function verifuint(value: number, max: number): void;
|
|
3
|
+
export declare function readUInt64LE(buffer: Buffer, offset: number): number;
|
|
4
|
+
export declare function readUInt64LEasString(buffer: Buffer, offset: number): string;
|
|
5
|
+
export declare function readInt64LE(buffer: Buffer, offset: number): number;
|
|
6
|
+
export declare function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
|
|
7
|
+
export declare function writeUInt64LEasString(buffer: Buffer, value: string | number, offset: number): number;
|
|
8
|
+
export declare function writeInt64LE(buffer: Buffer, value: number, offset: number): number;
|
|
9
|
+
export declare function readVarInt(buffer: Buffer, offset: number): {
|
|
10
|
+
number: number;
|
|
11
|
+
size: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function writeVarInt(buffer: Buffer, number: number, offset: number): number;
|
|
14
|
+
export declare function cloneBuffer(buffer: Buffer): Buffer;
|
|
15
|
+
type PushDataSize = (len: number) => number;
|
|
16
|
+
type ReadPushDataInt = (buffer: Buffer, offset: number) => {
|
|
17
|
+
opcode: number;
|
|
18
|
+
number: number;
|
|
19
|
+
size: number;
|
|
20
|
+
};
|
|
21
|
+
type WritePushDataInt = (buffer: Buffer, number: number, offset: number) => number;
|
|
22
|
+
export declare const pushDataSize: PushDataSize;
|
|
23
|
+
export declare const readPushDataInt: ReadPushDataInt;
|
|
24
|
+
export declare const varIntSize: typeof varuint.encodingLength;
|
|
25
|
+
export declare const writePushDataInt: WritePushDataInt;
|
|
26
|
+
export declare const reverseBuffer: (src: Buffer) => Buffer, getChunkSize: (n: number) => Buffer;
|
|
27
|
+
export declare class BufferWriter {
|
|
28
|
+
buffer: Buffer;
|
|
29
|
+
offset: number;
|
|
30
|
+
constructor(buffer: Buffer, offset?: number);
|
|
31
|
+
writeUInt8(i: number): void;
|
|
32
|
+
writeUInt16(i: number): void;
|
|
33
|
+
writeInt32(i: number): void;
|
|
34
|
+
writeUInt32(i: number): void;
|
|
35
|
+
writeInt64(i: number): void;
|
|
36
|
+
writeUInt64(i: number | string): void;
|
|
37
|
+
writeVarInt(i: number): void;
|
|
38
|
+
writeSlice(slice: Buffer): void;
|
|
39
|
+
writeVarSlice(slice: Buffer): void;
|
|
40
|
+
writeVector(vector: Buffer[]): void;
|
|
41
|
+
}
|
|
42
|
+
export declare class BufferReader {
|
|
43
|
+
buffer: Buffer;
|
|
44
|
+
offset: number;
|
|
45
|
+
constructor(buffer: Buffer, offset?: number);
|
|
46
|
+
readUInt8(): number;
|
|
47
|
+
readUInt16(): number;
|
|
48
|
+
readInt32(): number;
|
|
49
|
+
readUInt32(): number;
|
|
50
|
+
readInt64(): number;
|
|
51
|
+
readUInt64(): number;
|
|
52
|
+
readUInt64String(): string;
|
|
53
|
+
readVarInt(): number;
|
|
54
|
+
readSlice(n: number): Buffer;
|
|
55
|
+
readVarSlice(): Buffer;
|
|
56
|
+
readVector(): Buffer[];
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=bufferutils.d.ts.map
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BufferReader = exports.BufferWriter = exports.getChunkSize = exports.reverseBuffer = exports.writePushDataInt = exports.varIntSize = exports.readPushDataInt = exports.pushDataSize = void 0;
|
|
4
|
+
exports.verifuint = verifuint;
|
|
5
|
+
exports.readUInt64LE = readUInt64LE;
|
|
6
|
+
exports.readUInt64LEasString = readUInt64LEasString;
|
|
7
|
+
exports.readInt64LE = readInt64LE;
|
|
8
|
+
exports.writeUInt64LE = writeUInt64LE;
|
|
9
|
+
exports.writeUInt64LEasString = writeUInt64LEasString;
|
|
10
|
+
exports.writeInt64LE = writeInt64LE;
|
|
11
|
+
exports.readVarInt = readVarInt;
|
|
12
|
+
exports.writeVarInt = writeVarInt;
|
|
13
|
+
exports.cloneBuffer = cloneBuffer;
|
|
14
|
+
const tslib_1 = require("tslib");
|
|
15
|
+
const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
|
|
16
|
+
const pushdata_bitcoin_1 = tslib_1.__importDefault(require("pushdata-bitcoin"));
|
|
17
|
+
const varuint = tslib_1.__importStar(require("varuint-bitcoin"));
|
|
18
|
+
const int64_buffer_1 = require("int64-buffer");
|
|
19
|
+
const utils_1 = require("../utils");
|
|
20
|
+
const types = tslib_1.__importStar(require("./types"));
|
|
21
|
+
const OUT_OF_RANGE_ERROR = 'value out of range';
|
|
22
|
+
function verifuint(value, max) {
|
|
23
|
+
if (typeof value !== 'number')
|
|
24
|
+
throw new Error('cannot write a non-number as a number');
|
|
25
|
+
if (value < 0)
|
|
26
|
+
throw new Error('specified a negative value for writing an unsigned value');
|
|
27
|
+
if (value > max)
|
|
28
|
+
throw new Error(OUT_OF_RANGE_ERROR);
|
|
29
|
+
if (Math.floor(value) !== value)
|
|
30
|
+
throw new Error('value has a fractional component');
|
|
31
|
+
}
|
|
32
|
+
function readUInt64LE(buffer, offset) {
|
|
33
|
+
const a = buffer.readUInt32LE(offset);
|
|
34
|
+
let b = buffer.readUInt32LE(offset + 4);
|
|
35
|
+
b *= 0x100000000;
|
|
36
|
+
verifuint(b + a, 0x001fffffffffffff);
|
|
37
|
+
return b + a;
|
|
38
|
+
}
|
|
39
|
+
function readUInt64LEasString(buffer, offset) {
|
|
40
|
+
try {
|
|
41
|
+
const result = readUInt64LE(buffer, offset);
|
|
42
|
+
return result.toString();
|
|
43
|
+
}
|
|
44
|
+
catch (_a) {
|
|
45
|
+
const aUint = buffer.readUInt32LE(offset);
|
|
46
|
+
const bUint = buffer.readUInt32LE(offset + 4);
|
|
47
|
+
const m = new bn_js_1.default(0x100000000);
|
|
48
|
+
const a = new bn_js_1.default(aUint);
|
|
49
|
+
const b = new bn_js_1.default(bUint).mul(m);
|
|
50
|
+
return a.add(b).toString();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function readInt64LE(buffer, offset) {
|
|
54
|
+
const a = buffer.readUInt32LE(offset);
|
|
55
|
+
let b = buffer.readInt32LE(offset + 4);
|
|
56
|
+
b *= 0x100000000;
|
|
57
|
+
return b + a;
|
|
58
|
+
}
|
|
59
|
+
function writeUInt64LE(buffer, value, offset) {
|
|
60
|
+
verifuint(value, 0x001fffffffffffff);
|
|
61
|
+
buffer.writeInt32LE(value & -1, offset);
|
|
62
|
+
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4);
|
|
63
|
+
return offset + 8;
|
|
64
|
+
}
|
|
65
|
+
function writeUInt64LEasString(buffer, value, offset) {
|
|
66
|
+
if (typeof value !== 'string') {
|
|
67
|
+
return writeUInt64LE(buffer, value, offset);
|
|
68
|
+
}
|
|
69
|
+
const v = new int64_buffer_1.Int64LE(value);
|
|
70
|
+
v.toBuffer().copy(buffer, offset);
|
|
71
|
+
return offset + 8;
|
|
72
|
+
}
|
|
73
|
+
function writeInt64LE(buffer, value, offset) {
|
|
74
|
+
const v = new int64_buffer_1.Int64LE(value);
|
|
75
|
+
const a = v.toArray();
|
|
76
|
+
for (let i = 0; i < 8; i++) {
|
|
77
|
+
buffer.writeUInt8(a[i], offset + i);
|
|
78
|
+
}
|
|
79
|
+
return offset + 8;
|
|
80
|
+
}
|
|
81
|
+
function readVarInt(buffer, offset) {
|
|
82
|
+
const { numberValue, bytes } = varuint.decode(buffer, offset);
|
|
83
|
+
if (numberValue === null)
|
|
84
|
+
throw new Error(OUT_OF_RANGE_ERROR);
|
|
85
|
+
return {
|
|
86
|
+
number: numberValue,
|
|
87
|
+
size: bytes,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function writeVarInt(buffer, number, offset) {
|
|
91
|
+
const { bytes } = varuint.encode(number, buffer, offset);
|
|
92
|
+
return bytes;
|
|
93
|
+
}
|
|
94
|
+
function cloneBuffer(buffer) {
|
|
95
|
+
const clone = Buffer.allocUnsafe(buffer.length);
|
|
96
|
+
buffer.copy(clone);
|
|
97
|
+
return clone;
|
|
98
|
+
}
|
|
99
|
+
exports.pushDataSize = pushdata_bitcoin_1.default.encodingLength;
|
|
100
|
+
exports.readPushDataInt = pushdata_bitcoin_1.default.decode;
|
|
101
|
+
exports.varIntSize = varuint.encodingLength;
|
|
102
|
+
exports.writePushDataInt = pushdata_bitcoin_1.default.encode;
|
|
103
|
+
exports.reverseBuffer = utils_1.bufferUtils.reverseBuffer, exports.getChunkSize = utils_1.bufferUtils.getChunkSize;
|
|
104
|
+
class BufferWriter {
|
|
105
|
+
constructor(buffer, offset = 0) {
|
|
106
|
+
this.buffer = buffer;
|
|
107
|
+
this.offset = offset;
|
|
108
|
+
types.typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
|
|
109
|
+
}
|
|
110
|
+
writeUInt8(i) {
|
|
111
|
+
this.offset = this.buffer.writeUInt8(i, this.offset);
|
|
112
|
+
}
|
|
113
|
+
writeUInt16(i) {
|
|
114
|
+
this.offset = this.buffer.writeUInt16LE(i, this.offset);
|
|
115
|
+
}
|
|
116
|
+
writeInt32(i) {
|
|
117
|
+
this.offset = this.buffer.writeInt32LE(i, this.offset);
|
|
118
|
+
}
|
|
119
|
+
writeUInt32(i) {
|
|
120
|
+
this.offset = this.buffer.writeUInt32LE(i, this.offset);
|
|
121
|
+
}
|
|
122
|
+
writeInt64(i) {
|
|
123
|
+
this.offset = writeInt64LE(this.buffer, i, this.offset);
|
|
124
|
+
}
|
|
125
|
+
writeUInt64(i) {
|
|
126
|
+
this.offset =
|
|
127
|
+
typeof i === 'string'
|
|
128
|
+
? writeUInt64LEasString(this.buffer, i, this.offset)
|
|
129
|
+
: writeUInt64LE(this.buffer, i, this.offset);
|
|
130
|
+
}
|
|
131
|
+
writeVarInt(i) {
|
|
132
|
+
const { bytes } = varuint.encode(i, this.buffer, this.offset);
|
|
133
|
+
this.offset += bytes;
|
|
134
|
+
}
|
|
135
|
+
writeSlice(slice) {
|
|
136
|
+
if (this.buffer.length < this.offset + slice.length) {
|
|
137
|
+
throw new Error('Cannot write slice out of bounds');
|
|
138
|
+
}
|
|
139
|
+
this.offset += slice.copy(this.buffer, this.offset);
|
|
140
|
+
}
|
|
141
|
+
writeVarSlice(slice) {
|
|
142
|
+
this.writeVarInt(slice.length);
|
|
143
|
+
this.writeSlice(slice);
|
|
144
|
+
}
|
|
145
|
+
writeVector(vector) {
|
|
146
|
+
this.writeVarInt(vector.length);
|
|
147
|
+
vector.forEach((buf) => this.writeVarSlice(buf));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.BufferWriter = BufferWriter;
|
|
151
|
+
class BufferReader {
|
|
152
|
+
constructor(buffer, offset = 0) {
|
|
153
|
+
this.buffer = buffer;
|
|
154
|
+
this.offset = offset;
|
|
155
|
+
types.typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
|
|
156
|
+
}
|
|
157
|
+
readUInt8() {
|
|
158
|
+
const result = this.buffer.readUInt8(this.offset);
|
|
159
|
+
this.offset++;
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
readUInt16() {
|
|
163
|
+
const result = this.buffer.readUInt16LE(this.offset);
|
|
164
|
+
this.offset += 2;
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
readInt32() {
|
|
168
|
+
const result = this.buffer.readInt32LE(this.offset);
|
|
169
|
+
this.offset += 4;
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
readUInt32() {
|
|
173
|
+
const result = this.buffer.readUInt32LE(this.offset);
|
|
174
|
+
this.offset += 4;
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
readInt64() {
|
|
178
|
+
const result = readInt64LE(this.buffer, this.offset);
|
|
179
|
+
this.offset += 8;
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
readUInt64() {
|
|
183
|
+
const result = readUInt64LE(this.buffer, this.offset);
|
|
184
|
+
this.offset += 8;
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
readUInt64String() {
|
|
188
|
+
const result = readUInt64LEasString(this.buffer, this.offset);
|
|
189
|
+
this.offset += 8;
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
readVarInt() {
|
|
193
|
+
const { numberValue, bytes } = varuint.decode(this.buffer, this.offset);
|
|
194
|
+
if (numberValue === null)
|
|
195
|
+
throw new Error(OUT_OF_RANGE_ERROR);
|
|
196
|
+
this.offset += bytes;
|
|
197
|
+
return numberValue;
|
|
198
|
+
}
|
|
199
|
+
readSlice(n) {
|
|
200
|
+
if (this.buffer.length < this.offset + n) {
|
|
201
|
+
throw new Error('Cannot read slice out of bounds');
|
|
202
|
+
}
|
|
203
|
+
const result = this.buffer.subarray(this.offset, this.offset + n);
|
|
204
|
+
this.offset += n;
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
readVarSlice() {
|
|
208
|
+
return this.readSlice(this.readVarInt());
|
|
209
|
+
}
|
|
210
|
+
readVector() {
|
|
211
|
+
const count = this.readVarInt();
|
|
212
|
+
const vector = [];
|
|
213
|
+
for (let i = 0; i < count; i++)
|
|
214
|
+
vector.push(this.readVarSlice());
|
|
215
|
+
return vector;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.BufferReader = BufferReader;
|
|
219
|
+
//# sourceMappingURL=bufferutils.js.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import { CoinSelectPaymentType, CoinSelectAlgorithm, CoinSelectOptions, CoinSelectInput, CoinSelectOutput, CoinSelectOutputFinal } from '../types';
|
|
3
|
+
import { Network } from '../networks';
|
|
4
|
+
export declare const ZERO: BN;
|
|
5
|
+
export declare const INPUT_SCRIPT_LENGTH: Record<CoinSelectPaymentType, number>;
|
|
6
|
+
export declare const OUTPUT_SCRIPT_LENGTH: Record<CoinSelectPaymentType, number>;
|
|
7
|
+
type Vin = {
|
|
8
|
+
type: CoinSelectInput['type'];
|
|
9
|
+
script: {
|
|
10
|
+
length: number;
|
|
11
|
+
};
|
|
12
|
+
weight?: number;
|
|
13
|
+
};
|
|
14
|
+
type VinVout = {
|
|
15
|
+
script: {
|
|
16
|
+
length: number;
|
|
17
|
+
};
|
|
18
|
+
weight?: number;
|
|
19
|
+
};
|
|
20
|
+
export declare function getVarIntSize(length: number): 5 | 1 | 3 | 9;
|
|
21
|
+
export declare function inputWeight(input: Vin): number;
|
|
22
|
+
export declare function inputBytes(input: Vin): number;
|
|
23
|
+
export declare function outputWeight(output: VinVout): number;
|
|
24
|
+
export declare function outputBytes(output: VinVout): number;
|
|
25
|
+
export declare function getFeeForBytes(feeRate: number, bytes: number): number;
|
|
26
|
+
export declare function transactionWeight(inputs: Vin[], outputs: VinVout[]): number;
|
|
27
|
+
export declare function transactionBytes(inputs: Vin[], outputs: VinVout[]): number;
|
|
28
|
+
export declare function getDustAmount(feeRate: number, { txType, longTermFeeRate, dustThreshold, }: Pick<CoinSelectOptions, 'txType' | 'longTermFeeRate' | 'dustThreshold'>): number;
|
|
29
|
+
export declare function bignumberOrNaN(v?: BN | string): BN | undefined;
|
|
30
|
+
export declare function bignumberOrNaN<F extends boolean>(v?: BN | string, forgiving?: F): F extends true ? BN : BN | undefined;
|
|
31
|
+
export declare function sumOrNaN(range: {
|
|
32
|
+
value?: BN;
|
|
33
|
+
}[]): BN | undefined;
|
|
34
|
+
export declare function sumOrNaN<F extends boolean>(range: {
|
|
35
|
+
value?: BN;
|
|
36
|
+
}[], forgiving: F): F extends true ? BN : BN | undefined;
|
|
37
|
+
export declare function getFeePolicy(network?: Network): "bitcoin" | "doge" | "zcash";
|
|
38
|
+
export declare function getFee(inputs: CoinSelectInput[], outputs: CoinSelectOutput[], feeRate: number, { feePolicy, ...options }?: Partial<CoinSelectOptions>): number;
|
|
39
|
+
export declare function finalize(inputs: CoinSelectInput[], outputs: CoinSelectOutput[], feeRate: number, options: CoinSelectOptions): {
|
|
40
|
+
fee: number;
|
|
41
|
+
inputs?: undefined;
|
|
42
|
+
outputs?: undefined;
|
|
43
|
+
} | {
|
|
44
|
+
inputs: CoinSelectInput[];
|
|
45
|
+
outputs: CoinSelectOutputFinal[];
|
|
46
|
+
fee: number;
|
|
47
|
+
};
|
|
48
|
+
export declare function anyOf(algorithms: CoinSelectAlgorithm[]): CoinSelectAlgorithm;
|
|
49
|
+
export declare function utxoScore(x: CoinSelectInput, feeRate: number): BN;
|
|
50
|
+
export declare function sortByScore(feeRate: number): (a: CoinSelectInput, b: CoinSelectInput) => number;
|
|
51
|
+
export declare function filterCoinbase(utxos: CoinSelectInput[], minConfCoinbase: number): CoinSelectInput[];
|
|
52
|
+
export {};
|
|
53
|
+
//# sourceMappingURL=coinselectUtils.d.ts.map
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OUTPUT_SCRIPT_LENGTH = exports.INPUT_SCRIPT_LENGTH = exports.ZERO = void 0;
|
|
4
|
+
exports.getVarIntSize = getVarIntSize;
|
|
5
|
+
exports.inputWeight = inputWeight;
|
|
6
|
+
exports.inputBytes = inputBytes;
|
|
7
|
+
exports.outputWeight = outputWeight;
|
|
8
|
+
exports.outputBytes = outputBytes;
|
|
9
|
+
exports.getFeeForBytes = getFeeForBytes;
|
|
10
|
+
exports.transactionWeight = transactionWeight;
|
|
11
|
+
exports.transactionBytes = transactionBytes;
|
|
12
|
+
exports.getDustAmount = getDustAmount;
|
|
13
|
+
exports.bignumberOrNaN = bignumberOrNaN;
|
|
14
|
+
exports.sumOrNaN = sumOrNaN;
|
|
15
|
+
exports.getFeePolicy = getFeePolicy;
|
|
16
|
+
exports.getFee = getFee;
|
|
17
|
+
exports.finalize = finalize;
|
|
18
|
+
exports.anyOf = anyOf;
|
|
19
|
+
exports.utxoScore = utxoScore;
|
|
20
|
+
exports.sortByScore = sortByScore;
|
|
21
|
+
exports.filterCoinbase = filterCoinbase;
|
|
22
|
+
const tslib_1 = require("tslib");
|
|
23
|
+
const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
|
|
24
|
+
const networks_1 = require("../networks");
|
|
25
|
+
exports.ZERO = new bn_js_1.default(0);
|
|
26
|
+
exports.INPUT_SCRIPT_LENGTH = {
|
|
27
|
+
p2pkh: 108,
|
|
28
|
+
p2sh: 107,
|
|
29
|
+
p2tr: 65,
|
|
30
|
+
p2wpkh: 107,
|
|
31
|
+
p2wsh: 107,
|
|
32
|
+
};
|
|
33
|
+
exports.OUTPUT_SCRIPT_LENGTH = {
|
|
34
|
+
p2pkh: 25,
|
|
35
|
+
p2sh: 23,
|
|
36
|
+
p2tr: 34,
|
|
37
|
+
p2wpkh: 22,
|
|
38
|
+
p2wsh: 34,
|
|
39
|
+
};
|
|
40
|
+
const SEGWIT_INPUT_SCRIPT_TYPES = ['p2sh', 'p2tr', 'p2wpkh', 'p2wsh'];
|
|
41
|
+
const TX_BASE = 32;
|
|
42
|
+
const INPUT_SIZE = 160;
|
|
43
|
+
const DUST_RELAY_FEE_RATE = 3;
|
|
44
|
+
function getVarIntSize(length) {
|
|
45
|
+
if (length < 253)
|
|
46
|
+
return 1;
|
|
47
|
+
if (length < 65536)
|
|
48
|
+
return 3;
|
|
49
|
+
if (length < 4294967296)
|
|
50
|
+
return 5;
|
|
51
|
+
return 9;
|
|
52
|
+
}
|
|
53
|
+
function inputWeight(input) {
|
|
54
|
+
if (input.weight)
|
|
55
|
+
return input.weight;
|
|
56
|
+
let weight = INPUT_SIZE;
|
|
57
|
+
if (!SEGWIT_INPUT_SCRIPT_TYPES.includes(input.type)) {
|
|
58
|
+
weight += 4 * input.script.length;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
if (input.type === 'p2sh') {
|
|
62
|
+
weight += 4 * (2 + 22);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
weight += 4;
|
|
66
|
+
}
|
|
67
|
+
weight += 1 + input.script.length;
|
|
68
|
+
}
|
|
69
|
+
return weight;
|
|
70
|
+
}
|
|
71
|
+
function inputBytes(input) {
|
|
72
|
+
return Math.ceil(inputWeight(input) / 4);
|
|
73
|
+
}
|
|
74
|
+
function outputWeight(output) {
|
|
75
|
+
if (output.weight)
|
|
76
|
+
return output.weight;
|
|
77
|
+
return 4 * (8 + 1 + output.script.length);
|
|
78
|
+
}
|
|
79
|
+
function outputBytes(output) {
|
|
80
|
+
return Math.ceil(outputWeight(output) / 4);
|
|
81
|
+
}
|
|
82
|
+
function getFeeForBytes(feeRate, bytes) {
|
|
83
|
+
return Math.ceil(feeRate * bytes);
|
|
84
|
+
}
|
|
85
|
+
function transactionWeight(inputs, outputs) {
|
|
86
|
+
const segwitInputs = inputs.filter(i => SEGWIT_INPUT_SCRIPT_TYPES.includes(i.type)).length;
|
|
87
|
+
return (TX_BASE +
|
|
88
|
+
4 * getVarIntSize(inputs.length) +
|
|
89
|
+
inputs.reduce((x, i) => x + inputWeight(i), 0) +
|
|
90
|
+
4 * getVarIntSize(outputs.length) +
|
|
91
|
+
outputs.reduce((x, o) => x + outputWeight(o), 0) +
|
|
92
|
+
(segwitInputs ? 2 + (inputs.length - segwitInputs) : 0));
|
|
93
|
+
}
|
|
94
|
+
function transactionBytes(inputs, outputs) {
|
|
95
|
+
return Math.ceil(transactionWeight(inputs, outputs) / 4);
|
|
96
|
+
}
|
|
97
|
+
function getDustAmount(feeRate, { txType, longTermFeeRate, dustThreshold, }) {
|
|
98
|
+
const inputSize = inputBytes({
|
|
99
|
+
type: txType,
|
|
100
|
+
script: {
|
|
101
|
+
length: exports.INPUT_SCRIPT_LENGTH[txType],
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
const longTermFee = longTermFeeRate ? Math.min(feeRate, longTermFeeRate) : 0;
|
|
105
|
+
const dustRelayFeeRate = Math.max(longTermFee, DUST_RELAY_FEE_RATE);
|
|
106
|
+
return Math.max(dustThreshold || 0, getFeeForBytes(dustRelayFeeRate, inputSize));
|
|
107
|
+
}
|
|
108
|
+
function bignumberOrNaN(v, forgiving = false) {
|
|
109
|
+
if (bn_js_1.default.isBN(v))
|
|
110
|
+
return v;
|
|
111
|
+
const defaultValue = forgiving ? exports.ZERO : undefined;
|
|
112
|
+
if (!v || typeof v !== 'string' || !/^\d+$/.test(v))
|
|
113
|
+
return defaultValue;
|
|
114
|
+
try {
|
|
115
|
+
return new bn_js_1.default(v);
|
|
116
|
+
}
|
|
117
|
+
catch (_a) {
|
|
118
|
+
return defaultValue;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function sumOrNaN(range, forgiving = false) {
|
|
122
|
+
return range.reduce((a, x) => {
|
|
123
|
+
if (!a)
|
|
124
|
+
return a;
|
|
125
|
+
const value = bignumberOrNaN(x.value);
|
|
126
|
+
if (!value)
|
|
127
|
+
return forgiving ? exports.ZERO.add(a) : undefined;
|
|
128
|
+
return value.add(a);
|
|
129
|
+
}, exports.ZERO);
|
|
130
|
+
}
|
|
131
|
+
function getFeePolicy(network) {
|
|
132
|
+
if ((0, networks_1.isNetworkType)('doge', network))
|
|
133
|
+
return 'doge';
|
|
134
|
+
if ((0, networks_1.isNetworkType)('zcash', network))
|
|
135
|
+
return 'zcash';
|
|
136
|
+
return 'bitcoin';
|
|
137
|
+
}
|
|
138
|
+
function getBitcoinFee(inputs, outputs, feeRate, { baseFee = 0, floorBaseFee }) {
|
|
139
|
+
const bytes = transactionBytes(inputs, outputs);
|
|
140
|
+
const defaultFee = getFeeForBytes(feeRate, bytes);
|
|
141
|
+
return baseFee && floorBaseFee
|
|
142
|
+
?
|
|
143
|
+
baseFee * (1 + Math.floor(defaultFee / baseFee))
|
|
144
|
+
:
|
|
145
|
+
baseFee + defaultFee;
|
|
146
|
+
}
|
|
147
|
+
function getDogeFee(inputs, outputs, feeRate, _a) {
|
|
148
|
+
var { dustThreshold = 0 } = _a, options = tslib_1.__rest(_a, ["dustThreshold"]);
|
|
149
|
+
const fee = getBitcoinFee(inputs, outputs, feeRate, options);
|
|
150
|
+
const limit = new bn_js_1.default(dustThreshold);
|
|
151
|
+
const dustOutputsCount = outputs.filter(({ value }) => value && value.lt(limit)).length;
|
|
152
|
+
return fee + dustOutputsCount * dustThreshold;
|
|
153
|
+
}
|
|
154
|
+
const MARGINAL_FEE_ZAT_PER_ACTION = 5000;
|
|
155
|
+
const GRACE_ACTIONS = 2;
|
|
156
|
+
const P2PKH_STANDARD_INPUT_SIZE = 150;
|
|
157
|
+
const P2PKH_STANDARD_OUTPUT_SIZE = 34;
|
|
158
|
+
function getZcashFee(inputs, outputs, feeRate, options) {
|
|
159
|
+
const fee = getBitcoinFee(inputs, outputs, feeRate, options);
|
|
160
|
+
const txInTotalBytes = inputs.reduce((sum, i) => sum + inputBytes(i), 0);
|
|
161
|
+
const txOutTotalBytes = outputs.reduce((sum, o) => sum + outputBytes(o), 0);
|
|
162
|
+
const actions = Math.max(GRACE_ACTIONS, Math.ceil(txInTotalBytes / P2PKH_STANDARD_INPUT_SIZE), Math.ceil(txOutTotalBytes / P2PKH_STANDARD_OUTPUT_SIZE));
|
|
163
|
+
return Math.max(actions * MARGINAL_FEE_ZAT_PER_ACTION, fee);
|
|
164
|
+
}
|
|
165
|
+
function getFee(inputs, outputs, feeRate, _a = {}) {
|
|
166
|
+
var { feePolicy } = _a, options = tslib_1.__rest(_a, ["feePolicy"]);
|
|
167
|
+
switch (feePolicy) {
|
|
168
|
+
case 'doge':
|
|
169
|
+
return getDogeFee(inputs, outputs, feeRate, options);
|
|
170
|
+
case 'zcash':
|
|
171
|
+
return getZcashFee(inputs, outputs, feeRate, options);
|
|
172
|
+
default:
|
|
173
|
+
return getBitcoinFee(inputs, outputs, feeRate, options);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function finalize(inputs, outputs, feeRate, options) {
|
|
177
|
+
const changeOutput = options.changeOutput || {
|
|
178
|
+
script: { length: exports.OUTPUT_SCRIPT_LENGTH[options.txType] },
|
|
179
|
+
};
|
|
180
|
+
const fee = getFee(inputs, outputs, feeRate, options);
|
|
181
|
+
const feeAfterExtraOutput = getFee(inputs, [...outputs, changeOutput], feeRate, options);
|
|
182
|
+
const sumInputs = sumOrNaN(inputs);
|
|
183
|
+
const sumOutputs = sumOrNaN(outputs);
|
|
184
|
+
if (!sumInputs || !sumOutputs || sumInputs.sub(sumOutputs).lt(new bn_js_1.default(fee))) {
|
|
185
|
+
return { fee };
|
|
186
|
+
}
|
|
187
|
+
const remainderAfterExtraOutput = sumInputs.sub(sumOutputs.add(new bn_js_1.default(feeAfterExtraOutput)));
|
|
188
|
+
const dustAmount = getDustAmount(feeRate, options);
|
|
189
|
+
const finalOutputs = [...outputs];
|
|
190
|
+
if (remainderAfterExtraOutput.gte(new bn_js_1.default(dustAmount))) {
|
|
191
|
+
finalOutputs.push(Object.assign(Object.assign({}, changeOutput), { value: remainderAfterExtraOutput }));
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
inputs,
|
|
195
|
+
outputs: finalOutputs,
|
|
196
|
+
fee: sumInputs.sub(sumOrNaN(finalOutputs, true)).toNumber(),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function anyOf(algorithms) {
|
|
200
|
+
return (utxos, outputs, feeRate, options) => {
|
|
201
|
+
let result = { fee: 0 };
|
|
202
|
+
for (let i = 0; i < algorithms.length; i++) {
|
|
203
|
+
const algorithm = algorithms[i];
|
|
204
|
+
result = algorithm(utxos, outputs, feeRate, options);
|
|
205
|
+
if (result.inputs) {
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return result;
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function utxoScore(x, feeRate) {
|
|
213
|
+
return x.value.sub(new bn_js_1.default(getFeeForBytes(feeRate, inputBytes(x))));
|
|
214
|
+
}
|
|
215
|
+
function sortByScore(feeRate) {
|
|
216
|
+
return (a, b) => {
|
|
217
|
+
const difference = utxoScore(a, feeRate).sub(utxoScore(b, feeRate));
|
|
218
|
+
if (difference.eq(exports.ZERO)) {
|
|
219
|
+
return a.i - b.i;
|
|
220
|
+
}
|
|
221
|
+
return difference.isNeg() ? 1 : -1;
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function filterCoinbase(utxos, minConfCoinbase) {
|
|
225
|
+
return utxos.filter(utxo => {
|
|
226
|
+
if (utxo.coinbase && !utxo.required) {
|
|
227
|
+
return utxo.confirmations >= minConfCoinbase;
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=coinselectUtils.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.coinselect = coinselect;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const accumulative_1 = require("./inputs/accumulative");
|
|
6
|
+
const branchAndBound_1 = require("./inputs/branchAndBound");
|
|
7
|
+
const split_1 = require("./outputs/split");
|
|
8
|
+
const coinselectUtils_1 = require("./coinselectUtils");
|
|
9
|
+
const tryconfirmed_1 = require("./tryconfirmed");
|
|
10
|
+
function coinselect(_a) {
|
|
11
|
+
var { inputs, outputs, feeRate } = _a, options = tslib_1.__rest(_a, ["inputs", "outputs", "feeRate"]);
|
|
12
|
+
if (options.sendMaxOutputIndex >= 0) {
|
|
13
|
+
return (0, split_1.split)(inputs, outputs, feeRate, options);
|
|
14
|
+
}
|
|
15
|
+
const sortedInputs = options.sortingStrategy === 'none' ? inputs : inputs.sort((0, coinselectUtils_1.sortByScore)(feeRate));
|
|
16
|
+
const algorithm = (0, tryconfirmed_1.tryConfirmed)((0, coinselectUtils_1.anyOf)([branchAndBound_1.branchAndBound, accumulative_1.accumulative]), options);
|
|
17
|
+
return algorithm(sortedInputs, outputs, feeRate, options);
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=index.js.map
|