-
Notifications
You must be signed in to change notification settings - Fork 2
/
p2wsh_multisig_masterkey.js
96 lines (76 loc) · 2.4 KB
/
p2wsh_multisig_masterkey.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const { btc, send, bech32toScriptPubKey } = require('./btc');
const bitcoin = require('bitcoinjs-lib');
const network = bitcoin.networks.testnet;
const hashtype = bitcoin.Transaction.SIGHASH_ALL;
const ECPair = require('ecpair').ECPairFactory(require('tiny-secp256k1'));
function opcodeNumber(n) {
if (n == 0) {
return 0;
} else if (n > 0 && n < 17) {
return 80 + n;
}
return bitcoin.script.number.encode(n);
}
const txid = '1234....'; // txid hex here
const vout = 0;
const addrs = [
'tb1qbech32addresshere',
// ...
];
// the address of the key that can always unlock the funds
const master = 'tb1qbech32addresshere';
// multisig configuration
const m = 3;
const n = addrs.length;
if (m > n) {
// master key can always unlock so no error
//console.error('m > n results in an unsolvable script');
//process.exit(1);
}
main();
async function main() {
const keys = addrs.map(a => btc('dumpprivkey', a));
const masterKey = ECPair.fromWIF(await btc('dumpprivkey', master), network);
const ecpairs = [];
for (var i = 0; i < keys.length; i++) {
ecpairs.push(ECPair.fromWIF(await keys[i], network));
}
const witnessScript = bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
opcodeNumber(m),
...ecpairs.map(x => x.publicKey),
opcodeNumber(n),
bitcoin.opcodes.OP_CHECKMULTISIG,
bitcoin.opcodes.OP_ELSE,
masterKey.publicKey,
bitcoin.opcodes.OP_CHECKSIG,
bitcoin.opcodes.OP_ENDIF
]);
console.log('witnessScript: ' + witnessScript.toString('hex'));
console.log(
'send 1000 sat to ' +
bitcoin.payments.p2wsh({ redeem: { output: witnessScript, network }, network }).address
);
const tx = new bitcoin.Transaction(network);
tx.addInput(Buffer.from(txid, 'hex').reverse(), vout);
const amount = 1000;
const fee = 100;
tx.addOutput(bech32toScriptPubKey('tb1qbech32addresshere'), amount - fee);
const sighash = tx.hashForWitnessV0(0, witnessScript, amount, hashtype);
const witness = bitcoin.payments.p2wsh({
redeem: {
input: bitcoin.script.compile([
// master key
// bitcoin.script.signature.encode(masterKey.sign(sighash), hashtype),
// bitcoin.script.number.encode(0)
// multisig 3 of 4
bitcoin.script.number.encode(0),
...ecpairs.slice(1).map(x => bitcoin.script.signature.encode(x.sign(sighash), hashtype)),
bitcoin.script.number.encode(1)
]),
output: witnessScript
}
}).witness;
tx.setWitness(0, witness);
console.log(await send(tx.toHex()));
}