@teleportdao/bitcoin 2.0.5 → 2.0.8
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-interface-ordinal.d.ts +108 -108
- package/dist/bitcoin-interface-ordinal.js +140 -140
- package/dist/bitcoin-interface-teleswap.d.ts +83 -101
- package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
- package/dist/bitcoin-interface-teleswap.js +119 -176
- package/dist/bitcoin-interface-teleswap.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +20 -20
- package/dist/bitcoin-interface-utils.js +45 -45
- package/dist/bitcoin-interface-wallet.d.ts +29 -28
- package/dist/bitcoin-interface-wallet.d.ts.map +1 -1
- package/dist/bitcoin-interface-wallet.js +126 -125
- package/dist/bitcoin-interface-wallet.js.map +1 -1
- package/dist/bitcoin-interface.d.ts +63 -66
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +112 -119
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-utils.d.ts +96 -96
- package/dist/bitcoin-utils.js +514 -514
- package/dist/bitcoin-wallet-base.d.ts +111 -111
- package/dist/bitcoin-wallet-base.d.ts.map +1 -1
- package/dist/bitcoin-wallet-base.js +258 -258
- package/dist/helper/brc20-helper.d.ts +42 -42
- package/dist/helper/brc20-helper.js +127 -127
- package/dist/helper/index.d.ts +3 -3
- package/dist/helper/index.js +29 -29
- package/dist/helper/ordinal-helper.d.ts +12 -12
- package/dist/helper/ordinal-helper.js +129 -129
- package/dist/helper/teleswap-helper.d.ts +95 -95
- package/dist/helper/teleswap-helper.js +186 -186
- package/dist/index.d.ts +12 -12
- package/dist/index.js +41 -41
- package/dist/ordinal-wallet.d.ts +492 -495
- package/dist/ordinal-wallet.d.ts.map +1 -1
- package/dist/ordinal-wallet.js +386 -386
- package/dist/ordinal-wallet.js.map +1 -1
- package/dist/sign/index.d.ts +1 -1
- package/dist/sign/index.js +8 -8
- package/dist/sign/sign-transaction.d.ts +12 -12
- package/dist/sign/sign-transaction.js +82 -82
- package/dist/teleswap-wallet.d.ts +45 -45
- package/dist/teleswap-wallet.js +68 -68
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +9 -9
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +54 -54
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/index.d.ts +3 -3
- package/dist/transaction-builder/index.js +19 -19
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -63
- package/dist/transaction-builder/ordinal-transaction-builder.js +125 -125
- package/dist/transaction-builder/transaction-builder.d.ts +223 -223
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +442 -447
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/type.d.ts +55 -61
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +2 -2
- package/dist/utils/networks.d.ts +5 -5
- package/dist/utils/networks.js +53 -53
- package/dist/utils/tools.d.ts +18 -18
- package/dist/utils/tools.js +74 -74
- package/package.json +4 -4
- package/src/bitcoin-interface-ordinal.ts +185 -185
- package/src/bitcoin-interface-teleswap.ts +246 -251
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +112 -114
- package/src/bitcoin-interface.ts +146 -156
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +344 -344
- package/src/helper/brc20-helper.ts +179 -179
- package/src/helper/ordinal-helper.ts +118 -118
- package/src/index.ts +15 -15
- package/src/ordinal-wallet.ts +654 -659
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +133 -133
- package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -26
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +139 -139
- package/src/transaction-builder/transaction-builder.ts +686 -690
- package/src/type.ts +64 -74
- package/src/utils/networks.ts +33 -33
- package/src/utils/tools.ts +92 -92
- package/tsconfig.json +9 -9
- package/webpack.config.js +16 -16
- package/.tmp/block-parser.ts +0 -58
- package/.tmp/check.ts +0 -101
- package/.tmp/ordinal-helper.ts +0 -133
- package/.tmp/ordinal.ts +0 -25
- package/.tmp/psbt/sign-transaction.ts +0 -121
- package/.tmp/rbf.ts +0 -45
package/dist/bitcoin-utils.js
CHANGED
|
@@ -1,515 +1,515 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
-
};
|
|
37
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.addressTypeHelper = exports.publicKeyConvertor = exports.createAddressObjectByPublicKey = exports.createAddressObjectByHash = exports.createAddressObjectByScript = exports.validateAddress = exports.extractTransactionsAndBlockInfoFromRawBlock = exports.parseRawBlock = exports.convertBitcoinScriptToAddress = exports.createAddressObjectByAddress = exports.getAddressType = exports.parseBlockHeader = exports.calculateMerkleProof = exports.parseRawTransaction = exports.getPrivateKeyHexFromWIF = exports.getPubKeyFromPrivateKeyHex = exports.getPubKeyFromPrivateKeyWIF = exports.getPublicKeyHexByXpubAndIndex = exports.reverseBytes = exports.getPrivateKeyHexFromMnemonic = exports.generateMnemonic = exports.networks = void 0;
|
|
39
|
-
const bip39 = __importStar(require("bip39"));
|
|
40
|
-
const bip32_1 = __importDefault(require("bip32"));
|
|
41
|
-
const ecc = __importStar(require("@bitcoinerlab/secp256k1"));
|
|
42
|
-
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
43
|
-
const bitcoinEcPair = __importStar(require("bitcoinjs-ecpair"));
|
|
44
|
-
const networks_1 = __importDefault(require("./utils/networks"));
|
|
45
|
-
exports.networks = networks_1.default;
|
|
46
|
-
const bip32 = (0, bip32_1.default)(ecc);
|
|
47
|
-
const { ECPair } = bitcoinEcPair;
|
|
48
|
-
bitcoin.initEccLib(ecc);
|
|
49
|
-
const varUnit = require("varuint-bitcoin");
|
|
50
|
-
const fastRoot = require("merkle-lib/fastRoot");
|
|
51
|
-
const merkle = require("merkle-lib");
|
|
52
|
-
const merkleProof = require("merkle-lib/proof");
|
|
53
|
-
function generateMnemonic() {
|
|
54
|
-
const mnemonic = bip39.generateMnemonic(256);
|
|
55
|
-
return mnemonic;
|
|
56
|
-
}
|
|
57
|
-
exports.generateMnemonic = generateMnemonic;
|
|
58
|
-
function getPrivateKeyHexFromMnemonic(mnemonic, index = 0, isChange = false) {
|
|
59
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
const node = bip32.fromSeed(yield bip39.mnemonicToSeed(mnemonic));
|
|
61
|
-
return node
|
|
62
|
-
.derive(isChange ? 1 : 0)
|
|
63
|
-
.derive(index)
|
|
64
|
-
.privateKey.toString("hex");
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
exports.getPrivateKeyHexFromMnemonic = getPrivateKeyHexFromMnemonic;
|
|
68
|
-
function reverseBytes(hexInput) {
|
|
69
|
-
return Buffer.from(hexInput, "hex").reverse().toString("hex");
|
|
70
|
-
}
|
|
71
|
-
exports.reverseBytes = reverseBytes;
|
|
72
|
-
function getPublicKeyHexByXpubAndIndex(xpub, index = 0, isChange = false, network = networks_1.default.bitcoin) {
|
|
73
|
-
const node = bip32.fromBase58(xpub, network);
|
|
74
|
-
return node
|
|
75
|
-
.derive(isChange ? 1 : 0)
|
|
76
|
-
.derive(index)
|
|
77
|
-
.publicKey.toString("hex");
|
|
78
|
-
}
|
|
79
|
-
exports.getPublicKeyHexByXpubAndIndex = getPublicKeyHexByXpubAndIndex;
|
|
80
|
-
function getPubKeyFromPrivateKeyWIF(privateKeyWIF, network = bitcoin.networks.bitcoin) {
|
|
81
|
-
let key = ECPair.fromWIF(privateKeyWIF, network);
|
|
82
|
-
return key.publicKey;
|
|
83
|
-
}
|
|
84
|
-
exports.getPubKeyFromPrivateKeyWIF = getPubKeyFromPrivateKeyWIF;
|
|
85
|
-
function getPubKeyFromPrivateKeyHex(privateKeyHex, network = bitcoin.networks.bitcoin) {
|
|
86
|
-
let key = ECPair.fromPrivateKey(Buffer.from(privateKeyHex, "hex"), { network });
|
|
87
|
-
return key.publicKey;
|
|
88
|
-
}
|
|
89
|
-
exports.getPubKeyFromPrivateKeyHex = getPubKeyFromPrivateKeyHex;
|
|
90
|
-
function getPrivateKeyHexFromWIF(privateKeyWIF, network = bitcoin.networks.bitcoin) {
|
|
91
|
-
let key = ECPair.fromWIF(privateKeyWIF, network);
|
|
92
|
-
return key.privateKey.toString("hex");
|
|
93
|
-
}
|
|
94
|
-
exports.getPrivateKeyHexFromWIF = getPrivateKeyHexFromWIF;
|
|
95
|
-
function parseRawTransaction(rawTransaction) {
|
|
96
|
-
const size = {
|
|
97
|
-
version: 4,
|
|
98
|
-
flag: 2,
|
|
99
|
-
tx: 32,
|
|
100
|
-
index: 4,
|
|
101
|
-
sequence: 4,
|
|
102
|
-
amount: 8,
|
|
103
|
-
};
|
|
104
|
-
let offset = 0;
|
|
105
|
-
let version = rawTransaction.slice(offset, size.version * 2);
|
|
106
|
-
offset += size.version * 2;
|
|
107
|
-
let flag = rawTransaction.slice(offset, offset + size.flag * 2);
|
|
108
|
-
offset = flag === "0001" ? offset + size.flag * 2 : offset;
|
|
109
|
-
let inputsStartIndex = offset;
|
|
110
|
-
let numberOfInputs = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
111
|
-
let noiSize = varUnit.encodingLength(numberOfInputs);
|
|
112
|
-
offset += noiSize * 2;
|
|
113
|
-
for (let i = 0; i < numberOfInputs; i += 1) {
|
|
114
|
-
offset += size.tx * 2;
|
|
115
|
-
offset += size.index * 2;
|
|
116
|
-
let sigLength = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
117
|
-
let sigLengthSize = varUnit.encodingLength(sigLength);
|
|
118
|
-
offset += sigLengthSize * 2;
|
|
119
|
-
offset += sigLength * 2;
|
|
120
|
-
offset += size.sequence * 2;
|
|
121
|
-
}
|
|
122
|
-
let inputLastIndex = offset;
|
|
123
|
-
let outputStartIndex = offset;
|
|
124
|
-
let numberOfOutputs = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
125
|
-
let nooSize = varUnit.encodingLength(numberOfOutputs);
|
|
126
|
-
offset += nooSize * 2;
|
|
127
|
-
for (let i = 0; i < numberOfOutputs; i += 1) {
|
|
128
|
-
offset += size.amount * 2;
|
|
129
|
-
let unlockSigLength = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
130
|
-
let unlockSigLengthSize = varUnit.encodingLength(unlockSigLength);
|
|
131
|
-
offset += unlockSigLengthSize * 2;
|
|
132
|
-
offset += unlockSigLength * 2;
|
|
133
|
-
}
|
|
134
|
-
let outputLastIndex = offset;
|
|
135
|
-
version = `0x${version}`;
|
|
136
|
-
flag = `0x${flag}`;
|
|
137
|
-
const vin = `0x${rawTransaction.slice(inputsStartIndex, inputLastIndex)}`;
|
|
138
|
-
const vout = `0x${rawTransaction.slice(outputStartIndex, outputLastIndex)}`;
|
|
139
|
-
let witness = `0x${rawTransaction.slice(outputLastIndex, rawTransaction.length - 8)}`;
|
|
140
|
-
let locktime = `0x${rawTransaction.slice(rawTransaction.length - 8, rawTransaction.length)}`;
|
|
141
|
-
return { version, flag, vin, vout, witness, locktime };
|
|
142
|
-
}
|
|
143
|
-
exports.parseRawTransaction = parseRawTransaction;
|
|
144
|
-
function calculateMerkleProof(blockTransactions, txId, blockMerkleRoot) {
|
|
145
|
-
let transactionIndex = blockTransactions.findIndex((tx) => tx === txId);
|
|
146
|
-
if (transactionIndex < 0)
|
|
147
|
-
throw new Error("txId is not in this tree");
|
|
148
|
-
let data = blockTransactions.map((a) => Buffer.from(a, "hex").reverse());
|
|
149
|
-
if (blockMerkleRoot &&
|
|
150
|
-
blockMerkleRoot !== fastRoot(data, bitcoin.crypto.hash256).toString("hex")) {
|
|
151
|
-
throw new Error("calculated merkleRoot and block merkleRoot not matched");
|
|
152
|
-
}
|
|
153
|
-
let tree = merkle(data, bitcoin.crypto.hash256);
|
|
154
|
-
let proof = merkleProof(tree, data[transactionIndex]);
|
|
155
|
-
let intermediateNodesArray = proof
|
|
156
|
-
.map((_id) => _id && _id.toString("hex"))
|
|
157
|
-
.filter((_id) => _id != null);
|
|
158
|
-
let intermediateNodes = intermediateNodesArray.reduce((a, value, index) => index !== transactionIndex % 2 && index < intermediateNodesArray.length - 1 ? a + value : a, "0x");
|
|
159
|
-
return {
|
|
160
|
-
intermediateNodes,
|
|
161
|
-
transactionIndex,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
exports.calculateMerkleProof = calculateMerkleProof;
|
|
165
|
-
function parseBlockHeader(headerHex) {
|
|
166
|
-
const size = {
|
|
167
|
-
version: 4,
|
|
168
|
-
previousBlockHash: 32,
|
|
169
|
-
merkleRoot: 32,
|
|
170
|
-
timestamp: 4,
|
|
171
|
-
difficulty: 4,
|
|
172
|
-
nonce: 4,
|
|
173
|
-
};
|
|
174
|
-
let offset = 0;
|
|
175
|
-
let result = {};
|
|
176
|
-
for (let key in size) {
|
|
177
|
-
result[key] = headerHex.slice(offset, offset + size[key] * 2);
|
|
178
|
-
offset += size[key] * 2;
|
|
179
|
-
}
|
|
180
|
-
return result;
|
|
181
|
-
}
|
|
182
|
-
exports.parseBlockHeader = parseBlockHeader;
|
|
183
|
-
function getAddressType(address, network = bitcoin.networks.bitcoin) {
|
|
184
|
-
if (address.startsWith(`${network.bech32}1p`)) {
|
|
185
|
-
return "p2tr";
|
|
186
|
-
}
|
|
187
|
-
if (address.startsWith(network.bech32)) {
|
|
188
|
-
bitcoin.address.fromBech32(address);
|
|
189
|
-
return "p2wpkh";
|
|
190
|
-
}
|
|
191
|
-
let base58Data = bitcoin.address.fromBase58Check(address);
|
|
192
|
-
if (base58Data.version === Number(network.scriptHash)) {
|
|
193
|
-
return "p2sh";
|
|
194
|
-
}
|
|
195
|
-
if (base58Data.version === Number(network.pubKeyHash)) {
|
|
196
|
-
return "p2pkh";
|
|
197
|
-
}
|
|
198
|
-
throw new Error("invalid address");
|
|
199
|
-
}
|
|
200
|
-
exports.getAddressType = getAddressType;
|
|
201
|
-
function createAddressObjectByAddress(address, network = bitcoin.networks.bitcoin) {
|
|
202
|
-
let addressType = getAddressType(address, network);
|
|
203
|
-
let addressObject;
|
|
204
|
-
switch (addressType) {
|
|
205
|
-
case "p2pkh":
|
|
206
|
-
addressObject = bitcoin.payments.p2pkh({
|
|
207
|
-
address,
|
|
208
|
-
network,
|
|
209
|
-
});
|
|
210
|
-
break;
|
|
211
|
-
case "p2wpkh":
|
|
212
|
-
addressObject = bitcoin.payments.p2wpkh({
|
|
213
|
-
address,
|
|
214
|
-
network,
|
|
215
|
-
});
|
|
216
|
-
break;
|
|
217
|
-
case "p2sh":
|
|
218
|
-
addressObject = bitcoin.payments.p2sh({
|
|
219
|
-
address,
|
|
220
|
-
network,
|
|
221
|
-
});
|
|
222
|
-
break;
|
|
223
|
-
case "p2tr":
|
|
224
|
-
addressObject = bitcoin.payments.p2tr({
|
|
225
|
-
address,
|
|
226
|
-
network,
|
|
227
|
-
});
|
|
228
|
-
break;
|
|
229
|
-
default:
|
|
230
|
-
throw new Error("address type is incorrect");
|
|
231
|
-
}
|
|
232
|
-
return { addressObject, addressType };
|
|
233
|
-
}
|
|
234
|
-
exports.createAddressObjectByAddress = createAddressObjectByAddress;
|
|
235
|
-
function convertBitcoinScriptToAddress(script, network = bitcoin.networks.bitcoin) {
|
|
236
|
-
try {
|
|
237
|
-
return bitcoin.address.fromOutputScript(script, network);
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
return undefined;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
exports.convertBitcoinScriptToAddress = convertBitcoinScriptToAddress;
|
|
244
|
-
function parseRawBlock(rawBlockHex, blockNumber, network = bitcoin.networks.bitcoin) {
|
|
245
|
-
var _a;
|
|
246
|
-
let block = bitcoin.Block.fromBuffer(Buffer.from(rawBlockHex, "hex"));
|
|
247
|
-
let blockHash = block.getHash().toString("hex");
|
|
248
|
-
let merkleRoot = block.merkleRoot.toString("hex");
|
|
249
|
-
let prvBlockHash = block.prevHash.toString("hex");
|
|
250
|
-
return {
|
|
251
|
-
blockNumber,
|
|
252
|
-
merkleRoot,
|
|
253
|
-
prvBlockHash,
|
|
254
|
-
transactions: (_a = block.transactions) === null || _a === void 0 ? void 0 : _a.map((tx) => ({
|
|
255
|
-
txId: tx.getId(),
|
|
256
|
-
version: tx.version,
|
|
257
|
-
locktime: tx.locktime,
|
|
258
|
-
blockNumber,
|
|
259
|
-
blockHash,
|
|
260
|
-
vout: tx.outs.map((vo, index) => ({
|
|
261
|
-
address: convertBitcoinScriptToAddress(vo.script, network),
|
|
262
|
-
script: vo.script.toString("hex"),
|
|
263
|
-
value: vo.value,
|
|
264
|
-
index,
|
|
265
|
-
})),
|
|
266
|
-
vin: tx.ins.map((vi) => ({
|
|
267
|
-
txId: Buffer.from(vi.hash).reverse().toString("hex"),
|
|
268
|
-
index: vi.index,
|
|
269
|
-
})),
|
|
270
|
-
})),
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
exports.parseRawBlock = parseRawBlock;
|
|
274
|
-
function extractTransactionsAndBlockInfoFromRawBlock(rawBlockHex, blockNumber, addresses = [], inputTxIds = [], network = bitcoin.networks.bitcoin) {
|
|
275
|
-
var _a;
|
|
276
|
-
let block = bitcoin.Block.fromBuffer(Buffer.from(rawBlockHex, "hex"));
|
|
277
|
-
let blockHash = block.getHash().reverse().toString("hex");
|
|
278
|
-
let merkleRoot = block.merkleRoot.toString("hex");
|
|
279
|
-
let prvBlockHash = block.prevHash.toString("hex");
|
|
280
|
-
let blockInfo = {
|
|
281
|
-
blockNumber,
|
|
282
|
-
blockHash,
|
|
283
|
-
merkleRoot,
|
|
284
|
-
prvBlockHash,
|
|
285
|
-
};
|
|
286
|
-
let addressScript = addresses.map((address) => createAddressObjectByAddress(address, network).addressObject.output.toString("hex"));
|
|
287
|
-
let blockTxIds = block.transactions.map((tx) => tx.getId());
|
|
288
|
-
let withdrawTxs = [];
|
|
289
|
-
let depositTxs = [];
|
|
290
|
-
(_a = block.transactions) === null || _a === void 0 ? void 0 : _a.forEach((tx) => {
|
|
291
|
-
let txId = tx.getId();
|
|
292
|
-
let inputTxAddress;
|
|
293
|
-
let isWithdraw = tx.ins.find((vi) => {
|
|
294
|
-
let viTxId = Buffer.from(vi.hash).reverse().toString("hex");
|
|
295
|
-
let viInput = inputTxIds.find((vin) => vin.txId === viTxId && vin.index === vi.index);
|
|
296
|
-
inputTxAddress = viInput === null || viInput === void 0 ? void 0 : viInput.address;
|
|
297
|
-
return !!inputTxAddress;
|
|
298
|
-
});
|
|
299
|
-
if (isWithdraw && inputTxAddress) {
|
|
300
|
-
let txMerkleProof = calculateMerkleProof(blockTxIds, txId, merkleRoot);
|
|
301
|
-
withdrawTxs.push({
|
|
302
|
-
txId: tx.getId(),
|
|
303
|
-
version: tx.version,
|
|
304
|
-
locktime: tx.locktime,
|
|
305
|
-
blockNumber,
|
|
306
|
-
blockHash,
|
|
307
|
-
merkleProof: txMerkleProof,
|
|
308
|
-
vout: tx.outs.map((vo) => ({
|
|
309
|
-
address: convertBitcoinScriptToAddress(vo.script, network),
|
|
310
|
-
script: vo.script.toString("hex"),
|
|
311
|
-
value: vo.value,
|
|
312
|
-
})),
|
|
313
|
-
vin: tx.ins.map((vi) => {
|
|
314
|
-
let viTxId = Buffer.from(vi.hash).reverse().toString("hex");
|
|
315
|
-
let viInput = inputTxIds.find((vin) => vin.txId === viTxId && vin.index === vi.index);
|
|
316
|
-
return {
|
|
317
|
-
txId: Buffer.from(vi.hash).reverse().toString("hex"),
|
|
318
|
-
index: vi.index,
|
|
319
|
-
address: viInput === null || viInput === void 0 ? void 0 : viInput.address,
|
|
320
|
-
script: viInput === null || viInput === void 0 ? void 0 : viInput.script,
|
|
321
|
-
value: viInput === null || viInput === void 0 ? void 0 : viInput.value,
|
|
322
|
-
};
|
|
323
|
-
}),
|
|
324
|
-
address: inputTxAddress,
|
|
325
|
-
addressScript: createAddressObjectByAddress(inputTxAddress, network).addressObject.output.toString("hex"),
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
let addressIndex = -1;
|
|
329
|
-
let isDeposit = tx.outs.find((blockTxVo) => {
|
|
330
|
-
let sIndex = addressScript.findIndex((addScript) => addScript === blockTxVo.script.toString("hex"));
|
|
331
|
-
if (sIndex >= 0) {
|
|
332
|
-
addressIndex = sIndex;
|
|
333
|
-
return true;
|
|
334
|
-
}
|
|
335
|
-
return false;
|
|
336
|
-
});
|
|
337
|
-
if (isDeposit && addressIndex >= 0) {
|
|
338
|
-
let txMerkleProof = calculateMerkleProof(blockTxIds, txId, merkleRoot);
|
|
339
|
-
depositTxs.push({
|
|
340
|
-
txId: tx.getId(),
|
|
341
|
-
version: tx.version,
|
|
342
|
-
locktime: tx.locktime,
|
|
343
|
-
blockNumber,
|
|
344
|
-
blockHash,
|
|
345
|
-
merkleProof: txMerkleProof,
|
|
346
|
-
vout: tx.outs.map((vo) => ({
|
|
347
|
-
address: convertBitcoinScriptToAddress(vo.script, network),
|
|
348
|
-
script: vo.script.toString("hex"),
|
|
349
|
-
value: vo.value,
|
|
350
|
-
})),
|
|
351
|
-
vin: tx.ins.map((vi) => ({
|
|
352
|
-
txId: Buffer.from(vi.hash).reverse().toString("hex"),
|
|
353
|
-
index: vi.index,
|
|
354
|
-
})),
|
|
355
|
-
addressScript: addressScript[addressIndex],
|
|
356
|
-
address: addresses[addressIndex],
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
return {
|
|
361
|
-
blockInfo,
|
|
362
|
-
withdrawTxs,
|
|
363
|
-
depositTxs,
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
exports.extractTransactionsAndBlockInfoFromRawBlock = extractTransactionsAndBlockInfoFromRawBlock;
|
|
367
|
-
function validateAddress(address, network = bitcoin.networks.bitcoin) {
|
|
368
|
-
try {
|
|
369
|
-
let isValid = false;
|
|
370
|
-
let isAddressSegwit = address.startsWith(network.bech32);
|
|
371
|
-
if (isAddressSegwit) {
|
|
372
|
-
bitcoin.address.fromBech32(address);
|
|
373
|
-
isValid = true;
|
|
374
|
-
}
|
|
375
|
-
else {
|
|
376
|
-
let base58Data = bitcoin.address.fromBase58Check(address);
|
|
377
|
-
isValid =
|
|
378
|
-
base58Data.version === Number(network.scriptHash) ||
|
|
379
|
-
base58Data.version === Number(network.pubKeyHash);
|
|
380
|
-
}
|
|
381
|
-
return isValid;
|
|
382
|
-
}
|
|
383
|
-
catch (error) {
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
exports.validateAddress = validateAddress;
|
|
388
|
-
function toXOnlyPublicKey(pubKey) {
|
|
389
|
-
return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
|
|
390
|
-
}
|
|
391
|
-
function createAddressObjectByScript({ addressType, script }, network = bitcoin.networks.bitcoin) {
|
|
392
|
-
let addressObject;
|
|
393
|
-
switch (addressType) {
|
|
394
|
-
case "p2pkh":
|
|
395
|
-
addressObject = bitcoin.payments.p2pkh({
|
|
396
|
-
output: script,
|
|
397
|
-
network,
|
|
398
|
-
});
|
|
399
|
-
break;
|
|
400
|
-
case "p2wpkh":
|
|
401
|
-
addressObject = bitcoin.payments.p2wpkh({
|
|
402
|
-
output: script,
|
|
403
|
-
network,
|
|
404
|
-
});
|
|
405
|
-
break;
|
|
406
|
-
case "p2sh":
|
|
407
|
-
addressObject = bitcoin.payments.p2sh({
|
|
408
|
-
output: script,
|
|
409
|
-
network,
|
|
410
|
-
});
|
|
411
|
-
break;
|
|
412
|
-
case "p2wsh":
|
|
413
|
-
addressObject = bitcoin.payments.p2wsh({
|
|
414
|
-
output: script,
|
|
415
|
-
network,
|
|
416
|
-
});
|
|
417
|
-
break;
|
|
418
|
-
case "p2tr":
|
|
419
|
-
addressObject = bitcoin.payments.p2tr({
|
|
420
|
-
output: script,
|
|
421
|
-
network,
|
|
422
|
-
});
|
|
423
|
-
break;
|
|
424
|
-
default:
|
|
425
|
-
throw new Error("address type is incorrect");
|
|
426
|
-
}
|
|
427
|
-
return addressObject;
|
|
428
|
-
}
|
|
429
|
-
exports.createAddressObjectByScript = createAddressObjectByScript;
|
|
430
|
-
function createAddressObjectByHash({ addressType, hash }, network = bitcoin.networks.bitcoin) {
|
|
431
|
-
let addressObject;
|
|
432
|
-
switch (addressType) {
|
|
433
|
-
case "p2pkh":
|
|
434
|
-
addressObject = bitcoin.payments.p2pkh({
|
|
435
|
-
hash,
|
|
436
|
-
network,
|
|
437
|
-
});
|
|
438
|
-
break;
|
|
439
|
-
case "p2wpkh":
|
|
440
|
-
addressObject = bitcoin.payments.p2wpkh({
|
|
441
|
-
hash,
|
|
442
|
-
network,
|
|
443
|
-
});
|
|
444
|
-
break;
|
|
445
|
-
case "p2sh":
|
|
446
|
-
addressObject = bitcoin.payments.p2sh({
|
|
447
|
-
hash,
|
|
448
|
-
network,
|
|
449
|
-
});
|
|
450
|
-
break;
|
|
451
|
-
case "p2wsh":
|
|
452
|
-
addressObject = bitcoin.payments.p2wsh({
|
|
453
|
-
hash,
|
|
454
|
-
network,
|
|
455
|
-
});
|
|
456
|
-
break;
|
|
457
|
-
case "p2tr":
|
|
458
|
-
addressObject = bitcoin.payments.p2tr({
|
|
459
|
-
pubkey: hash,
|
|
460
|
-
network,
|
|
461
|
-
});
|
|
462
|
-
break;
|
|
463
|
-
default:
|
|
464
|
-
throw new Error("address type is incorrect");
|
|
465
|
-
}
|
|
466
|
-
return addressObject;
|
|
467
|
-
}
|
|
468
|
-
exports.createAddressObjectByHash = createAddressObjectByHash;
|
|
469
|
-
function createAddressObjectByPublicKey({ addressType, publicKey }, network = bitcoin.networks.bitcoin) {
|
|
470
|
-
let addressObject;
|
|
471
|
-
switch (addressType) {
|
|
472
|
-
case "p2pkh":
|
|
473
|
-
addressObject = bitcoin.payments.p2pkh({
|
|
474
|
-
pubkey: publicKey,
|
|
475
|
-
network,
|
|
476
|
-
});
|
|
477
|
-
break;
|
|
478
|
-
case "p2wpkh":
|
|
479
|
-
addressObject = bitcoin.payments.p2wpkh({
|
|
480
|
-
pubkey: publicKey,
|
|
481
|
-
network,
|
|
482
|
-
});
|
|
483
|
-
break;
|
|
484
|
-
case "p2sh-p2wpkh":
|
|
485
|
-
addressObject = bitcoin.payments.p2sh({
|
|
486
|
-
redeem: bitcoin.payments.p2wpkh({
|
|
487
|
-
pubkey: publicKey,
|
|
488
|
-
network,
|
|
489
|
-
}),
|
|
490
|
-
});
|
|
491
|
-
break;
|
|
492
|
-
case "p2tr":
|
|
493
|
-
addressObject = bitcoin.payments.p2tr({
|
|
494
|
-
internalPubkey: toXOnlyPublicKey(publicKey),
|
|
495
|
-
network,
|
|
496
|
-
});
|
|
497
|
-
break;
|
|
498
|
-
default:
|
|
499
|
-
throw new Error("address type is incorrect");
|
|
500
|
-
}
|
|
501
|
-
return addressObject;
|
|
502
|
-
}
|
|
503
|
-
exports.createAddressObjectByPublicKey = createAddressObjectByPublicKey;
|
|
504
|
-
function publicKeyConvertor(publicKeyHex, compressed = true) {
|
|
505
|
-
let pubkey = ECPair.fromPublicKey(Buffer.from(publicKeyHex, "hex"), {
|
|
506
|
-
compressed,
|
|
507
|
-
});
|
|
508
|
-
return pubkey.publicKey.toString("hex");
|
|
509
|
-
}
|
|
510
|
-
exports.publicKeyConvertor = publicKeyConvertor;
|
|
511
|
-
exports.addressTypeHelper = {
|
|
512
|
-
addressTypesNumber: { p2pk: 0, p2pkh: 1, p2sh: 2, p2wpkh: 3, p2wsh: 4, p2tr: 5 },
|
|
513
|
-
addressTypes: ["p2pk", "p2pkh", "p2sh", "p2wpkh", "p2wsh", "p2tr"],
|
|
514
|
-
};
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.addressTypeHelper = exports.publicKeyConvertor = exports.createAddressObjectByPublicKey = exports.createAddressObjectByHash = exports.createAddressObjectByScript = exports.validateAddress = exports.extractTransactionsAndBlockInfoFromRawBlock = exports.parseRawBlock = exports.convertBitcoinScriptToAddress = exports.createAddressObjectByAddress = exports.getAddressType = exports.parseBlockHeader = exports.calculateMerkleProof = exports.parseRawTransaction = exports.getPrivateKeyHexFromWIF = exports.getPubKeyFromPrivateKeyHex = exports.getPubKeyFromPrivateKeyWIF = exports.getPublicKeyHexByXpubAndIndex = exports.reverseBytes = exports.getPrivateKeyHexFromMnemonic = exports.generateMnemonic = exports.networks = void 0;
|
|
39
|
+
const bip39 = __importStar(require("bip39"));
|
|
40
|
+
const bip32_1 = __importDefault(require("bip32"));
|
|
41
|
+
const ecc = __importStar(require("@bitcoinerlab/secp256k1"));
|
|
42
|
+
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
43
|
+
const bitcoinEcPair = __importStar(require("bitcoinjs-ecpair"));
|
|
44
|
+
const networks_1 = __importDefault(require("./utils/networks"));
|
|
45
|
+
exports.networks = networks_1.default;
|
|
46
|
+
const bip32 = (0, bip32_1.default)(ecc);
|
|
47
|
+
const { ECPair } = bitcoinEcPair;
|
|
48
|
+
bitcoin.initEccLib(ecc);
|
|
49
|
+
const varUnit = require("varuint-bitcoin");
|
|
50
|
+
const fastRoot = require("merkle-lib/fastRoot");
|
|
51
|
+
const merkle = require("merkle-lib");
|
|
52
|
+
const merkleProof = require("merkle-lib/proof");
|
|
53
|
+
function generateMnemonic() {
|
|
54
|
+
const mnemonic = bip39.generateMnemonic(256);
|
|
55
|
+
return mnemonic;
|
|
56
|
+
}
|
|
57
|
+
exports.generateMnemonic = generateMnemonic;
|
|
58
|
+
function getPrivateKeyHexFromMnemonic(mnemonic, index = 0, isChange = false) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const node = bip32.fromSeed(yield bip39.mnemonicToSeed(mnemonic));
|
|
61
|
+
return node
|
|
62
|
+
.derive(isChange ? 1 : 0)
|
|
63
|
+
.derive(index)
|
|
64
|
+
.privateKey.toString("hex");
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.getPrivateKeyHexFromMnemonic = getPrivateKeyHexFromMnemonic;
|
|
68
|
+
function reverseBytes(hexInput) {
|
|
69
|
+
return Buffer.from(hexInput, "hex").reverse().toString("hex");
|
|
70
|
+
}
|
|
71
|
+
exports.reverseBytes = reverseBytes;
|
|
72
|
+
function getPublicKeyHexByXpubAndIndex(xpub, index = 0, isChange = false, network = networks_1.default.bitcoin) {
|
|
73
|
+
const node = bip32.fromBase58(xpub, network);
|
|
74
|
+
return node
|
|
75
|
+
.derive(isChange ? 1 : 0)
|
|
76
|
+
.derive(index)
|
|
77
|
+
.publicKey.toString("hex");
|
|
78
|
+
}
|
|
79
|
+
exports.getPublicKeyHexByXpubAndIndex = getPublicKeyHexByXpubAndIndex;
|
|
80
|
+
function getPubKeyFromPrivateKeyWIF(privateKeyWIF, network = bitcoin.networks.bitcoin) {
|
|
81
|
+
let key = ECPair.fromWIF(privateKeyWIF, network);
|
|
82
|
+
return key.publicKey;
|
|
83
|
+
}
|
|
84
|
+
exports.getPubKeyFromPrivateKeyWIF = getPubKeyFromPrivateKeyWIF;
|
|
85
|
+
function getPubKeyFromPrivateKeyHex(privateKeyHex, network = bitcoin.networks.bitcoin) {
|
|
86
|
+
let key = ECPair.fromPrivateKey(Buffer.from(privateKeyHex, "hex"), { network });
|
|
87
|
+
return key.publicKey;
|
|
88
|
+
}
|
|
89
|
+
exports.getPubKeyFromPrivateKeyHex = getPubKeyFromPrivateKeyHex;
|
|
90
|
+
function getPrivateKeyHexFromWIF(privateKeyWIF, network = bitcoin.networks.bitcoin) {
|
|
91
|
+
let key = ECPair.fromWIF(privateKeyWIF, network);
|
|
92
|
+
return key.privateKey.toString("hex");
|
|
93
|
+
}
|
|
94
|
+
exports.getPrivateKeyHexFromWIF = getPrivateKeyHexFromWIF;
|
|
95
|
+
function parseRawTransaction(rawTransaction) {
|
|
96
|
+
const size = {
|
|
97
|
+
version: 4,
|
|
98
|
+
flag: 2,
|
|
99
|
+
tx: 32,
|
|
100
|
+
index: 4,
|
|
101
|
+
sequence: 4,
|
|
102
|
+
amount: 8,
|
|
103
|
+
};
|
|
104
|
+
let offset = 0;
|
|
105
|
+
let version = rawTransaction.slice(offset, size.version * 2);
|
|
106
|
+
offset += size.version * 2;
|
|
107
|
+
let flag = rawTransaction.slice(offset, offset + size.flag * 2);
|
|
108
|
+
offset = flag === "0001" ? offset + size.flag * 2 : offset;
|
|
109
|
+
let inputsStartIndex = offset;
|
|
110
|
+
let numberOfInputs = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
111
|
+
let noiSize = varUnit.encodingLength(numberOfInputs);
|
|
112
|
+
offset += noiSize * 2;
|
|
113
|
+
for (let i = 0; i < numberOfInputs; i += 1) {
|
|
114
|
+
offset += size.tx * 2;
|
|
115
|
+
offset += size.index * 2;
|
|
116
|
+
let sigLength = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
117
|
+
let sigLengthSize = varUnit.encodingLength(sigLength);
|
|
118
|
+
offset += sigLengthSize * 2;
|
|
119
|
+
offset += sigLength * 2;
|
|
120
|
+
offset += size.sequence * 2;
|
|
121
|
+
}
|
|
122
|
+
let inputLastIndex = offset;
|
|
123
|
+
let outputStartIndex = offset;
|
|
124
|
+
let numberOfOutputs = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
125
|
+
let nooSize = varUnit.encodingLength(numberOfOutputs);
|
|
126
|
+
offset += nooSize * 2;
|
|
127
|
+
for (let i = 0; i < numberOfOutputs; i += 1) {
|
|
128
|
+
offset += size.amount * 2;
|
|
129
|
+
let unlockSigLength = varUnit.decode(Buffer.from(rawTransaction.slice(offset), "hex"));
|
|
130
|
+
let unlockSigLengthSize = varUnit.encodingLength(unlockSigLength);
|
|
131
|
+
offset += unlockSigLengthSize * 2;
|
|
132
|
+
offset += unlockSigLength * 2;
|
|
133
|
+
}
|
|
134
|
+
let outputLastIndex = offset;
|
|
135
|
+
version = `0x${version}`;
|
|
136
|
+
flag = `0x${flag}`;
|
|
137
|
+
const vin = `0x${rawTransaction.slice(inputsStartIndex, inputLastIndex)}`;
|
|
138
|
+
const vout = `0x${rawTransaction.slice(outputStartIndex, outputLastIndex)}`;
|
|
139
|
+
let witness = `0x${rawTransaction.slice(outputLastIndex, rawTransaction.length - 8)}`;
|
|
140
|
+
let locktime = `0x${rawTransaction.slice(rawTransaction.length - 8, rawTransaction.length)}`;
|
|
141
|
+
return { version, flag, vin, vout, witness, locktime };
|
|
142
|
+
}
|
|
143
|
+
exports.parseRawTransaction = parseRawTransaction;
|
|
144
|
+
function calculateMerkleProof(blockTransactions, txId, blockMerkleRoot) {
|
|
145
|
+
let transactionIndex = blockTransactions.findIndex((tx) => tx === txId);
|
|
146
|
+
if (transactionIndex < 0)
|
|
147
|
+
throw new Error("txId is not in this tree");
|
|
148
|
+
let data = blockTransactions.map((a) => Buffer.from(a, "hex").reverse());
|
|
149
|
+
if (blockMerkleRoot &&
|
|
150
|
+
blockMerkleRoot !== fastRoot(data, bitcoin.crypto.hash256).toString("hex")) {
|
|
151
|
+
throw new Error("calculated merkleRoot and block merkleRoot not matched");
|
|
152
|
+
}
|
|
153
|
+
let tree = merkle(data, bitcoin.crypto.hash256);
|
|
154
|
+
let proof = merkleProof(tree, data[transactionIndex]);
|
|
155
|
+
let intermediateNodesArray = proof
|
|
156
|
+
.map((_id) => _id && _id.toString("hex"))
|
|
157
|
+
.filter((_id) => _id != null);
|
|
158
|
+
let intermediateNodes = intermediateNodesArray.reduce((a, value, index) => index !== transactionIndex % 2 && index < intermediateNodesArray.length - 1 ? a + value : a, "0x");
|
|
159
|
+
return {
|
|
160
|
+
intermediateNodes,
|
|
161
|
+
transactionIndex,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
exports.calculateMerkleProof = calculateMerkleProof;
|
|
165
|
+
function parseBlockHeader(headerHex) {
|
|
166
|
+
const size = {
|
|
167
|
+
version: 4,
|
|
168
|
+
previousBlockHash: 32,
|
|
169
|
+
merkleRoot: 32,
|
|
170
|
+
timestamp: 4,
|
|
171
|
+
difficulty: 4,
|
|
172
|
+
nonce: 4,
|
|
173
|
+
};
|
|
174
|
+
let offset = 0;
|
|
175
|
+
let result = {};
|
|
176
|
+
for (let key in size) {
|
|
177
|
+
result[key] = headerHex.slice(offset, offset + size[key] * 2);
|
|
178
|
+
offset += size[key] * 2;
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
exports.parseBlockHeader = parseBlockHeader;
|
|
183
|
+
function getAddressType(address, network = bitcoin.networks.bitcoin) {
|
|
184
|
+
if (address.startsWith(`${network.bech32}1p`)) {
|
|
185
|
+
return "p2tr";
|
|
186
|
+
}
|
|
187
|
+
if (address.startsWith(network.bech32)) {
|
|
188
|
+
bitcoin.address.fromBech32(address);
|
|
189
|
+
return "p2wpkh";
|
|
190
|
+
}
|
|
191
|
+
let base58Data = bitcoin.address.fromBase58Check(address);
|
|
192
|
+
if (base58Data.version === Number(network.scriptHash)) {
|
|
193
|
+
return "p2sh";
|
|
194
|
+
}
|
|
195
|
+
if (base58Data.version === Number(network.pubKeyHash)) {
|
|
196
|
+
return "p2pkh";
|
|
197
|
+
}
|
|
198
|
+
throw new Error("invalid address");
|
|
199
|
+
}
|
|
200
|
+
exports.getAddressType = getAddressType;
|
|
201
|
+
function createAddressObjectByAddress(address, network = bitcoin.networks.bitcoin) {
|
|
202
|
+
let addressType = getAddressType(address, network);
|
|
203
|
+
let addressObject;
|
|
204
|
+
switch (addressType) {
|
|
205
|
+
case "p2pkh":
|
|
206
|
+
addressObject = bitcoin.payments.p2pkh({
|
|
207
|
+
address,
|
|
208
|
+
network,
|
|
209
|
+
});
|
|
210
|
+
break;
|
|
211
|
+
case "p2wpkh":
|
|
212
|
+
addressObject = bitcoin.payments.p2wpkh({
|
|
213
|
+
address,
|
|
214
|
+
network,
|
|
215
|
+
});
|
|
216
|
+
break;
|
|
217
|
+
case "p2sh":
|
|
218
|
+
addressObject = bitcoin.payments.p2sh({
|
|
219
|
+
address,
|
|
220
|
+
network,
|
|
221
|
+
});
|
|
222
|
+
break;
|
|
223
|
+
case "p2tr":
|
|
224
|
+
addressObject = bitcoin.payments.p2tr({
|
|
225
|
+
address,
|
|
226
|
+
network,
|
|
227
|
+
});
|
|
228
|
+
break;
|
|
229
|
+
default:
|
|
230
|
+
throw new Error("address type is incorrect");
|
|
231
|
+
}
|
|
232
|
+
return { addressObject, addressType };
|
|
233
|
+
}
|
|
234
|
+
exports.createAddressObjectByAddress = createAddressObjectByAddress;
|
|
235
|
+
function convertBitcoinScriptToAddress(script, network = bitcoin.networks.bitcoin) {
|
|
236
|
+
try {
|
|
237
|
+
return bitcoin.address.fromOutputScript(script, network);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
exports.convertBitcoinScriptToAddress = convertBitcoinScriptToAddress;
|
|
244
|
+
function parseRawBlock(rawBlockHex, blockNumber, network = bitcoin.networks.bitcoin) {
|
|
245
|
+
var _a;
|
|
246
|
+
let block = bitcoin.Block.fromBuffer(Buffer.from(rawBlockHex, "hex"));
|
|
247
|
+
let blockHash = block.getHash().toString("hex");
|
|
248
|
+
let merkleRoot = block.merkleRoot.toString("hex");
|
|
249
|
+
let prvBlockHash = block.prevHash.toString("hex");
|
|
250
|
+
return {
|
|
251
|
+
blockNumber,
|
|
252
|
+
merkleRoot,
|
|
253
|
+
prvBlockHash,
|
|
254
|
+
transactions: (_a = block.transactions) === null || _a === void 0 ? void 0 : _a.map((tx) => ({
|
|
255
|
+
txId: tx.getId(),
|
|
256
|
+
version: tx.version,
|
|
257
|
+
locktime: tx.locktime,
|
|
258
|
+
blockNumber,
|
|
259
|
+
blockHash,
|
|
260
|
+
vout: tx.outs.map((vo, index) => ({
|
|
261
|
+
address: convertBitcoinScriptToAddress(vo.script, network),
|
|
262
|
+
script: vo.script.toString("hex"),
|
|
263
|
+
value: vo.value,
|
|
264
|
+
index,
|
|
265
|
+
})),
|
|
266
|
+
vin: tx.ins.map((vi) => ({
|
|
267
|
+
txId: Buffer.from(vi.hash).reverse().toString("hex"),
|
|
268
|
+
index: vi.index,
|
|
269
|
+
})),
|
|
270
|
+
})),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
exports.parseRawBlock = parseRawBlock;
|
|
274
|
+
function extractTransactionsAndBlockInfoFromRawBlock(rawBlockHex, blockNumber, addresses = [], inputTxIds = [], network = bitcoin.networks.bitcoin) {
|
|
275
|
+
var _a;
|
|
276
|
+
let block = bitcoin.Block.fromBuffer(Buffer.from(rawBlockHex, "hex"));
|
|
277
|
+
let blockHash = block.getHash().reverse().toString("hex");
|
|
278
|
+
let merkleRoot = block.merkleRoot.toString("hex");
|
|
279
|
+
let prvBlockHash = block.prevHash.toString("hex");
|
|
280
|
+
let blockInfo = {
|
|
281
|
+
blockNumber,
|
|
282
|
+
blockHash,
|
|
283
|
+
merkleRoot,
|
|
284
|
+
prvBlockHash,
|
|
285
|
+
};
|
|
286
|
+
let addressScript = addresses.map((address) => createAddressObjectByAddress(address, network).addressObject.output.toString("hex"));
|
|
287
|
+
let blockTxIds = block.transactions.map((tx) => tx.getId());
|
|
288
|
+
let withdrawTxs = [];
|
|
289
|
+
let depositTxs = [];
|
|
290
|
+
(_a = block.transactions) === null || _a === void 0 ? void 0 : _a.forEach((tx) => {
|
|
291
|
+
let txId = tx.getId();
|
|
292
|
+
let inputTxAddress;
|
|
293
|
+
let isWithdraw = tx.ins.find((vi) => {
|
|
294
|
+
let viTxId = Buffer.from(vi.hash).reverse().toString("hex");
|
|
295
|
+
let viInput = inputTxIds.find((vin) => vin.txId === viTxId && vin.index === vi.index);
|
|
296
|
+
inputTxAddress = viInput === null || viInput === void 0 ? void 0 : viInput.address;
|
|
297
|
+
return !!inputTxAddress;
|
|
298
|
+
});
|
|
299
|
+
if (isWithdraw && inputTxAddress) {
|
|
300
|
+
let txMerkleProof = calculateMerkleProof(blockTxIds, txId, merkleRoot);
|
|
301
|
+
withdrawTxs.push({
|
|
302
|
+
txId: tx.getId(),
|
|
303
|
+
version: tx.version,
|
|
304
|
+
locktime: tx.locktime,
|
|
305
|
+
blockNumber,
|
|
306
|
+
blockHash,
|
|
307
|
+
merkleProof: txMerkleProof,
|
|
308
|
+
vout: tx.outs.map((vo) => ({
|
|
309
|
+
address: convertBitcoinScriptToAddress(vo.script, network),
|
|
310
|
+
script: vo.script.toString("hex"),
|
|
311
|
+
value: vo.value,
|
|
312
|
+
})),
|
|
313
|
+
vin: tx.ins.map((vi) => {
|
|
314
|
+
let viTxId = Buffer.from(vi.hash).reverse().toString("hex");
|
|
315
|
+
let viInput = inputTxIds.find((vin) => vin.txId === viTxId && vin.index === vi.index);
|
|
316
|
+
return {
|
|
317
|
+
txId: Buffer.from(vi.hash).reverse().toString("hex"),
|
|
318
|
+
index: vi.index,
|
|
319
|
+
address: viInput === null || viInput === void 0 ? void 0 : viInput.address,
|
|
320
|
+
script: viInput === null || viInput === void 0 ? void 0 : viInput.script,
|
|
321
|
+
value: viInput === null || viInput === void 0 ? void 0 : viInput.value,
|
|
322
|
+
};
|
|
323
|
+
}),
|
|
324
|
+
address: inputTxAddress,
|
|
325
|
+
addressScript: createAddressObjectByAddress(inputTxAddress, network).addressObject.output.toString("hex"),
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
let addressIndex = -1;
|
|
329
|
+
let isDeposit = tx.outs.find((blockTxVo) => {
|
|
330
|
+
let sIndex = addressScript.findIndex((addScript) => addScript === blockTxVo.script.toString("hex"));
|
|
331
|
+
if (sIndex >= 0) {
|
|
332
|
+
addressIndex = sIndex;
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
return false;
|
|
336
|
+
});
|
|
337
|
+
if (isDeposit && addressIndex >= 0) {
|
|
338
|
+
let txMerkleProof = calculateMerkleProof(blockTxIds, txId, merkleRoot);
|
|
339
|
+
depositTxs.push({
|
|
340
|
+
txId: tx.getId(),
|
|
341
|
+
version: tx.version,
|
|
342
|
+
locktime: tx.locktime,
|
|
343
|
+
blockNumber,
|
|
344
|
+
blockHash,
|
|
345
|
+
merkleProof: txMerkleProof,
|
|
346
|
+
vout: tx.outs.map((vo) => ({
|
|
347
|
+
address: convertBitcoinScriptToAddress(vo.script, network),
|
|
348
|
+
script: vo.script.toString("hex"),
|
|
349
|
+
value: vo.value,
|
|
350
|
+
})),
|
|
351
|
+
vin: tx.ins.map((vi) => ({
|
|
352
|
+
txId: Buffer.from(vi.hash).reverse().toString("hex"),
|
|
353
|
+
index: vi.index,
|
|
354
|
+
})),
|
|
355
|
+
addressScript: addressScript[addressIndex],
|
|
356
|
+
address: addresses[addressIndex],
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
return {
|
|
361
|
+
blockInfo,
|
|
362
|
+
withdrawTxs,
|
|
363
|
+
depositTxs,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
exports.extractTransactionsAndBlockInfoFromRawBlock = extractTransactionsAndBlockInfoFromRawBlock;
|
|
367
|
+
function validateAddress(address, network = bitcoin.networks.bitcoin) {
|
|
368
|
+
try {
|
|
369
|
+
let isValid = false;
|
|
370
|
+
let isAddressSegwit = address.startsWith(network.bech32);
|
|
371
|
+
if (isAddressSegwit) {
|
|
372
|
+
bitcoin.address.fromBech32(address);
|
|
373
|
+
isValid = true;
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
let base58Data = bitcoin.address.fromBase58Check(address);
|
|
377
|
+
isValid =
|
|
378
|
+
base58Data.version === Number(network.scriptHash) ||
|
|
379
|
+
base58Data.version === Number(network.pubKeyHash);
|
|
380
|
+
}
|
|
381
|
+
return isValid;
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
exports.validateAddress = validateAddress;
|
|
388
|
+
function toXOnlyPublicKey(pubKey) {
|
|
389
|
+
return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
|
|
390
|
+
}
|
|
391
|
+
function createAddressObjectByScript({ addressType, script }, network = bitcoin.networks.bitcoin) {
|
|
392
|
+
let addressObject;
|
|
393
|
+
switch (addressType) {
|
|
394
|
+
case "p2pkh":
|
|
395
|
+
addressObject = bitcoin.payments.p2pkh({
|
|
396
|
+
output: script,
|
|
397
|
+
network,
|
|
398
|
+
});
|
|
399
|
+
break;
|
|
400
|
+
case "p2wpkh":
|
|
401
|
+
addressObject = bitcoin.payments.p2wpkh({
|
|
402
|
+
output: script,
|
|
403
|
+
network,
|
|
404
|
+
});
|
|
405
|
+
break;
|
|
406
|
+
case "p2sh":
|
|
407
|
+
addressObject = bitcoin.payments.p2sh({
|
|
408
|
+
output: script,
|
|
409
|
+
network,
|
|
410
|
+
});
|
|
411
|
+
break;
|
|
412
|
+
case "p2wsh":
|
|
413
|
+
addressObject = bitcoin.payments.p2wsh({
|
|
414
|
+
output: script,
|
|
415
|
+
network,
|
|
416
|
+
});
|
|
417
|
+
break;
|
|
418
|
+
case "p2tr":
|
|
419
|
+
addressObject = bitcoin.payments.p2tr({
|
|
420
|
+
output: script,
|
|
421
|
+
network,
|
|
422
|
+
});
|
|
423
|
+
break;
|
|
424
|
+
default:
|
|
425
|
+
throw new Error("address type is incorrect");
|
|
426
|
+
}
|
|
427
|
+
return addressObject;
|
|
428
|
+
}
|
|
429
|
+
exports.createAddressObjectByScript = createAddressObjectByScript;
|
|
430
|
+
function createAddressObjectByHash({ addressType, hash }, network = bitcoin.networks.bitcoin) {
|
|
431
|
+
let addressObject;
|
|
432
|
+
switch (addressType) {
|
|
433
|
+
case "p2pkh":
|
|
434
|
+
addressObject = bitcoin.payments.p2pkh({
|
|
435
|
+
hash,
|
|
436
|
+
network,
|
|
437
|
+
});
|
|
438
|
+
break;
|
|
439
|
+
case "p2wpkh":
|
|
440
|
+
addressObject = bitcoin.payments.p2wpkh({
|
|
441
|
+
hash,
|
|
442
|
+
network,
|
|
443
|
+
});
|
|
444
|
+
break;
|
|
445
|
+
case "p2sh":
|
|
446
|
+
addressObject = bitcoin.payments.p2sh({
|
|
447
|
+
hash,
|
|
448
|
+
network,
|
|
449
|
+
});
|
|
450
|
+
break;
|
|
451
|
+
case "p2wsh":
|
|
452
|
+
addressObject = bitcoin.payments.p2wsh({
|
|
453
|
+
hash,
|
|
454
|
+
network,
|
|
455
|
+
});
|
|
456
|
+
break;
|
|
457
|
+
case "p2tr":
|
|
458
|
+
addressObject = bitcoin.payments.p2tr({
|
|
459
|
+
pubkey: hash,
|
|
460
|
+
network,
|
|
461
|
+
});
|
|
462
|
+
break;
|
|
463
|
+
default:
|
|
464
|
+
throw new Error("address type is incorrect");
|
|
465
|
+
}
|
|
466
|
+
return addressObject;
|
|
467
|
+
}
|
|
468
|
+
exports.createAddressObjectByHash = createAddressObjectByHash;
|
|
469
|
+
function createAddressObjectByPublicKey({ addressType, publicKey }, network = bitcoin.networks.bitcoin) {
|
|
470
|
+
let addressObject;
|
|
471
|
+
switch (addressType) {
|
|
472
|
+
case "p2pkh":
|
|
473
|
+
addressObject = bitcoin.payments.p2pkh({
|
|
474
|
+
pubkey: publicKey,
|
|
475
|
+
network,
|
|
476
|
+
});
|
|
477
|
+
break;
|
|
478
|
+
case "p2wpkh":
|
|
479
|
+
addressObject = bitcoin.payments.p2wpkh({
|
|
480
|
+
pubkey: publicKey,
|
|
481
|
+
network,
|
|
482
|
+
});
|
|
483
|
+
break;
|
|
484
|
+
case "p2sh-p2wpkh":
|
|
485
|
+
addressObject = bitcoin.payments.p2sh({
|
|
486
|
+
redeem: bitcoin.payments.p2wpkh({
|
|
487
|
+
pubkey: publicKey,
|
|
488
|
+
network,
|
|
489
|
+
}),
|
|
490
|
+
});
|
|
491
|
+
break;
|
|
492
|
+
case "p2tr":
|
|
493
|
+
addressObject = bitcoin.payments.p2tr({
|
|
494
|
+
internalPubkey: toXOnlyPublicKey(publicKey),
|
|
495
|
+
network,
|
|
496
|
+
});
|
|
497
|
+
break;
|
|
498
|
+
default:
|
|
499
|
+
throw new Error("address type is incorrect");
|
|
500
|
+
}
|
|
501
|
+
return addressObject;
|
|
502
|
+
}
|
|
503
|
+
exports.createAddressObjectByPublicKey = createAddressObjectByPublicKey;
|
|
504
|
+
function publicKeyConvertor(publicKeyHex, compressed = true) {
|
|
505
|
+
let pubkey = ECPair.fromPublicKey(Buffer.from(publicKeyHex, "hex"), {
|
|
506
|
+
compressed,
|
|
507
|
+
});
|
|
508
|
+
return pubkey.publicKey.toString("hex");
|
|
509
|
+
}
|
|
510
|
+
exports.publicKeyConvertor = publicKeyConvertor;
|
|
511
|
+
exports.addressTypeHelper = {
|
|
512
|
+
addressTypesNumber: { p2pk: 0, p2pkh: 1, p2sh: 2, p2wpkh: 3, p2wsh: 4, p2tr: 5 },
|
|
513
|
+
addressTypes: ["p2pk", "p2pkh", "p2sh", "p2wpkh", "p2wsh", "p2tr"],
|
|
514
|
+
};
|
|
515
515
|
//# sourceMappingURL=bitcoin-utils.js.map
|