@atomicfinance/utils 3.4.7 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin.d.ts +1 -1
- package/dist/bitcoin.js +10 -6
- package/dist/bitcoin.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/lib/bitcoin.js +243 -0
- package/lib/index.js +84 -0
- package/package.json +4 -4
- package/CHANGELOG.md +0 -198
package/dist/bitcoin.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ declare function compressPubKey(pubKey: string): string;
|
|
|
15
15
|
* @return {Network}
|
|
16
16
|
*/
|
|
17
17
|
declare function getAddressNetwork(address: string): BitcoinNetwork;
|
|
18
|
-
|
|
18
|
+
type CoinSelectTarget = {
|
|
19
19
|
value: number;
|
|
20
20
|
script?: Buffer;
|
|
21
21
|
id?: string;
|
package/dist/bitcoin.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
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);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -63,13 +67,13 @@ function getAddressNetwork(address) {
|
|
|
63
67
|
// TODO: can this be simplified using just bitcoinjs-lib??
|
|
64
68
|
let networkKey;
|
|
65
69
|
// bech32
|
|
66
|
-
networkKey = lodash_1.findKey(bitcoin_networks_1.BitcoinNetworks, (network) => address.startsWith(network.bech32));
|
|
70
|
+
networkKey = (0, lodash_1.findKey)(bitcoin_networks_1.BitcoinNetworks, (network) => address.startsWith(network.bech32));
|
|
67
71
|
// base58
|
|
68
72
|
if (!networkKey) {
|
|
69
73
|
const prefix = crypto_1.base58.decode(address).toString('hex').substring(0, 2);
|
|
70
|
-
networkKey = lodash_1.findKey(bitcoin_networks_1.BitcoinNetworks, (network) => {
|
|
71
|
-
const pubKeyHashPrefix = crypto_1.padHexStart(network.pubKeyHash.toString(16), 1);
|
|
72
|
-
const scriptHashPrefix = crypto_1.padHexStart(network.scriptHash.toString(16), 1);
|
|
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);
|
|
73
77
|
return [pubKeyHashPrefix, scriptHashPrefix].includes(prefix);
|
|
74
78
|
});
|
|
75
79
|
}
|
|
@@ -220,7 +224,7 @@ function getPubKeyHash(address, network) {
|
|
|
220
224
|
}
|
|
221
225
|
exports.getPubKeyHash = getPubKeyHash;
|
|
222
226
|
function validateAddress(_address, network) {
|
|
223
|
-
const address = _1.addressToString(_address);
|
|
227
|
+
const address = (0, _1.addressToString)(_address);
|
|
224
228
|
if (typeof address !== 'string') {
|
|
225
229
|
throw new errors_1.InvalidAddressError(`Invalid address: ${address}`);
|
|
226
230
|
}
|
package/dist/bitcoin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bitcoin.js","sourceRoot":"","sources":["../lib/bitcoin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bitcoin.js","sourceRoot":"","sources":["../lib/bitcoin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4D;AAC5D,kDAA4D;AAC5D,gDAK8B;AAC9B,gEAAqC;AACrC,yEAA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,CAAC,EAAE;QACjE,MAAM,IAAI,KAAK,CACb,4CAA4C,OAAO,YAAY,IAAI,2CAA2C,CAC/G,CAAC;KACH;IAED,IAAI;QACF,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QACnD,OAAO,MAAM,CAAC,IAAI,CAAC;KACpB;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;QACxD,OAAO,MAAM,CAAC,IAAI,CAAC;KACpB;AACH,CAAC;AAiCC,sCAAa;AA/Bf,SAAS,eAAe,CAAC,QAA0B,EAAE,OAAuB;IAC1E,MAAM,OAAO,GAAG,IAAA,kBAAe,EAAC,QAAQ,CAAC,CAAC;IAE1C,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC/B,MAAM,IAAI,4BAAmB,CAAC,oBAAoB,OAAO,EAAE,CAAC,CAAC;KAC9D;IAED,IAAI,UAAU,CAAC;IACf,IAAI;QACF,UAAU,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;KAC9C;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,IAAI,4BAAmB,CAC3B,qCAAqC,OAAO,EAAE,CAC/C,CAAC;KACH;IAED,IAAI,CAAC,UAAU,EAAE;QACf,MAAM,IAAI,4BAAmB,CAAC,oBAAoB,OAAO,EAAE,CAAC,CAAC;KAC9D;AACH,CAAC;AAaC,0CAAe"}
|
package/dist/index.js
CHANGED
|
@@ -45,7 +45,7 @@ function validateSecretHash(secretHash) {
|
|
|
45
45
|
if (Buffer.byteLength(secretHash, 'hex') !== 32) {
|
|
46
46
|
throw new errors_1.InvalidSecretError(`Invalid secret hash: ${secretHash}`);
|
|
47
47
|
}
|
|
48
|
-
if (crypto_1.sha256('0000000000000000000000000000000000000000000000000000000000000000') === secretHash) {
|
|
48
|
+
if ((0, crypto_1.sha256)('0000000000000000000000000000000000000000000000000000000000000000') === secretHash) {
|
|
49
49
|
throw new errors_1.InvalidSecretError(`Invalid secret hash: ${secretHash}. Secret 0 detected.`);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
@@ -66,7 +66,7 @@ exports.validateSecret = validateSecret;
|
|
|
66
66
|
function validateSecretAndHash(secret, secretHash) {
|
|
67
67
|
validateSecret(secret);
|
|
68
68
|
validateSecretHash(secretHash);
|
|
69
|
-
const computedSecretHash = Buffer.from(crypto_1.sha256(secret), 'hex');
|
|
69
|
+
const computedSecretHash = Buffer.from((0, crypto_1.sha256)(secret), 'hex');
|
|
70
70
|
if (!computedSecretHash.equals(Buffer.from(secretHash, 'hex'))) {
|
|
71
71
|
throw new errors_1.InvalidSecretError(`Invalid secret: Does not match expected secret hash: ${secretHash}`);
|
|
72
72
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;AAAA,wBAAsB;AAEtB,kDAA+C;AAC/C,kDAG+B;AAC/B,gDAA0D;AAE1D,SAAS,eAAe,CAAC,OAAyB;IAChD,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,OAAO,CAAC;;QAC3C,OAAO,OAAO,CAAC,OAAO,CAAC;AAC9B,CAAC;AA4FC,0CAAe;AA1FjB,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAuFC,sBAAK;AArFP,SAAS,iBAAiB;IACxB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;AACzD,CAAC;AAqFC,8CAAiB;AAnFnB,SAAS,oBAAoB,CAAC,IAAY,EAAE,KAAa;IACvD,IAAI,GAAG,IAAI,IAAI,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,IAAI,KAAK,CAAC,UAAU,CAAC,EAAE;QACrB,MAAM,IAAI,+BAAsB,CAAC,4BAA4B,UAAU,EAAE,CAAC,CAAC;KAC5E;IAED,IAAI,UAAU,GAAG,SAAS,IAAI,UAAU,GAAG,aAAa,EAAE;QACxD,MAAM,IAAI,+BAAsB,CAC9B,sCAAsC,UAAU,EAAE,CACnD,CAAC;KACH;AACH,CAAC;AAWC,gDAAkB"}
|
package/lib/bitcoin.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
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 bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
34
|
+
const varuint = __importStar(require("bip174/src/lib/converter/varint"));
|
|
35
|
+
const bitcoin_networks_1 = require("bitcoin-networks");
|
|
36
|
+
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
37
|
+
const classify = __importStar(require("bitcoinjs-lib/src/classify"));
|
|
38
|
+
const coinselect_1 = __importDefault(require("coinselect"));
|
|
39
|
+
const accumulative_1 = __importDefault(require("coinselect/accumulative"));
|
|
40
|
+
const lodash_1 = require("lodash");
|
|
41
|
+
const _1 = require("./");
|
|
42
|
+
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
43
|
+
exports.AddressTypes = AddressTypes;
|
|
44
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function validateAddress(_address, network) {
|
|
227
|
+
const address = (0, _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=bitcoin.js.map
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateExpiration = exports.validateSecretAndHash = exports.validateSecretHash = exports.validateSecret = exports.validateValue = exports.caseInsensitiveEqual = exports.asyncSetImmediate = exports.addressToString = exports.sleep = void 0;
|
|
4
|
+
require("setimmediate");
|
|
5
|
+
const crypto_1 = require("@atomicfinance/crypto");
|
|
6
|
+
const errors_1 = require("@atomicfinance/errors");
|
|
7
|
+
const types_1 = require("@atomicfinance/types");
|
|
8
|
+
function addressToString(address) {
|
|
9
|
+
if (typeof address === 'string')
|
|
10
|
+
return address;
|
|
11
|
+
else
|
|
12
|
+
return address.address;
|
|
13
|
+
}
|
|
14
|
+
exports.addressToString = addressToString;
|
|
15
|
+
function sleep(ms) {
|
|
16
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
17
|
+
}
|
|
18
|
+
exports.sleep = sleep;
|
|
19
|
+
function asyncSetImmediate() {
|
|
20
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
21
|
+
}
|
|
22
|
+
exports.asyncSetImmediate = asyncSetImmediate;
|
|
23
|
+
function caseInsensitiveEqual(left, right) {
|
|
24
|
+
left = left && left.toLowerCase();
|
|
25
|
+
right = right && right.toLowerCase();
|
|
26
|
+
return left === right;
|
|
27
|
+
}
|
|
28
|
+
exports.caseInsensitiveEqual = caseInsensitiveEqual;
|
|
29
|
+
function validateValue(value) {
|
|
30
|
+
if (!types_1.BigNumber.isBigNumber(value)) {
|
|
31
|
+
throw new Error(`Invalid value: ${value}`);
|
|
32
|
+
}
|
|
33
|
+
if (value.lte(0)) {
|
|
34
|
+
throw new Error(`Invalid value: ${value}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.validateValue = validateValue;
|
|
38
|
+
function validateSecretHash(secretHash) {
|
|
39
|
+
if (typeof secretHash !== 'string') {
|
|
40
|
+
throw new errors_1.InvalidSecretError(`Invalid secret hash type`);
|
|
41
|
+
}
|
|
42
|
+
if (Buffer.from(secretHash, 'hex').toString('hex') !== secretHash) {
|
|
43
|
+
throw new errors_1.InvalidSecretError(`Invalid secret hash. Not Hex.`);
|
|
44
|
+
}
|
|
45
|
+
if (Buffer.byteLength(secretHash, 'hex') !== 32) {
|
|
46
|
+
throw new errors_1.InvalidSecretError(`Invalid secret hash: ${secretHash}`);
|
|
47
|
+
}
|
|
48
|
+
if ((0, crypto_1.sha256)('0000000000000000000000000000000000000000000000000000000000000000') === secretHash) {
|
|
49
|
+
throw new errors_1.InvalidSecretError(`Invalid secret hash: ${secretHash}. Secret 0 detected.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.validateSecretHash = validateSecretHash;
|
|
53
|
+
function validateSecret(secret) {
|
|
54
|
+
if (typeof secret !== 'string') {
|
|
55
|
+
throw new errors_1.InvalidSecretError(`Invalid secret type`);
|
|
56
|
+
}
|
|
57
|
+
if (Buffer.from(secret, 'hex').toString('hex') !== secret) {
|
|
58
|
+
throw new errors_1.InvalidSecretError(`Invalid secret. Not Hex.`);
|
|
59
|
+
}
|
|
60
|
+
const secretBuff = Buffer.from(secret, 'hex');
|
|
61
|
+
if (secretBuff.length !== 32) {
|
|
62
|
+
throw new errors_1.InvalidSecretError(`Invalid secret size`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.validateSecret = validateSecret;
|
|
66
|
+
function validateSecretAndHash(secret, secretHash) {
|
|
67
|
+
validateSecret(secret);
|
|
68
|
+
validateSecretHash(secretHash);
|
|
69
|
+
const computedSecretHash = Buffer.from((0, crypto_1.sha256)(secret), 'hex');
|
|
70
|
+
if (!computedSecretHash.equals(Buffer.from(secretHash, 'hex'))) {
|
|
71
|
+
throw new errors_1.InvalidSecretError(`Invalid secret: Does not match expected secret hash: ${secretHash}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.validateSecretAndHash = validateSecretAndHash;
|
|
75
|
+
function validateExpiration(expiration) {
|
|
76
|
+
if (isNaN(expiration)) {
|
|
77
|
+
throw new errors_1.InvalidExpirationError(`Invalid expiration. NaN: ${expiration}`);
|
|
78
|
+
}
|
|
79
|
+
if (expiration < 500000000 || expiration > 5000000000000) {
|
|
80
|
+
throw new errors_1.InvalidExpirationError(`Invalid expiration. Out of bounds: ${expiration}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.validateExpiration = validateExpiration;
|
|
84
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"node": ">=14"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@atomicfinance/crypto": "^3.
|
|
27
|
-
"@atomicfinance/errors": "^3.
|
|
28
|
-
"@atomicfinance/types": "^3.
|
|
26
|
+
"@atomicfinance/crypto": "^3.5.1",
|
|
27
|
+
"@atomicfinance/errors": "^3.5.1",
|
|
28
|
+
"@atomicfinance/types": "^3.5.1",
|
|
29
29
|
"@babel/runtime": "^7.12.1",
|
|
30
30
|
"bignumber.js": "^9.0.0",
|
|
31
31
|
"bip174": "^2.0.1",
|
package/CHANGELOG.md
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
# @atomicfinance/utils
|
|
2
|
-
|
|
3
|
-
## 3.4.7
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- 0ba01ef: Bump @node-dlc to 0.23.6
|
|
8
|
-
- Updated dependencies [0ba01ef]
|
|
9
|
-
- @atomicfinance/types@3.4.7
|
|
10
|
-
- @atomicfinance/crypto@3.4.7
|
|
11
|
-
- @atomicfinance/errors@3.4.7
|
|
12
|
-
|
|
13
|
-
## 3.4.6
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- b765b9c: Bump nodep-dlc to 0.23.5
|
|
18
|
-
- Updated dependencies [b765b9c]
|
|
19
|
-
- @atomicfinance/types@3.4.6
|
|
20
|
-
- @atomicfinance/crypto@3.4.6
|
|
21
|
-
- @atomicfinance/errors@3.4.6
|
|
22
|
-
|
|
23
|
-
## 3.4.5
|
|
24
|
-
|
|
25
|
-
### Patch Changes
|
|
26
|
-
|
|
27
|
-
- 74b7a4f: Bump node-dlc to 0.23.4
|
|
28
|
-
- Updated dependencies [74b7a4f]
|
|
29
|
-
- @atomicfinance/types@3.4.5
|
|
30
|
-
- @atomicfinance/crypto@3.4.5
|
|
31
|
-
- @atomicfinance/errors@3.4.5
|
|
32
|
-
|
|
33
|
-
## 3.4.4
|
|
34
|
-
|
|
35
|
-
### Patch Changes
|
|
36
|
-
|
|
37
|
-
- cc8f06a: Bump node-dlc to 0.23.3
|
|
38
|
-
- Updated dependencies [cc8f06a]
|
|
39
|
-
- @atomicfinance/types@3.4.4
|
|
40
|
-
- @atomicfinance/crypto@3.4.4
|
|
41
|
-
- @atomicfinance/errors@3.4.4
|
|
42
|
-
|
|
43
|
-
## 3.4.3
|
|
44
|
-
|
|
45
|
-
### Patch Changes
|
|
46
|
-
|
|
47
|
-
- db49017: Bump node-dlc to 0.23.2 for all packages
|
|
48
|
-
- Updated dependencies [db49017]
|
|
49
|
-
- @atomicfinance/types@3.4.3
|
|
50
|
-
- @atomicfinance/crypto@3.4.3
|
|
51
|
-
- @atomicfinance/errors@3.4.3
|
|
52
|
-
|
|
53
|
-
## 3.4.2
|
|
54
|
-
|
|
55
|
-
### Patch Changes
|
|
56
|
-
|
|
57
|
-
- e701ba6: Bump node-dlc- to 0.23.1 and add node-dlc batch tx builder validation tests
|
|
58
|
-
- Updated dependencies [e701ba6]
|
|
59
|
-
- @atomicfinance/types@3.4.2
|
|
60
|
-
- @atomicfinance/crypto@3.4.2
|
|
61
|
-
- @atomicfinance/errors@3.4.2
|
|
62
|
-
|
|
63
|
-
## 3.4.1
|
|
64
|
-
|
|
65
|
-
### Patch Changes
|
|
66
|
-
|
|
67
|
-
- 9781efe: Add getInputsForDualFunding functionality and bump @node-dlc
|
|
68
|
-
- Updated dependencies [9781efe]
|
|
69
|
-
- @atomicfinance/types@3.4.1
|
|
70
|
-
- @atomicfinance/crypto@3.4.1
|
|
71
|
-
- @atomicfinance/errors@3.4.1
|
|
72
|
-
|
|
73
|
-
## 3.4.0
|
|
74
|
-
|
|
75
|
-
### Minor Changes
|
|
76
|
-
|
|
77
|
-
- 314d7d7: Add batch dlc funding transactions
|
|
78
|
-
|
|
79
|
-
### Patch Changes
|
|
80
|
-
|
|
81
|
-
- Updated dependencies [314d7d7]
|
|
82
|
-
- @atomicfinance/types@3.4.0
|
|
83
|
-
- @atomicfinance/crypto@3.4.0
|
|
84
|
-
- @atomicfinance/errors@3.4.0
|
|
85
|
-
|
|
86
|
-
## 3.3.1
|
|
87
|
-
|
|
88
|
-
### Patch Changes
|
|
89
|
-
|
|
90
|
-
- 7f6f4a8: Fix rounding error with find payout
|
|
91
|
-
- Updated dependencies [7f6f4a8]
|
|
92
|
-
- @atomicfinance/crypto@3.3.1
|
|
93
|
-
- @atomicfinance/errors@3.3.1
|
|
94
|
-
- @atomicfinance/types@3.3.1
|
|
95
|
-
|
|
96
|
-
## 3.3.0
|
|
97
|
-
|
|
98
|
-
### Minor Changes
|
|
99
|
-
|
|
100
|
-
- 4a512ee: Fix find outcome index for hyperbola payout curve edge case
|
|
101
|
-
|
|
102
|
-
### Patch Changes
|
|
103
|
-
|
|
104
|
-
- Updated dependencies [4a512ee]
|
|
105
|
-
- @atomicfinance/crypto@3.3.0
|
|
106
|
-
- @atomicfinance/errors@3.3.0
|
|
107
|
-
- @atomicfinance/types@3.3.0
|
|
108
|
-
|
|
109
|
-
## 3.2.5
|
|
110
|
-
|
|
111
|
-
### Patch Changes
|
|
112
|
-
|
|
113
|
-
- Updated dependencies [a72c7b9]
|
|
114
|
-
- @atomicfinance/types@3.2.5
|
|
115
|
-
- @atomicfinance/crypto@3.2.5
|
|
116
|
-
- @atomicfinance/errors@3.2.5
|
|
117
|
-
|
|
118
|
-
## 3.2.4
|
|
119
|
-
|
|
120
|
-
### Patch Changes
|
|
121
|
-
|
|
122
|
-
- 177b76a: Upgrade node-dlc to 0.22.4
|
|
123
|
-
- Updated dependencies [177b76a]
|
|
124
|
-
- @atomicfinance/crypto@3.2.4
|
|
125
|
-
- @atomicfinance/errors@3.2.4
|
|
126
|
-
- @atomicfinance/types@3.2.4
|
|
127
|
-
|
|
128
|
-
## 3.2.3
|
|
129
|
-
|
|
130
|
-
### Patch Changes
|
|
131
|
-
|
|
132
|
-
- @atomicfinance/crypto@3.2.3
|
|
133
|
-
- @atomicfinance/errors@3.2.3
|
|
134
|
-
- @atomicfinance/types@3.2.3
|
|
135
|
-
|
|
136
|
-
## 3.2.2
|
|
137
|
-
|
|
138
|
-
### Patch Changes
|
|
139
|
-
|
|
140
|
-
- @atomicfinance/crypto@3.2.2
|
|
141
|
-
- @atomicfinance/errors@3.2.2
|
|
142
|
-
- @atomicfinance/types@3.2.2
|
|
143
|
-
|
|
144
|
-
## 3.2.1
|
|
145
|
-
|
|
146
|
-
### Patch Changes
|
|
147
|
-
|
|
148
|
-
- Updated dependencies [f69141e]
|
|
149
|
-
- @atomicfinance/types@3.2.1
|
|
150
|
-
- @atomicfinance/crypto@3.2.1
|
|
151
|
-
- @atomicfinance/errors@3.2.1
|
|
152
|
-
|
|
153
|
-
## 3.2.0
|
|
154
|
-
|
|
155
|
-
### Patch Changes
|
|
156
|
-
|
|
157
|
-
- Updated dependencies [dc9ac8a]
|
|
158
|
-
- @atomicfinance/types@3.2.0
|
|
159
|
-
- @atomicfinance/crypto@3.2.0
|
|
160
|
-
- @atomicfinance/errors@3.2.0
|
|
161
|
-
|
|
162
|
-
## 3.1.1
|
|
163
|
-
|
|
164
|
-
### Patch Changes
|
|
165
|
-
|
|
166
|
-
- @atomicfinance/crypto@3.1.1
|
|
167
|
-
- @atomicfinance/errors@3.1.1
|
|
168
|
-
- @atomicfinance/types@3.1.1
|
|
169
|
-
|
|
170
|
-
## 3.1.0
|
|
171
|
-
|
|
172
|
-
### Patch Changes
|
|
173
|
-
|
|
174
|
-
- Updated dependencies [c60033b]
|
|
175
|
-
- @atomicfinance/types@3.1.0
|
|
176
|
-
- @atomicfinance/crypto@3.1.0
|
|
177
|
-
- @atomicfinance/errors@3.1.0
|
|
178
|
-
|
|
179
|
-
## 3.0.1
|
|
180
|
-
|
|
181
|
-
### Patch Changes
|
|
182
|
-
|
|
183
|
-
- @atomicfinance/crypto@3.0.1
|
|
184
|
-
- @atomicfinance/errors@3.0.1
|
|
185
|
-
- @atomicfinance/types@3.0.1
|
|
186
|
-
|
|
187
|
-
## 3.0.0
|
|
188
|
-
|
|
189
|
-
### Major Changes
|
|
190
|
-
|
|
191
|
-
- a06082e: Create unified standalone `bitcoin-abstraction-layer` package
|
|
192
|
-
|
|
193
|
-
### Patch Changes
|
|
194
|
-
|
|
195
|
-
- Updated dependencies [a06082e]
|
|
196
|
-
- @atomicfinance/types@3.0.0
|
|
197
|
-
- @atomicfinance/crypto@3.0.0
|
|
198
|
-
- @atomicfinance/errors@3.0.0
|