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:
- Time-locked transactions (OP_CHECKLOCKTIMEVERIFY or equivalent)
- Hash-locked transactions (OP_HASH160, OP_SHA256, etc.)
- Signature verification (OP_CHECKSIG or OP_BLSCHECKSIG)
- 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-Specific 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
- Alice (Navio holder) and Bob (BTC holder) agree on:
- Exchange amount:
X NAV
forY BTC
- Timeout period:
T
blocks -
Exchange rate and fees
-
Secret Generation:
- Alice generates a random 32-byte secret:
secret_A
- Alice computes the hash:
hash_A = HASH160(secret_A)
- 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
- Bob broadcasts his contract transaction on Bitcoin
- Alice verifies Bob's transaction and broadcasts her contract transaction on Navio
- Both parties wait for their respective transactions to be confirmed
Phase 4: Fund Claim
Option A: Successful Exchange
-
Alice reveals her secret by spending Bob's output:
bitcoin <Alice's_signature> <secret_A> 1
-
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:
-
Alice can refund her Navio by spending her own output:
navio <Alice's_bls_signature> <dummy_data> 0
-
Bob can refund his Bitcoin by spending his own output:
bitcoin <Bob's_signature> <dummy_data> 0
Implementation Examples
Navio Contract Creation
// 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;
}