What is the operational logic?

BTC and Honey Atomic Swap Logic Flow

The atomic swap between BTC and Honey is a cross-chain exchange mechanism implemented using Hash Time Lock Contract (HTLC). The entire process utilizes the transfer of Hash Puzzles and Secret Keys to ensure asset security and seamless exchange between both parties. The process is divided into normal processing flow and exception handling flow, fully considering the interests of both customers and nodes.


Normal Processing Flow

Under normal circumstances, customers and nodes utilize hash puzzles and decryption keys to complete the exchange of BTC and HONEY. The process is as follows:

Customer generates Secret and creates hash:

The user generates a random Secret (decryption key) and calculates its hash value Hash(Secret).

This Hash value will serve as the core verification condition for the entire transaction.

Customer stakes HONEY:

The user locks a certain amount of HONEY to a smart contract or script-controlled address.

At this point, the contract stipulates: the staked HONEY can only be released after the user completes the transaction and withdraws BTC.

Node locks BTC:

After receiving the Hash value submitted by the user, the node creates a BTC lock transaction on the chain.

The transaction conditions are as follows:

  • The customer must submit the correct Secret (matching the Hash) to withdraw BTC;

  • If the customer does not submit the Secret within the specified time, the node can reclaim BTC through the timeout condition (Timelock).

// Puzzle
var puzzle []byte
// Timeout window
var lockTime int64
// Node public key
var nodePubKey []byte

script, err := txscript.NewScriptBuilder().
    AddOp(txscript.OP_IF).                   // Branch condition: customer puzzle solving
    AddOp(txscript.OP_HASH256).              // Hash verification
    AddData(puzzle).                         // Submit puzzle hash value
    AddOp(txscript.OP_EQUAL).                // Check if hash matches
    AddOp(txscript.OP_ELSE).                 // Branch condition: timeout unlock
    AddInt64(int64(lockTime)).               // Timeout value
    AddOp(txscript.OP_CHECKLOCKTIMEVERIFY).  // Check timelock condition
    AddOp(txscript.OP_DROP).                 // Remove timestamp data
    AddOp(txscript.OP_HASH160).              // Hash node public key
    AddData(btcutil.Hash160(nodePubKey)).    // Add node public key hash
    AddOp(txscript.OP_EQUAL).                // Verify public key hash match
    AddOp(txscript.OP_ENDIF).                // End branch condition
    Script()

User Withdraws BTC:

After verifying that the node's locked BTC transaction is correct, the user submits the decryption key Secret to complete the puzzle.

After submitting the Secret, the user can unlock and complete the withdrawal of BTC.

// key
var secret []byte
// pledge script
var script []byte

redeemScript, err := txscript.NewScriptBuilder().
    AddData(secret).       // submit decryption key (Secret)
    AddOp(1).              // select puzzle branch
    AddData(script).       // submit original pledge script
    Script()

Node Obtains Secret and Withdraws Honey:

When the user submits the Secret, the node can also obtain this Secret on-chain.

The node uses the Secret to unlock the user's pledged HONEY and completes the HONEY withdrawal.

Transaction Complete:

The user receives BTC, the node obtains HONEY, both parties complete the asset exchange, and the transaction ends.


Exception Handling Process

If the client fails to withdraw BTC after pledging HONEY, the system will enter an exception handling process to protect the node's interests. This process also relies on the Secret transfer logic:

Timeout Detection:

The system sets a time window (Timelock) when the node creates the BTC locking transaction. If the client fails to submit the Secret to unlock BTC within the specified time, the transaction enters a timeout state.

Node BTC Redemption:

In the timeout state, the node can redeem the locked BTC through the timeout unlock condition.

This operation does not require the client to submit a Secret, thus allowing the node to prioritize protecting its BTC assets when the client is inactive.

// node public key
var nodePubKey []byte
// pledge script
var script []byte

refundScript, err := txscript.NewScriptBuilder().
    AddData(nodePubKey).   // submit node public key
    AddOp(0).              // choose timelock branch
    AddData(script).       // submit original pledge script
    Script()

Pledged Honey Freeze:

If the node successfully redeems the BTC, the client's pledged HONEY will remain in a frozen state.

Only after the node redeems the BTC can the client submit an unlock request to retrieve their pledged HONEY.

Secret Restrictions:

When redeeming their HONEY, the client must meet specific conditions to ensure that the HONEY withdrawal operation does not conflict with the BTC redemption process.

Once the node completes the BTC redemption, the client's authority to redeem HONEY may be subject to time constraints or additional verification to prevent potential disputes due to Secret exposure.

Process Completion:

If the client fails to withdraw BTC in time, the node redeems BTC through timeout, and the HONEY is returned to the client or handled according to contract terms.

Last updated