@atomicfinance/bitcoin-utils 3.4.0 → 3.4.2
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/CHANGELOG.md +22 -0
- package/dist/index.d.ts +12 -11
- package/dist/index.js +18 -18
- package/dist/index.js.map +1 -1
- package/lib/index.ts +25 -22
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @atomicfinance/bitcoin-utils
|
|
2
2
|
|
|
3
|
+
## 3.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- e701ba6: Bump node-dlc- to 0.23.1 and add node-dlc batch tx builder validation tests
|
|
8
|
+
- Updated dependencies [e701ba6]
|
|
9
|
+
- @atomicfinance/types@3.4.2
|
|
10
|
+
- @atomicfinance/crypto@3.4.2
|
|
11
|
+
- @atomicfinance/errors@3.4.2
|
|
12
|
+
- @atomicfinance/utils@3.4.2
|
|
13
|
+
|
|
14
|
+
## 3.4.1
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 9781efe: Add getInputsForDualFunding functionality and bump @node-dlc
|
|
19
|
+
- Updated dependencies [9781efe]
|
|
20
|
+
- @atomicfinance/types@3.4.1
|
|
21
|
+
- @atomicfinance/crypto@3.4.1
|
|
22
|
+
- @atomicfinance/errors@3.4.1
|
|
23
|
+
- @atomicfinance/utils@3.4.1
|
|
24
|
+
|
|
3
25
|
## 3.4.0
|
|
4
26
|
|
|
5
27
|
### Minor Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -2,36 +2,37 @@
|
|
|
2
2
|
import { Address, bitcoin as bT, Transaction } from '@atomicfinance/types';
|
|
3
3
|
import { BitcoinNetwork } from 'bitcoin-networks';
|
|
4
4
|
declare const AddressTypes: string[];
|
|
5
|
-
declare
|
|
5
|
+
declare const calculateFee: (numInputs: number, numOutputs: number, feePerByte: number) => number;
|
|
6
6
|
/**
|
|
7
7
|
* Get compressed pubKey from pubKey.
|
|
8
8
|
* @param {!string} pubKey - 65 byte string with prefix, x, y.
|
|
9
9
|
* @return {string} Returns the compressed pubKey of uncompressed pubKey.
|
|
10
10
|
*/
|
|
11
|
-
declare
|
|
11
|
+
declare const compressPubKey: (pubKey: string) => string;
|
|
12
12
|
/**
|
|
13
13
|
* Get a network object from an address
|
|
14
14
|
* @param {string} address The bitcoin address
|
|
15
15
|
* @return {Network}
|
|
16
16
|
*/
|
|
17
|
-
declare
|
|
17
|
+
declare const getAddressNetwork: (address: string) => BitcoinNetwork;
|
|
18
18
|
declare type CoinSelectTarget = {
|
|
19
19
|
value: number;
|
|
20
20
|
script?: Buffer;
|
|
21
21
|
id?: string;
|
|
22
22
|
};
|
|
23
|
-
declare
|
|
23
|
+
declare type CoinSelectResponse = {
|
|
24
24
|
inputs: bT.UTXO[];
|
|
25
25
|
outputs: CoinSelectTarget[];
|
|
26
|
+
change: CoinSelectTarget;
|
|
26
27
|
fee: number;
|
|
27
|
-
change: any;
|
|
28
28
|
};
|
|
29
|
-
declare
|
|
30
|
-
declare
|
|
29
|
+
declare const selectCoins: (utxos: bT.UTXO[], targets: CoinSelectTarget[], feePerByte: number, fixedInputs?: bT.UTXO[]) => CoinSelectResponse;
|
|
30
|
+
declare const decodeRawTransaction: (hex: string, network: BitcoinNetwork) => bT.Transaction;
|
|
31
|
+
declare const normalizeTransactionObject: (tx: bT.Transaction, fee: number, block?: {
|
|
31
32
|
number: number;
|
|
32
33
|
hash: string;
|
|
33
|
-
})
|
|
34
|
-
declare
|
|
35
|
-
declare
|
|
36
|
-
declare
|
|
34
|
+
}) => Transaction<bT.Transaction>;
|
|
35
|
+
declare const witnessStackToScriptWitness: (witness: Buffer[]) => Buffer;
|
|
36
|
+
declare const getPubKeyHash: (address: string, network: BitcoinNetwork) => Buffer;
|
|
37
|
+
declare const validateAddress: (_address: Address | string, network: BitcoinNetwork) => void;
|
|
37
38
|
export { calculateFee, compressPubKey, getAddressNetwork, CoinSelectTarget, selectCoins, decodeRawTransaction, normalizeTransactionObject, witnessStackToScriptWitness, AddressTypes, getPubKeyHash, validateAddress, };
|
package/dist/index.js
CHANGED
|
@@ -37,29 +37,29 @@ const accumulative_1 = __importDefault(require("coinselect/accumulative"));
|
|
|
37
37
|
const lodash_1 = require("lodash");
|
|
38
38
|
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
39
39
|
exports.AddressTypes = AddressTypes;
|
|
40
|
-
|
|
40
|
+
const calculateFee = (numInputs, numOutputs, feePerByte) => {
|
|
41
41
|
return (numInputs * 148 + numOutputs * 34 + 10) * feePerByte;
|
|
42
|
-
}
|
|
42
|
+
};
|
|
43
43
|
exports.calculateFee = calculateFee;
|
|
44
44
|
/**
|
|
45
45
|
* Get compressed pubKey from pubKey.
|
|
46
46
|
* @param {!string} pubKey - 65 byte string with prefix, x, y.
|
|
47
47
|
* @return {string} Returns the compressed pubKey of uncompressed pubKey.
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
const compressPubKey = (pubKey) => {
|
|
50
50
|
const x = pubKey.substring(2, 66);
|
|
51
51
|
const y = pubKey.substring(66, 130);
|
|
52
52
|
const even = parseInt(y.substring(62, 64), 16) % 2 === 0;
|
|
53
53
|
const prefix = even ? '02' : '03';
|
|
54
54
|
return prefix + x;
|
|
55
|
-
}
|
|
55
|
+
};
|
|
56
56
|
exports.compressPubKey = compressPubKey;
|
|
57
57
|
/**
|
|
58
58
|
* Get a network object from an address
|
|
59
59
|
* @param {string} address The bitcoin address
|
|
60
60
|
* @return {Network}
|
|
61
61
|
*/
|
|
62
|
-
|
|
62
|
+
const getAddressNetwork = (address) => {
|
|
63
63
|
// TODO: can this be simplified using just bitcoinjs-lib??
|
|
64
64
|
let networkKey;
|
|
65
65
|
// bech32
|
|
@@ -74,9 +74,9 @@ function getAddressNetwork(address) {
|
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
76
|
return bitcoin_networks_1.BitcoinNetworks[networkKey];
|
|
77
|
-
}
|
|
77
|
+
};
|
|
78
78
|
exports.getAddressNetwork = getAddressNetwork;
|
|
79
|
-
|
|
79
|
+
const selectCoins = (utxos, targets, feePerByte, fixedInputs = []) => {
|
|
80
80
|
let selectUtxos = utxos;
|
|
81
81
|
// Default coinselect won't accumulate some inputs
|
|
82
82
|
// TODO: does coinselect need to be modified to ABSOLUTELY not skip an input?
|
|
@@ -96,13 +96,13 @@ function selectCoins(utxos, targets, feePerByte, fixedInputs = []) {
|
|
|
96
96
|
change = outputs.find((output) => output.id !== 'main');
|
|
97
97
|
}
|
|
98
98
|
return { inputs, outputs, fee, change };
|
|
99
|
-
}
|
|
99
|
+
};
|
|
100
100
|
exports.selectCoins = selectCoins;
|
|
101
101
|
const OUTPUT_TYPES_MAP = {
|
|
102
102
|
[classify.types.P2WPKH]: 'witness_v0_keyhash',
|
|
103
103
|
[classify.types.P2WSH]: 'witness_v0_scripthash',
|
|
104
104
|
};
|
|
105
|
-
|
|
105
|
+
const decodeRawTransaction = (hex, network) => {
|
|
106
106
|
const bjsTx = bitcoin.Transaction.fromHex(hex);
|
|
107
107
|
const vin = bjsTx.ins.map((input) => {
|
|
108
108
|
return {
|
|
@@ -150,9 +150,9 @@ function decodeRawTransaction(hex, network) {
|
|
|
150
150
|
vout,
|
|
151
151
|
hex,
|
|
152
152
|
};
|
|
153
|
-
}
|
|
153
|
+
};
|
|
154
154
|
exports.decodeRawTransaction = decodeRawTransaction;
|
|
155
|
-
|
|
155
|
+
const normalizeTransactionObject = (tx, fee, block) => {
|
|
156
156
|
const value = tx.vout.reduce((p, n) => p.plus(new bignumber_js_1.default(n.value).times(1e8)), new bignumber_js_1.default(0));
|
|
157
157
|
const result = {
|
|
158
158
|
hash: tx.txid,
|
|
@@ -176,11 +176,11 @@ function normalizeTransactionObject(tx, fee, block) {
|
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
178
|
return result;
|
|
179
|
-
}
|
|
179
|
+
};
|
|
180
180
|
exports.normalizeTransactionObject = normalizeTransactionObject;
|
|
181
181
|
// TODO: This is copy pasta because it's not exported from bitcoinjs-lib
|
|
182
182
|
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts#L477
|
|
183
|
-
|
|
183
|
+
const witnessStackToScriptWitness = (witness) => {
|
|
184
184
|
let buffer = Buffer.allocUnsafe(0);
|
|
185
185
|
function writeSlice(slice) {
|
|
186
186
|
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
|
|
@@ -201,9 +201,9 @@ function witnessStackToScriptWitness(witness) {
|
|
|
201
201
|
}
|
|
202
202
|
writeVector(witness);
|
|
203
203
|
return buffer;
|
|
204
|
-
}
|
|
204
|
+
};
|
|
205
205
|
exports.witnessStackToScriptWitness = witnessStackToScriptWitness;
|
|
206
|
-
|
|
206
|
+
const getPubKeyHash = (address, network) => {
|
|
207
207
|
const outputScript = bitcoin.address.toOutputScript(address, network);
|
|
208
208
|
const type = classify.output(outputScript);
|
|
209
209
|
if (![classify.types.P2PKH, classify.types.P2WPKH].includes(type)) {
|
|
@@ -217,9 +217,9 @@ function getPubKeyHash(address, network) {
|
|
|
217
217
|
const base58 = bitcoin.address.fromBase58Check(address);
|
|
218
218
|
return base58.hash;
|
|
219
219
|
}
|
|
220
|
-
}
|
|
220
|
+
};
|
|
221
221
|
exports.getPubKeyHash = getPubKeyHash;
|
|
222
|
-
|
|
222
|
+
const validateAddress = (_address, network) => {
|
|
223
223
|
const address = utils_1.addressToString(_address);
|
|
224
224
|
if (typeof address !== 'string') {
|
|
225
225
|
throw new errors_1.InvalidAddressError(`Invalid address: ${address}`);
|
|
@@ -234,6 +234,6 @@ function validateAddress(_address, network) {
|
|
|
234
234
|
if (!pubKeyHash) {
|
|
235
235
|
throw new errors_1.InvalidAddressError(`Invalid Address: ${address}`);
|
|
236
236
|
}
|
|
237
|
-
}
|
|
237
|
+
};
|
|
238
238
|
exports.validateAddress = validateAddress;
|
|
239
239
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4D;AAC5D,kDAA4D;AAC5D,gDAK8B;AAC9B,gDAAuD;AACvD,gEAAqC;AACrC,yEAA2D;AAC3D,uDAAmE;AACnE,uDAAyC;AACzC,qEAAuD;AACvD,4DAAoC;AACpC,2EAA6D;AAC7D,mCAAiC;AAEjC,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4D;AAC5D,kDAA4D;AAC5D,gDAK8B;AAC9B,gDAAuD;AACvD,gEAAqC;AACrC,yEAA2D;AAC3D,uDAAmE;AACnE,uDAAyC;AACzC,qEAAuD;AACvD,4DAAoC;AACpC,2EAA6D;AAC7D,mCAAiC;AAEjC,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC;AAiSvD,oCAAY;AA/Rd,MAAM,YAAY,GAAG,CACnB,SAAiB,EACjB,UAAkB,EAClB,UAAkB,EACV,EAAE;IACV,OAAO,CAAC,SAAS,GAAG,GAAG,GAAG,UAAU,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,UAAU,CAAC;AAC/D,CAAC,CAAC;AAiRA,oCAAY;AA/Qd;;;;GAIG;AACH,MAAM,cAAc,GAAG,CAAC,MAAc,EAAU,EAAE;IAChD,MAAM,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAClC,MAAM,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IACpC,MAAM,IAAI,GAAG,QAAQ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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,CAAC;AAaA,0CAAe"}
|
package/lib/index.ts
CHANGED
|
@@ -18,34 +18,34 @@ import { findKey } from 'lodash';
|
|
|
18
18
|
|
|
19
19
|
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const calculateFee = (
|
|
22
22
|
numInputs: number,
|
|
23
23
|
numOutputs: number,
|
|
24
24
|
feePerByte: number,
|
|
25
|
-
) {
|
|
25
|
+
): number => {
|
|
26
26
|
return (numInputs * 148 + numOutputs * 34 + 10) * feePerByte;
|
|
27
|
-
}
|
|
27
|
+
};
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Get compressed pubKey from pubKey.
|
|
31
31
|
* @param {!string} pubKey - 65 byte string with prefix, x, y.
|
|
32
32
|
* @return {string} Returns the compressed pubKey of uncompressed pubKey.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
const compressPubKey = (pubKey: string): string => {
|
|
35
35
|
const x = pubKey.substring(2, 66);
|
|
36
36
|
const y = pubKey.substring(66, 130);
|
|
37
37
|
const even = parseInt(y.substring(62, 64), 16) % 2 === 0;
|
|
38
38
|
const prefix = even ? '02' : '03';
|
|
39
39
|
|
|
40
40
|
return prefix + x;
|
|
41
|
-
}
|
|
41
|
+
};
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* Get a network object from an address
|
|
45
45
|
* @param {string} address The bitcoin address
|
|
46
46
|
* @return {Network}
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
const getAddressNetwork = (address: string): BitcoinNetwork => {
|
|
49
49
|
// TODO: can this be simplified using just bitcoinjs-lib??
|
|
50
50
|
let networkKey;
|
|
51
51
|
// bech32
|
|
@@ -62,7 +62,7 @@ function getAddressNetwork(address: string) {
|
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
return (BitcoinNetworks as { [key: string]: BitcoinNetwork })[networkKey];
|
|
65
|
-
}
|
|
65
|
+
};
|
|
66
66
|
|
|
67
67
|
type CoinSelectTarget = {
|
|
68
68
|
value: number;
|
|
@@ -83,12 +83,12 @@ type CoinSelectFunction = (
|
|
|
83
83
|
feePerByte: number,
|
|
84
84
|
) => CoinSelectResponse;
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
const selectCoins = (
|
|
87
87
|
utxos: bT.UTXO[],
|
|
88
88
|
targets: CoinSelectTarget[],
|
|
89
89
|
feePerByte: number,
|
|
90
90
|
fixedInputs: bT.UTXO[] = [],
|
|
91
|
-
) {
|
|
91
|
+
): CoinSelectResponse => {
|
|
92
92
|
let selectUtxos = utxos;
|
|
93
93
|
|
|
94
94
|
// Default coinselect won't accumulate some inputs
|
|
@@ -121,17 +121,17 @@ function selectCoins(
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
return { inputs, outputs, fee, change };
|
|
124
|
-
}
|
|
124
|
+
};
|
|
125
125
|
|
|
126
126
|
const OUTPUT_TYPES_MAP = {
|
|
127
127
|
[classify.types.P2WPKH]: 'witness_v0_keyhash',
|
|
128
128
|
[classify.types.P2WSH]: 'witness_v0_scripthash',
|
|
129
129
|
};
|
|
130
130
|
|
|
131
|
-
|
|
131
|
+
const decodeRawTransaction = (
|
|
132
132
|
hex: string,
|
|
133
133
|
network: BitcoinNetwork,
|
|
134
|
-
): bT.Transaction {
|
|
134
|
+
): bT.Transaction => {
|
|
135
135
|
const bjsTx = bitcoin.Transaction.fromHex(hex);
|
|
136
136
|
|
|
137
137
|
const vin = bjsTx.ins.map((input) => {
|
|
@@ -184,13 +184,13 @@ function decodeRawTransaction(
|
|
|
184
184
|
vout,
|
|
185
185
|
hex,
|
|
186
186
|
};
|
|
187
|
-
}
|
|
187
|
+
};
|
|
188
188
|
|
|
189
|
-
|
|
189
|
+
const normalizeTransactionObject = (
|
|
190
190
|
tx: bT.Transaction,
|
|
191
191
|
fee: number,
|
|
192
192
|
block?: { number: number; hash: string },
|
|
193
|
-
): Transaction<bT.Transaction> {
|
|
193
|
+
): Transaction<bT.Transaction> => {
|
|
194
194
|
const value = tx.vout.reduce(
|
|
195
195
|
(p, n) => p.plus(new BigNumber(n.value).times(1e8)),
|
|
196
196
|
new BigNumber(0),
|
|
@@ -220,11 +220,11 @@ function normalizeTransactionObject(
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
return result;
|
|
223
|
-
}
|
|
223
|
+
};
|
|
224
224
|
|
|
225
225
|
// TODO: This is copy pasta because it's not exported from bitcoinjs-lib
|
|
226
226
|
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts#L477
|
|
227
|
-
|
|
227
|
+
const witnessStackToScriptWitness = (witness: Buffer[]): Buffer => {
|
|
228
228
|
let buffer = Buffer.allocUnsafe(0);
|
|
229
229
|
|
|
230
230
|
function writeSlice(slice: Buffer): void {
|
|
@@ -252,9 +252,9 @@ function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
|
|
|
252
252
|
writeVector(witness);
|
|
253
253
|
|
|
254
254
|
return buffer;
|
|
255
|
-
}
|
|
255
|
+
};
|
|
256
256
|
|
|
257
|
-
|
|
257
|
+
const getPubKeyHash = (address: string, network: BitcoinNetwork): Buffer => {
|
|
258
258
|
const outputScript = bitcoin.address.toOutputScript(address, network);
|
|
259
259
|
const type = classify.output(outputScript);
|
|
260
260
|
if (![classify.types.P2PKH, classify.types.P2WPKH].includes(type)) {
|
|
@@ -270,9 +270,12 @@ function getPubKeyHash(address: string, network: BitcoinNetwork) {
|
|
|
270
270
|
const base58 = bitcoin.address.fromBase58Check(address);
|
|
271
271
|
return base58.hash;
|
|
272
272
|
}
|
|
273
|
-
}
|
|
273
|
+
};
|
|
274
274
|
|
|
275
|
-
|
|
275
|
+
const validateAddress = (
|
|
276
|
+
_address: Address | string,
|
|
277
|
+
network: BitcoinNetwork,
|
|
278
|
+
): void => {
|
|
276
279
|
const address = addressToString(_address);
|
|
277
280
|
|
|
278
281
|
if (typeof address !== 'string') {
|
|
@@ -291,7 +294,7 @@ function validateAddress(_address: Address | string, network: BitcoinNetwork) {
|
|
|
291
294
|
if (!pubKeyHash) {
|
|
292
295
|
throw new InvalidAddressError(`Invalid Address: ${address}`);
|
|
293
296
|
}
|
|
294
|
-
}
|
|
297
|
+
};
|
|
295
298
|
|
|
296
299
|
export {
|
|
297
300
|
calculateFee,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/bitcoin-utils",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.2",
|
|
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.4.
|
|
28
|
-
"@atomicfinance/errors": "^3.4.
|
|
29
|
-
"@atomicfinance/types": "^3.4.
|
|
30
|
-
"@atomicfinance/utils": "^3.4.
|
|
27
|
+
"@atomicfinance/crypto": "^3.4.2",
|
|
28
|
+
"@atomicfinance/errors": "^3.4.2",
|
|
29
|
+
"@atomicfinance/types": "^3.4.2",
|
|
30
|
+
"@atomicfinance/utils": "^3.4.2",
|
|
31
31
|
"@babel/runtime": "^7.12.1",
|
|
32
32
|
"bignumber.js": "^9.0.0",
|
|
33
33
|
"bip174": "^2.0.1",
|