@atomicfinance/bitcoin-js-wallet-provider 3.5.2 → 3.6.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/package.json +6 -6
- package/lib/BitcoinJsWalletProvider.js +0 -424
- package/lib/index.js +0 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/bitcoin-js-wallet-provider",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"node": ">=14"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@atomicfinance/bitcoin-utils": "^3.
|
|
28
|
-
"@atomicfinance/bitcoin-wallet-provider": "^3.
|
|
29
|
-
"@atomicfinance/types": "^3.
|
|
30
|
-
"@atomicfinance/utils": "^3.
|
|
27
|
+
"@atomicfinance/bitcoin-utils": "^3.6.0",
|
|
28
|
+
"@atomicfinance/bitcoin-wallet-provider": "^3.6.0",
|
|
29
|
+
"@atomicfinance/types": "^3.6.0",
|
|
30
|
+
"@atomicfinance/utils": "^3.6.0",
|
|
31
31
|
"@babel/runtime": "^7.12.1",
|
|
32
|
-
"@node-dlc/core": "0.
|
|
32
|
+
"@node-dlc/core": "0.24.0",
|
|
33
33
|
"bip32": "^2.0.6",
|
|
34
34
|
"bip39": "^3.0.2",
|
|
35
35
|
"bitcoin-networks": "^1.0.0",
|
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
const bitcoin_utils_1 = require("@atomicfinance/bitcoin-utils");
|
|
30
|
-
const bitcoin_wallet_provider_1 = __importDefault(require("@atomicfinance/bitcoin-wallet-provider"));
|
|
31
|
-
const provider_1 = __importDefault(require("@atomicfinance/provider"));
|
|
32
|
-
const types_1 = require("@atomicfinance/types");
|
|
33
|
-
const assert_1 = __importDefault(require("assert"));
|
|
34
|
-
const bip32_1 = require("bip32");
|
|
35
|
-
const bip39_1 = require("bip39");
|
|
36
|
-
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
37
|
-
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
38
|
-
const bitcoinjs_message_1 = require("bitcoinjs-message");
|
|
39
|
-
const secp256k1_1 = __importDefault(require("secp256k1"));
|
|
40
|
-
const FEE_PER_BYTE_FALLBACK = 5;
|
|
41
|
-
class BitcoinJsWalletProvider extends (0, bitcoin_wallet_provider_1.default)(provider_1.default) {
|
|
42
|
-
constructor(options) {
|
|
43
|
-
const { network, mnemonic, baseDerivationPath, addressType = types_1.bitcoin.AddressType.BECH32, addressIndex = 0, changeAddressIndex = 0, } = options;
|
|
44
|
-
super({
|
|
45
|
-
network,
|
|
46
|
-
baseDerivationPath,
|
|
47
|
-
addressType,
|
|
48
|
-
addressIndex,
|
|
49
|
-
changeAddressIndex,
|
|
50
|
-
});
|
|
51
|
-
if (!mnemonic)
|
|
52
|
-
throw new Error('Mnemonic should not be empty');
|
|
53
|
-
this._mnemonic = mnemonic;
|
|
54
|
-
}
|
|
55
|
-
async seedNode() {
|
|
56
|
-
if (this._seedNode)
|
|
57
|
-
return this._seedNode;
|
|
58
|
-
const seed = await (0, bip39_1.mnemonicToSeed)(this._mnemonic);
|
|
59
|
-
this._seedNode = (0, bip32_1.fromSeed)(seed, this._network);
|
|
60
|
-
return this._seedNode;
|
|
61
|
-
}
|
|
62
|
-
async baseDerivationNode() {
|
|
63
|
-
if (this._baseDerivationNode)
|
|
64
|
-
return this._baseDerivationNode;
|
|
65
|
-
const baseNode = await this.seedNode();
|
|
66
|
-
this._baseDerivationNode = baseNode.derivePath(this._baseDerivationPath);
|
|
67
|
-
return this._baseDerivationNode;
|
|
68
|
-
}
|
|
69
|
-
async keyPair(derivationPath) {
|
|
70
|
-
const wif = await this._toWIF(derivationPath);
|
|
71
|
-
return bitcoinjs_lib_1.ECPair.fromWIF(wif, this._network);
|
|
72
|
-
}
|
|
73
|
-
async _toWIF(derivationPath) {
|
|
74
|
-
const node = await this.seedNode();
|
|
75
|
-
return node.derivePath(derivationPath).toWIF();
|
|
76
|
-
}
|
|
77
|
-
async exportPrivateKey() {
|
|
78
|
-
return this._toWIF(this._baseDerivationPath);
|
|
79
|
-
}
|
|
80
|
-
async signMessage(message, from) {
|
|
81
|
-
const address = await this.getWalletAddress(from);
|
|
82
|
-
const keyPair = await this.keyPair(address.derivationPath);
|
|
83
|
-
const signature = await (0, bitcoinjs_message_1.signAsync)(message, keyPair.privateKey, keyPair.compressed);
|
|
84
|
-
return signature.toString('hex');
|
|
85
|
-
}
|
|
86
|
-
_createMultisigPayment(m, pubkeys) {
|
|
87
|
-
if (m > pubkeys.length) {
|
|
88
|
-
throw new Error(`not enough keys supplied (got ${pubkeys.length} keys, but need at least ${m} to redeem)`);
|
|
89
|
-
}
|
|
90
|
-
// Create m-of-n multisig
|
|
91
|
-
const p2ms = bitcoin.payments.p2ms({
|
|
92
|
-
m: m,
|
|
93
|
-
pubkeys: pubkeys.map((key) => Buffer.from(key, 'hex')),
|
|
94
|
-
network: this._network,
|
|
95
|
-
});
|
|
96
|
-
// Create p2wsh for multisig
|
|
97
|
-
const p2wsh = bitcoin.payments.p2wsh({
|
|
98
|
-
redeem: p2ms,
|
|
99
|
-
network: this._network,
|
|
100
|
-
});
|
|
101
|
-
return p2wsh;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Creates a native-segwit multi-signature address (P2MS in P2WSH) with n signatures of m required keys
|
|
105
|
-
* https://developer.bitcoin.org/reference/rpc/createmultisig.html
|
|
106
|
-
* @param m the number of required signatures
|
|
107
|
-
* @param pubkeys n possible pubkeys in total
|
|
108
|
-
* @returns a json object containing the `address` and `redeemScript`
|
|
109
|
-
*/
|
|
110
|
-
createMultisig(m, pubkeys) {
|
|
111
|
-
const p2wsh = this._createMultisigPayment(m, pubkeys);
|
|
112
|
-
return {
|
|
113
|
-
address: p2wsh.address,
|
|
114
|
-
redeemScript: p2wsh.redeem?.output?.toString('hex'),
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Creates a PSBT of a native-segwit multi-signature address (P2MS in P2WSH) with n signatures of m required keys
|
|
119
|
-
* https://developer.bitcoin.org/reference/rpc/createmultisig.html
|
|
120
|
-
* https://developer.bitcoin.org/reference/rpc/createpsbt.html
|
|
121
|
-
* @param m the number of required signatures
|
|
122
|
-
* @param pubkeys n possible pubkeys in total
|
|
123
|
-
* @param inputs the Inputs to the PSBT
|
|
124
|
-
* @param ouputs the Outputs to the PSBT
|
|
125
|
-
* @returns a base64 encoded psbt string
|
|
126
|
-
*/
|
|
127
|
-
buildMultisigPSBT(m, pubkeys, inputs, outputs) {
|
|
128
|
-
(0, assert_1.default)(inputs.length > 0, 'no inputs found');
|
|
129
|
-
(0, assert_1.default)(outputs.length > 0, 'no outputs found');
|
|
130
|
-
const p2wsh = this._createMultisigPayment(m, pubkeys);
|
|
131
|
-
// Verify pubkeyhash for all inputs matches the p2wsh hash
|
|
132
|
-
(0, assert_1.default)(inputs.every((input) => p2wsh.output.toString('hex') === input.scriptPubKey), 'address pubkeyhash does not match input scriptPubKey');
|
|
133
|
-
// creator
|
|
134
|
-
const psbt = new bitcoin.Psbt({ network: this._network });
|
|
135
|
-
// updater
|
|
136
|
-
inputs.forEach((input) => {
|
|
137
|
-
psbt.addInput({
|
|
138
|
-
hash: input.txid,
|
|
139
|
-
index: input.vout,
|
|
140
|
-
witnessUtxo: { script: p2wsh.output, value: input.value },
|
|
141
|
-
witnessScript: p2wsh.redeem.output,
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
outputs.forEach((output) => {
|
|
145
|
-
psbt.addOutput({
|
|
146
|
-
address: output.to,
|
|
147
|
-
value: output.value,
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
return psbt.toBase64();
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Update a PSBT with input information from our wallet and then sign inputs that we can sign for
|
|
154
|
-
* https://developer.bitcoin.org/reference/rpc/walletprocesspsbt.html
|
|
155
|
-
* @param psbt a base64 encoded psbt string (P2WSH only)
|
|
156
|
-
* @returns a base64 encoded signed psbt string
|
|
157
|
-
*/
|
|
158
|
-
async walletProcessPSBT(psbtString) {
|
|
159
|
-
const psbt = bitcoin.Psbt.fromBase64(psbtString);
|
|
160
|
-
await Promise.all(psbt.data.inputs.map(async (input, i) => {
|
|
161
|
-
(0, assert_1.default)(psbt.getInputType(i).slice(0, 5) === 'p2wsh', 'only accepts P2WSH inputs');
|
|
162
|
-
const scriptStack = bitcoin.script.decompile(input.witnessScript);
|
|
163
|
-
const pubkeys = scriptStack.filter((data) => Buffer.isBuffer(data) && secp256k1_1.default.publicKeyVerify(data));
|
|
164
|
-
await Promise.all(pubkeys.map(async (key) => {
|
|
165
|
-
// create address using pubkey
|
|
166
|
-
const { address: addressString } = bitcoin.payments.p2wpkh({
|
|
167
|
-
pubkey: key,
|
|
168
|
-
network: this._network,
|
|
169
|
-
});
|
|
170
|
-
// Retrieve address object from wallet using address
|
|
171
|
-
const address = await this.findAddress([addressString]);
|
|
172
|
-
// exit if address doesn't exist in wallet
|
|
173
|
-
if (!address)
|
|
174
|
-
return;
|
|
175
|
-
// derive keypair
|
|
176
|
-
const keyPair = await this.keyPair(address.derivationPath);
|
|
177
|
-
// sign PSBT using keypair
|
|
178
|
-
psbt.signInput(i, keyPair);
|
|
179
|
-
}));
|
|
180
|
-
}));
|
|
181
|
-
psbt.validateSignaturesOfAllInputs(); // ensure all signatures are valid!
|
|
182
|
-
return psbt.toBase64();
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Finalize the inputs of a PSBT. If the transaction is fully signed, it will
|
|
186
|
-
* produce a network serialized transaction which can be broadcast with sendrawtransaction
|
|
187
|
-
* https://developer.bitcoin.org/reference/rpc/finalizepsbt.html
|
|
188
|
-
* @param psbt a base64 encoded psbt string
|
|
189
|
-
* @returns a json object containing `psbt` in base64, `hex` for transaction and `complete` for if
|
|
190
|
-
* the transaction has a complete set of signatures
|
|
191
|
-
*/
|
|
192
|
-
finalizePSBT(psbtString) {
|
|
193
|
-
const psbt = bitcoin.Psbt.fromBase64(psbtString);
|
|
194
|
-
try {
|
|
195
|
-
psbt.validateSignaturesOfAllInputs(); // ensure all signatures are valid!
|
|
196
|
-
psbt.finalizeAllInputs();
|
|
197
|
-
}
|
|
198
|
-
catch (error) {
|
|
199
|
-
return {
|
|
200
|
-
psbt: psbt.toBase64(),
|
|
201
|
-
complete: false,
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
return {
|
|
205
|
-
psbt: psbt.toBase64(),
|
|
206
|
-
hex: psbt.extractTransaction().toHex(),
|
|
207
|
-
complete: true,
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
async sendSweepTransactionWithSetOutputs(externalChangeAddress, feePerByte, _outputs, fixedInputs) {
|
|
211
|
-
const { hex, fee } = await this._buildSweepTransactionWithSetOutputs(externalChangeAddress, feePerByte, _outputs, fixedInputs);
|
|
212
|
-
await this.getMethod('sendRawTransaction')(hex);
|
|
213
|
-
return (0, bitcoin_utils_1.normalizeTransactionObject)((0, bitcoin_utils_1.decodeRawTransaction)(hex, this._network), fee);
|
|
214
|
-
}
|
|
215
|
-
async buildSweepTransactionWithSetOutputs(externalChangeAddress, feePerByte, _outputs, fixedInputs) {
|
|
216
|
-
return this._buildSweepTransactionWithSetOutputs(externalChangeAddress, feePerByte, _outputs, fixedInputs);
|
|
217
|
-
}
|
|
218
|
-
async _buildSweepTransactionWithSetOutputs(externalChangeAddress, feePerByte, _outputs = [], fixedInputs) {
|
|
219
|
-
const _feePerByte = feePerByte ||
|
|
220
|
-
(await this.getMethod('getFeePerByte')()) ||
|
|
221
|
-
FEE_PER_BYTE_FALLBACK;
|
|
222
|
-
const inputs = [];
|
|
223
|
-
const outputs = [];
|
|
224
|
-
try {
|
|
225
|
-
const inputsForAmount = await this.getInputsForAmount(_outputs, _feePerByte, fixedInputs, 100, true);
|
|
226
|
-
if (inputsForAmount.change) {
|
|
227
|
-
throw Error('There should not be any change for sweeping transaction');
|
|
228
|
-
}
|
|
229
|
-
inputs.push(...(inputsForAmount.inputs || []));
|
|
230
|
-
outputs.push(...(inputsForAmount.outputs || []));
|
|
231
|
-
}
|
|
232
|
-
catch (e) {
|
|
233
|
-
if (fixedInputs.length === 0) {
|
|
234
|
-
throw Error(`Inputs for amount doesn't exist and no fixedInputs provided`);
|
|
235
|
-
}
|
|
236
|
-
const inputsForAmount = await this._getInputForAmountWithoutUtxoCheck(_outputs, _feePerByte, fixedInputs);
|
|
237
|
-
inputs.push(...(inputsForAmount.inputs.map((utxo) => types_1.Input.fromUTXO(utxo)) || []));
|
|
238
|
-
outputs.push(...(inputsForAmount.outputs || []));
|
|
239
|
-
}
|
|
240
|
-
_outputs.forEach((output) => {
|
|
241
|
-
const spliceIndex = outputs.findIndex((sweepOutput) => output.value === sweepOutput.value);
|
|
242
|
-
outputs.splice(spliceIndex, 1);
|
|
243
|
-
});
|
|
244
|
-
_outputs.push({
|
|
245
|
-
to: externalChangeAddress,
|
|
246
|
-
value: outputs[0].value,
|
|
247
|
-
});
|
|
248
|
-
return this._buildTransactionWithoutUtxoCheck(_outputs, _feePerByte, inputs);
|
|
249
|
-
}
|
|
250
|
-
_getInputForAmountWithoutUtxoCheck(_outputs, _feePerByte, fixedInputs) {
|
|
251
|
-
const utxoBalance = fixedInputs.reduce((a, b) => a + (b['value'] || 0), 0);
|
|
252
|
-
const outputBalance = _outputs.reduce((a, b) => a + (b['value'] || 0), 0);
|
|
253
|
-
const amountToSend = utxoBalance -
|
|
254
|
-
_feePerByte * ((_outputs.length + 1) * 39 + fixedInputs.length * 153); // todo better calculation
|
|
255
|
-
const targets = _outputs.map((target, i) => ({
|
|
256
|
-
id: 'main',
|
|
257
|
-
value: target.value,
|
|
258
|
-
}));
|
|
259
|
-
if (amountToSend - outputBalance > 0) {
|
|
260
|
-
targets.push({ id: 'main', value: amountToSend - outputBalance });
|
|
261
|
-
}
|
|
262
|
-
return (0, bitcoin_utils_1.selectCoins)(fixedInputs, targets, Math.ceil(_feePerByte), fixedInputs);
|
|
263
|
-
}
|
|
264
|
-
async _buildTransactionWithoutUtxoCheck(outputs, feePerByte, fixedInputs) {
|
|
265
|
-
const network = this._network;
|
|
266
|
-
const { fee } = this._getInputForAmountWithoutUtxoCheck(outputs, feePerByte, fixedInputs);
|
|
267
|
-
const inputs = fixedInputs;
|
|
268
|
-
const txb = new bitcoin.TransactionBuilder(network);
|
|
269
|
-
for (const output of outputs) {
|
|
270
|
-
const to = output.to; // Allow for OP_RETURN
|
|
271
|
-
txb.addOutput(to, output.value);
|
|
272
|
-
}
|
|
273
|
-
const prevOutScriptType = 'p2wpkh';
|
|
274
|
-
for (let i = 0; i < inputs.length; i++) {
|
|
275
|
-
const wallet = await this.getWalletAddress(inputs[i].address);
|
|
276
|
-
const keyPair = await this.keyPair(wallet.derivationPath);
|
|
277
|
-
const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey);
|
|
278
|
-
txb.addInput(inputs[i].txid, inputs[i].vout, 0, paymentVariant.output);
|
|
279
|
-
}
|
|
280
|
-
for (let i = 0; i < inputs.length; i++) {
|
|
281
|
-
const wallet = await this.getWalletAddress(inputs[i].address);
|
|
282
|
-
const keyPair = await this.keyPair(wallet.derivationPath);
|
|
283
|
-
const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey);
|
|
284
|
-
const needsWitness = true;
|
|
285
|
-
const signParams = {
|
|
286
|
-
prevOutScriptType,
|
|
287
|
-
vin: i,
|
|
288
|
-
keyPair,
|
|
289
|
-
witnessValue: 0,
|
|
290
|
-
};
|
|
291
|
-
if (needsWitness) {
|
|
292
|
-
signParams.witnessValue = inputs[i].value;
|
|
293
|
-
}
|
|
294
|
-
txb.sign(signParams);
|
|
295
|
-
}
|
|
296
|
-
return { hex: txb.build().toHex(), fee };
|
|
297
|
-
}
|
|
298
|
-
async _buildTransaction(targets, feePerByte, fixedInputs) {
|
|
299
|
-
const network = this._network;
|
|
300
|
-
const unusedAddress = await this.getUnusedAddress(true);
|
|
301
|
-
const { inputs, change, fee } = await this.getInputsForAmount(targets, feePerByte, fixedInputs);
|
|
302
|
-
if (change) {
|
|
303
|
-
targets.push({
|
|
304
|
-
address: unusedAddress.address,
|
|
305
|
-
value: change.value,
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
const psbt = new bitcoinjs_lib_1.Psbt({ network });
|
|
309
|
-
const needsWitness = [
|
|
310
|
-
types_1.bitcoin.AddressType.BECH32,
|
|
311
|
-
types_1.bitcoin.AddressType.P2SH_SEGWIT,
|
|
312
|
-
].includes(this._addressType);
|
|
313
|
-
for (let i = 0; i < inputs.length; i++) {
|
|
314
|
-
const wallet = await this.getWalletAddress(inputs[i].address);
|
|
315
|
-
const keyPair = await this.keyPair(wallet.derivationPath);
|
|
316
|
-
const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey);
|
|
317
|
-
const psbtInput = {
|
|
318
|
-
hash: inputs[i].txid,
|
|
319
|
-
index: inputs[i].vout,
|
|
320
|
-
sequence: 0,
|
|
321
|
-
};
|
|
322
|
-
if (needsWitness) {
|
|
323
|
-
psbtInput.witnessUtxo = {
|
|
324
|
-
script: paymentVariant.output,
|
|
325
|
-
value: inputs[i].value,
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
329
|
-
const inputTxRaw = await this.getMethod('getRawTransactionByHash')(inputs[i].txid);
|
|
330
|
-
psbtInput.nonWitnessUtxo = Buffer.from(inputTxRaw, 'hex');
|
|
331
|
-
}
|
|
332
|
-
if (this._addressType === types_1.bitcoin.AddressType.P2SH_SEGWIT) {
|
|
333
|
-
psbtInput.redeemScript = paymentVariant.redeem.output;
|
|
334
|
-
}
|
|
335
|
-
psbt.addInput(psbtInput);
|
|
336
|
-
}
|
|
337
|
-
for (const output of targets) {
|
|
338
|
-
if (output.script) {
|
|
339
|
-
psbt.addOutput({
|
|
340
|
-
value: output.value,
|
|
341
|
-
script: output.script,
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
psbt.addOutput({
|
|
346
|
-
value: output.value,
|
|
347
|
-
address: output.address,
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
for (let i = 0; i < inputs.length; i++) {
|
|
352
|
-
const wallet = await this.getWalletAddress(inputs[i].address);
|
|
353
|
-
const keyPair = await this.keyPair(wallet.derivationPath);
|
|
354
|
-
psbt.signInput(i, keyPair);
|
|
355
|
-
psbt.validateSignaturesOfInput(i);
|
|
356
|
-
}
|
|
357
|
-
psbt.finalizeAllInputs();
|
|
358
|
-
return { hex: psbt.extractTransaction().toHex(), fee };
|
|
359
|
-
}
|
|
360
|
-
async _buildSweepTransaction(externalChangeAddress, feePerByte) {
|
|
361
|
-
let _feePerByte = feePerByte || null;
|
|
362
|
-
if (!_feePerByte)
|
|
363
|
-
_feePerByte = await this.getMethod('getFeePerByte')();
|
|
364
|
-
const { inputs, outputs, change } = await this.getInputsForAmount([], _feePerByte, [], 100, true);
|
|
365
|
-
if (change) {
|
|
366
|
-
throw new Error('There should not be any change for sweeping transaction');
|
|
367
|
-
}
|
|
368
|
-
const _outputs = [
|
|
369
|
-
{
|
|
370
|
-
address: externalChangeAddress,
|
|
371
|
-
value: outputs[0].value,
|
|
372
|
-
},
|
|
373
|
-
];
|
|
374
|
-
return this._buildTransaction(_outputs, feePerByte, inputs);
|
|
375
|
-
}
|
|
376
|
-
async signPSBT(data, inputs) {
|
|
377
|
-
const psbt = bitcoinjs_lib_1.Psbt.fromBase64(data, { network: this._network });
|
|
378
|
-
for (const input of inputs) {
|
|
379
|
-
const keyPair = await this.keyPair(input.derivationPath);
|
|
380
|
-
psbt.signInput(input.index, keyPair);
|
|
381
|
-
}
|
|
382
|
-
return psbt.toBase64();
|
|
383
|
-
}
|
|
384
|
-
async signBatchP2SHTransaction(inputs, addresses, tx, _lockTime, segwit) {
|
|
385
|
-
const keyPairs = [];
|
|
386
|
-
for (const address of addresses) {
|
|
387
|
-
const wallet = await this.getWalletAddress(address);
|
|
388
|
-
const keyPair = await this.keyPair(wallet.derivationPath);
|
|
389
|
-
keyPairs.push(keyPair);
|
|
390
|
-
}
|
|
391
|
-
const sigs = [];
|
|
392
|
-
for (let i = 0; i < inputs.length; i++) {
|
|
393
|
-
const index = inputs[i].txInputIndex
|
|
394
|
-
? inputs[i].txInputIndex
|
|
395
|
-
: inputs[i].index;
|
|
396
|
-
let sigHash;
|
|
397
|
-
if (segwit) {
|
|
398
|
-
sigHash = tx.hashForWitnessV0(index, inputs[i].outputScript, inputs[i].vout.vSat, bitcoinjs_lib_1.Transaction.SIGHASH_ALL);
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
sigHash = tx.hashForSignature(index, inputs[i].outputScript, bitcoinjs_lib_1.Transaction.SIGHASH_ALL);
|
|
402
|
-
}
|
|
403
|
-
const sig = bitcoinjs_lib_1.script.signature.encode(keyPairs[i].sign(sigHash), bitcoinjs_lib_1.Transaction.SIGHASH_ALL);
|
|
404
|
-
sigs.push(sig);
|
|
405
|
-
}
|
|
406
|
-
return sigs;
|
|
407
|
-
}
|
|
408
|
-
getScriptType() {
|
|
409
|
-
if (this._addressType === types_1.bitcoin.AddressType.LEGACY)
|
|
410
|
-
return 'p2pkh';
|
|
411
|
-
else if (this._addressType === types_1.bitcoin.AddressType.P2SH_SEGWIT)
|
|
412
|
-
return 'p2sh-p2wpkh';
|
|
413
|
-
else if (this._addressType === types_1.bitcoin.AddressType.BECH32)
|
|
414
|
-
return 'p2wpkh';
|
|
415
|
-
}
|
|
416
|
-
async getConnectedNetwork() {
|
|
417
|
-
return this._network;
|
|
418
|
-
}
|
|
419
|
-
async isWalletAvailable() {
|
|
420
|
-
return true;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
exports.default = BitcoinJsWalletProvider;
|
|
424
|
-
//# sourceMappingURL=BitcoinJsWalletProvider.js.map
|
package/lib/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.BitcoinJsWalletProvider = void 0;
|
|
7
|
-
const BitcoinJsWalletProvider_1 = __importDefault(require("./BitcoinJsWalletProvider"));
|
|
8
|
-
exports.BitcoinJsWalletProvider = BitcoinJsWalletProvider_1.default;
|
|
9
|
-
//# sourceMappingURL=index.js.map
|