# 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 (<mark style="color:orange;">`HTLC`</mark>). The entire process utilizes the transfer of Hash Puzzles and <mark style="color:orange;">`Secret`</mark> 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**

<figure><img src="https://3164221739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqmz8b1Xn8Ts4iju0Z5Ox%2Fuploads%2F7JpCeOrZqcCIqklNnQau%2Fimage.png?alt=media&#x26;token=f87e91aa-0356-4b9b-bcd5-afb0e2aff96f" alt=""><figcaption></figcaption></figure>

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:&#x20;

### Customer generates Secret and creates hash:

The user generates a random <mark style="color:orange;">`Secret`</mark> (decryption key) and calculates its hash value Hash(Secret).&#x20;

This Hash value will serve as the core verification condition for the entire transaction.&#x20;

### Customer stakes HONEY:&#x20;

The user locks a certain amount of **HONEY** to a smart contract or script-controlled address.&#x20;

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

### Node locks BTC:&#x20;

After receiving the Hash value submitted by the user, the node creates a **BTC** lock transaction on the chain.&#x20;

The transaction conditions are as follows:&#x20;

* The customer must submit the correct <mark style="color:orange;">`Secret`</mark> (matching the Hash) to withdraw **BTC**;&#x20;
* If the customer does not submit the Secret within the specified time, the node can reclaim **BTC** through the timeout condition (Timelock).

```go
// 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:&#x20;

After verifying that the node's locked BTC transaction is correct, the user submits the decryption key <mark style="color:orange;">`Secret`</mark> to complete the puzzle.&#x20;

After submitting the <mark style="color:orange;">`Secret`</mark>, the user can unlock and complete the withdrawal of **BTC**.

{% code overflow="wrap" %}

```go
// 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()
```

{% endcode %}

### Node Obtains Secret and Withdraws Honey:&#x20;

When the user submits the <mark style="color:orange;">`Secret,`</mark> the node can also obtain this <mark style="color:orange;">`Secret`</mark> on-chain.&#x20;

The node uses the <mark style="color:orange;">`Secret`</mark> to unlock the user's pledged **HONEY** and completes the **HONEY** withdrawal.

### Transaction Complete:&#x20;

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

***

## **Exception Handling Process**

<figure><img src="https://3164221739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqmz8b1Xn8Ts4iju0Z5Ox%2Fuploads%2Ff0LhAHnd9OTnFsRBSnNL%2Fimage.png?alt=media&#x26;token=a950b8f5-bfae-4f9d-bf4f-a7c4c77c3cc0" alt=""><figcaption></figcaption></figure>

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 <mark style="color:orange;">`Secret`</mark> transfer logic:&#x20;

### Timeout Detection:&#x20;

The system sets a time window (Timelock) when the node creates the **BTC** locking transaction. If the client fails to submit the <mark style="color:orange;">`Secret`</mark> to unlock **BTC** within the specified time, the transaction enters a timeout state.&#x20;

### Node BTC Redemption:&#x20;

In the timeout state, the node can redeem the locked **BTC** through the timeout unlock condition.&#x20;

This operation does not require the client to submit a <mark style="color:orange;">`Secret`</mark>, thus allowing the node to prioritize protecting its **BTC** assets when the client is inactive.

{% code overflow="wrap" %}

```go
// 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()
```

{% endcode %}

### Pledged Honey Freeze:&#x20;

If the node successfully redeems the **BTC**, the client's pledged **HONEY** will remain in a frozen state.&#x20;

Only after the node redeems the **BTC** can the client submit an unlock request to retrieve their pledged **HONEY**.&#x20;

### Secret Restrictions:&#x20;

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.&#x20;

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 <mark style="color:orange;">`Secret`</mark> exposure.&#x20;

### Process Completion:&#x20;

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.
