Bitcoin–Monero Cross-chain Atomic Swap
Go to file
bors[bot] c0501627c9
Merge #438
438: Add support for swapping through Tor. r=bonomat a=bonomat

Resolves: #447 

This PR does a few things.
* It adds a TorTransport which either dials through Tor's socks5 proxy or via clearnet.
* It enables ASB to register hidden services for each network it is listening on. We assume that we only care about different ports and re-use the same onion-address for all of them. The ASB requires to have access to Tor's control port.
* It adds support to dial through a local Tor socks5 proxy. We assume that Tor is always available on localhost.  Swap cli only requires Tor to be running so that it can send messages via Tor's socks5 proxy.
* It adds a new e2e test which swaps through Tor. For this we assume that Tor is currently running on localhost. All other tests are running via clear net.


Note: It is expected that the new test will fail on CI because we do not have Tor running. That's why it wasn't added yet. I'll play around in my own fork to not use precious CI resources :) 
The rest is ready for review. 


// edit:

Swapping through Tor on one machine seems to be working: 

Asb: 
```
cargo run --bin asb -- --config "/Users/bonomat/Library/Application Support/xmr-btc-swap/asb/config.toml" start
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/asb --config '/Users/bonomat/Library/Application Support/xmr-btc-swap/asb/config.toml' start`
Apr 22 11:14:05.090  INFO Initialized tracing with level: debug
Apr 22 11:14:05.092  INFO Using config file at default path: /Users/bonomat/Library/Application Support/xmr-btc-swap/asb/config.toml
Apr 22 11:14:05.094  INFO Database and Seed will be stored in directory: /Users/bonomat/Library/Application Support/xmr-btc-swap/asb
Apr 22 11:14:05.094 DEBUG Opening database at /Users/bonomat/Library/Application Support/xmr-btc-swap/asb/database
Apr 22 11:14:05.226 DEBUG Reading in seed from /Users/bonomat/Library/Application Support/xmr-btc-swap/asb/seed.pem
Apr 22 11:14:06.903  INFO Tor found. Setting up hidden service. 
Apr 22 11:14:08.805  INFO /onion3/jugnrrdp7imu5tqphvlrctudguq3sapmjideu6d63i5y7qqtzzwvf4id:9939
Apr 22 11:14:08.805  INFO /onion3/jugnrrdp7imu5tqphvlrctudguq3sapmjideu6d63i5y7qqtzzwvf4id:9940
Apr 22 11:14:13.431 DEBUG Opened Monero wallet asb-wallet
Apr 22 11:14:13.433  INFO Bitcoin balance: 0.00000000 BTC
Apr 22 11:14:13.433  WARN The Monero balance is 0, make sure to deposit funds at: 5A3iBfDbhGfUUL5WKFcdY5JK7oLcMXYmD9VnUjxmBgFHXswtENMjFsHUDeeCWVvRYaNRCAJDRS7jY85iyNt7s3syVNJtwLd
Apr 22 11:14:13.436 DEBUG Trying to listen on: /ip4/0.0.0.0/tcp/9939
Apr 22 11:14:13.437 DEBUG Trying to listen on: /ip4/0.0.0.0/tcp/9940
Apr 22 11:14:13.437  INFO Our peer id is 12D3KooWNqWpoLUp6YSoUYTinwJFh92wzggeanLgSutmZbZYG9cN
Apr 22 11:14:13.438  INFO Listening on /ip4/127.0.0.1/tcp/9940/ws
Apr 22 11:14:13.438  INFO Listening on /ip4/127.0.0.1/tcp/9939
Apr 22 11:14:13.438  INFO Listening on /ip4/192.168.1.63/tcp/9940/ws
Apr 22 11:14:13.438  INFO Listening on /ip4/192.168.1.63/tcp/9939
Apr 22 11:14:14.275 DEBUG Connected to Kraken websocket API
Apr 22 11:14:14.462 DEBUG Subscribed to updates for ticker
Apr 22 11:23:11.833 DEBUG New connection established peer=12D3KooWHBMKm8Wnq4WD7ehVdSHaf4ccjdzR97UTFtMGT6LBmRev address=/ip4/127.0.0.1/tcp/63655
Apr 22 11:23:25.700  INFO swap{id=c4f51a28-b2fb-4885-9f7a-5d1852de5f0d}: Current state: started
Apr 22 11:23:25.700  INFO swap{id=c4f51a28-b2fb-4885-9f7a-5d1852de5f0d}: Waiting for 1 confirmation of Bitcoin transaction txid=537070d7a6f75e74caa65cebbbf0997b50914fd86b7a752f0d74eb0c5701291f
Apr 22 11:23:33.732 DEBUG Transaction is in mempool txid=537070d7a6f75e74caa65cebbbf0997b50914fd86b7a752f0d74eb0c5701291f
Apr 22 11:42:49.668 DEBUG Transaction is confirmed with 1 blocks txid=537070d7a6f75e74caa65cebbbf0997b50914fd86b7a752f0d74eb0c5701291f
Apr 22 11:42:49.671  INFO swap{id=c4f51a28-b2fb-4885-9f7a-5d1852de5f0d}: Bitcoin tx has 1 out of 1 confirmation txid=537070d7a6f75e74caa65cebbbf0997b50914fd86b7a752f0d74eb0c5701291f
Apr 22 11:42:49.703  INFO swap{id=c4f51a28-b2fb-4885-9f7a-5d1852de5f0d}: Current state: btc is locked
Apr 22 11:43:02.670 DEBUG swap{id=c4f51a28-b2fb-4885-9f7a-5d1852de5f0d}: sent transfer of 0.695214974374 XMR to 05d77c136ad84eff1184a14971f8ec7f5884bb25b646a6c1eb95e0c26782f0a5 in c00f4543d9c9831fa892a2180b4dabbc33eff1fcd6f58ac24495dcf1450bbab3
Apr 22 11:43:02.740  INFO swap{id=c4f51a28-b2fb-4885-9f7a-5d1852de5f0d}: Current state: xmr lock transaction sent

...
```

Swap:
```
 cargo run --bin swap buy-xmr --seller-peer-id 12D3KooWNqWpoLUp6YSoUYTinwJFh92wzggeanLgSutmZbZYG9cN --seller-addr /onion3/jugnrrdp7imu5tqphvlrctudguq3sapmjideu6d63i5y7qqtzzwvf4id:9939 --receive-address 5A3iBfDbhGfUUL5WKFcdY5JK7oLcMXYmD9VnUjxmBgFHXswtENMjFsHUDeeCWVvRYaNRCAJDRS7jY85iyNt7s3syVNJtwLd 
    Finished dev [unoptimized + debuginfo] target(s) in 0.37s
     Running `target/debug/swap buy-xmr --seller-peer-id 12D3KooWNqWpoLUp6YSoUYTinwJFh92wzggeanLgSutmZbZYG9cN --seller-addr '/onion3/jugnrrdp7imu5tqphvlrctudguq3sapmjideu6d63i5y7qqtzzwvf4id:9939' --receive-address 5A3iBfDbhGfUUL5WKFcdY5JK7oLcMXYmD9VnUjxmBgFHXswtENMjFsHUDeeCWVvRYaNRCAJDRS7jY85iyNt7s3syVNJtwLd`
 Connecting to Tor proxy ...
 Connection established
 Connected to Alice at /onion3/jugnrrdp7imu5tqphvlrctudguq3sapmjideu6d63i5y7qqtzzwvf4id:9939/p2p/12D3KooWNqWpoLUp6YSoUYTinwJFh92wzggeanLgSutmZbZYG9cN
 Received quote: 1 XMR ~ 0.00719202 BTC
 Found 0.01559763 BTC in wallet
 Swapping 0.00500000 BTC with 0.00000610 BTC fees
 Spot price for 0.00500000 BTC is 0.695214974374 XMR
 Published Bitcoin lock transaction txid=537070d7a6f75e74caa65cebbbf0997b50914fd86b7a752f0d74eb0c5701291f
 Waiting for Alice to lock Monero
 Alice locked Monero txid=c00f4543d9c9831fa892a2180b4dabbc33eff1fcd6f58ac24495dcf1450bbab3
 Waiting for 10 confirmations of Monero transaction txid=c00f4543d9c9831fa892a2180b4dabbc33eff1fcd6f58ac24495dcf1450bbab3
 Monero lock tx has 1 out of 10 confirmations txid=c00f4543d9c9831fa892a2180b4dabbc33eff1fcd6f58ac24495dcf1450bbab3
 Monero lock tx has 2 out of 10 confirmations txid=c00f4543d9c9831fa892a2180b4dabbc33eff1fcd6f58ac24495dcf1450bbab3
 Monero lock tx has 3 out of 10 confirmations txid=c00f4543d9c9831fa892a2180b4dabbc33eff1fcd6f58ac24495dcf1450bbab3
...

```

Co-authored-by: Philipp Hoenisch <philipp@hoenisch.at>
Co-authored-by: Philipp Hoenisch <philipp@coblox.tech>
2021-04-29 05:09:23 +00:00
.cargo .cargo/config.toml for specifying linker 2021-03-15 14:36:05 +11:00
.github Always display log output in CI 2021-04-26 18:12:58 +10:00
docs/asb use '--seller-peer-id' 2021-04-21 10:23:18 +02:00
monero-harness Fix messages for awaiting containers ready 2021-04-28 11:44:55 +10:00
monero-rpc Initialize reqwest clients with verbose logging 2021-04-26 18:12:56 +10:00
swap Simplify to_address_string by implying more context on the structure. 2021-04-28 16:21:31 +10:00
tokio-tar Enforce consistent formatting of Markdown files 2021-03-25 11:11:57 +11:00
.dprintrc.json Enforce consistent formatting of Markdown files 2021-03-25 11:11:57 +11:00
.gitignore Push cargo lock to git as we are building a binary 2020-12-01 09:31:41 +11:00
bors.toml Multiple swaps with the same peer 2021-04-13 18:16:19 +10:00
Cargo.lock Derive Tor secret key from seed. 2021-04-27 16:39:32 +10:00
Cargo.toml Optimize torut features. 2021-04-27 16:39:32 +10:00
CHANGELOG.md Apply suggestions from code review 2021-04-28 16:04:25 +10:00
LICENSE Change license to GPLv3 2020-10-12 17:13:25 +11:00
README.md dnsaddr instead of dns4 2021-04-19 22:48:08 +10:00
rust-toolchain Add asb to release including ARM build 2021-03-15 09:58:18 +11:00
rustfmt.toml Change imports_granularity to module 2021-03-04 14:48:13 +11:00

XMR to BTC Atomic Swap

This repository hosts an MVP for atomically swapping BTC to XMR. It implements the protocol described in section 3 of this paper.

More information about the protocol in this presentation and this blog post.

Quick start - CLI

  1. Download the latest swap binary release for your operating system
  2. Run the binary specifying the monero address where you wish to receive monero and the connection details of the seller: ./swap buy-xmr --receive-address <YOUR MONERO ADDRESS> --seller-peer-id <SELLERS PEER ID> --seller-addr <SELLERS MULTIADDRESS> You can generate a receive address using your monero wallet. The seller will provide you their peer id and multiaddress. We are running an asb instance on testnet. You can swap with to get familiar with the swap CLI. Our peer id is 12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi and our multiaddress is /dnsaddr/xmr-btc-asb.coblox.tech
  3. Follow the instructions printed to the terminal

Limitations

For now, the MVP is limited to testnet3 on Bitcoin and stagenet on Monero.

How it works

This repository primarily hosts two components:

swap CLI

The swap CLI acts in the role of Bob and swaps BTC for XMR. See ./swap --help for a description of all commands. The main command is buy-xmr which automatically connects to an instance of asb.

asb service

asb is short for automated swap backend (we are open to suggestions for better names!). The service acts as the counter-party for the swap CLI in the role of Alice. It provides the CLI with a quote and the liquidity necessary for swapping BTC into XMR.

For details on how to run the ASB please refer to the ASB docs.

Contact

Feel free to reach out to us in the COMIT-Monero Matrix channel.