-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbip65.js
31 lines (27 loc) · 1.09 KB
/
bip65.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
let bitcoin = require("bitcoinjs-lib");
let bip65 = require('bip65');
function cltvCheckSigOutput (aQ, bQ, lockTime) {
return bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
bitcoin.script.number.encode(lockTime),
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
bitcoin.opcodes.OP_DROP,
bitcoin.opcodes.OP_ELSE,
bQ.publicKey,
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,
aQ.publicKey,
bitcoin.opcodes.OP_CHECKSIG
])
}
function utcNow () {
return Math.floor(Date.now() / 1000)
}
let NETWORK = bitcoin.networks.testnet;
let alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', NETWORK);
let bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', NETWORK)
let lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) });
let redeemScript = cltvCheckSigOutput(alice, bob, lockTime);
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript));
let address = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
console.log(address);