-
Notifications
You must be signed in to change notification settings - Fork 0
/
psbt_sign.js
57 lines (48 loc) · 1.6 KB
/
psbt_sign.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const bitcoin = require('bitcoinjs-lib');
const ecpair = bitcoin.ECPair;
const crypto = bitcoin.crypto;
const bscript = bitcoin.script;
const bip39 = require('bip39');
const LOCK_TIME = 0;
const SIGHASH_ALL = 1;
let NETWORK;
/*
Usage:
var p = require("./psbt_sign.js")
var psbt = PSBT_STRING_BASE64
const mnemonics = MNEMONICS_STRING
p.run("TESTNET", mnemonics, psbt)
p.run("MAIN", mnemonics, psbt)
*/
const isSegwitTx = (rawTx) => {
return rawTx.substring(8, 12) === '0001';
};
// const PATHS = ["m/84'/0'/0'/0/0", "m/84'/0'/0'/1/0"];
const run = async(network, mnemonics, psbtStr) => {
let NETWORK;
if (network === "MAIN") {
NETWORK = bitcoin.networks.bitcoin;
}
else if (network = "TESTNET") {
NETWORK = bitcoin.networks.testnet;
}
const psbt = bitcoin.Psbt.fromBase64(psbtStr);
const is = psbt.data.inputs;
const seed = await bip39.mnemonicToSeed(mnemonics);
const node = bitcoin.bip32.fromSeed(seed, NETWORK);
for(let i=0; i<is.length; i++) {
const der = is[i].bip32Derivation[0];
const walletDer = node.derivePath(der.path);
if(0 === Buffer.compare(der.pubkey, walletDer.publicKey)) {
console.log(`Signing ${der.path}`);
psbt.signInput(i, bitcoin.ECPair.fromPrivateKey(walletDer.privateKey));
}
const validate = await psbt.validateSignaturesOfAllInputs();
await psbt.finalizeAllInputs();
const hex = psbt.extractTransaction().toHex();
console.log(bitcoin.Transaction.fromHex(hex).getId())
console.log({ validate, hex});
}
return;
};
exports.run = run;