mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Bitcoin–Monero Cross-chain Atomic Swap
5fe8f3108a
161: Introduce configuration file and initial setup through CLI r=da-kami a=da-kami PRs chained on top of this one: - [x] https://github.com/comit-network/xmr-btc-swap/pull/163 Configurable config with `--config` option on CLI (we will need this for swaps with ourselves...) - [x] ~~https://github.com/comit-network/xmr-btc-swap/pull/165 Reset config with `--reset-config` option on CLI (this makes it easier for the user to clean up)~~ - [x] https://github.com/comit-network/xmr-btc-swap/pull/166 data-dir, in accordance to config-dir, should be pointed to the appropriate default directory using `directory-next` Looks n' feel: Initial startup: ``` 2021-01-28T01:35:45.000205Z INFO swap::trace: Initialized tracing with level: INFO 2021-01-28T01:35:45.001286Z INFO swap: Database and Seed will be stored in directory: /Users/dakami/CoBloX/TEMP/swap-data-dir/alice 2021-01-28T01:35:45.003391Z INFO swap::config::seed: Read in seed from file: /Users/dakami/CoBloX/TEMP/swap-data-dir/alice/seed.pem 2021-01-28T01:35:45.003603Z INFO swap::config: Config file not found, running initial setup... ? Enter Bitcoind URL (including username and password if applicable) or hit return to use default (http://127.0.0.1:18332) › http://dakami:xkz4nyywpKf3BigwKIRdVijmWzaHCOmUisepQpDlsXnhpNd6uO@127.0.0.1:18332/ ✔ Enter Bitcoind URL (including username and password if applicable) or hit return to use default · http://dakami:xkz4nyywpKf3BigwKIRdVijmWzaHCOmUisepQpDlsXnhpNd6uO@127.0.0.1:18332/ ? Enter Bitcoind wallet name › alice ✔ Enter Bitcoind wallet name · alice ? Enter Monero Wallet RPC URL or hit enter to use default (http://127.0.0.1:38083/json_rpc) › ✔ Enter Monero Wallet RPC URL or hit enter to use default · http://127.0.0.1:38083/json_rpc 2021-01-28T01:35:58.553401Z INFO swap::config: Initial setup complete, config file created at /Users/dakami/Library/Application Support/xmr-btc-swap/config.toml 2021-01-28T01:35:58.647761Z INFO swap: Connection to Bitcoin wallet succeeded, balance: 0.00744521 BTC 2021-01-28T01:35:58.650060Z INFO swap: Connection to Monero wallet succeeded, balance: 29.095359550000 XMR 2021-01-28T01:35:58.650258Z INFO swap: Swap sending 0.030000000000 XMR and receiving 0.00060000 BTC started with ID e07b2cc1-3749-48fd-931a-6cbaf57b8124 2021-01-28T01:35:59.004306Z INFO swap::protocol::alice::swap: Current state:started ``` After: ``` 2021-01-28T01:36:57.881654Z INFO swap::trace: Initialized tracing with level: INFO 2021-01-28T01:36:57.882691Z INFO swap: Database and Seed will be stored in directory: /Users/dakami/CoBloX/TEMP/swap-data-dir/alice 2021-01-28T01:36:57.884171Z INFO swap::config::seed: Read in seed from file: /Users/dakami/CoBloX/TEMP/swap-data-dir/alice/seed.pem 2021-01-28T01:36:57.884353Z INFO swap::config: Using config file at default path: /Users/dakami/Library/Application Support/xmr-btc-swap/config.toml 2021-01-28T01:36:57.996153Z INFO swap: Connection to Bitcoin wallet succeeded, balance: 0.00744521 BTC 2021-01-28T01:36:57.998648Z INFO swap: Connection to Monero wallet succeeded, balance: 29.095359550000 XMR 2021-01-28T01:36:57.998928Z INFO swap: Swap sending 0.030000000000 XMR and receiving 0.00060000 BTC started with ID 08dd6dc1-9460-4c0a-91ef-a05df309b6ed 2021-01-28T01:36:58.353738Z INFO swap::protocol::alice::swap: Current state:started ``` Command: ``` run --package swap --bin swap -- --data-dir /Users/dakami/CoBloX/TEMP/swap-data-dir/alice sell-xmr --receive-btc 0.0006 --send-xmr 0.03 ``` Co-authored-by: Daniel Karzel <daniel@comit.network> Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech> |
||
---|---|---|
.github/workflows | ||
docs | ||
monero-harness | ||
swap | ||
.gitignore | ||
.mergify.yml | ||
bors.toml | ||
BTC_XMR_atomic_swap_protocol.svg | ||
Cargo.lock | ||
Cargo.toml | ||
LICENSE | ||
README.md | ||
rust-toolchain | ||
rustfmt.toml |
XMR to BTC Atomic Swap
This repository is a proof of concept for atomically swapping XMR for BTC.
We define:
- Alice to be the actor that initially holds XMR.
- Bob to be the actor that initially holds BTC.
In the best-case scenario the protocol looks like this:
- Alice and Bob exchange a set of addresses, keys, zero-knowledge proofs and signatures.
- Bob publishes
Tx_lock
, locking up his bitcoin in a 2-of-2 multisig output owned by Alice and Bob. Given the information exchanged in step 1, Bob can refund his bitcoin if he waits until timet_1
by usingTx_cancel
andTx_refund
. If Bob doesn't refund after timet_1
, Alice can punish Bob for being inactive by first publishingTx_cancel
and, aftert_2
, spending the output usingTx_punish
. - Alice sees that Bob has locked up the bitcoin, so she publishes
Tx_lock
on the Monero blockchain, locking up her monero in an output which can only be spent with a secret key owned by Alice (s_a
) and a secret key owned by Bob (s_b
). This means that neither of them can actually spend this output unless they learn the secret key of the other party. - Bob sees that Alice has locked up the monero, so he now sends Alice a missing key bit of information which will allow Alice to redeem the bitcoin using
Tx_redeem
. - Alice uses this information to spend the bitcoin to an address owned by her.
When doing so she leaks her Monero secret key
s_a
to Bob through the magic of adaptor signatures. - Bob sees Alice's
Tx_redeem
on Bitcoin, extracts Alice's secret key from it and combines it with his own to spend the monero to an address of his own.
The repository is structured as a library and a single test function that executes the swap. The library has the following modules:
alice
: Defines the state machine that describes the swap for Alice. This includes the messages sent to/from Alice.bob
: Defines the state machine that describes the swap for Bob. This includes the messages sent to/from Bob.bitcoin
: Keys, signing functions, transactions etc. for Bitcoin. Also includes a test wallet (see below).monero
: Keys, signing functions, transactions etc. for Monero. Also includes a test wallet (see below).
Currently we have a single test function that proves the following:
- Interaction with both block chains and their respective wallets works.
- The messages required are correct and can manually drive the state transitions to execute a swap.
- It is possible to interact with, and watch, the monero blockchain using
monero-wallet-rpc
. - It is possible to watch a bitcoind instance using
bitcoin-harness
(we already knew this :)
Currently we do not do:
- Actual network communication.
- Verification that the UI is acceptable. Since we do everything in a single test function there is no user interaction, this is unrealistic for a real product.
Testing
We wrote a few additional libraries to facilitate testing:
Wallets
bitcoin
module contains a test wallet by way ofbitcoind
.monero
: module contains a test wallet by way ofmonero-wallet-rpc
.
Blockchain harnesses
We have written two harnesses for interacting with bitcoin and monero.
These harnesses wrap interaction with bitcoind
and monerod
/monero-wallet-rpc
.
We use testcontainers-rs to spin up bitcoind
, monerod
, and monero-wallet-rpc
in docker containers during unit/integration testing.