-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathp2sh.js
More file actions
31 lines (22 loc) · 769 Bytes
/
p2sh.js
File metadata and controls
31 lines (22 loc) · 769 Bytes
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
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([
bitcoin.opcodes.OP_6,
bitcoin.opcodes.OP_7,
redeemScript
]));
const fee_sat = 100;
const input_sat = 1000;
tx.addOutput(bech32toScriptPubKey('tb1qbech32addresshere'), input_sat-fee_sat);
send(tx.toHex()).then(console.log);