@@ -12,11 +12,10 @@ var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLw
1212describe ( 'bitcoinjs-lib (transactions w/ CLTV)' , function ( ) {
1313 var hashType = bitcoin . Transaction . SIGHASH_ALL
1414
15- // waitUntil is of the form { blocks: ... } or { utc: ... }
16- function cltvCheckSigOutput ( aQ , bQ , waitUntil ) {
15+ function cltvCheckSigOutput ( aQ , bQ , lockTime ) {
1716 return bitcoin . script . compile ( [
1817 bitcoin . opcodes . OP_IF ,
19- bitcoin . script . number . encode ( bip65 . encode ( waitUntil ) ) ,
18+ bitcoin . script . number . encode ( lockTime ) ,
2019 bitcoin . opcodes . OP_CHECKLOCKTIMEVERIFY ,
2120 bitcoin . opcodes . OP_DROP ,
2221
@@ -39,8 +38,8 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
3938 this . timeout ( 30000 )
4039
4140 // 3 hours ago
42- var timeUtc = utcNow ( ) - ( 3600 * 3 )
43- var redeemScript = cltvCheckSigOutput ( alice , bob , { utc : timeUtc } )
41+ var lockTime = bip65 . encode ( { utc : utcNow ( ) - ( 3600 * 3 ) } )
42+ var redeemScript = cltvCheckSigOutput ( alice , bob , lockTime )
4443 var scriptPubKey = bitcoin . script . scriptHash . output . encode ( bitcoin . crypto . hash160 ( redeemScript ) )
4544 var address = bitcoin . address . fromOutputScript ( scriptPubKey , regtest )
4645
@@ -49,7 +48,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
4948 if ( err ) return done ( err )
5049
5150 var txb = new bitcoin . TransactionBuilder ( regtest )
52- txb . setLockTime ( timeUtc )
51+ txb . setLockTime ( lockTime )
5352 txb . addInput ( unspent . txId , unspent . vout , 0xfffffffe )
5453 txb . addOutput ( regtestUtils . RANDOM_ADDRESS , 7e4 )
5554
@@ -79,47 +78,55 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
7978 it ( 'can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)' , function ( done ) {
8079 this . timeout ( 30000 )
8180
82- // 50 blocks from now
83- var time
84- var redeemScript = cltvCheckSigOutput ( alice , bob , timeUtc )
85- var scriptPubKey = bitcoin . script . scriptHash . output . encode ( bitcoin . crypto . hash160 ( redeemScript ) )
86- var address = bitcoin . address . fromOutputScript ( scriptPubKey , regtest )
87-
88- // fund the P2SH(CLTV) address
89- regtestUtils . faucet ( address , 1e5 , function ( err , unspent ) {
81+ regtestUtils . height ( function ( err , height ) {
9082 if ( err ) return done ( err )
9183
92- var txb = new bitcoin . TransactionBuilder ( regtest )
93- txb . setLockTime ( timeUtc )
94- txb . addInput ( unspent . txId , unspent . vout , 0xfffffffe )
95- txb . addOutput ( regtestUtils . RANDOM_ADDRESS , 7e4 )
96-
97- // {Alice's signature} OP_TRUE
98- var tx = txb . buildIncomplete ( )
99- var signatureHash = tx . hashForSignature ( 0 , redeemScript , hashType )
100- var redeemScriptSig = bitcoin . script . scriptHash . input . encode ( [
101- alice . sign ( signatureHash ) . toScriptSignature ( hashType ) ,
102- bitcoin . opcodes . OP_TRUE
103- ] , redeemScript )
104- tx . setInputScript ( 0 , redeemScriptSig )
84+ // 50 blocks from now
85+ var lockTime = bip65 . encode ( { blocks : height + 50 } )
86+ var redeemScript = cltvCheckSigOutput ( alice , bob , lockTime )
87+ var scriptPubKey = bitcoin . script . scriptHash . output . encode ( bitcoin . crypto . hash160 ( redeemScript ) )
88+ var address = bitcoin . address . fromOutputScript ( scriptPubKey , regtest )
10589
106- regtestUtils . broadcast ( tx . toHex ( ) , function ( err ) {
90+ // fund the P2SH(CLTV) address
91+ regtestUtils . faucet ( address , 1e5 , function ( err , unspent ) {
10792 if ( err ) return done ( err )
10893
109- // fails before the expiry
110- assert . throws ( function ( ) {
111- if ( err ) throw err
112- } , / E r r o r : 6 4 : n o n - f i n a l / )
113-
114- // into the future!
115- regtestUtils . mine (
116-
117- regtestUtils . verify ( {
118- txId : tx . getId ( ) ,
119- address : regtestUtils . RANDOM_ADDRESS ,
120- vout : 0 ,
121- value : 7e4
122- } , done )
94+ var txb = new bitcoin . TransactionBuilder ( regtest )
95+ txb . setLockTime ( lockTime )
96+ txb . addInput ( unspent . txId , unspent . vout , 0xfffffffe )
97+ txb . addOutput ( regtestUtils . RANDOM_ADDRESS , 7e4 )
98+
99+ // {Alice's signature} OP_TRUE
100+ var tx = txb . buildIncomplete ( )
101+ var signatureHash = tx . hashForSignature ( 0 , redeemScript , hashType )
102+ var redeemScriptSig = bitcoin . script . scriptHash . input . encode ( [
103+ alice . sign ( signatureHash ) . toScriptSignature ( hashType ) ,
104+ bitcoin . opcodes . OP_TRUE
105+ ] , redeemScript )
106+ tx . setInputScript ( 0 , redeemScriptSig )
107+
108+ regtestUtils . broadcast ( tx . toHex ( ) , function ( err ) {
109+ // fails before the expiry
110+ assert . throws ( function ( ) {
111+ if ( err ) throw err
112+ } , / E r r o r : 6 4 : n o n - f i n a l / )
113+
114+ // into the future!
115+ regtestUtils . mine ( 51 , function ( err ) {
116+ if ( err ) return done ( err )
117+
118+ regtestUtils . broadcast ( tx . toHex ( ) , function ( err ) {
119+ if ( err ) return done ( err )
120+
121+ regtestUtils . verify ( {
122+ txId : tx . getId ( ) ,
123+ address : regtestUtils . RANDOM_ADDRESS ,
124+ vout : 0 ,
125+ value : 7e4
126+ } , done )
127+ } )
128+ } )
129+ } )
123130 } )
124131 } )
125132 } )
0 commit comments