-
Notifications
You must be signed in to change notification settings - Fork 2
/
p2wsh_timelock.js
55 lines (41 loc) · 1.47 KB
/
p2wsh_timelock.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
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'));
const txid = '1234....'; // txid hex here
const vout = 0;
// address to lock the coins
const addr = 'tb1qbech32addresshere';
main();
async function main() {
const key = btc('dumpprivkey', addr);
const ecpair = ECPair.fromWIF(await key, network);
const witnessScript = bitcoin.script.compile([
ecpair.publicKey,
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.script.number.encode(2000000),
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY
]);
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, 0xfffffffe); // 0xfffffffe -> no rbf, activate locktime
tx.locktime = 2134581;
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([
bitcoin.script.signature.encode(ecpair.sign(sighash), hashtype)
]),
output: witnessScript
}
}).witness;
tx.setWitness(0, witness);
console.log(await send(tx.toHex()));
}