<p>Yes, you can use <code>OP_CHECKLOCKTIMEVERIFY</code> to make a transaction output unspendable for a certain number of blocks.  However, you can't use it directly in an if-else block as you have described.  When <code>OP_CHECKLOCKTIMEVERIFY</code> executes, it doesn't place a result value on the stack.  It either throws an error (invalidating the transaction attempting to spend the output), or continues executing as if nothing happened.</p>

<p>To create a transaction as you describe, you will need to use multisig, such that one party can spend the transaction as soon as the correct number is provided (input to a hash function, I presume?), OR you can spend it back to yourself after the lock time expires.  This is basically how the lightning network works.  You should check out <a href="https://rusty.ozlabs.org/?p=462" rel="nofollow">Lightning Networks Part II: Hashed Timelock Contracts (HTLCs)</a></p>

CC BY-SA 3.0
2016-05-12T21:16:29.453Z
Thanks for the info. On the BIP Page Peter Todd suggests a script similar to mine though, right? https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
Tom Taylor
CC BY-SA 3.0
2016-05-12T21:33:51.833Z
Yes, his examples are similar.  Just remember that `OP_CHECKLOCKTIMEVERIFY` is a consequence of a condition, not the condition itself.  The condition is the `0` or `1` at the end of his example redemption scripts.  That's what determines if `OP_CHECKLOCKTIMEVERIFY` is called.
Jestin
CC BY-SA 3.0
2016-05-12T21:47:10.070Z
Great, thanks. Will come back here with any further questions.
Tom Taylor
CC BY-SA 3.0
2016-05-12T17:02:29.127Z
2016-05-12T17:02:29.127Z
Jestin
<p>I'm creating a Bitcoin based lottery application. An entry into the lottery is a transaction spending 1BTC. </p>

<p>If someone guesses some arbitrary value correctly, they can claim the entry and send this 1 Btc to an address they control. </p>

<p>I wish to have functionality as follows: if no one guesses the correct number after 100 blocks, then I can claim it as the operator of the lottery. </p>

<p>Can I do this via:
Entry transaction 
  IF
     CHECKLOCKTIMEVERIFY 
    payable to a correct guess
  ELSE
    Payable to my operator public key
  END</p>

<p>Then when I try to spend this entry transaction, I set the nLockTime to the current block. </p>

<p>Throughout I set all the sequence numbers to 0xFFF... So that the transactions are mined. </p>

<development><locktime><checklocktimeverify>
How do I use CHECKLOCKTIMEVERIFY to prevent a transaction being spent before 100 blocks?
Tom Taylor
# How do I use CHECKLOCKTIMEVERIFY to prevent a transaction being spent before 100 blocks?

(Asked by: username_0 on 2016-05-12)

I'm creating a Bitcoin based lottery application. An entry into the lottery is a transaction spending 1BTC.

If someone guesses some arbitrary value correctly, they can claim the entry and send this 1 Btc to an address they control.

I wish to have functionality as follows: if no one guesses the correct number after 100 blocks, then I can claim it as the operator of the lottery.

Can I do this via:
Entry transaction
IF
CHECKLOCKTIMEVERIFY
payable to a correct guess
ELSE
Payable to my operator public key
END

Then when I try to spend this entry transaction, I set the nLockTime to the current block.

Throughout I set all the sequence numbers to 0xFFF... So that the transactions are mined.

**Tags:** development, locktime, checklocktimeverify


---

## 1 answers

### Answer by username_1 on 2016-05-12. Score: 0 | Accepted answer

Yes, you can use `OP_CHECKLOCKTIMEVERIFY` to make a transaction output unspendable for a certain number of blocks. However, you can't use it directly in an if-else block as you have described. When `OP_CHECKLOCKTIMEVERIFY` executes, it doesn't place a result value on the stack. It either throws an error (invalidating the transaction attempting to spend the output), or continues executing as if nothing happened.

To create a transaction as you describe, you will need to use multisig, such that one party can spend the transaction as soon as the correct number is provided (input to a hash function, I presume?), OR you can spend it back to yourself after the lock time expires. This is basically how the lightning network works. You should check out [Lightning Networks Part II: Hashed Timelock Contracts (HTLCs)](https://rusty.ozlabs.org/?p=462)

> **Comments:**
> **username_0**: Thanks for the info. On the BIP Page Peter Todd suggests a script similar to mine though, right? https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki  
> **username_1**: Yes, his examples are similar.  Just remember that `OP_CHECKLOCKTIMEVERIFY` is a consequence of a condition, not the condition itself.  The condition is the `0` or `1` at the end of his example redemption scripts.  That's what determines if `OP_CHECKLOCKTIMEVERIFY` is called.  
> **username_0**: Great, thanks. Will come back here with any further questions.  

---

