@yaswap/bitcoin 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/lib/chain/BitcoinBaseChainProvider.d.ts +11 -0
- package/dist/lib/chain/BitcoinBaseChainProvider.js +7 -0
- package/dist/lib/chain/BitcoinBaseChainProvider.js.map +1 -0
- package/dist/lib/chain/esplora/BitcoinEsploraApiProvider.d.ts +20 -0
- package/dist/lib/chain/esplora/BitcoinEsploraApiProvider.js +151 -0
- package/dist/lib/chain/esplora/BitcoinEsploraApiProvider.js.map +1 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBaseProvider.d.ts +19 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBaseProvider.js +96 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBaseProvider.js.map +1 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBatchBaseProvider.d.ts +12 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBatchBaseProvider.js +48 -0
- package/dist/lib/chain/esplora/BitcoinEsploraBatchBaseProvider.js.map +1 -0
- package/dist/lib/chain/esplora/types.d.ts +93 -0
- package/dist/lib/chain/esplora/types.js +3 -0
- package/dist/lib/chain/esplora/types.js.map +1 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcBaseProvider.d.ts +17 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcBaseProvider.js +83 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcBaseProvider.js.map +1 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcProvider.d.ts +20 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcProvider.js +181 -0
- package/dist/lib/chain/jsonRpc/BitcoinJsonRpcProvider.js.map +1 -0
- package/dist/lib/chain/jsonRpc/types.d.ts +85 -0
- package/dist/lib/chain/jsonRpc/types.js +3 -0
- package/dist/lib/chain/jsonRpc/types.js.map +1 -0
- package/dist/lib/fee/BitcoinFeeApiProvider.d.ts +7 -0
- package/dist/lib/fee/BitcoinFeeApiProvider.js +40 -0
- package/dist/lib/fee/BitcoinFeeApiProvider.js.map +1 -0
- package/dist/lib/index.d.ts +16 -0
- package/dist/lib/index.js +57 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/networks.d.ts +7 -0
- package/dist/lib/networks.js +14 -0
- package/dist/lib/networks.js.map +1 -0
- package/dist/lib/swap/BitcoinSwapBaseProvider.d.ts +50 -0
- package/dist/lib/swap/BitcoinSwapBaseProvider.js +365 -0
- package/dist/lib/swap/BitcoinSwapBaseProvider.js.map +1 -0
- package/dist/lib/swap/BitcoinSwapEsploraProvider.d.ts +11 -0
- package/dist/lib/swap/BitcoinSwapEsploraProvider.js +50 -0
- package/dist/lib/swap/BitcoinSwapEsploraProvider.js.map +1 -0
- package/dist/lib/swap/BitcoinSwapRpcProvider.d.ts +10 -0
- package/dist/lib/swap/BitcoinSwapRpcProvider.js +28 -0
- package/dist/lib/swap/BitcoinSwapRpcProvider.js.map +1 -0
- package/dist/lib/swap/types.d.ts +14 -0
- package/dist/lib/swap/types.js +4 -0
- package/dist/lib/swap/types.js.map +1 -0
- package/dist/lib/types.d.ts +89 -0
- package/dist/lib/types.js +45 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utils.d.ts +31 -0
- package/dist/lib/utils.js +214 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/lib/wallet/BitcoinBaseWallet.d.ts +67 -0
- package/dist/lib/wallet/BitcoinBaseWallet.js +408 -0
- package/dist/lib/wallet/BitcoinBaseWallet.js.map +1 -0
- package/dist/lib/wallet/BitcoinHDWallet.d.ts +42 -0
- package/dist/lib/wallet/BitcoinHDWallet.js +215 -0
- package/dist/lib/wallet/BitcoinHDWallet.js.map +1 -0
- package/dist/lib/wallet/BitcoinNodeWallet.d.ts +41 -0
- package/dist/lib/wallet/BitcoinNodeWallet.js +237 -0
- package/dist/lib/wallet/BitcoinNodeWallet.js.map +1 -0
- package/dist/lib/wallet/IBitcoinWallet.d.ts +10 -0
- package/dist/lib/wallet/IBitcoinWallet.js +3 -0
- package/dist/lib/wallet/IBitcoinWallet.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BitcoinSwapBaseProvider = void 0;
|
|
13
|
+
const client_1 = require("@yaswap/client");
|
|
14
|
+
const types_1 = require("@yaswap/types");
|
|
15
|
+
const utils_1 = require("@yaswap/utils");
|
|
16
|
+
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
17
|
+
const types_2 = require("../types");
|
|
18
|
+
const utils_2 = require("../utils");
|
|
19
|
+
class BitcoinSwapBaseProvider extends client_1.Swap {
|
|
20
|
+
constructor(options, walletProvider) {
|
|
21
|
+
super(walletProvider);
|
|
22
|
+
const { network, mode = types_2.SwapMode.P2WSH } = options;
|
|
23
|
+
const swapModes = Object.values(types_2.SwapMode);
|
|
24
|
+
if (!swapModes.includes(mode)) {
|
|
25
|
+
throw new Error(`Mode must be one of ${swapModes.join(',')}`);
|
|
26
|
+
}
|
|
27
|
+
this._network = network;
|
|
28
|
+
this._mode = mode;
|
|
29
|
+
}
|
|
30
|
+
validateSwapParams(swapParams) {
|
|
31
|
+
(0, utils_1.validateValue)(swapParams.value);
|
|
32
|
+
(0, utils_2.validateAddress)(swapParams.recipientAddress, this._network);
|
|
33
|
+
(0, utils_2.validateAddress)(swapParams.refundAddress, this._network);
|
|
34
|
+
(0, utils_1.validateSecretHash)(swapParams.secretHash);
|
|
35
|
+
(0, utils_1.validateExpiration)(swapParams.expiration);
|
|
36
|
+
}
|
|
37
|
+
initiateSwap(swapParams, feePerByte) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
this.validateSwapParams(swapParams);
|
|
40
|
+
const swapOutput = this.getSwapOutput(swapParams);
|
|
41
|
+
const address = this.getSwapPaymentVariants(swapOutput)[this._mode].address;
|
|
42
|
+
return this.walletProvider.sendTransaction({
|
|
43
|
+
to: address,
|
|
44
|
+
value: swapParams.value,
|
|
45
|
+
fee: feePerByte,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
fundSwap() {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
return null;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
findFundSwapTransaction() {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
return null;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
claimSwap(swapParams, initiationTxHash, secret, feePerByte) {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
this.validateSwapParams(swapParams);
|
|
62
|
+
(0, utils_1.validateSecret)(secret);
|
|
63
|
+
(0, utils_1.validateSecretAndHash)(secret, swapParams.secretHash);
|
|
64
|
+
yield this.verifyInitiateSwapTransaction(swapParams, initiationTxHash);
|
|
65
|
+
return this._redeemSwap(swapParams, initiationTxHash, true, secret, feePerByte);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
refundSwap(swapParams, initiationTxHash, feePerByte) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
this.validateSwapParams(swapParams);
|
|
71
|
+
yield this.verifyInitiateSwapTransaction(swapParams, initiationTxHash);
|
|
72
|
+
return this._redeemSwap(swapParams, initiationTxHash, false, undefined, feePerByte);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
findInitiateSwapTransaction(swapParams, blockNumber) {
|
|
76
|
+
this.validateSwapParams(swapParams);
|
|
77
|
+
return this.findSwapTransaction(swapParams, blockNumber, (tx) => this.doesTransactionMatchInitiation(swapParams, tx));
|
|
78
|
+
}
|
|
79
|
+
getSwapSecret(claimTxHash, initTxHash) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const claimSwapTransaction = yield this.walletProvider
|
|
82
|
+
.getChainProvider()
|
|
83
|
+
.getTransactionByHash(claimTxHash);
|
|
84
|
+
if (claimSwapTransaction) {
|
|
85
|
+
const swapInput = claimSwapTransaction._raw.vin.find((vin) => vin.txid === initTxHash);
|
|
86
|
+
if (!swapInput) {
|
|
87
|
+
throw new Error('Claim input missing');
|
|
88
|
+
}
|
|
89
|
+
const inputScript = this.getInputScript(swapInput);
|
|
90
|
+
const secret = inputScript[2];
|
|
91
|
+
return secret;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
findClaimSwapTransaction(swapParams, initTxHash, blockNumber) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
this.validateSwapParams(swapParams);
|
|
98
|
+
const claimSwapTransaction = yield this.findSwapTransaction(swapParams, blockNumber, (tx) => this.doesTransactionMatchRedeem(initTxHash, tx, false));
|
|
99
|
+
if (claimSwapTransaction) {
|
|
100
|
+
const swapInput = claimSwapTransaction._raw.vin.find((vin) => vin.txid === initTxHash);
|
|
101
|
+
if (!swapInput) {
|
|
102
|
+
throw new Error('Claim input missing');
|
|
103
|
+
}
|
|
104
|
+
const inputScript = this.getInputScript(swapInput);
|
|
105
|
+
const secret = inputScript[2];
|
|
106
|
+
(0, utils_1.validateSecretAndHash)(secret, swapParams.secretHash);
|
|
107
|
+
return Object.assign(Object.assign({}, claimSwapTransaction), { secret, _raw: claimSwapTransaction });
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
findRefundSwapTransaction(swapParams, initiationTxHash, blockNumber) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
this.validateSwapParams(swapParams);
|
|
114
|
+
const refundSwapTransaction = yield this.findSwapTransaction(swapParams, blockNumber, (tx) => this.doesTransactionMatchRedeem(initiationTxHash, tx, true));
|
|
115
|
+
return refundSwapTransaction;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
onWalletProviderUpdate(_wallet) {
|
|
119
|
+
// do nothing
|
|
120
|
+
}
|
|
121
|
+
getSwapOutput(swapParams) {
|
|
122
|
+
this.validateSwapParams(swapParams);
|
|
123
|
+
const secretHashBuff = Buffer.from(swapParams.secretHash, 'hex');
|
|
124
|
+
const recipientPubKeyHash = (0, utils_2.getPubKeyHash)(swapParams.recipientAddress.toString(), this._network);
|
|
125
|
+
const refundPubKeyHash = (0, utils_2.getPubKeyHash)(swapParams.refundAddress.toString(), this._network);
|
|
126
|
+
const OPS = bitcoinjs_lib_1.script.OPS;
|
|
127
|
+
const script = bitcoinjs_lib_1.script.compile([
|
|
128
|
+
OPS.OP_IF,
|
|
129
|
+
OPS.OP_SIZE,
|
|
130
|
+
bitcoinjs_lib_1.script.number.encode(32),
|
|
131
|
+
OPS.OP_EQUALVERIFY,
|
|
132
|
+
OPS.OP_SHA256,
|
|
133
|
+
secretHashBuff,
|
|
134
|
+
OPS.OP_EQUALVERIFY,
|
|
135
|
+
OPS.OP_DUP,
|
|
136
|
+
OPS.OP_HASH160,
|
|
137
|
+
recipientPubKeyHash,
|
|
138
|
+
OPS.OP_ELSE,
|
|
139
|
+
bitcoinjs_lib_1.script.number.encode(swapParams.expiration),
|
|
140
|
+
OPS.OP_CHECKLOCKTIMEVERIFY,
|
|
141
|
+
OPS.OP_DROP,
|
|
142
|
+
OPS.OP_DUP,
|
|
143
|
+
OPS.OP_HASH160,
|
|
144
|
+
refundPubKeyHash,
|
|
145
|
+
OPS.OP_ENDIF,
|
|
146
|
+
OPS.OP_EQUALVERIFY,
|
|
147
|
+
OPS.OP_CHECKSIG,
|
|
148
|
+
]);
|
|
149
|
+
if (![97, 98].includes(Buffer.byteLength(script))) {
|
|
150
|
+
throw new Error('Invalid swap script');
|
|
151
|
+
}
|
|
152
|
+
return script;
|
|
153
|
+
}
|
|
154
|
+
getSwapInput(sig, pubKey, isClaim, secret) {
|
|
155
|
+
const OPS = bitcoinjs_lib_1.script.OPS;
|
|
156
|
+
const redeem = isClaim ? OPS.OP_TRUE : OPS.OP_FALSE;
|
|
157
|
+
const secretParams = isClaim ? [Buffer.from(secret, 'hex')] : [];
|
|
158
|
+
return bitcoinjs_lib_1.script.compile([sig, pubKey, ...secretParams, redeem]);
|
|
159
|
+
}
|
|
160
|
+
getSwapPaymentVariants(swapOutput) {
|
|
161
|
+
const p2wsh = bitcoinjs_lib_1.payments.p2wsh({
|
|
162
|
+
redeem: { output: swapOutput, network: this._network },
|
|
163
|
+
network: this._network,
|
|
164
|
+
});
|
|
165
|
+
const p2shSegwit = bitcoinjs_lib_1.payments.p2sh({
|
|
166
|
+
redeem: p2wsh,
|
|
167
|
+
network: this._network,
|
|
168
|
+
});
|
|
169
|
+
const p2sh = bitcoinjs_lib_1.payments.p2sh({
|
|
170
|
+
redeem: { output: swapOutput, network: this._network },
|
|
171
|
+
network: this._network,
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
[types_2.SwapMode.P2WSH]: p2wsh,
|
|
175
|
+
[types_2.SwapMode.P2SH_SEGWIT]: p2shSegwit,
|
|
176
|
+
[types_2.SwapMode.P2SH]: p2sh,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
_redeemSwap(swapParams, initiationTxHash, isClaim, secret, feePerByte) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
const address = isClaim ? swapParams.recipientAddress : swapParams.refundAddress;
|
|
182
|
+
const swapOutput = this.getSwapOutput(swapParams);
|
|
183
|
+
return this._redeemSwapOutput(initiationTxHash, swapParams.value, address.toString(), swapOutput, swapParams.expiration, isClaim, secret, feePerByte);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
_redeemSwapOutput(initiationTxHash, value, address, swapOutput, expiration, isClaim, secret, _feePerByte) {
|
|
187
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
+
const network = this._network;
|
|
189
|
+
const swapPaymentVariants = this.getSwapPaymentVariants(swapOutput);
|
|
190
|
+
const initiationTxRaw = yield this.walletProvider.getChainProvider().getProvider().getRawTransactionByHash(initiationTxHash);
|
|
191
|
+
const initiationTx = (0, utils_2.decodeRawTransaction)(initiationTxRaw, this._network);
|
|
192
|
+
let swapVout;
|
|
193
|
+
let paymentVariantName;
|
|
194
|
+
let paymentVariant;
|
|
195
|
+
for (const vout of initiationTx.vout) {
|
|
196
|
+
const paymentVariantEntry = Object.entries(swapPaymentVariants).find(([, payment]) => payment.output.toString('hex') === vout.scriptPubKey.hex);
|
|
197
|
+
const voutValue = new types_1.BigNumber(vout.value).times(1e8);
|
|
198
|
+
if (paymentVariantEntry && voutValue.eq(new types_1.BigNumber(value))) {
|
|
199
|
+
paymentVariantName = paymentVariantEntry[0];
|
|
200
|
+
paymentVariant = paymentVariantEntry[1];
|
|
201
|
+
swapVout = vout;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (!swapVout) {
|
|
205
|
+
throw new Error('Valid swap output not found');
|
|
206
|
+
}
|
|
207
|
+
const feePerByte = _feePerByte || (yield this.walletProvider.getChainProvider().getProvider().getFeePerByte());
|
|
208
|
+
// TODO: Implement proper fee calculation that counts bytes in inputs and outputs
|
|
209
|
+
const txfee = (0, utils_2.calculateFee)(1, 1, feePerByte);
|
|
210
|
+
const swapValue = new types_1.BigNumber(swapVout.value).times(1e8).toNumber();
|
|
211
|
+
if (swapValue - txfee < 0) {
|
|
212
|
+
throw new Error('Transaction amount does not cover fee.');
|
|
213
|
+
}
|
|
214
|
+
const psbt = new bitcoinjs_lib_1.Psbt({ network });
|
|
215
|
+
if (!isClaim) {
|
|
216
|
+
psbt.setLocktime(expiration);
|
|
217
|
+
}
|
|
218
|
+
const isSegwit = paymentVariantName === types_2.SwapMode.P2WSH || paymentVariantName === types_2.SwapMode.P2SH_SEGWIT;
|
|
219
|
+
const input = {
|
|
220
|
+
hash: initiationTxHash,
|
|
221
|
+
index: swapVout.n,
|
|
222
|
+
sequence: 0,
|
|
223
|
+
};
|
|
224
|
+
if (isSegwit) {
|
|
225
|
+
input.witnessUtxo = {
|
|
226
|
+
script: paymentVariant.output,
|
|
227
|
+
value: swapValue,
|
|
228
|
+
};
|
|
229
|
+
input.witnessScript = swapPaymentVariants.p2wsh.redeem.output; // Strip the push bytes (0020) off the script
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
input.nonWitnessUtxo = Buffer.from(initiationTxRaw, 'hex');
|
|
233
|
+
input.redeemScript = paymentVariant.redeem.output;
|
|
234
|
+
}
|
|
235
|
+
const output = {
|
|
236
|
+
address: address,
|
|
237
|
+
value: swapValue - txfee,
|
|
238
|
+
};
|
|
239
|
+
psbt.addInput(input);
|
|
240
|
+
psbt.addOutput(output);
|
|
241
|
+
const walletAddress = yield this.walletProvider.getWalletAddress(address);
|
|
242
|
+
const signedPSBTHex = yield this.walletProvider.signPSBT(psbt.toBase64(), [
|
|
243
|
+
{ index: 0, derivationPath: walletAddress.derivationPath },
|
|
244
|
+
]);
|
|
245
|
+
const signedPSBT = bitcoinjs_lib_1.Psbt.fromBase64(signedPSBTHex, { network });
|
|
246
|
+
const sig = signedPSBT.data.inputs[0].partialSig[0].signature;
|
|
247
|
+
const swapInput = this.getSwapInput(sig, Buffer.from(walletAddress.publicKey, 'hex'), isClaim, secret);
|
|
248
|
+
const paymentParams = { redeem: { output: swapOutput, input: swapInput, network }, network };
|
|
249
|
+
const paymentWithInput = isSegwit ? bitcoinjs_lib_1.payments.p2wsh(paymentParams) : bitcoinjs_lib_1.payments.p2sh(paymentParams);
|
|
250
|
+
const getFinalScripts = () => {
|
|
251
|
+
let finalScriptSig;
|
|
252
|
+
let finalScriptWitness;
|
|
253
|
+
// create witness stack
|
|
254
|
+
if (isSegwit) {
|
|
255
|
+
finalScriptWitness = (0, utils_2.witnessStackToScriptWitness)(paymentWithInput.witness);
|
|
256
|
+
}
|
|
257
|
+
if (paymentVariantName === types_2.SwapMode.P2SH_SEGWIT) {
|
|
258
|
+
// Adds the necessary push OP (PUSH34 (00 + witness script hash))
|
|
259
|
+
const inputScript = bitcoinjs_lib_1.script.compile([swapPaymentVariants.p2shSegwit.redeem.output]);
|
|
260
|
+
finalScriptSig = inputScript;
|
|
261
|
+
}
|
|
262
|
+
else if (paymentVariantName === types_2.SwapMode.P2SH) {
|
|
263
|
+
finalScriptSig = paymentWithInput.input;
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
finalScriptSig,
|
|
267
|
+
finalScriptWitness,
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
psbt.finalizeInput(0, getFinalScripts);
|
|
271
|
+
const hex = psbt.extractTransaction().toHex();
|
|
272
|
+
yield this.walletProvider.getChainProvider().sendRawTransaction(hex);
|
|
273
|
+
return (0, utils_2.normalizeTransactionObject)((0, utils_2.decodeRawTransaction)(hex, this._network), txfee);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
extractSwapParams(outputScript) {
|
|
277
|
+
const buffer = bitcoinjs_lib_1.script.decompile(Buffer.from(outputScript, 'hex'));
|
|
278
|
+
if (buffer.length !== 20) {
|
|
279
|
+
throw new Error('Invalid swap output script');
|
|
280
|
+
}
|
|
281
|
+
const secretHash = buffer[5].reverse().toString('hex');
|
|
282
|
+
const recipientPublicKey = buffer[9].reverse().toString('hex');
|
|
283
|
+
const expiration = parseInt(buffer[11].reverse().toString('hex'), 16);
|
|
284
|
+
const refundPublicKey = buffer[16].reverse().toString('hex');
|
|
285
|
+
return { recipientPublicKey, refundPublicKey, secretHash, expiration };
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Only to be used for situations where transaction is trusted. e.g to bump fee
|
|
289
|
+
* DO NOT USE THIS TO VERIFY THE REDEEM
|
|
290
|
+
*/
|
|
291
|
+
UNSAFE_isSwapRedeemTransaction(transaction) {
|
|
292
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
293
|
+
// eslint-disable-line
|
|
294
|
+
if (transaction._raw.vin.length === 1 && transaction._raw.vout.length === 1) {
|
|
295
|
+
const swapInput = transaction._raw.vin[0];
|
|
296
|
+
const inputScript = this.getInputScript(swapInput);
|
|
297
|
+
const initiationTransaction = yield this.walletProvider
|
|
298
|
+
.getChainProvider()
|
|
299
|
+
.getTransactionByHash(transaction._raw.vin[0].txid);
|
|
300
|
+
const scriptType = initiationTransaction._raw.vout[transaction._raw.vin[0].vout].scriptPubKey.type;
|
|
301
|
+
if (['scripthash', 'witness_v0_scripthash'].includes(scriptType) && [4, 5].includes(inputScript.length))
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
canUpdateFee() {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
updateTransactionFee(tx, newFeePerByte) {
|
|
311
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
const txHash = typeof tx === 'string' ? tx : tx.hash;
|
|
313
|
+
const transaction = yield this.walletProvider.getChainProvider().getTransactionByHash(txHash);
|
|
314
|
+
if (yield this.UNSAFE_isSwapRedeemTransaction(transaction)) {
|
|
315
|
+
const swapInput = transaction._raw.vin[0];
|
|
316
|
+
const inputScript = this.getInputScript(swapInput);
|
|
317
|
+
const initiationTxHash = swapInput.txid;
|
|
318
|
+
const initiationTx = yield this.walletProvider
|
|
319
|
+
.getChainProvider()
|
|
320
|
+
.getTransactionByHash(initiationTxHash);
|
|
321
|
+
const swapOutput = initiationTx._raw.vout[swapInput.vout];
|
|
322
|
+
const value = new types_1.BigNumber(swapOutput.value).times(1e8);
|
|
323
|
+
const address = transaction._raw.vout[0].scriptPubKey.addresses[0];
|
|
324
|
+
const isClaim = inputScript.length === 5;
|
|
325
|
+
const secret = isClaim ? inputScript[2] : undefined;
|
|
326
|
+
const outputScript = isClaim ? inputScript[4] : inputScript[3];
|
|
327
|
+
const { expiration } = this.extractSwapParams(outputScript);
|
|
328
|
+
return this._redeemSwapOutput(initiationTxHash, value, address, Buffer.from(outputScript, 'hex'), expiration, isClaim, secret, newFeePerByte);
|
|
329
|
+
}
|
|
330
|
+
return this.walletProvider.updateTransactionFee(tx, newFeePerByte);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
getInputScript(vin) {
|
|
334
|
+
const inputScript = vin.txinwitness
|
|
335
|
+
? vin.txinwitness
|
|
336
|
+
: bitcoinjs_lib_1.script.decompile(Buffer.from(vin.scriptSig.hex, 'hex')).map((b) => (Buffer.isBuffer(b) ? b.toString('hex') : b));
|
|
337
|
+
return inputScript;
|
|
338
|
+
}
|
|
339
|
+
doesTransactionMatchRedeem(initiationTxHash, tx, isRefund) {
|
|
340
|
+
const swapInput = tx._raw.vin.find((vin) => vin.txid === initiationTxHash);
|
|
341
|
+
if (!swapInput)
|
|
342
|
+
return false;
|
|
343
|
+
const inputScript = this.getInputScript(swapInput);
|
|
344
|
+
if (!inputScript)
|
|
345
|
+
return false;
|
|
346
|
+
if (isRefund) {
|
|
347
|
+
if (inputScript.length !== 4)
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
if (inputScript.length !== 5)
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
doesTransactionMatchInitiation(swapParams, transaction) {
|
|
357
|
+
const swapOutput = this.getSwapOutput(swapParams);
|
|
358
|
+
const swapPaymentVariants = this.getSwapPaymentVariants(swapOutput);
|
|
359
|
+
const vout = transaction._raw.vout.find((vout) => Object.values(swapPaymentVariants).find((payment) => payment.output.toString('hex') === vout.scriptPubKey.hex &&
|
|
360
|
+
new types_1.BigNumber(vout.value).times(1e8).eq(new types_1.BigNumber(swapParams.value))));
|
|
361
|
+
return Boolean(vout);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
exports.BitcoinSwapBaseProvider = BitcoinSwapBaseProvider;
|
|
365
|
+
//# sourceMappingURL=BitcoinSwapBaseProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BitcoinSwapBaseProvider.js","sourceRoot":"","sources":["../../../lib/swap/BitcoinSwapBaseProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAsC;AACtC,yCAA4E;AAC5E,yCAA6H;AAC7H,iDAAkE;AAElE,oCAA8F;AAC9F,oCAOkB;AAIlB,MAAsB,uBAAwB,SAAQ,aAA8E;IAIhI,YAAY,OAAmC,EAAE,cAAyD;QACtG,KAAK,CAAC,cAAc,CAAC,CAAC;QACtB,MAAM,EAAE,OAAO,EAAE,IAAI,GAAG,gBAAQ,CAAC,KAAK,EAAE,GAAG,OAAO,CAAC;QACnD,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAQ,CAAC,CAAC;QAC1C,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,uBAAuB,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;SACjE;QACD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACtB,CAAC;IAEM,kBAAkB,CAAC,UAAsB;QAC5C,IAAA,qBAAa,EAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAChC,IAAA,uBAAe,EAAC,UAAU,CAA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IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,iBAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAC/E,CACJ,CAAC;QACF,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CAOJ;AAtaD,0DAsaC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SwapParams, Transaction } from '@yaswap/types';
|
|
2
|
+
import { Transaction as BitcoinTransaction } from '../types';
|
|
3
|
+
import { BitcoinBaseWalletProvider } from '../wallet/BitcoinBaseWallet';
|
|
4
|
+
import { BitcoinSwapBaseProvider } from './BitcoinSwapBaseProvider';
|
|
5
|
+
import { BitcoinSwapProviderOptions, TransactionMatchesFunction } from './types';
|
|
6
|
+
export declare class BitcoinSwapEsploraProvider extends BitcoinSwapBaseProvider {
|
|
7
|
+
private _httpClient;
|
|
8
|
+
constructor(options: BitcoinSwapProviderOptions, walletProvider?: BitcoinBaseWalletProvider);
|
|
9
|
+
findSwapTransaction(swapParams: SwapParams, _blockNumber: number, predicate: TransactionMatchesFunction): Promise<Transaction<BitcoinTransaction>>;
|
|
10
|
+
private findAddressTransaction;
|
|
11
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BitcoinSwapEsploraProvider = void 0;
|
|
13
|
+
const client_1 = require("@yaswap/client");
|
|
14
|
+
const BitcoinSwapBaseProvider_1 = require("./BitcoinSwapBaseProvider");
|
|
15
|
+
class BitcoinSwapEsploraProvider extends BitcoinSwapBaseProvider_1.BitcoinSwapBaseProvider {
|
|
16
|
+
constructor(options, walletProvider) {
|
|
17
|
+
super(options, walletProvider);
|
|
18
|
+
this._httpClient = new client_1.HttpClient({ baseURL: options.scraperUrl });
|
|
19
|
+
}
|
|
20
|
+
findSwapTransaction(swapParams, _blockNumber, predicate) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const currentHeight = yield this.walletProvider.getChainProvider().getBlockHeight();
|
|
23
|
+
const swapOutput = this.getSwapOutput(swapParams);
|
|
24
|
+
const paymentVariants = this.getSwapPaymentVariants(swapOutput);
|
|
25
|
+
for (const paymentVariant of Object.values(paymentVariants)) {
|
|
26
|
+
const addressTransaction = this.findAddressTransaction(paymentVariant.address, currentHeight, predicate);
|
|
27
|
+
if (addressTransaction)
|
|
28
|
+
return addressTransaction;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
findAddressTransaction(address, currentHeight, predicate) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
// TODO: This does not go through pages as swap addresses have at most 2 transactions
|
|
35
|
+
// Investigate whether retrieving more transactions is required.
|
|
36
|
+
const transactions = yield this._httpClient.nodeGet(`/address/${address}/txs`);
|
|
37
|
+
for (const transaction of transactions) {
|
|
38
|
+
const formattedTransaction = yield this.walletProvider
|
|
39
|
+
.getChainProvider()
|
|
40
|
+
.getProvider()
|
|
41
|
+
.formatTransaction(transaction, currentHeight);
|
|
42
|
+
if (predicate(formattedTransaction)) {
|
|
43
|
+
return formattedTransaction;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.BitcoinSwapEsploraProvider = BitcoinSwapEsploraProvider;
|
|
50
|
+
//# sourceMappingURL=BitcoinSwapEsploraProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BitcoinSwapEsploraProvider.js","sourceRoot":"","sources":["../../../lib/swap/BitcoinSwapEsploraProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAI5C,uEAAoE;AAGpE,MAAa,0BAA2B,SAAQ,iDAAuB;IAGnE,YAAY,OAAmC,EAAE,cAA0C;QACvF,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,IAAI,mBAAU,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;IACvE,CAAC;IAEY,mBAAmB,CAAC,UAAsB,EAAE,YAAoB,EAAE,SAAqC;;YAChH,MAAM,aAAa,GAAW,MAAM,IAAI,CAAC,cAAc,CAAC,gBAAgB,EAAE,CAAC,cAAc,EAAE,CAAC;YAC5F,MAAM,UAAU,GAAW,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;YAC1D,MAAM,eAAe,GAAoB,IAAI,CAAC,sBAAsB,CAAC,UAAU,CAAC,CAAC;YACjF,KAAK,MAAM,cAAc,IAAI,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE;gBACzD,MAAM,kBAAkB,GAAG,IAAI,CAAC,sBAAsB,CAAC,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;gBACzG,IAAI,kBAAkB;oBAAE,OAAO,kBAAkB,CAAC;aACrD;QACL,CAAC;KAAA;IAEa,sBAAsB,CAAC,OAAe,EAAE,aAAqB,EAAE,SAAqC;;YAC9G,qFAAqF;YACrF,gEAAgE;YAChE,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,YAAY,OAAO,MAAM,CAAC,CAAC;YAE/E,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;gBACpC,MAAM,oBAAoB,GAAoC,MAAM,IAAI,CAAC,cAAc;qBAClF,gBAAgB,EAAE;qBAClB,WAAW,EAAE;qBACb,iBAAiB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;gBACnD,IAAI,SAAS,CAAC,oBAAoB,CAAC,EAAE;oBACjC,OAAO,oBAAoB,CAAC;iBAC/B;aACJ;QACL,CAAC;KAAA;CACJ;AAjCD,gEAiCC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SwapParams, Transaction } from '@yaswap/types';
|
|
2
|
+
import { BitcoinBaseChainProvider } from '../chain/BitcoinBaseChainProvider';
|
|
3
|
+
import { Transaction as BitcoinTransaction } from '../types';
|
|
4
|
+
import { IBitcoinWallet } from '../wallet/IBitcoinWallet';
|
|
5
|
+
import { BitcoinSwapBaseProvider } from './BitcoinSwapBaseProvider';
|
|
6
|
+
import { BitcoinSwapProviderOptions } from './types';
|
|
7
|
+
export declare class BitcoinSwapRpcProvider extends BitcoinSwapBaseProvider {
|
|
8
|
+
constructor(options: BitcoinSwapProviderOptions, walletProvider?: IBitcoinWallet<BitcoinBaseChainProvider>);
|
|
9
|
+
findSwapTransaction(_swapParams: SwapParams, blockNumber: number, predicate: (tx: Transaction<BitcoinTransaction>) => boolean): Promise<Transaction<any>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BitcoinSwapRpcProvider = void 0;
|
|
13
|
+
const BitcoinSwapBaseProvider_1 = require("./BitcoinSwapBaseProvider");
|
|
14
|
+
class BitcoinSwapRpcProvider extends BitcoinSwapBaseProvider_1.BitcoinSwapBaseProvider {
|
|
15
|
+
constructor(options, walletProvider) {
|
|
16
|
+
super(options, walletProvider);
|
|
17
|
+
}
|
|
18
|
+
findSwapTransaction(_swapParams, blockNumber, predicate) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
// TODO: Are mempool TXs possible?
|
|
21
|
+
const block = yield this.walletProvider.getChainProvider().getBlockByNumber(blockNumber, true);
|
|
22
|
+
const swapTransaction = block.transactions.find(predicate);
|
|
23
|
+
return swapTransaction;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.BitcoinSwapRpcProvider = BitcoinSwapRpcProvider;
|
|
28
|
+
//# sourceMappingURL=BitcoinSwapRpcProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BitcoinSwapRpcProvider.js","sourceRoot":"","sources":["../../../lib/swap/BitcoinSwapRpcProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,uEAAoE;AAGpE,MAAa,sBAAuB,SAAQ,iDAAuB;IAC/D,YAAY,OAAmC,EAAE,cAAyD;QACtG,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IACnC,CAAC;IAEY,mBAAmB,CAC5B,WAAuB,EACvB,WAAmB,EACnB,SAA2D;;YAE3D,kCAAkC;YAClC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,gBAAgB,EAAE,CAAC,gBAAgB,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;YAC/F,MAAM,eAAe,GAAG,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAC3D,OAAO,eAAe,CAAC;QAC3B,CAAC;KAAA;CACJ;AAfD,wDAeC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Transaction } from '@yaswap/types';
|
|
2
|
+
import { payments } from 'bitcoinjs-lib';
|
|
3
|
+
import { BitcoinNetwork, SwapMode, Transaction as BitcoinTransaction } from '../types';
|
|
4
|
+
export interface BitcoinSwapProviderOptions {
|
|
5
|
+
network: BitcoinNetwork;
|
|
6
|
+
mode?: SwapMode;
|
|
7
|
+
scraperUrl?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare type TransactionMatchesFunction = (tx: Transaction<BitcoinTransaction>) => boolean;
|
|
10
|
+
export declare type PaymentVariants = {
|
|
11
|
+
[SwapMode.P2WSH]?: payments.Payment;
|
|
12
|
+
[SwapMode.P2SH_SEGWIT]?: payments.Payment;
|
|
13
|
+
[SwapMode.P2SH]?: payments.Payment;
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/swap/types.ts"],"names":[],"mappings":";;AAEA,oCAAuF"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Network } from '@yaswap/types';
|
|
3
|
+
import { Network as BitcoinJsLibNetwork } from 'bitcoinjs-lib';
|
|
4
|
+
export * as BitcoinEsploraTypes from './chain/esplora/types';
|
|
5
|
+
export * as BitcoinJsonRpcTypes from './chain/jsonRpc/types';
|
|
6
|
+
export * from './swap/types';
|
|
7
|
+
export interface BitcoinNetwork extends Network, BitcoinJsLibNetwork {
|
|
8
|
+
}
|
|
9
|
+
export interface BitcoinNodeWalletOptions {
|
|
10
|
+
addressType?: AddressType;
|
|
11
|
+
network?: BitcoinNetwork;
|
|
12
|
+
}
|
|
13
|
+
export interface BitcoinWalletProviderOptions extends BitcoinNodeWalletOptions {
|
|
14
|
+
baseDerivationPath: string;
|
|
15
|
+
}
|
|
16
|
+
export interface BitcoinHDWalletProviderOptions extends BitcoinWalletProviderOptions {
|
|
17
|
+
mnemonic: string;
|
|
18
|
+
}
|
|
19
|
+
export interface OutputTarget {
|
|
20
|
+
address?: string;
|
|
21
|
+
script?: Buffer;
|
|
22
|
+
value: number;
|
|
23
|
+
}
|
|
24
|
+
export interface ScriptPubKey {
|
|
25
|
+
asm: string;
|
|
26
|
+
hex: string;
|
|
27
|
+
reqSigs: number;
|
|
28
|
+
type: string;
|
|
29
|
+
addresses: string[];
|
|
30
|
+
}
|
|
31
|
+
export interface Output {
|
|
32
|
+
value: number;
|
|
33
|
+
n: number;
|
|
34
|
+
scriptPubKey: ScriptPubKey;
|
|
35
|
+
}
|
|
36
|
+
export interface Input {
|
|
37
|
+
txid: string;
|
|
38
|
+
vout: number;
|
|
39
|
+
scriptSig: {
|
|
40
|
+
asm: string;
|
|
41
|
+
hex: string;
|
|
42
|
+
};
|
|
43
|
+
txinwitness: string[];
|
|
44
|
+
sequence: number;
|
|
45
|
+
coinbase?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface Transaction {
|
|
48
|
+
txid: string;
|
|
49
|
+
hash: string;
|
|
50
|
+
version: number;
|
|
51
|
+
locktime: number;
|
|
52
|
+
size: number;
|
|
53
|
+
vsize: number;
|
|
54
|
+
weight: number;
|
|
55
|
+
vin: Input[];
|
|
56
|
+
vout: Output[];
|
|
57
|
+
confirmations?: number;
|
|
58
|
+
hex: string;
|
|
59
|
+
}
|
|
60
|
+
export interface UTXO {
|
|
61
|
+
txid: string;
|
|
62
|
+
vout: number;
|
|
63
|
+
value: number;
|
|
64
|
+
address: string;
|
|
65
|
+
derivationPath?: string;
|
|
66
|
+
}
|
|
67
|
+
export declare enum AddressType {
|
|
68
|
+
LEGACY = "legacy",
|
|
69
|
+
P2SH_SEGWIT = "p2sh-segwit",
|
|
70
|
+
BECH32 = "bech32"
|
|
71
|
+
}
|
|
72
|
+
export declare enum SwapMode {
|
|
73
|
+
P2SH = "p2sh",
|
|
74
|
+
P2SH_SEGWIT = "p2shSegwit",
|
|
75
|
+
P2WSH = "p2wsh"
|
|
76
|
+
}
|
|
77
|
+
export declare type AddressTxCounts = {
|
|
78
|
+
[index: string]: number;
|
|
79
|
+
};
|
|
80
|
+
export interface PsbtInputTarget {
|
|
81
|
+
index: number;
|
|
82
|
+
derivationPath: string;
|
|
83
|
+
}
|
|
84
|
+
export interface P2SHInput {
|
|
85
|
+
inputTxHex: string;
|
|
86
|
+
index: number;
|
|
87
|
+
vout: any;
|
|
88
|
+
outputScript: Buffer;
|
|
89
|
+
}
|