What is a Hashed Timelock Contract (HTLC)? | CaptainAltcoin

BIP 199 HTLC initial stack state

Hey fellow bitcoiners and redditors

I'm trying to understand how HTLCs (Hashed Time-Locked Contracts) can be implemented in Bitcoin and BIP-199 turned out to be the best resource for that purpose.

TL;DR
Question answered!
  1. Does the stack need to feature the secret/image twice in the OP_IF case? => call the script with [pubKey, signature, 1]
  2. How can the OP_ELSE flow ever be executed successfully (since this requires the stack to be empty, but [pubKey, signature] are required for OP_CHECKSIG at the very end of the script )? => call the script with [pubKey, signature, 0]

Full story
This is the example HTLC script I copied from BIP-199:
OP_IF
[HASHOP] OP_EQUALVERIFY OP_DUP OP_HASH160
OP_ELSE
[TIMEOUTOP] OP_DROP OP_DUP OP_HASH160
OP_ENDIF
OP_EQUALVERIFY
OP_CHECKSIG

IF-Case (seller can claim the buyers deposited BTCs)
TL;DR: detailed debug log of this path. Question: Can I avoid providing the preimage twice?

Before running this script, the preimage should be known and the stack should looks:
preimage
preimage
pubKey
signature

  • So since the stack is not empty, it will jump into the OP_IF clause.
    • the OP_IF will pop the top value => stack is now: [preimage, pubKey, signature]
  • [HASHOP] will hash the preimage
    • the preimage on the stack will, be replaced by the image => stack: [image, pubKey, signature]
  • digest (a synonym for image) will be pushed
    • => stack: [image, image pubKey, signature]
  • OP_EQUALVERIFY checks the two top items for equality: image == image => true
    • ...and removes them, if they are equal => stack: [pubKey, signature]
  • OP_DUP will duplicate the pubKey, => stack: [pubKey, pubKey, signature]
  • OP_HASH160 will double-hash the pubKey => stack: [p2pkhAddress, pubKey, signature]
  • represents the p2pkhAddress => stack: [p2pkhAddress, p2pkhAddress, pubKey, signature]
  • OP_EQUALVERIFY checks the two top items for equality: p2pkhAddress == p2pkhAddress => true
    • ...and removes them, if they are equal => stack: [pubKey, signature]
  • OP_CHECKSIG checks the signature
  • Done, all fine!
  • Question: This flow requires the secret to be on the stack twice before starting the script; Can I avoid that somehow?

ELSE-Case (buyer claims refund of his/her deposited BTCs)
Since the script runs an OP_CHECKSIG at the very end. This means the stack has to contain at least [pubKey, signature] before the execution pointer moves to OP_IF. However, OP_IF returns true "If the top stack value is not False". So how can the script possibly ever execute the OP_ELSE flow?

Disclaimer
  • My mother tongue is not English, please forgive me any mistakes. :)
  • I'm just getting started with BTC scripting, please forgive me any noob mistakes.
  • I also searched the whole stackexchange/search engine/reddit/github universum for this issue, obivously without success.
PS - buy Bitcoin :)
submitted by redditeraya to Bitcoin [link] [comments]

Hashop.io guarantees daily payouts and no additional fees except the maintenance fee. The user is required to make a one-time payment that implies a 2-year contract. The company accepts credit cards, wire transfer and Bitcoin payments via Bitpay, BIPS and Coinbase. Hashop.io has an on-going discount system. hashop -1 points 0 points 1 point 8 days ago Because bch stated goal was to flip btc and become bitcoin. Falling from 20% of btc's value to 5% and the bch fans still think it's possible lol Hashop.io guarantees daily payouts and no additional fees except the maintenance fee. The user is required to make a one-time payment that implies a 2-year contract. The company accepts credit cards, wire transfer and Bitcoin payments via Bitpay, BIPS and Coinbase. Hashop.io has an on-going discount system. Bitcoin mining is a game of numbers. The most important number is: 25 Bitcoins are mined approximately every 10 minutes on planet Earth. And this number reduces to half every 4 years, until all the bitcoins (21 million) are mined. How does mining work? Miners verify and record bitcoin transactions in an online ledger. The Hashed Timelock Contract is a very technical implementation of cryptocurrency payments.It requires the recipient of a payment to acknowledge the reception of said transfer prior to a deadline, which is done by generating a cryptographic proof of payment or forfeit the ability to claim the payment, returning it to the payer.

[index] [9553] [8589] [356] [23953] [23468] [23115] [26637] [29009] [10011] [15699]

Flag Counter