-
Notifications
You must be signed in to change notification settings - Fork 2
/
p2sh-p2wsh.js
34 lines (24 loc) · 881 Bytes
/
p2sh-p2wsh.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
const { send, bech32toScriptPubKey } = require('./btc');
const bitcoin = require('bitcoinjs-lib');
const network = bitcoin.networks.testnet;
const redeemScript = bitcoin.script.compile([
bitcoin.opcodes.OP_ADD,
bitcoin.opcodes.OP_13,
bitcoin.opcodes.OP_EQUAL
]);
console.log('Locking script: ' + redeemScript.toString('hex'));
const tx = new bitcoin.Transaction(network);
const txid = '1234....'; // txid hex here
const vout = 0;
tx.addInput(Buffer.from(txid, 'hex').reverse(), vout);
tx.setInputScript(0, bitcoin.script.compile([
Buffer.from('0020' + bitcoin.crypto.sha256(redeemScript).toString('hex'), 'hex')
]));
tx.setWitness(0, [
...[ '06', '07' ].map(x => Buffer.from(x, 'hex')),
redeemScript
]);
const fee_sat = 100;
const input_sat = 1000;
tx.addOutput(bech32toScriptPubKey('tb1qbech32addresshere'), input_sat-fee_sat);
send(tx.toHex()).then(console.log);