@atomicfinance/bitcoin-utils 3.5.3 → 3.6.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/index.js +78 -6
- package/dist/index.js.map +1 -1
- package/lib/index.ts +100 -6
- package/package.json +6 -7
- package/lib/bitcoinjs-lib-classify.d.ts +0 -1
- package/lib/index.js +0 -243
package/dist/index.js
CHANGED
|
@@ -35,10 +35,82 @@ const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
|
35
35
|
const varuint = __importStar(require("bip174/src/lib/converter/varint"));
|
|
36
36
|
const bitcoin_networks_1 = require("bitcoin-networks");
|
|
37
37
|
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
38
|
-
const classify = __importStar(require("bitcoinjs-lib/src/classify"));
|
|
39
38
|
const coinselect_1 = __importDefault(require("coinselect"));
|
|
40
39
|
const accumulative_1 = __importDefault(require("coinselect/accumulative"));
|
|
41
40
|
const lodash_1 = require("lodash");
|
|
41
|
+
// Script type constants (replacing the classify module)
|
|
42
|
+
const SCRIPT_TYPES = {
|
|
43
|
+
P2PKH: 'pubkeyhash',
|
|
44
|
+
P2SH: 'scripthash',
|
|
45
|
+
P2WPKH: 'witness_v0_keyhash',
|
|
46
|
+
P2WSH: 'witness_v0_scripthash',
|
|
47
|
+
P2TR: 'witness_v1_taproot',
|
|
48
|
+
NULLDATA: 'nulldata',
|
|
49
|
+
NONSTANDARD: 'nonstandard',
|
|
50
|
+
MULTISIG: 'multisig',
|
|
51
|
+
P2PK: 'pubkey',
|
|
52
|
+
};
|
|
53
|
+
// Custom script classification function
|
|
54
|
+
const classifyScript = (script) => {
|
|
55
|
+
if (script.length === 0)
|
|
56
|
+
return SCRIPT_TYPES.NONSTANDARD;
|
|
57
|
+
// P2PKH: OP_DUP OP_HASH160 <20 bytes> OP_EQUALVERIFY OP_CHECKSIG
|
|
58
|
+
if (script.length === 25 &&
|
|
59
|
+
script[0] === 0x76 && // OP_DUP
|
|
60
|
+
script[1] === 0xa9 && // OP_HASH160
|
|
61
|
+
script[2] === 0x14 && // 20 bytes
|
|
62
|
+
script[23] === 0x88 && // OP_EQUALVERIFY
|
|
63
|
+
script[24] === 0xac) {
|
|
64
|
+
// OP_CHECKSIG
|
|
65
|
+
return SCRIPT_TYPES.P2PKH;
|
|
66
|
+
}
|
|
67
|
+
// P2SH: OP_HASH160 <20 bytes> OP_EQUAL
|
|
68
|
+
if (script.length === 23 &&
|
|
69
|
+
script[0] === 0xa9 && // OP_HASH160
|
|
70
|
+
script[1] === 0x14 && // 20 bytes
|
|
71
|
+
script[22] === 0x87) {
|
|
72
|
+
// OP_EQUAL
|
|
73
|
+
return SCRIPT_TYPES.P2SH;
|
|
74
|
+
}
|
|
75
|
+
// P2WPKH: OP_0 <20 bytes>
|
|
76
|
+
if (script.length === 22 &&
|
|
77
|
+
script[0] === 0x00 && // OP_0
|
|
78
|
+
script[1] === 0x14) {
|
|
79
|
+
// 20 bytes
|
|
80
|
+
return SCRIPT_TYPES.P2WPKH;
|
|
81
|
+
}
|
|
82
|
+
// P2WSH: OP_0 <32 bytes>
|
|
83
|
+
if (script.length === 34 &&
|
|
84
|
+
script[0] === 0x00 && // OP_0
|
|
85
|
+
script[1] === 0x20) {
|
|
86
|
+
// 32 bytes
|
|
87
|
+
return SCRIPT_TYPES.P2WSH;
|
|
88
|
+
}
|
|
89
|
+
// P2TR: OP_1 <32 bytes>
|
|
90
|
+
if (script.length === 34 &&
|
|
91
|
+
script[0] === 0x51 && // OP_1
|
|
92
|
+
script[1] === 0x20) {
|
|
93
|
+
// 32 bytes
|
|
94
|
+
return SCRIPT_TYPES.P2TR;
|
|
95
|
+
}
|
|
96
|
+
// OP_RETURN (nulldata)
|
|
97
|
+
if (script.length >= 1 && script[0] === 0x6a) {
|
|
98
|
+
// OP_RETURN
|
|
99
|
+
return SCRIPT_TYPES.NULLDATA;
|
|
100
|
+
}
|
|
101
|
+
// P2PK: <33 or 65 bytes pubkey> OP_CHECKSIG
|
|
102
|
+
if ((script.length === 35 || script.length === 67) &&
|
|
103
|
+
script[script.length - 1] === 0xac) {
|
|
104
|
+
// OP_CHECKSIG
|
|
105
|
+
return SCRIPT_TYPES.P2PK;
|
|
106
|
+
}
|
|
107
|
+
// Basic multisig detection: OP_M <pubkeys> OP_N OP_CHECKMULTISIG
|
|
108
|
+
if (script.length >= 4 && script[script.length - 1] === 0xae) {
|
|
109
|
+
// OP_CHECKMULTISIG
|
|
110
|
+
return SCRIPT_TYPES.MULTISIG;
|
|
111
|
+
}
|
|
112
|
+
return SCRIPT_TYPES.NONSTANDARD;
|
|
113
|
+
};
|
|
42
114
|
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
43
115
|
exports.AddressTypes = AddressTypes;
|
|
44
116
|
const calculateFee = (numInputs, numOutputs, feePerByte) => {
|
|
@@ -103,8 +175,8 @@ const selectCoins = (utxos, targets, feePerByte, fixedInputs = []) => {
|
|
|
103
175
|
};
|
|
104
176
|
exports.selectCoins = selectCoins;
|
|
105
177
|
const OUTPUT_TYPES_MAP = {
|
|
106
|
-
[
|
|
107
|
-
[
|
|
178
|
+
[SCRIPT_TYPES.P2WPKH]: 'witness_v0_keyhash',
|
|
179
|
+
[SCRIPT_TYPES.P2WSH]: 'witness_v0_scripthash',
|
|
108
180
|
};
|
|
109
181
|
const decodeRawTransaction = (hex, network) => {
|
|
110
182
|
const bjsTx = bitcoin.Transaction.fromHex(hex);
|
|
@@ -121,7 +193,7 @@ const decodeRawTransaction = (hex, network) => {
|
|
|
121
193
|
};
|
|
122
194
|
});
|
|
123
195
|
const vout = bjsTx.outs.map((output, n) => {
|
|
124
|
-
const type =
|
|
196
|
+
const type = classifyScript(output.script);
|
|
125
197
|
const vout = {
|
|
126
198
|
value: output.value / 1e8,
|
|
127
199
|
n,
|
|
@@ -209,8 +281,8 @@ const witnessStackToScriptWitness = (witness) => {
|
|
|
209
281
|
exports.witnessStackToScriptWitness = witnessStackToScriptWitness;
|
|
210
282
|
const getPubKeyHash = (address, network) => {
|
|
211
283
|
const outputScript = bitcoin.address.toOutputScript(address, network);
|
|
212
|
-
const type =
|
|
213
|
-
if (
|
|
284
|
+
const type = classifyScript(outputScript);
|
|
285
|
+
if (type !== SCRIPT_TYPES.P2PKH && type !== SCRIPT_TYPES.P2WPKH) {
|
|
214
286
|
throw new Error(`Bitcoin swap doesn't support the address ${address} type of ${type}. Not possible to derive public key hash.`);
|
|
215
287
|
}
|
|
216
288
|
try {
|
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,
|
|
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,4DAAoC;AACpC,2EAA6D;AAC7D,mCAAiC;AAEjC,wDAAwD;AACxD,MAAM,YAAY,GAAG;IACnB,KAAK,EAAE,YAAY;IACnB,IAAI,EAAE,YAAY;IAClB,MAAM,EAAE,oBAAoB;IAC5B,KAAK,EAAE,uBAAuB;IAC9B,IAAI,EAAE,oBAAoB;IAC1B,QAAQ,EAAE,UAAU;IACpB,WAAW,EAAE,aAAa;IAC1B,QAAQ,EAAE,UAAU;IACpB,IAAI,EAAE,QAAQ;CACN,CAAC;AAEX,wCAAwC;AACxC,MAAM,cAAc,GAAG,CAAC,MAAc,EAAU,EAAE;IAChD,IAAI,MAAM,CAAC,MAAM,KAAK,CA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,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
|
@@ -11,11 +11,105 @@ import BigNumber from 'bignumber.js';
|
|
|
11
11
|
import * as varuint from 'bip174/src/lib/converter/varint';
|
|
12
12
|
import { BitcoinNetwork, BitcoinNetworks } from 'bitcoin-networks';
|
|
13
13
|
import * as bitcoin from 'bitcoinjs-lib';
|
|
14
|
-
import * as classify from 'bitcoinjs-lib/src/classify';
|
|
15
14
|
import coinselect from 'coinselect';
|
|
16
15
|
import coinselectAccumulative from 'coinselect/accumulative';
|
|
17
16
|
import { findKey } from 'lodash';
|
|
18
17
|
|
|
18
|
+
// Script type constants (replacing the classify module)
|
|
19
|
+
const SCRIPT_TYPES = {
|
|
20
|
+
P2PKH: 'pubkeyhash',
|
|
21
|
+
P2SH: 'scripthash',
|
|
22
|
+
P2WPKH: 'witness_v0_keyhash',
|
|
23
|
+
P2WSH: 'witness_v0_scripthash',
|
|
24
|
+
P2TR: 'witness_v1_taproot',
|
|
25
|
+
NULLDATA: 'nulldata',
|
|
26
|
+
NONSTANDARD: 'nonstandard',
|
|
27
|
+
MULTISIG: 'multisig',
|
|
28
|
+
P2PK: 'pubkey',
|
|
29
|
+
} as const;
|
|
30
|
+
|
|
31
|
+
// Custom script classification function
|
|
32
|
+
const classifyScript = (script: Buffer): string => {
|
|
33
|
+
if (script.length === 0) return SCRIPT_TYPES.NONSTANDARD;
|
|
34
|
+
|
|
35
|
+
// P2PKH: OP_DUP OP_HASH160 <20 bytes> OP_EQUALVERIFY OP_CHECKSIG
|
|
36
|
+
if (
|
|
37
|
+
script.length === 25 &&
|
|
38
|
+
script[0] === 0x76 && // OP_DUP
|
|
39
|
+
script[1] === 0xa9 && // OP_HASH160
|
|
40
|
+
script[2] === 0x14 && // 20 bytes
|
|
41
|
+
script[23] === 0x88 && // OP_EQUALVERIFY
|
|
42
|
+
script[24] === 0xac
|
|
43
|
+
) {
|
|
44
|
+
// OP_CHECKSIG
|
|
45
|
+
return SCRIPT_TYPES.P2PKH;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// P2SH: OP_HASH160 <20 bytes> OP_EQUAL
|
|
49
|
+
if (
|
|
50
|
+
script.length === 23 &&
|
|
51
|
+
script[0] === 0xa9 && // OP_HASH160
|
|
52
|
+
script[1] === 0x14 && // 20 bytes
|
|
53
|
+
script[22] === 0x87
|
|
54
|
+
) {
|
|
55
|
+
// OP_EQUAL
|
|
56
|
+
return SCRIPT_TYPES.P2SH;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// P2WPKH: OP_0 <20 bytes>
|
|
60
|
+
if (
|
|
61
|
+
script.length === 22 &&
|
|
62
|
+
script[0] === 0x00 && // OP_0
|
|
63
|
+
script[1] === 0x14
|
|
64
|
+
) {
|
|
65
|
+
// 20 bytes
|
|
66
|
+
return SCRIPT_TYPES.P2WPKH;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// P2WSH: OP_0 <32 bytes>
|
|
70
|
+
if (
|
|
71
|
+
script.length === 34 &&
|
|
72
|
+
script[0] === 0x00 && // OP_0
|
|
73
|
+
script[1] === 0x20
|
|
74
|
+
) {
|
|
75
|
+
// 32 bytes
|
|
76
|
+
return SCRIPT_TYPES.P2WSH;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// P2TR: OP_1 <32 bytes>
|
|
80
|
+
if (
|
|
81
|
+
script.length === 34 &&
|
|
82
|
+
script[0] === 0x51 && // OP_1
|
|
83
|
+
script[1] === 0x20
|
|
84
|
+
) {
|
|
85
|
+
// 32 bytes
|
|
86
|
+
return SCRIPT_TYPES.P2TR;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// OP_RETURN (nulldata)
|
|
90
|
+
if (script.length >= 1 && script[0] === 0x6a) {
|
|
91
|
+
// OP_RETURN
|
|
92
|
+
return SCRIPT_TYPES.NULLDATA;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// P2PK: <33 or 65 bytes pubkey> OP_CHECKSIG
|
|
96
|
+
if (
|
|
97
|
+
(script.length === 35 || script.length === 67) &&
|
|
98
|
+
script[script.length - 1] === 0xac
|
|
99
|
+
) {
|
|
100
|
+
// OP_CHECKSIG
|
|
101
|
+
return SCRIPT_TYPES.P2PK;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Basic multisig detection: OP_M <pubkeys> OP_N OP_CHECKMULTISIG
|
|
105
|
+
if (script.length >= 4 && script[script.length - 1] === 0xae) {
|
|
106
|
+
// OP_CHECKMULTISIG
|
|
107
|
+
return SCRIPT_TYPES.MULTISIG;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return SCRIPT_TYPES.NONSTANDARD;
|
|
111
|
+
};
|
|
112
|
+
|
|
19
113
|
const AddressTypes = ['legacy', 'p2sh-segwit', 'bech32'];
|
|
20
114
|
|
|
21
115
|
const calculateFee = (
|
|
@@ -124,8 +218,8 @@ const selectCoins = (
|
|
|
124
218
|
};
|
|
125
219
|
|
|
126
220
|
const OUTPUT_TYPES_MAP = {
|
|
127
|
-
[
|
|
128
|
-
[
|
|
221
|
+
[SCRIPT_TYPES.P2WPKH]: 'witness_v0_keyhash',
|
|
222
|
+
[SCRIPT_TYPES.P2WSH]: 'witness_v0_scripthash',
|
|
129
223
|
};
|
|
130
224
|
|
|
131
225
|
const decodeRawTransaction = (
|
|
@@ -148,7 +242,7 @@ const decodeRawTransaction = (
|
|
|
148
242
|
});
|
|
149
243
|
|
|
150
244
|
const vout = bjsTx.outs.map((output, n) => {
|
|
151
|
-
const type =
|
|
245
|
+
const type = classifyScript(output.script);
|
|
152
246
|
|
|
153
247
|
const vout: bT.Output = {
|
|
154
248
|
value: output.value / 1e8,
|
|
@@ -256,8 +350,8 @@ const witnessStackToScriptWitness = (witness: Buffer[]): Buffer => {
|
|
|
256
350
|
|
|
257
351
|
const getPubKeyHash = (address: string, network: BitcoinNetwork): Buffer => {
|
|
258
352
|
const outputScript = bitcoin.address.toOutputScript(address, network);
|
|
259
|
-
const type =
|
|
260
|
-
if (
|
|
353
|
+
const type = classifyScript(outputScript);
|
|
354
|
+
if (type !== SCRIPT_TYPES.P2PKH && type !== SCRIPT_TYPES.P2WPKH) {
|
|
261
355
|
throw new Error(
|
|
262
356
|
`Bitcoin swap doesn't support the address ${address} type of ${type}. Not possible to derive public key hash.`,
|
|
263
357
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/bitcoin-utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,15 +24,15 @@
|
|
|
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.1",
|
|
28
|
+
"@atomicfinance/errors": "^3.6.1",
|
|
29
|
+
"@atomicfinance/types": "^3.6.1",
|
|
30
|
+
"@atomicfinance/utils": "^3.6.1",
|
|
31
31
|
"@babel/runtime": "^7.12.1",
|
|
32
32
|
"bignumber.js": "^9.0.0",
|
|
33
33
|
"bip174": "^2.0.1",
|
|
34
34
|
"bitcoin-networks": "^1.0.0",
|
|
35
|
-
"bitcoinjs-lib": "
|
|
35
|
+
"bitcoinjs-lib": "6.1.7",
|
|
36
36
|
"coinselect": "^3.1.11",
|
|
37
37
|
"lodash": "^4.17.20"
|
|
38
38
|
},
|
|
@@ -40,7 +40,6 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@types/bitcoinjs-lib": "^5.0.0",
|
|
44
43
|
"@types/lodash": "^4.14.168",
|
|
45
44
|
"@types/node": "16.10.3"
|
|
46
45
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
declare module 'bitcoinjs-lib/src/classify'
|
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
|