<p>There's a lot difference between ethereum and bitcoin regarding locking and unlocking fund, but let me roughly compare.</p>
<ol>
<li>Locking Funds</li>
</ol>
<p>Unlike ethereum, most transaction of bitcoin itself is an action of locking fund, using smart contract called <strong>&quot;Script&quot;</strong>. You can build <code>ScriptPubKey</code> with <strong>&quot;Script&quot;</strong>, and transaction is an action of sending bitcoin to <code>ScriptPubKey</code>. You could think most transaction of bitcoin is a bit similar to deploying ethereum smart contract which locks ethereum.</p>
<ol start="2">
<li>Unlocking Conditions</li>
</ol>
<p>To unlock bitcoin transaction, you have to provide <code>ScriptSig</code>(or <code>Witness</code> for segwit), which has to pass the condition of <code>ScriptPubKey</code> to spend. <code>ScriptSig</code> consists of data to pass the condition of <code>ScriptPubKey</code>. For example, if <code>ScriptPubKey</code> requires signature verifiable with specific public key, signature would be placed into <code>ScriptSig</code>. Unlocking condition can be any of combination implemented with <strong>”Script”</strong>, and time-based conditions or fulfilling certain cryptographic proofs can be. Hash Time Lock Contract can be implemented as well.</p>
<ol start="3">
<li>Tools and Practices</li>
</ol>
<p>There's a <a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">description</a> about <strong>”Script”</strong>, or <a href="https://bitcoin.sipa.be/miniscript/" rel="nofollow noreferrer">Miniscript</a> is also widely used. Or just search bitcoin library in GitHub with your preferred language.</p>
<ol start="4">
<li>Any insights or guidance on how to approach conditional fund locking in Bitcoin similar to Ethereum's smart contracts</li>
</ol>
<p>Most common smart contracts of bitcoin are <code>p2pkh</code>, <code>p2wpkh</code> and <code>p2tr</code>(key-path), which is similar to Ethereum Transfer Transaction without calling smart contract(i.e. just transfer eth by signing tx). It requires the fund owner to sign transaction(and provide public key if not <code>p2tr</code>). However, as mentioned above, most transaction of bitcoin is an action of locking fund, so it means <code>p2pkh</code>, <code>p2wpkh</code>, and <code>p2tr</code>(key-path) are also way of locking fund using smart contract, of which unlocking condition is to provide signature(and public key if not <code>p2tr</code>).</p>
<p>Other smart contracts of bitcoin can be implemented with <code>p2sh</code>, <code>p2wsh</code> and <code>p2tr</code>(tapscript).  <code>p2sh</code> and <code>p2wsh</code> is way to send bitcoin to hash of smart contract(<code>ScriptPubKey</code>), which is the hash of <code>redeem script</code>(or <code>witness script</code> if segwit). To spend this, you need to provide both locking script and unlocking script in <code>ScriptSig</code>(or <code>Witness</code> for segwit. For more detail, check <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki" rel="nofollow noreferrer">BIP16</a>).
<code>p2tr</code>(tapscript) is using MAST to lock and unlock bitcoin(For more detail check <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki" rel="nofollow noreferrer">BIP341</a>).</p>
<p>You can just send to bare <code>ScriptPubKey</code> instead of above standards, but it’s definitely not recommended.</p>
<p>Below is an example of HTLC in bitcoin.</p>
<pre><code>OP_IF
    blockheight_or_utc 
    OP_CHECKLOCKTIMEVERIFY
    OP_DROP
    &lt;pubkey_alice&gt;
OP_ELSE
    OP_SHA256
    preimage_hash
    OP_EQUALVERIFY
    &lt;pubkey_bob&gt;
OP_ENDIF
OP_CHECKSIG
</code></pre>

CC BY-SA 4.0
2024-07-04T09:37:56.840Z
Thank you for taking the time to provide such a detailed explanation.
Nerses Asaturyan
CC BY-SA 4.0
2024-07-04T05:08:56.637Z
2024-07-04T05:31:25.117Z
2024-07-04T05:31:25.117Z
Hyunhum Cho
<p>I'm familiar with how Ethereum allows users to lock funds in smart contracts, which can then be released under specific conditions using mechanisms like the Hashed TimeLock Contract (HTLC) protocol. I'm interested in exploring whether similar functionality can be achieved within the Bitcoin protocol.</p>
<p>Locking Funds: How can funds be locked in Bitcoin? Is there a direct way to implement conditions within a Bitcoin transaction itself?</p>
<p>Unlocking Conditions: What mechanisms are available in Bitcoin to unlock these funds based on specific requirements, such as time-based conditions or upon fulfilling certain cryptographic proofs?</p>
<p>Tools and Practices: Are there specific tools or practices within the Bitcoin ecosystem that facilitate this kind of conditional locking and unlocking of funds?</p>
<p>Any insights or guidance on how to approach conditional fund locking in Bitcoin similar to Ethereum's smart contracts would be greatly appreciated.</p>

<blockchain><script><ethereum><bip174-psbt><hash-time-locked-contract>
Implementing Conditional Fund Locking in Bitcoin Similar to Ethereum's Smart Contracts
Nerses Asaturyan
# Implementing Conditional Fund Locking in Bitcoin Similar to Ethereum's Smart Contracts

(Asked by: username_0 on 2024-06-27)

I'm familiar with how Ethereum allows users to lock funds in smart contracts, which can then be released under specific conditions using mechanisms like the Hashed TimeLock Contract (HTLC) protocol. I'm interested in exploring whether similar functionality can be achieved within the Bitcoin protocol.

Locking Funds: How can funds be locked in Bitcoin? Is there a direct way to implement conditions within a Bitcoin transaction itself?

Unlocking Conditions: What mechanisms are available in Bitcoin to unlock these funds based on specific requirements, such as time-based conditions or upon fulfilling certain cryptographic proofs?

Tools and Practices: Are there specific tools or practices within the Bitcoin ecosystem that facilitate this kind of conditional locking and unlocking of funds?

Any insights or guidance on how to approach conditional fund locking in Bitcoin similar to Ethereum's smart contracts would be greatly appreciated.

**Tags:** blockchain, script, ethereum, bip174-psbt, hash-time-locked-contract


---

## 1 answers

### Answer by username_1 on 2024-07-04. Score: 0 | Accepted answer

There's a lot difference between ethereum and bitcoin regarding locking and unlocking fund, but let me roughly compare.

Locking Funds

Unlike ethereum, most transaction of bitcoin itself is an action of locking fund, using smart contract called "Script". You can build ScriptPubKey with "Script", and transaction is an action of sending bitcoin to ScriptPubKey. You could think most transaction of bitcoin is a bit similar to deploying ethereum smart contract which locks ethereum.

Unlocking Conditions

To unlock bitcoin transaction, you have to provide ScriptSig(or Witness for segwit), which has to pass the condition of ScriptPubKey to spend. ScriptSig consists of data to pass the condition of ScriptPubKey. For example, if ScriptPubKey requires signature verifiable with specific public key, signature would be placed into ScriptSig. Unlocking condition can be any of combination implemented with ”Script”, and time-based conditions or fulfilling certain cryptographic proofs can be. Hash Time Lock Contract can be implemented as well.

Tools and Practices

There's a description about ”Script”, or Miniscript is also widely used. Or just search bitcoin library in GitHub with your preferred language.

Any insights or guidance on how to approach conditional fund locking in Bitcoin similar to Ethereum's smart contracts

Most common smart contracts of bitcoin are p2pkh, p2wpkh and p2tr(key-path), which is similar to Ethereum Transfer Transaction without calling smart contract(i.e. just transfer eth by signing tx). It requires the fund owner to sign transaction(and provide public key if not p2tr). However, as mentioned above, most transaction of bitcoin is an action of locking fund, so it means p2pkh, p2wpkh, and p2tr(key-path) are also way of locking fund using smart contract, of which unlocking condition is to provide signature(and public key if not p2tr).
Other smart contracts of bitcoin can be implemented with p2sh, p2wsh and p2tr(tapscript).  p2sh and p2wsh is way to send bitcoin to hash of smart contract(ScriptPubKey), which is the hash of redeem script(or witness script if segwit). To spend this, you need to provide both locking script and unlocking script in ScriptSig(or Witness for segwit. For more detail, check BIP16).
p2tr(tapscript) is using MAST to lock and unlock bitcoin(For more detail check BIP341).
You can just send to bare ScriptPubKey instead of above standards, but it’s definitely not recommended.
Below is an example of HTLC in bitcoin.
OP_IF
    blockheight_or_utc 
    OP_CHECKLOCKTIMEVERIFY
    OP_DROP
    
OP_ELSE
    OP_SHA256
    preimage_hash
    OP_EQUALVERIFY
    
OP_ENDIF
OP_CHECKSIG

> **Comments:**
> **username_0**: Thank you for taking the time to provide such a detailed explanation.  

---

