Escrow
Escrow Smart Contract
Code: https://github.com/socious-io/midnight-escrow/
Purpose: Facilitates secure transactions by holding funds in a contract-controlled treasury until specific conditions are met.
Technical Specifications
State Machine:
active: Funds are locked and ready for release.released: Funds have been sent to the beneficiary.refunded: Funds returned to the depositor (lifecycle state defined, logic implementation pending in snippet).
Data Structure:
Functions
create(contributor, coin)Action: Ingests a user's coin (
receive), locks it in the contract'streasury(writeCoin), and generates a unique Escrow ID.Privacy Note: The
amountis disclosed publicly (disclose(coin.value)), but the transaction graph remains shielded via Z-swap.
release(id)Action: Checks if the escrow is
active. If yes, transfers funds to thecontributoraddress.Change Management: Automatically calculates UTXO change. If the treasury coin is larger than the escrow amount, the remainder is written back to the treasury.
Developer Tutorials
Tutorial 1: Deploying the Escrow Contract
Prerequisite: A Midnight wallet (e.g., Lace) with tDUST for fees.
Step-by-Step Guide:
Setup Environment: Ensure you are running the
Compactcompiler version 0.16–0.18.Deploy Contract: Run the deployment script. The contract constructor will initialize
last_escrow_idto 0.Witness Functions:
Warning: The current implementation requires careful handling of the
releasewitness function. You must ensurequeryContext.insertCommitment(commitment, index)is called to allow the runtime to locate the coin in the Merkle tree.
Tutorial 2: Creating an Escrow (Deposit)
Code Reference: export circuit create(...)
Prepare Coin Info: Select a coin from your wallet. You must pass the
CoinInfostruct, including the nonce and value.Call Circuit:
Verify: The contract will return a
Uint<32>ID. Store this ID; you will need it to release funds.
Tutorial 3: Releasing Funds
Code Reference: export circuit release(...)
Input: Provide the
Escrow ID.Execution: The contract verifies the state is
active.Output: The recipient receives the funds. Any "change" from the input coin is securely returned to the contract treasury.