@teleportdao/bitcoin 2.0.5 → 2.0.7
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 +101 -101
- package/dist/bitcoin-interface-teleswap.js +176 -176
- package/dist/bitcoin-interface-utils.d.ts +20 -20
- package/dist/bitcoin-interface-utils.js +45 -45
- package/dist/bitcoin-interface-wallet.d.ts +28 -28
- package/dist/bitcoin-interface-wallet.js +125 -125
- package/dist/bitcoin-interface.d.ts +66 -66
- package/dist/bitcoin-interface.js +119 -119
- 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.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 +495 -495
- package/dist/ordinal-wallet.js +386 -386
- 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.js +54 -54
- 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.js +447 -447
- package/dist/type.d.ts +61 -61
- 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 +251 -251
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +114 -114
- package/src/bitcoin-interface.ts +156 -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 +659 -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 +690 -690
- package/src/type.ts +74 -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
|
@@ -1,259 +1,259 @@
|
|
|
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.BitcoinBaseWallet = void 0;
|
|
39
|
-
const bip39 = __importStar(require("bip39"));
|
|
40
|
-
const configs_1 = require("@teleportdao/configs");
|
|
41
|
-
const bip32_1 = __importDefault(require("bip32"));
|
|
42
|
-
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
|
|
43
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
|
-
const transaction_builder_1 = require("./transaction-builder");
|
|
45
|
-
const sign_transaction_1 = __importDefault(require("./sign/sign-transaction"));
|
|
46
|
-
const bitcoin_utils_1 = require("./bitcoin-utils");
|
|
47
|
-
const networks_1 = __importDefault(require("./utils/networks"));
|
|
48
|
-
const tools_1 = require("./utils/tools");
|
|
49
|
-
const bip32 = (0, bip32_1.default)(secp256k1_1.default);
|
|
50
|
-
class BitcoinBaseWallet {
|
|
51
|
-
constructor(networkName, connectionInfo) {
|
|
52
|
-
this.network = networks_1.default[networkName];
|
|
53
|
-
this.hdWalletPath = configs_1.hdWalletPath.bitcoin;
|
|
54
|
-
this.transactionBuilder = new transaction_builder_1.BitcoinTransactionBuilder(networkName, this.network, connectionInfo);
|
|
55
|
-
this.btcInterface = this.transactionBuilder.btcInterface;
|
|
56
|
-
this.signer = new sign_transaction_1.default(this.network);
|
|
57
|
-
this.currentAccount = undefined;
|
|
58
|
-
this.currentAccountType = undefined;
|
|
59
|
-
this.privateKey = undefined;
|
|
60
|
-
this.publicKey = undefined;
|
|
61
|
-
}
|
|
62
|
-
static satoshiToBTC(satoshi) {
|
|
63
|
-
return new bignumber_js_1.default(satoshi).dividedBy(1e8).toString();
|
|
64
|
-
}
|
|
65
|
-
static btcToSatoshi(btc) {
|
|
66
|
-
return new bignumber_js_1.default(btc).multipliedBy(1e8).toFixed(0);
|
|
67
|
-
}
|
|
68
|
-
get signerInfo() {
|
|
69
|
-
return this.privateKey
|
|
70
|
-
? {
|
|
71
|
-
address: this.bitcoinAddress,
|
|
72
|
-
publicKey: this.publicKey.toString("hex"),
|
|
73
|
-
addressType: this.currentAccountType,
|
|
74
|
-
}
|
|
75
|
-
: undefined;
|
|
76
|
-
}
|
|
77
|
-
setAccountPrivateKey(privateKeyHex) {
|
|
78
|
-
this.privateKey = Buffer.from(privateKeyHex, "hex");
|
|
79
|
-
let publicKey = (0, bitcoin_utils_1.getPubKeyFromPrivateKeyHex)(privateKeyHex, this.network);
|
|
80
|
-
this.publicKey = publicKey;
|
|
81
|
-
this.setAccountType("p2wpkh");
|
|
82
|
-
}
|
|
83
|
-
setAccountType(accountType = "p2pkh") {
|
|
84
|
-
if (!this.publicKey) {
|
|
85
|
-
throw new Error("account not initialized");
|
|
86
|
-
}
|
|
87
|
-
let addressObj = this.transactionBuilder.createAddressObject({
|
|
88
|
-
addressType: accountType,
|
|
89
|
-
publicKey: this.publicKey,
|
|
90
|
-
});
|
|
91
|
-
this.currentAccount = addressObj.address;
|
|
92
|
-
this.currentAccountType = accountType;
|
|
93
|
-
this.addressObj = addressObj;
|
|
94
|
-
this.bitcoinAddress = addressObj.address;
|
|
95
|
-
return addressObj.address;
|
|
96
|
-
}
|
|
97
|
-
setAccountPrivateKeyByMnemonic({ mnemonic, mnemonicPassword = "", index = 0, walletNumber = 0, addressType = "p2sh-p2wpkh", }) {
|
|
98
|
-
if (!bip39.validateMnemonic(mnemonic))
|
|
99
|
-
throw new Error("invalid mnemonic");
|
|
100
|
-
const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword);
|
|
101
|
-
const node = bip32.fromSeed(seed);
|
|
102
|
-
let basePath = this.hdWalletPath[addressType];
|
|
103
|
-
if (!basePath) {
|
|
104
|
-
throw new Error("incorrect path or addressType");
|
|
105
|
-
}
|
|
106
|
-
const path = `${basePath}/${walletNumber}`;
|
|
107
|
-
const account = node.derivePath(path);
|
|
108
|
-
const userKeyPair = account.derive(index);
|
|
109
|
-
this.setAccountPrivateKey(userKeyPair.privateKey.toString("hex"));
|
|
110
|
-
return this.setAccountType(addressType);
|
|
111
|
-
}
|
|
112
|
-
checkBalanceIsSufficient({ targets, extendedUtxo, changeAddress, feeRate, }) {
|
|
113
|
-
try {
|
|
114
|
-
this.transactionBuilder.helperHandleInputsAndOutputs({
|
|
115
|
-
targets,
|
|
116
|
-
extendedUtxo,
|
|
117
|
-
changeObject: {
|
|
118
|
-
address: changeAddress,
|
|
119
|
-
},
|
|
120
|
-
feeRate,
|
|
121
|
-
});
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
catch (err) {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
getExtendedUtxo(input) {
|
|
129
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
return this.btcInterface.getExtendedUtxo(input);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
getFeeRate(feeRate = "normal") {
|
|
134
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
135
|
-
if (feeRate === 0)
|
|
136
|
-
throw new Error("feeRate should be greater than 0");
|
|
137
|
-
if (+feeRate > 0)
|
|
138
|
-
return +feeRate;
|
|
139
|
-
if (typeof feeRate === "string")
|
|
140
|
-
return this.btcInterface.getFeeRate(feeRate);
|
|
141
|
-
throw new Error("incorrect feeRate");
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
sendBTC(receiverAddress, amountInSatoshi, fee = "normal", staticExtendedUtxo) {
|
|
145
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
-
if (!this.signerInfo || !this.privateKey) {
|
|
147
|
-
throw new Error("account not initialized");
|
|
148
|
-
}
|
|
149
|
-
const unsignedTx = yield this.sendBTCUnsignedTx(receiverAddress, amountInSatoshi, this.signerInfo, fee, staticExtendedUtxo);
|
|
150
|
-
let signedPsbt = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
151
|
-
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
152
|
-
let txId = yield this.sendSignedTx(signedTx);
|
|
153
|
-
return txId;
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
sendBTCMultipleAddress(receivers, fee = "normal", staticExtendedUtxo) {
|
|
157
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
-
if (!this.signerInfo || !this.privateKey) {
|
|
159
|
-
throw new Error("account not initialized");
|
|
160
|
-
}
|
|
161
|
-
const unsignedTx = yield this.sendBTCToMultipleAddressUnsignedTx(receivers, this.signerInfo, fee, staticExtendedUtxo);
|
|
162
|
-
let signedPsbt = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
163
|
-
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
164
|
-
let txId = yield this.sendSignedTx(signedTx);
|
|
165
|
-
return txId;
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
sendBTCToMultipleAddressUnsignedTx(receivers, signerInfo, fee = "normal", staticExtendedUtxo, fullAmount = false) {
|
|
169
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
if (!fullAmount) {
|
|
171
|
-
receivers.forEach((r) => {
|
|
172
|
-
if ((0, bignumber_js_1.default)(r.value).isEqualTo(0))
|
|
173
|
-
throw new Error("incorrect amount");
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
else if (receivers.length > 1)
|
|
177
|
-
throw new Error("fullAmount only support one receiver");
|
|
178
|
-
let feeRate = yield this.getFeeRate(fee);
|
|
179
|
-
let extendedUtxo = staticExtendedUtxo || (yield this.getExtendedUtxo(signerInfo));
|
|
180
|
-
let unsignedTx = yield this.transactionBuilder.processUnsignedTransaction({
|
|
181
|
-
extendedUtxo,
|
|
182
|
-
targets: receivers,
|
|
183
|
-
changeAddress: signerInfo.address,
|
|
184
|
-
feeRate,
|
|
185
|
-
fullAmount,
|
|
186
|
-
});
|
|
187
|
-
return unsignedTx;
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
sendBTCUnsignedTx(receiver, amountInSatoshi, signerInfo, fee = "normal", staticExtendedUtxo) {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
return this.sendBTCToMultipleAddressUnsignedTx([{ address: receiver, value: amountInSatoshi === "all" ? 0 : amountInSatoshi }], signerInfo, fee, staticExtendedUtxo, amountInSatoshi === "all");
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
sendSignedTx(signedTx) {
|
|
196
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
-
let txId = yield this.btcInterface.sendRawTransaction(signedTx);
|
|
198
|
-
return txId;
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
sendSignedPsbt(signedPsbt) {
|
|
202
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
-
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
204
|
-
let txId = yield this.sendSignedTx(signedTx);
|
|
205
|
-
return txId;
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
sendSignedPsbtWithRetry(signedPsbt, { maxTries = 5, retrySleep = 5000 } = {}) {
|
|
209
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
210
|
-
return (0, tools_1.runWithRetries)(() => this.sendSignedPsbt(signedPsbt), {
|
|
211
|
-
retrySleep,
|
|
212
|
-
maxTries,
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
sendMultiSignedPsbt(signedPsbts = []) {
|
|
217
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
-
let signedTx = this.signer.finalizePsbts(signedPsbts);
|
|
219
|
-
let txId = yield this.btcInterface.sendRawTransaction(signedTx);
|
|
220
|
-
return txId;
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
increaseTransactionFeeUnsignedPsbt(txId, signerInfos, extraExtendedUtxo, changeAddress, staticFeeRate) {
|
|
224
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
225
|
-
let transaction = yield this.btcInterface.apiProvider.getTransaction(txId);
|
|
226
|
-
let extendedUtxo = transaction.vin.map((vi) => ({
|
|
227
|
-
signerInfo: signerInfos.find((s) => s.address === vi.address),
|
|
228
|
-
hash: vi.txId,
|
|
229
|
-
value: +vi.value,
|
|
230
|
-
index: vi.index,
|
|
231
|
-
}));
|
|
232
|
-
if (extendedUtxo.find((x) => { var _a; return !((_a = x.signerInfo) === null || _a === void 0 ? void 0 : _a.address); })) {
|
|
233
|
-
throw new Error("signerInfo not match");
|
|
234
|
-
}
|
|
235
|
-
let changeIndex = transaction.vout.findIndex((vo) => transaction.vin.find((vi) => vo.address === vi.address || vo.address === changeAddress));
|
|
236
|
-
const feeRate = staticFeeRate || (yield this.btcInterface.getFeeRate("fast"));
|
|
237
|
-
let targets = transaction.vout
|
|
238
|
-
.filter((_, index) => index !== changeIndex)
|
|
239
|
-
.map((vo) => vo.address
|
|
240
|
-
? {
|
|
241
|
-
address: vo.address,
|
|
242
|
-
value: vo.value,
|
|
243
|
-
}
|
|
244
|
-
: {
|
|
245
|
-
script: Buffer.from(vo.script, "hex"),
|
|
246
|
-
value: vo.value,
|
|
247
|
-
});
|
|
248
|
-
return this.transactionBuilder.processUnsignedTransaction({
|
|
249
|
-
extendedUtxo: [...extendedUtxo, ...extraExtendedUtxo],
|
|
250
|
-
targets,
|
|
251
|
-
feeRate,
|
|
252
|
-
changeAddress: changeIndex >= 0 ? transaction.vout[changeIndex].address : changeAddress,
|
|
253
|
-
selectType: "inOrder",
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
exports.BitcoinBaseWallet = BitcoinBaseWallet;
|
|
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.BitcoinBaseWallet = void 0;
|
|
39
|
+
const bip39 = __importStar(require("bip39"));
|
|
40
|
+
const configs_1 = require("@teleportdao/configs");
|
|
41
|
+
const bip32_1 = __importDefault(require("bip32"));
|
|
42
|
+
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
|
|
43
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
|
+
const transaction_builder_1 = require("./transaction-builder");
|
|
45
|
+
const sign_transaction_1 = __importDefault(require("./sign/sign-transaction"));
|
|
46
|
+
const bitcoin_utils_1 = require("./bitcoin-utils");
|
|
47
|
+
const networks_1 = __importDefault(require("./utils/networks"));
|
|
48
|
+
const tools_1 = require("./utils/tools");
|
|
49
|
+
const bip32 = (0, bip32_1.default)(secp256k1_1.default);
|
|
50
|
+
class BitcoinBaseWallet {
|
|
51
|
+
constructor(networkName, connectionInfo) {
|
|
52
|
+
this.network = networks_1.default[networkName];
|
|
53
|
+
this.hdWalletPath = configs_1.hdWalletPath.bitcoin;
|
|
54
|
+
this.transactionBuilder = new transaction_builder_1.BitcoinTransactionBuilder(networkName, this.network, connectionInfo);
|
|
55
|
+
this.btcInterface = this.transactionBuilder.btcInterface;
|
|
56
|
+
this.signer = new sign_transaction_1.default(this.network);
|
|
57
|
+
this.currentAccount = undefined;
|
|
58
|
+
this.currentAccountType = undefined;
|
|
59
|
+
this.privateKey = undefined;
|
|
60
|
+
this.publicKey = undefined;
|
|
61
|
+
}
|
|
62
|
+
static satoshiToBTC(satoshi) {
|
|
63
|
+
return new bignumber_js_1.default(satoshi).dividedBy(1e8).toString();
|
|
64
|
+
}
|
|
65
|
+
static btcToSatoshi(btc) {
|
|
66
|
+
return new bignumber_js_1.default(btc).multipliedBy(1e8).toFixed(0);
|
|
67
|
+
}
|
|
68
|
+
get signerInfo() {
|
|
69
|
+
return this.privateKey
|
|
70
|
+
? {
|
|
71
|
+
address: this.bitcoinAddress,
|
|
72
|
+
publicKey: this.publicKey.toString("hex"),
|
|
73
|
+
addressType: this.currentAccountType,
|
|
74
|
+
}
|
|
75
|
+
: undefined;
|
|
76
|
+
}
|
|
77
|
+
setAccountPrivateKey(privateKeyHex) {
|
|
78
|
+
this.privateKey = Buffer.from(privateKeyHex, "hex");
|
|
79
|
+
let publicKey = (0, bitcoin_utils_1.getPubKeyFromPrivateKeyHex)(privateKeyHex, this.network);
|
|
80
|
+
this.publicKey = publicKey;
|
|
81
|
+
this.setAccountType("p2wpkh");
|
|
82
|
+
}
|
|
83
|
+
setAccountType(accountType = "p2pkh") {
|
|
84
|
+
if (!this.publicKey) {
|
|
85
|
+
throw new Error("account not initialized");
|
|
86
|
+
}
|
|
87
|
+
let addressObj = this.transactionBuilder.createAddressObject({
|
|
88
|
+
addressType: accountType,
|
|
89
|
+
publicKey: this.publicKey,
|
|
90
|
+
});
|
|
91
|
+
this.currentAccount = addressObj.address;
|
|
92
|
+
this.currentAccountType = accountType;
|
|
93
|
+
this.addressObj = addressObj;
|
|
94
|
+
this.bitcoinAddress = addressObj.address;
|
|
95
|
+
return addressObj.address;
|
|
96
|
+
}
|
|
97
|
+
setAccountPrivateKeyByMnemonic({ mnemonic, mnemonicPassword = "", index = 0, walletNumber = 0, addressType = "p2sh-p2wpkh", }) {
|
|
98
|
+
if (!bip39.validateMnemonic(mnemonic))
|
|
99
|
+
throw new Error("invalid mnemonic");
|
|
100
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword);
|
|
101
|
+
const node = bip32.fromSeed(seed);
|
|
102
|
+
let basePath = this.hdWalletPath[addressType];
|
|
103
|
+
if (!basePath) {
|
|
104
|
+
throw new Error("incorrect path or addressType");
|
|
105
|
+
}
|
|
106
|
+
const path = `${basePath}/${walletNumber}`;
|
|
107
|
+
const account = node.derivePath(path);
|
|
108
|
+
const userKeyPair = account.derive(index);
|
|
109
|
+
this.setAccountPrivateKey(userKeyPair.privateKey.toString("hex"));
|
|
110
|
+
return this.setAccountType(addressType);
|
|
111
|
+
}
|
|
112
|
+
checkBalanceIsSufficient({ targets, extendedUtxo, changeAddress, feeRate, }) {
|
|
113
|
+
try {
|
|
114
|
+
this.transactionBuilder.helperHandleInputsAndOutputs({
|
|
115
|
+
targets,
|
|
116
|
+
extendedUtxo,
|
|
117
|
+
changeObject: {
|
|
118
|
+
address: changeAddress,
|
|
119
|
+
},
|
|
120
|
+
feeRate,
|
|
121
|
+
});
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
getExtendedUtxo(input) {
|
|
129
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
+
return this.btcInterface.getExtendedUtxo(input);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
getFeeRate(feeRate = "normal") {
|
|
134
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
if (feeRate === 0)
|
|
136
|
+
throw new Error("feeRate should be greater than 0");
|
|
137
|
+
if (+feeRate > 0)
|
|
138
|
+
return +feeRate;
|
|
139
|
+
if (typeof feeRate === "string")
|
|
140
|
+
return this.btcInterface.getFeeRate(feeRate);
|
|
141
|
+
throw new Error("incorrect feeRate");
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
sendBTC(receiverAddress, amountInSatoshi, fee = "normal", staticExtendedUtxo) {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
if (!this.signerInfo || !this.privateKey) {
|
|
147
|
+
throw new Error("account not initialized");
|
|
148
|
+
}
|
|
149
|
+
const unsignedTx = yield this.sendBTCUnsignedTx(receiverAddress, amountInSatoshi, this.signerInfo, fee, staticExtendedUtxo);
|
|
150
|
+
let signedPsbt = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
151
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
152
|
+
let txId = yield this.sendSignedTx(signedTx);
|
|
153
|
+
return txId;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
sendBTCMultipleAddress(receivers, fee = "normal", staticExtendedUtxo) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
if (!this.signerInfo || !this.privateKey) {
|
|
159
|
+
throw new Error("account not initialized");
|
|
160
|
+
}
|
|
161
|
+
const unsignedTx = yield this.sendBTCToMultipleAddressUnsignedTx(receivers, this.signerInfo, fee, staticExtendedUtxo);
|
|
162
|
+
let signedPsbt = yield this.signer.signPsbt(unsignedTx, this.privateKey);
|
|
163
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
164
|
+
let txId = yield this.sendSignedTx(signedTx);
|
|
165
|
+
return txId;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
sendBTCToMultipleAddressUnsignedTx(receivers, signerInfo, fee = "normal", staticExtendedUtxo, fullAmount = false) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
if (!fullAmount) {
|
|
171
|
+
receivers.forEach((r) => {
|
|
172
|
+
if ((0, bignumber_js_1.default)(r.value).isEqualTo(0))
|
|
173
|
+
throw new Error("incorrect amount");
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else if (receivers.length > 1)
|
|
177
|
+
throw new Error("fullAmount only support one receiver");
|
|
178
|
+
let feeRate = yield this.getFeeRate(fee);
|
|
179
|
+
let extendedUtxo = staticExtendedUtxo || (yield this.getExtendedUtxo(signerInfo));
|
|
180
|
+
let unsignedTx = yield this.transactionBuilder.processUnsignedTransaction({
|
|
181
|
+
extendedUtxo,
|
|
182
|
+
targets: receivers,
|
|
183
|
+
changeAddress: signerInfo.address,
|
|
184
|
+
feeRate,
|
|
185
|
+
fullAmount,
|
|
186
|
+
});
|
|
187
|
+
return unsignedTx;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
sendBTCUnsignedTx(receiver, amountInSatoshi, signerInfo, fee = "normal", staticExtendedUtxo) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
return this.sendBTCToMultipleAddressUnsignedTx([{ address: receiver, value: amountInSatoshi === "all" ? 0 : amountInSatoshi }], signerInfo, fee, staticExtendedUtxo, amountInSatoshi === "all");
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
sendSignedTx(signedTx) {
|
|
196
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
let txId = yield this.btcInterface.sendRawTransaction(signedTx);
|
|
198
|
+
return txId;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
sendSignedPsbt(signedPsbt) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt]);
|
|
204
|
+
let txId = yield this.sendSignedTx(signedTx);
|
|
205
|
+
return txId;
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
sendSignedPsbtWithRetry(signedPsbt, { maxTries = 5, retrySleep = 5000 } = {}) {
|
|
209
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
210
|
+
return (0, tools_1.runWithRetries)(() => this.sendSignedPsbt(signedPsbt), {
|
|
211
|
+
retrySleep,
|
|
212
|
+
maxTries,
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
sendMultiSignedPsbt(signedPsbts = []) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
let signedTx = this.signer.finalizePsbts(signedPsbts);
|
|
219
|
+
let txId = yield this.btcInterface.sendRawTransaction(signedTx);
|
|
220
|
+
return txId;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
increaseTransactionFeeUnsignedPsbt(txId, signerInfos, extraExtendedUtxo, changeAddress, staticFeeRate) {
|
|
224
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
225
|
+
let transaction = yield this.btcInterface.apiProvider.getTransaction(txId);
|
|
226
|
+
let extendedUtxo = transaction.vin.map((vi) => ({
|
|
227
|
+
signerInfo: signerInfos.find((s) => s.address === vi.address),
|
|
228
|
+
hash: vi.txId,
|
|
229
|
+
value: +vi.value,
|
|
230
|
+
index: vi.index,
|
|
231
|
+
}));
|
|
232
|
+
if (extendedUtxo.find((x) => { var _a; return !((_a = x.signerInfo) === null || _a === void 0 ? void 0 : _a.address); })) {
|
|
233
|
+
throw new Error("signerInfo not match");
|
|
234
|
+
}
|
|
235
|
+
let changeIndex = transaction.vout.findIndex((vo) => transaction.vin.find((vi) => vo.address === vi.address || vo.address === changeAddress));
|
|
236
|
+
const feeRate = staticFeeRate || (yield this.btcInterface.getFeeRate("fast"));
|
|
237
|
+
let targets = transaction.vout
|
|
238
|
+
.filter((_, index) => index !== changeIndex)
|
|
239
|
+
.map((vo) => vo.address
|
|
240
|
+
? {
|
|
241
|
+
address: vo.address,
|
|
242
|
+
value: vo.value,
|
|
243
|
+
}
|
|
244
|
+
: {
|
|
245
|
+
script: Buffer.from(vo.script, "hex"),
|
|
246
|
+
value: vo.value,
|
|
247
|
+
});
|
|
248
|
+
return this.transactionBuilder.processUnsignedTransaction({
|
|
249
|
+
extendedUtxo: [...extendedUtxo, ...extraExtendedUtxo],
|
|
250
|
+
targets,
|
|
251
|
+
feeRate,
|
|
252
|
+
changeAddress: changeIndex >= 0 ? transaction.vout[changeIndex].address : changeAddress,
|
|
253
|
+
selectType: "inOrder",
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
exports.BitcoinBaseWallet = BitcoinBaseWallet;
|
|
259
259
|
//# sourceMappingURL=bitcoin-wallet-base.js.map
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
export type WrapOpReturn = {
|
|
2
|
-
chainId: number;
|
|
3
|
-
appId: number;
|
|
4
|
-
brc20TokenId: number;
|
|
5
|
-
recipientAddress: string;
|
|
6
|
-
inputAmount: string;
|
|
7
|
-
outputAmount?: string;
|
|
8
|
-
outputToken?: string;
|
|
9
|
-
thirdPartyId?: number;
|
|
10
|
-
};
|
|
11
|
-
export type WrapOpReturnAndType = WrapOpReturn & {
|
|
12
|
-
requestType: "Wrap" | "WrapAndSwap";
|
|
13
|
-
};
|
|
14
|
-
export declare function generateBrc2OpReturn({ chainId, appId, brc20TokenId, inputAmount, recipientAddress, thirdPartyId, isExchange, outputToken, outputAmount, }: WrapOpReturn & {
|
|
15
|
-
isExchange?: boolean;
|
|
16
|
-
}): string;
|
|
17
|
-
export declare function parseBrc20OpReturn(data: string): {
|
|
18
|
-
status: boolean;
|
|
19
|
-
data: WrapOpReturnAndType;
|
|
20
|
-
message?: undefined;
|
|
21
|
-
code?: undefined;
|
|
22
|
-
} | {
|
|
23
|
-
status: boolean;
|
|
24
|
-
message: string;
|
|
25
|
-
code: string;
|
|
26
|
-
data?: undefined;
|
|
27
|
-
};
|
|
28
|
-
export declare function checkAndParseBrc20Request(vouts: {
|
|
29
|
-
address?: string;
|
|
30
|
-
script: string;
|
|
31
|
-
value: number;
|
|
32
|
-
}[], address: string): {
|
|
33
|
-
status: boolean;
|
|
34
|
-
data?: WrapOpReturnAndType;
|
|
35
|
-
dataOutputIndex?: number;
|
|
36
|
-
lockerFeeValue?: number;
|
|
37
|
-
lockerFeeOutputIndex?: number;
|
|
38
|
-
brc20OutputIndex?: number;
|
|
39
|
-
brc20OutputValue?: number;
|
|
40
|
-
message?: string;
|
|
41
|
-
code?: string;
|
|
42
|
-
};
|
|
1
|
+
export type WrapOpReturn = {
|
|
2
|
+
chainId: number;
|
|
3
|
+
appId: number;
|
|
4
|
+
brc20TokenId: number;
|
|
5
|
+
recipientAddress: string;
|
|
6
|
+
inputAmount: string;
|
|
7
|
+
outputAmount?: string;
|
|
8
|
+
outputToken?: string;
|
|
9
|
+
thirdPartyId?: number;
|
|
10
|
+
};
|
|
11
|
+
export type WrapOpReturnAndType = WrapOpReturn & {
|
|
12
|
+
requestType: "Wrap" | "WrapAndSwap";
|
|
13
|
+
};
|
|
14
|
+
export declare function generateBrc2OpReturn({ chainId, appId, brc20TokenId, inputAmount, recipientAddress, thirdPartyId, isExchange, outputToken, outputAmount, }: WrapOpReturn & {
|
|
15
|
+
isExchange?: boolean;
|
|
16
|
+
}): string;
|
|
17
|
+
export declare function parseBrc20OpReturn(data: string): {
|
|
18
|
+
status: boolean;
|
|
19
|
+
data: WrapOpReturnAndType;
|
|
20
|
+
message?: undefined;
|
|
21
|
+
code?: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
status: boolean;
|
|
24
|
+
message: string;
|
|
25
|
+
code: string;
|
|
26
|
+
data?: undefined;
|
|
27
|
+
};
|
|
28
|
+
export declare function checkAndParseBrc20Request(vouts: {
|
|
29
|
+
address?: string;
|
|
30
|
+
script: string;
|
|
31
|
+
value: number;
|
|
32
|
+
}[], address: string): {
|
|
33
|
+
status: boolean;
|
|
34
|
+
data?: WrapOpReturnAndType;
|
|
35
|
+
dataOutputIndex?: number;
|
|
36
|
+
lockerFeeValue?: number;
|
|
37
|
+
lockerFeeOutputIndex?: number;
|
|
38
|
+
brc20OutputIndex?: number;
|
|
39
|
+
brc20OutputValue?: number;
|
|
40
|
+
message?: string;
|
|
41
|
+
code?: string;
|
|
42
|
+
};
|
|
43
43
|
//# sourceMappingURL=brc20-helper.d.ts.map
|