@atomicfinance/bitcoin-utils 3.5.3 → 3.6.0
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/package.json +5 -5
- package/lib/index.js +0 -243
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/bitcoin-utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"node": ">=14"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@atomicfinance/crypto": "^3.
|
|
28
|
-
"@atomicfinance/errors": "^3.
|
|
29
|
-
"@atomicfinance/types": "^3.
|
|
30
|
-
"@atomicfinance/utils": "^3.
|
|
27
|
+
"@atomicfinance/crypto": "^3.6.0",
|
|
28
|
+
"@atomicfinance/errors": "^3.6.0",
|
|
29
|
+
"@atomicfinance/types": "^3.6.0",
|
|
30
|
+
"@atomicfinance/utils": "^3.6.0",
|
|
31
31
|
"@babel/runtime": "^7.12.1",
|
|
32
32
|
"bignumber.js": "^9.0.0",
|
|
33
33
|
"bip174": "^2.0.1",
|
package/lib/index.js
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.validateAddress = exports.getPubKeyHash = exports.AddressTypes = exports.witnessStackToScriptWitness = exports.normalizeTransactionObject = exports.decodeRawTransaction = exports.selectCoins = exports.getAddressNetwork = exports.compressPubKey = exports.calculateFee = void 0;
|
|
30
|
-
const crypto_1 = require("@atomicfinance/crypto");
|
|
31
|
-
const errors_1 = require("@atomicfinance/errors");
|
|
32
|
-
const types_1 = require("@atomicfinance/types");
|
|
33
|
-
const utils_1 = require("@atomicfinance/utils");
|
|
34
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
35
|
-
const varuint = __importStar(require("bip174/src/lib/converter/varint"));
|
|
36
|
-
const bitcoin_networks_1 = require("bitcoin-networks");
|
|
37
|
-
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
38
|
-
const classify = __importStar(require("bitcoinjs-lib/src/classify"));
|
|
39
|
-
const coinselect_1 = __importDefault(require("coinselect"));
|
|
40
|
-
const accumulative_1 = __importDefault(require("coinselect/accumulative"));
|
|
41
|
-
const lodash_1 = require("lodash");
|
|
42
|
-
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
43
|
-
exports.AddressTypes = AddressTypes;
|
|
44
|
-
const calculateFee = (numInputs, numOutputs, feePerByte) => {
|
|
45
|
-
return (numInputs * 148 + numOutputs * 34 + 10) * feePerByte;
|
|
46
|
-
};
|
|
47
|
-
exports.calculateFee = calculateFee;
|
|
48
|
-
/**
|
|
49
|
-
* Get compressed pubKey from pubKey.
|
|
50
|
-
* @param {!string} pubKey - 65 byte string with prefix, x, y.
|
|
51
|
-
* @return {string} Returns the compressed pubKey of uncompressed pubKey.
|
|
52
|
-
*/
|
|
53
|
-
const compressPubKey = (pubKey) => {
|
|
54
|
-
const x = pubKey.substring(2, 66);
|
|
55
|
-
const y = pubKey.substring(66, 130);
|
|
56
|
-
const even = parseInt(y.substring(62, 64), 16) % 2 === 0;
|
|
57
|
-
const prefix = even ? '02' : '03';
|
|
58
|
-
return prefix + x;
|
|
59
|
-
};
|
|
60
|
-
exports.compressPubKey = compressPubKey;
|
|
61
|
-
/**
|
|
62
|
-
* Get a network object from an address
|
|
63
|
-
* @param {string} address The bitcoin address
|
|
64
|
-
* @return {Network}
|
|
65
|
-
*/
|
|
66
|
-
const getAddressNetwork = (address) => {
|
|
67
|
-
// TODO: can this be simplified using just bitcoinjs-lib??
|
|
68
|
-
let networkKey;
|
|
69
|
-
// bech32
|
|
70
|
-
networkKey = (0, lodash_1.findKey)(bitcoin_networks_1.BitcoinNetworks, (network) => address.startsWith(network.bech32));
|
|
71
|
-
// base58
|
|
72
|
-
if (!networkKey) {
|
|
73
|
-
const prefix = crypto_1.base58.decode(address).toString('hex').substring(0, 2);
|
|
74
|
-
networkKey = (0, lodash_1.findKey)(bitcoin_networks_1.BitcoinNetworks, (network) => {
|
|
75
|
-
const pubKeyHashPrefix = (0, crypto_1.padHexStart)(network.pubKeyHash.toString(16), 1);
|
|
76
|
-
const scriptHashPrefix = (0, crypto_1.padHexStart)(network.scriptHash.toString(16), 1);
|
|
77
|
-
return [pubKeyHashPrefix, scriptHashPrefix].includes(prefix);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
return bitcoin_networks_1.BitcoinNetworks[networkKey];
|
|
81
|
-
};
|
|
82
|
-
exports.getAddressNetwork = getAddressNetwork;
|
|
83
|
-
const selectCoins = (utxos, targets, feePerByte, fixedInputs = []) => {
|
|
84
|
-
let selectUtxos = utxos;
|
|
85
|
-
// Default coinselect won't accumulate some inputs
|
|
86
|
-
// TODO: does coinselect need to be modified to ABSOLUTELY not skip an input?
|
|
87
|
-
const coinselectStrat = fixedInputs.length
|
|
88
|
-
? accumulative_1.default
|
|
89
|
-
: coinselect_1.default;
|
|
90
|
-
if (fixedInputs.length) {
|
|
91
|
-
selectUtxos = [
|
|
92
|
-
// Order fixed inputs to the start of the list so they are used
|
|
93
|
-
...fixedInputs,
|
|
94
|
-
...utxos.filter((utxo) => !fixedInputs.find((input) => input.vout === utxo.vout && input.txid === utxo.txid)),
|
|
95
|
-
];
|
|
96
|
-
}
|
|
97
|
-
const { inputs, outputs, fee } = coinselectStrat(selectUtxos, targets, Math.ceil(feePerByte));
|
|
98
|
-
let change;
|
|
99
|
-
if (inputs && outputs) {
|
|
100
|
-
change = outputs.find((output) => output.id !== 'main');
|
|
101
|
-
}
|
|
102
|
-
return { inputs, outputs, fee, change };
|
|
103
|
-
};
|
|
104
|
-
exports.selectCoins = selectCoins;
|
|
105
|
-
const OUTPUT_TYPES_MAP = {
|
|
106
|
-
[classify.types.P2WPKH]: 'witness_v0_keyhash',
|
|
107
|
-
[classify.types.P2WSH]: 'witness_v0_scripthash',
|
|
108
|
-
};
|
|
109
|
-
const decodeRawTransaction = (hex, network) => {
|
|
110
|
-
const bjsTx = bitcoin.Transaction.fromHex(hex);
|
|
111
|
-
const vin = bjsTx.ins.map((input) => {
|
|
112
|
-
return {
|
|
113
|
-
txid: Buffer.from(input.hash).reverse().toString('hex'),
|
|
114
|
-
vout: input.index,
|
|
115
|
-
scriptSig: {
|
|
116
|
-
asm: bitcoin.script.toASM(input.script),
|
|
117
|
-
hex: input.script.toString('hex'),
|
|
118
|
-
},
|
|
119
|
-
txinwitness: input.witness.map((w) => w.toString('hex')),
|
|
120
|
-
sequence: input.sequence,
|
|
121
|
-
};
|
|
122
|
-
});
|
|
123
|
-
const vout = bjsTx.outs.map((output, n) => {
|
|
124
|
-
const type = classify.output(output.script);
|
|
125
|
-
const vout = {
|
|
126
|
-
value: output.value / 1e8,
|
|
127
|
-
n,
|
|
128
|
-
scriptPubKey: {
|
|
129
|
-
asm: bitcoin.script.toASM(output.script),
|
|
130
|
-
hex: output.script.toString('hex'),
|
|
131
|
-
reqSigs: 1,
|
|
132
|
-
type: OUTPUT_TYPES_MAP[type] || type,
|
|
133
|
-
addresses: [],
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
try {
|
|
137
|
-
const address = bitcoin.address.fromOutputScript(output.script, network);
|
|
138
|
-
vout.scriptPubKey.addresses.push(address);
|
|
139
|
-
}
|
|
140
|
-
catch (e) {
|
|
141
|
-
/** If output script is not parasable, we just skip it */
|
|
142
|
-
}
|
|
143
|
-
return vout;
|
|
144
|
-
});
|
|
145
|
-
return {
|
|
146
|
-
txid: bjsTx.getHash(false).reverse().toString('hex'),
|
|
147
|
-
hash: bjsTx.getHash(true).reverse().toString('hex'),
|
|
148
|
-
version: bjsTx.version,
|
|
149
|
-
locktime: bjsTx.locktime,
|
|
150
|
-
size: bjsTx.byteLength(),
|
|
151
|
-
vsize: bjsTx.virtualSize(),
|
|
152
|
-
weight: bjsTx.weight(),
|
|
153
|
-
vin,
|
|
154
|
-
vout,
|
|
155
|
-
hex,
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
exports.decodeRawTransaction = decodeRawTransaction;
|
|
159
|
-
const normalizeTransactionObject = (tx, fee, block) => {
|
|
160
|
-
const value = tx.vout.reduce((p, n) => p.plus(new bignumber_js_1.default(n.value).times(1e8)), new bignumber_js_1.default(0));
|
|
161
|
-
const result = {
|
|
162
|
-
hash: tx.txid,
|
|
163
|
-
value: value.toNumber(),
|
|
164
|
-
_raw: tx,
|
|
165
|
-
confirmations: 0,
|
|
166
|
-
status: tx.confirmations > 0 ? types_1.TxStatus.Success : types_1.TxStatus.Pending,
|
|
167
|
-
};
|
|
168
|
-
if (fee) {
|
|
169
|
-
const feePrice = Math.round(fee / tx.vsize);
|
|
170
|
-
Object.assign(result, {
|
|
171
|
-
fee,
|
|
172
|
-
feePrice,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
if (block) {
|
|
176
|
-
Object.assign(result, {
|
|
177
|
-
blockHash: block.hash,
|
|
178
|
-
blockNumber: block.number,
|
|
179
|
-
confirmations: tx.confirmations,
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return result;
|
|
183
|
-
};
|
|
184
|
-
exports.normalizeTransactionObject = normalizeTransactionObject;
|
|
185
|
-
// TODO: This is copy pasta because it's not exported from bitcoinjs-lib
|
|
186
|
-
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts#L477
|
|
187
|
-
const witnessStackToScriptWitness = (witness) => {
|
|
188
|
-
let buffer = Buffer.allocUnsafe(0);
|
|
189
|
-
function writeSlice(slice) {
|
|
190
|
-
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
|
|
191
|
-
}
|
|
192
|
-
function writeVarInt(i) {
|
|
193
|
-
const currentLen = buffer.length;
|
|
194
|
-
const varintLen = varuint.encodingLength(i);
|
|
195
|
-
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
|
|
196
|
-
varuint.encode(i, buffer, currentLen);
|
|
197
|
-
}
|
|
198
|
-
function writeVarSlice(slice) {
|
|
199
|
-
writeVarInt(slice.length);
|
|
200
|
-
writeSlice(slice);
|
|
201
|
-
}
|
|
202
|
-
function writeVector(vector) {
|
|
203
|
-
writeVarInt(vector.length);
|
|
204
|
-
vector.forEach(writeVarSlice);
|
|
205
|
-
}
|
|
206
|
-
writeVector(witness);
|
|
207
|
-
return buffer;
|
|
208
|
-
};
|
|
209
|
-
exports.witnessStackToScriptWitness = witnessStackToScriptWitness;
|
|
210
|
-
const getPubKeyHash = (address, network) => {
|
|
211
|
-
const outputScript = bitcoin.address.toOutputScript(address, network);
|
|
212
|
-
const type = classify.output(outputScript);
|
|
213
|
-
if (![classify.types.P2PKH, classify.types.P2WPKH].includes(type)) {
|
|
214
|
-
throw new Error(`Bitcoin swap doesn't support the address ${address} type of ${type}. Not possible to derive public key hash.`);
|
|
215
|
-
}
|
|
216
|
-
try {
|
|
217
|
-
const bech32 = bitcoin.address.fromBech32(address);
|
|
218
|
-
return bech32.data;
|
|
219
|
-
}
|
|
220
|
-
catch (e) {
|
|
221
|
-
const base58 = bitcoin.address.fromBase58Check(address);
|
|
222
|
-
return base58.hash;
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
exports.getPubKeyHash = getPubKeyHash;
|
|
226
|
-
const validateAddress = (_address, network) => {
|
|
227
|
-
const address = (0, utils_1.addressToString)(_address);
|
|
228
|
-
if (typeof address !== 'string') {
|
|
229
|
-
throw new errors_1.InvalidAddressError(`Invalid address: ${address}`);
|
|
230
|
-
}
|
|
231
|
-
let pubKeyHash;
|
|
232
|
-
try {
|
|
233
|
-
pubKeyHash = getPubKeyHash(address, network);
|
|
234
|
-
}
|
|
235
|
-
catch (e) {
|
|
236
|
-
throw new errors_1.InvalidAddressError(`Invalid Address. Failed to parse: ${address}`);
|
|
237
|
-
}
|
|
238
|
-
if (!pubKeyHash) {
|
|
239
|
-
throw new errors_1.InvalidAddressError(`Invalid Address: ${address}`);
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
exports.validateAddress = validateAddress;
|
|
243
|
-
//# sourceMappingURL=index.js.map
|