Complete protocol specification for the trustless swap system.
Pending → WaitingConfirmations → ReadyToClaim → Completed
| |
Cancelled (on expiration)
struct SwapId([u8; 32]); // 32-byte identifier
// L2 → L1: blake3(l1_addr || l1_amount || l2_sender || l2_recipient)
// Deterministic: same parameters = same swap ID
struct Swap {
id: SwapId,
direction: SwapDirection, // L1ToL2 or L2ToL1
parent_chain: ParentChainType, // BTC, BCH, LTC, etc.
l1_txid: TxId,
required_confirmations: u32, // Default: 6 BTC, 3 others
state: SwapState,
l2_recipient: Address,
l2_amount: Amount,
l1_recipient_address: Option<String>,
l1_amount: Option<Amount>,
created_at_height: u32,
expires_at_height: Option<u32>,
}
enum TxData {
SwapCreate {
swap_id: [u8; 32],
parent_chain: ParentChainType,
l1_txid_bytes: Vec<u8>, // [0u8; 32] placeholder for L2→L1
required_confirmations: u32,
l2_recipient: Address,
l2_amount: u64, // satoshis
l1_recipient_address: Option<String>,
l1_amount: Option<u64>,
},
SwapClaim {
swap_id: [u8; 32],
proof_data: Option<Vec<u8>>, // reserved for future
},
}
swaps: SwapId → Swap (primary storage)swaps_by_l1_txid: (ParentChainType, TxId) → SwapId (L1 tx lookup)swaps_by_recipient: Address → Vec<SwapId> (recipient lookup)locked_swap_outputs: OutPoint → SwapId (output lock tracking)l2_amount > 0ReadyToClaimswap.l2_recipientAll non-SwapClaim transactions: no inputs may be locked to any swap. Only SwapClaim can spend locked outputs.
ReadyToClaim stateCompletedReadyToClaimPOST /create_swap
{
"parent_chain": "BTC",
"l1_recipient_address": "bc1q...",
"l1_amount_sats": 100000,
"l2_recipient": "0x...",
"l2_amount_sats": 100000,
"required_confirmations": 3
}
Response: { "swap_id": "abc...", "txid": "def..." }
POST /update_swap_l1_txid
{ "swap_id": "abc...", "l1_txid": "def..." }
Response: { "success": true }
GET /get_swap_status?swap_id=abc...
Response: {
"id": "abc...", "state": "WaitingConfirmations",
"current_confirmations": 2, "required_confirmations": 3,
"l1_txid": "def...", "l2_amount": 100000
}
POST /claim_swap
{ "swap_id": "abc..." }
Response: { "txid": "xyz..." }
GET /list_swaps
Response: [
{ "id": "abc...", "state": "ReadyToClaim", ... },
{ "id": "def...", "state": "Pending", ... }
]