Atomic Swaps: Trustless BTC↔MARS Exchange
How atomic swaps work in Electrum-Mars — the cryptography behind trustless Bitcoin-to-Marscoin trading, the hash time-locked contracts that make it safe, and why this matters for an interplanetary economy.

The Problem with Exchanges
Every cryptocurrency exchange — whether it’s Binance, Coinbase, or the smaller venues that list Marscoin — operates on the same fundamental model: you deposit your money, they hold it, they match your order with someone else’s, and they give you the result. For the duration of that process, your funds are in someone else’s hands. You are trusting a company, its security practices, its solvency, and its willingness to let you withdraw. The history of cryptocurrency is littered with the consequences of that trust: Mt. Gox, FTX, QuadrigaCX, and dozens of smaller failures where depositors lost everything.
For most cryptocurrencies, this is an inconvenience and a risk. For Marscoin, it’s an existential architectural problem. The entire thesis of the project — building financial infrastructure for a Mars settlement operating under a 4 to 24 minute communication delay with Earth — demands that value can be exchanged without depending on a centralized counterparty. You cannot run an exchange on Mars that clears through a server on Earth. You cannot escrow funds with a company that might be 225 million kilometers away when you need to withdraw.
Atomic swaps solve this problem at the protocol level.
What an Atomic Swap Actually Is
An atomic swap is a direct trade between two different cryptocurrencies — in this case, Bitcoin and Marscoin — that either completes fully for both parties or doesn’t happen at all. The word “atomic” comes from the Greek atomos, meaning indivisible: the swap cannot be split into a state where one party has received but the other has not. It’s all or nothing, enforced by mathematics rather than trust.
The technology that makes this possible is a construction from Bitcoin Script called a Hash Time-Locked Contract, or HTLC. An HTLC is a special kind of transaction output that can be spent in exactly two ways: either by providing a cryptographic secret (the “hash lock”), or by waiting until a specific time has passed (the “time lock”). These two paths are embedded directly in the transaction’s script — they are enforced by every miner on the network, not by any trusted party.
The elegance of atomic swaps comes from using the same secret across two different blockchains. When one party reveals the secret to claim their funds on one chain, the other party can observe that reveal and use the same secret to claim their funds on the other chain. The secret binds the two transactions together across the blockchain boundary, creating atomicity without any shared infrastructure.
How It Works in Practice
The atomic swap system is built directly into Electrum-Mars, the desktop Marscoin wallet. There is no separate application, no website to visit, no account to create. The swap tab sits alongside the standard Send and Receive tabs.
The Maker’s Perspective
Imagine you hold Marscoin and want Bitcoin. You open the Atomic Swap tab and create an offer — say, 100 MARS at a price derived from the current market rate. Behind the scenes, your wallet generates a random 32-byte number (the preimage) and computes its HASH160 — a double hash that acts as a fingerprint of the secret without revealing it. Your offer, containing the hash and your public key, is broadcast to every ElectrumX server your wallet is connected to. Other servers gossip it further. Anyone running Electrum-Mars can see it.
When a taker accepts, their wallet sends back their own public key and the address of a Bitcoin HTLC they’ve constructed using your hash. Your wallet verifies this address independently — it recomputes the Bitcoin HTLC from the shared parameters and confirms the address matches, preventing a malicious taker from substituting a different address. Then your wallet locks your MARS into a Marscoin HTLC: a script that says, in essence, “anyone who knows the preimage and has the taker’s private key can claim these coins; alternatively, the maker can reclaim them after 234 Marscoin blocks (approximately eight hours).”
The taker sees your MARS locked on-chain and sends Bitcoin to the matching Bitcoin HTLC. Your wallet, monitoring Bitcoin via the mempool.space API, detects the payment. Once confirmed, it automatically builds a claim transaction that spends the Bitcoin HTLC — and in doing so, reveals the preimage in the Bitcoin transaction’s witness data. Your BTC arrives in whatever Bitcoin wallet address you specified when creating the offer.
The Taker’s Perspective
From the taker’s side, the experience is simpler. You browse offers, find one with an acceptable price, and click Accept. Your wallet generates a keypair for this swap and sends the acceptance to the maker through ElectrumX. A dialog shows you a Bitcoin address and an exact amount. You send BTC from any Bitcoin wallet — Exodus, Sparrow, a hardware wallet, even an exchange withdrawal.
Then you wait. Your wallet watches the Bitcoin blockchain. When the maker claims the BTC, the preimage appears in the transaction witness. Your wallet extracts it, constructs a Marscoin claim transaction using that preimage, and broadcasts it. The MARS arrive in your wallet. The entire claim sequence is automated by the SwapWorker, a background engine that checks both chains every thirty seconds.

The Safety Guarantees
The most important question with any financial system is: what happens when things go wrong? With atomic swaps, the answer is built into the protocol.
What if the maker disappears after I send BTC?
Your Bitcoin is not gone. It is locked in an HTLC that you constructed, using your own public key in the refund path. After 24 Bitcoin blocks — approximately four hours — you can broadcast a refund transaction that returns the BTC to any address you choose. The refund path is encoded in the Bitcoin Script itself. No one can prevent it from becoming available once the timelock expires. The maker cannot take your BTC without revealing the preimage, and if they reveal the preimage, your wallet uses it to claim the MARS. There is no scenario where you lose BTC and don’t receive MARS.
What if the taker never sends BTC?
The maker’s MARS sits in the Marscoin HTLC. After 234 blocks — approximately eight hours — the maker can refund. The asymmetry in timelocks is deliberate: the party who funds last (the taker, with BTC) gets the shorter refund window. This ensures the maker has time to claim the BTC before their own MARS refund becomes available, preventing a race condition where a taker could extract both currencies.
What if my wallet crashes?
All swap state is persisted to a local SQLite database. When you restart Electrum-Mars, the SwapWorker resumes monitoring and continues driving the swap through its state machine. The worst case is that you miss the optimal claim window and end up refunding after the timelock — annoying, but your funds are safe.
Can either party cheat?
No, and this is the fundamental property that distinguishes atomic swaps from every form of escrow, custody, or exchange. The preimage that unlocks one HTLC must unlock the other, because both HTLCs use the same hash. Claiming one side necessarily reveals the secret that claims the other. A party that refuses to claim at all simply triggers the refund path for both sides. There is no game-theoretic strategy that results in one party holding both currencies.
The Auto-Maker
For those who want to provide liquidity without actively managing trades, Electrum-Mars includes an Auto-Maker — a passive market-making engine. When enabled, it fetches the current MARS/BTC rate from a CoinMarketCap price feed, applies a configurable spread (defaulting to 5%), and automatically creates and maintains multiple swap offers. When a taker accepts one, the SwapWorker handles the entire execution automatically.
This means any Marscoin holder can turn their desktop into a decentralized exchange point. The Auto-Maker respects configurable limits — daily volume caps, maximum per-swap amounts, and a reserve percentage to keep a portion of the balance unlocked. It runs as a background process alongside the main wallet, quietly posting offers and executing swaps.
The implications for the Marscoin ecosystem are significant. Liquidity no longer depends on a single exchange listing or a market maker’s willingness to maintain an order book on a third-party platform. It can emerge organically from the community itself, distributed across as many wallets as there are willing participants.
The Technical Foundation
The HTLC script used on both chains is a standard P2WSH (Pay-to-Witness-Script-Hash) output with two spending paths. The claim path requires a valid signature from the recipient plus the preimage whose HASH160 matches the hash embedded in the script. The refund path requires a valid signature from the sender plus a transaction whose nLockTime exceeds the timelock value, enforced by OP_CHECKLOCKTIMEVERIFY. Both paths are encoded in the same script — the blockchain enforces both, and no one can remove either path after the HTLC is funded.
The Bitcoin side uses a timelock of 24 blocks (roughly four hours at ten minutes per block). The Marscoin side uses 234 blocks (roughly eight hours at 123 seconds per block). This four-hour gap is the safety margin: enough time for the maker to detect the BTC, claim it, and for the taker to observe the preimage reveal and claim the MARS — all before the maker’s MARS refund becomes available.
The order book is decentralized through ElectrumX server extensions. Offers propagate through three layers: client push to all connected servers, server-to-server gossip, and client pull on refresh. No single server controls the order book. If one goes down, others continue to relay.
For developers who want to audit every line of the implementation or build a compatible client, the full technical reference on GitHub documents the HTLC construction, the state machine, the Bitcoin address handling (a nontrivial challenge in a Marscoin wallet), the threat model, and every bug discovered and fixed during development.
Getting Started
To use atomic swaps, you need Electrum-Mars version 4.3.2.0+mars or later, available from the GitHub releases page. Open the wallet, navigate to the Atomic Swap tab, and either browse existing offers or create your own. The news article covering the first mainnet swap describes the experience in narrative detail, including the bugs encountered and how they were resolved.
A practical suggestion: start with a small amount. The system works — it has been proven on mainnet with real BTC and real MARS — but it is new software in its first weeks of production use. The refund timelock is your safety net. If anything unexpected happens, your funds return to you after four hours (BTC) or eight hours (MARS). That guarantee is not a promise from a company. It is a property of the Bitcoin Script executed by every miner on the network.
Why This Matters Beyond Earth
The atomic swap represents something larger than a convenient way to trade one cryptocurrency for another. It demonstrates that value can move between independent blockchain networks without any trusted intermediary — a property that becomes not just desirable but necessary for an interplanetary civilization.
On Mars, there will be no Coinbase. There will be no banking system reachable in real-time. The communication delay makes centralized clearing impossible. The governance philosophy of the Martian Republic — democratic, decentralized, code-as-constitution — demands financial infrastructure that operates on the same principles. Atomic swaps are that infrastructure: peer-to-peer, trustless, and sovereign.
The first swap was 104 MARS for a fraction of a Bitcoin, executed between two machines on Earth. But the protocol it proved is the same one that will work between a Mars settlement and an Earth trader, across a 225-million-kilometer gap, with no one in between.
Atomic swaps in Electrum-Mars use hash time-locked contracts on both Bitcoin and Marscoin. No intermediary ever controls user funds. Both parties can always refund after the timelock if the swap doesn’t complete. This is experimental software — the Marscoin Foundation recommends starting with small amounts and maintaining wallet backups. For the complete technical specification, see the GitHub technical reference.