Atomic Swaps Protocol

Overview

This document describes an atomic swap protocol that enables trustless cross-chain cryptocurrency exchanges between Navio and other cryptocurrencies that support similar scripting capabilities (such as Bitcoin and Litecoin). The protocol leverages Navio's unique BLS signature system and shared hash functions to create secure, decentralized exchanges.

Key Features

  • Trustless: No third-party intermediaries required
  • Atomic: Either both parties receive their funds or neither does
  • Cross-chain: Works between Navio and other compatible cryptocurrencies
  • BLS-based: Utilizes Navio's OP_BLSCHECKSIG for enhanced efficiency
  • Hash-locked: Uses shared hash functions

Prerequisites

Supported Cryptocurrencies

The atomic swap protocol works with cryptocurrencies that support:

  1. Time-locked transactions (OP_CHECKLOCKTIMEVERIFY or equivalent)
  2. Hash-locked transactions (OP_HASH160, OP_SHA256, etc.)
  3. Signature verification (OP_CHECKSIG or OP_BLSCHECKSIG)
  4. Conditional execution (OP_IF, OP_ELSE, OP_ENDIF)

Protocol Components

1. Hash Functions

Both chains must support the same hash functions for compatibility.

2. Script Operations

Navio uses OP_BLSCHECKSIG (opcode 0xbb) instead of the traditional OP_CHECKSIG:

// Navio BLS signature verification
OP_BLSCHECKSIG  // (pubkey -- bool)

Standard Operations

Both chains support:

OP_HASH160      // (data -- hash160)
OP_EQUALVERIFY  // (x y -- )
OP_IF           // (bool -- )
OP_ELSE         // ( -- )
OP_ENDIF        // ( -- )
OP_CHECKLOCKTIMEVERIFY  // (locktime -- )
OP_CHECKSIG     // (sig pubkey -- bool) [for other chains]

Protocol Flow

Phase 1: Setup

  1. Alice (Navio holder) and Bob (BTC holder) agree on:
  2. Exchange amount: X NAV for Y BTC
  3. Timeout period: T blocks
  4. Exchange rate and fees

  5. Secret Generation:

  6. Alice generates a random 32-byte secret: secret_A
  7. Alice computes the hash: hash_A = HASH160(secret_A)
  8. Alice shares hash_A with Bob

Phase 2: Contract Creation

Bob's Contract (Bitcoin)

Bob creates a transaction with the following script:

OP_IF
    OP_HASH160 <hash_A> OP_EQUALVERIFY
    <Bob's_pubkey> OP_CHECKSIG
OP_ELSE
    <locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP
    <Alice's_pubkey> OP_CHECKSIG
OP_ENDIF

Alice's Contract (Navio)

Alice creates a transaction with the following script:

OP_IF
    OP_HASH160 <hash_A> OP_EQUALVERIFY
    <Alice's_pubkey> OP_BLSCHECKSIG
OP_ELSE
    <locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP
    <Bob's_pubkey> OP_BLSCHECKSIG
OP_ENDIF

The spending key of this output, must be set to the zero value, as to ensure the spending conditions are exclusively governed by the script.

Phase 3: Fund Deposit

  1. Bob broadcasts his contract transaction on Bitcoin
  2. Alice verifies Bob's transaction and broadcasts her contract transaction on Navio
  3. Both parties wait for their respective transactions to be confirmed

Phase 4: Fund Claim

Option A: Successful Exchange

  1. Alice reveals her secret by spending Bob's output: bitcoin <Alice's_signature> <secret_A> 1

  2. Bob learns secret_A from Alice's transaction and spends Alice's output: navio <Bob's_bls_signature> <secret_A> 1

Option B: Refund (Timeout)

If the exchange doesn't complete within the timeout period:

  1. Alice can refund her Navio by spending her own output: navio <Alice's_bls_signature> <dummy_data> 0

  2. Bob can refund his Bitcoin by spending his own output: bitcoin <Bob's_signature> <dummy_data> 0

Implementation Examples

// Create Navio atomic swap contract
CScript CreateNavioSwapContract(
    const uint160& hash,
    const blsct::PublicKey& alice_pubkey,
    const blsct::PublicKey& bob_pubkey,
    uint32_t locktime
) {
    CScript script;
    script << OP_IF;
    script << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY;
    script << ToByteVector(alice_pubkey.GetVch()) << OP_BLSCHECKSIG;
    script << OP_ELSE;
    script << locktime << OP_CHECKLOCKTIMEVERIFY << OP_DROP;
    script << ToByteVector(bob_pubkey.GetVch()) << OP_BLSCHECKSIG;
    script << OP_ENDIF;
    return script;
}

Bitcoin Contract Creation

// Create Bitcoin atomic swap contract
CScript CreateBitcoinSwapContract(
    const uint160& hash,
    const CPubKey& alice_pubkey,
    const CPubKey& bob_pubkey,
    uint32_t locktime
) {
    CScript script;
    script << OP_IF;
    script << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY;
    script << ToByteVector(alice_pubkey) << OP_CHECKSIG;
    script << OP_ELSE;
    script << locktime << OP_CHECKLOCKTIMEVERIFY << OP_DROP;
    script << ToByteVector(bob_pubkey) << OP_CHECKSIG;
    script << OP_ENDIF;
    return script;
}