Compare commits

..

No commits in common. 'master' and '0.11.0' have entirely different histories.

@ -6,9 +6,8 @@ on:
jobs:
build_binaries:
name: Build
name: Build swap and asb binaries
strategy:
fail-fast: false
matrix:
include:
- bin: swap
@ -21,10 +20,6 @@ jobs:
archive_ext: tar
- bin: swap
target: x86_64-apple-darwin
os: macos-12
archive_ext: tar
- bin: swap
target: aarch64-apple-darwin
os: macos-latest
archive_ext: tar
- bin: swap
@ -41,10 +36,6 @@ jobs:
archive_ext: tar
- bin: asb
target: x86_64-apple-darwin
os: macos-12
archive_ext: tar
- bin: asb
target: aarch64-apple-darwin
os: macos-latest
archive_ext: tar
- bin: asb
@ -54,33 +45,32 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout tagged commit
uses: actions/checkout@v4.1.6
uses: actions/checkout@v3.0.2
with:
ref: ${{ github.event.release.target_commitish }}
token: ${{ secrets.BOTTY_GITHUB_TOKEN }}
- uses: Swatinem/rust-cache@v2.7.3
- uses: Swatinem/rust-cache@v2.0.0
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.74"
- name: Cross Build ${{ matrix.target }} ${{ matrix.bin }} binary
- name: Install compiler for armhf arch
if: matrix.target == 'armv7-unknown-linux-gnueabihf'
run: |
curl -L "https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-x86_64-unknown-linux-gnu.tar.gz" | tar xzv
sudo mv cross /usr/bin
sudo mv cross-util /usr/bin
cross build --target=${{ matrix.target }} --release --package swap --bin ${{ matrix.bin }}
sudo apt-get update
sudo apt-get install gcc-arm-linux-gnueabihf
- name: Build ${{ matrix.target }} ${{ matrix.bin }} release binary
if: matrix.target != 'armv7-unknown-linux-gnueabihf'
run: cargo build --target=${{ matrix.target }} --release --package swap --bin ${{ matrix.bin }}
- name: Smoke test the binary
if: matrix.target != 'armv7-unknown-linux-gnueabihf'
if: matrix.target != 'armv7-unknown-linux-gnueabihf' # armv7-unknown-linux-gnueabihf is only cross-compiled, no smoke test
run: target/${{ matrix.target }}/release/${{ matrix.bin }} --help
# Remove once python 3 is the default
- uses: actions/setup-python@v4
with:
python-version: "3.x"
- id: create-archive-name
shell: python # Use python to have a prettier name for the archive on Windows.
run: |
@ -97,7 +87,7 @@ jobs:
print(f'::set-output name=archive::{archive_name}')
- name: Pack macos archive
if: startsWith(matrix.os, 'macos')
if: matrix.os == 'macos-latest'
shell: bash
run: gtar -C ./target/${{ matrix.target }}/release --create --file=${{ steps.create-archive-name.outputs.archive }} ${{ matrix.bin }}

@ -4,6 +4,8 @@ on:
pull_request: # Need to run on pull-requests, otherwise PRs from forks don't run
push:
branches:
- "staging" # Bors uses this branch
- "trying" # Bors uses this branch
- "master" # Always build head of master for the badge in the README
jobs:
@ -11,19 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
uses: actions/checkout@v3.0.2
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.74"
components: clippy,rustfmt
- uses: Swatinem/rust-cache@v2.7.3
- uses: Swatinem/rust-cache@v2.0.0
- name: Check formatting
uses: dprint/check@v2.2
with:
dprint-version: 0.39.1
uses: dprint/check@v2.1
- name: Run clippy with default features
run: cargo clippy --workspace --all-targets -- -D warnings
@ -31,36 +26,6 @@ jobs:
- name: Run clippy with all features enabled
run: cargo clippy --workspace --all-targets --all-features -- -D warnings
bdk_test:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
- uses: Swatinem/rust-cache@v2.7.3
- name: Build swap
run: cargo build --bin swap
- name: Run BDK regression script
run: ./swap/tests/bdk.sh
sqlx_test:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
- uses: Swatinem/rust-cache@v2.7.3
- name: Install sqlx-cli
run: cargo install --locked --version 0.6.3 sqlx-cli
- name: Run sqlite_dev_setup.sh script
run: |
cd swap
./sqlite_dev_setup.sh
build:
strategy:
matrix:
@ -70,43 +35,34 @@ jobs:
- target: armv7-unknown-linux-gnueabihf
os: ubuntu-latest
- target: x86_64-apple-darwin
os: macos-12
- target: aarch64-apple-darwin
os: macos-latest
- target: x86_64-pc-windows-msvc
os: windows-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
- uses: Swatinem/rust-cache@v2.7.3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.74"
targets: armv7-unknown-linux-gnueabihf
uses: actions/checkout@v3.0.2
- name: Build binary
if: matrix.target != 'armv7-unknown-linux-gnueabihf'
run: cargo build -p swap --target ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2.0.0
- name: Install cross (armv7)
- name: Install compiler for armhf arch
if: matrix.target == 'armv7-unknown-linux-gnueabihf'
run: cargo install cross --locked
run: |
sudo apt-get update
sudo apt-get install gcc-arm-linux-gnueabihf
- name: Build binary (armv7)
if: matrix.target == 'armv7-unknown-linux-gnueabihf'
run: cross build -p swap --target ${{ matrix.target }}
- name: Build binary
run: |
cargo build -p swap --target ${{ matrix.target }}
- name: Upload swap binary
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v2-preview
with:
name: swap-${{ matrix.target }}
path: target/${{ matrix.target }}/debug/swap
- name: Upload asb binary
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v2-preview
with:
name: asb-${{ matrix.target }}
path: target/${{ matrix.target }}/debug/asb
@ -117,23 +73,10 @@ jobs:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: (Free disk space on Ubuntu)
if: matrix.os == 'ubuntu-latest'
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be
with:
# removing all of these takes ~10 mins, so just do as needed
android: true
dotnet: true
haskell: true
docker-images: false
large-packages: false
swap-storage: false
tool-cache: false
- name: Checkout sources
uses: actions/checkout@v4.1.6
uses: actions/checkout@v3.0.2
- uses: Swatinem/rust-cache@v2.7.3
- uses: Swatinem/rust-cache@v2.0.0
- name: Build tests
run: cargo build --tests --workspace --all-features
@ -155,7 +98,6 @@ jobs:
happy_path_restart_bob_before_xmr_locked,
happy_path_restart_alice_after_xmr_locked,
alice_and_bob_refund_using_cancel_and_refund_command,
alice_and_bob_refund_using_cancel_then_refund_command,
alice_and_bob_refund_using_cancel_and_refund_command_timelock_not_expired,
punish,
alice_punishes_after_restart_bob_dead,
@ -168,33 +110,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
uses: actions/checkout@v3.0.2
- uses: Swatinem/rust-cache@v2.7.3
- uses: Swatinem/rust-cache@v2.0.0
- name: Run test ${{ matrix.test_name }}
run: cargo test --package swap --all-features --test ${{ matrix.test_name }} -- --nocapture
rpc_tests:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
- uses: Swatinem/rust-cache@v2.7.3
- name: Run RPC server tests
run: cargo test --package swap --all-features --test rpc -- --nocapture
check_stable:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.6
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2.7.3
- name: Run cargo check on stable rust
run: cargo check --all-targets

@ -11,7 +11,7 @@ jobs:
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.6
- uses: actions/checkout@v3.0.2
- name: Extract version from branch name
id: extract-version

@ -12,7 +12,7 @@ jobs:
name: "Draft a new release"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.6
- uses: actions/checkout@v3.0.2
with:
token: ${{ secrets.BOTTY_GITHUB_TOKEN }}
@ -20,7 +20,7 @@ jobs:
run: git checkout -b release/${{ github.event.inputs.version }}
- name: Update changelog
uses: thomaseizinger/keep-a-changelog-new-release@3.0.0
uses: thomaseizinger/keep-a-changelog-new-release@1.3.0
with:
version: ${{ github.event.inputs.version }}
changelogPath: CHANGELOG.md
@ -41,12 +41,8 @@ jobs:
- name: Commit changelog and manifest files
id: make-commit
env:
DPRINT_VERSION: "0.39.1"
RUST_TOOLCHAIN: "1.74"
run: |
rustup component add rustfmt --toolchain "$RUST_TOOLCHAIN-x86_64-unknown-linux-gnu"
curl -fsSL https://dprint.dev/install.sh | sh -s $DPRINT_VERSION
curl -fsSL https://dprint.dev/install.sh | sh
/home/runner/.dprint/bin/dprint fmt
git add CHANGELOG.md Cargo.lock swap/Cargo.toml
@ -58,7 +54,7 @@ jobs:
run: git push origin release/${{ github.event.inputs.version }} --force
- name: Create pull request
uses: thomaseizinger/create-pull-request@1.4.0
uses: thomaseizinger/create-pull-request@1.2.2
with:
GITHUB_TOKEN: ${{ secrets.BOTTY_GITHUB_TOKEN }}
head: release/${{ github.event.inputs.version }}

@ -10,7 +10,7 @@ jobs:
name: Create preview release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.6
- uses: actions/checkout@v3.0.2
- name: Delete 'preview' release
uses: larryjoelane/delete-release-action@v1.0.24

3
.gitignore vendored

@ -159,5 +159,4 @@ flycheck_*.el
# End of https://www.toptal.com/developers/gitignore/api/rust,clion+all,emacs
*.log
.vscode
swap/tempdb
.vscode

@ -7,42 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.13.0] - 2024-05-29
- Minimum Supported Rust Version (MSRV) bumped to 1.74
- Lowered default Bitcoin confirmation target for Bob to 1 to make sure Bitcoin transactions get confirmed in time
- Added support for starting the CLI (using the `start-daemon` subcommand) as a Daemon that accepts JSON-RPC requests
- Update monero-wallet-rpc version to v0.18.3.1
## [0.12.3] - 2023-09-20
- Swap: If no Monero daemon is manually specified, we will automatically choose one from a list of public daemons by connecting to each and checking their availability.
## [0.12.2] - 2023-08-08
### Changed
- Minimum Supported Rust Version (MSRV) bumped to 1.67
- ASB can now register with multiple rendezvous nodes. The `rendezvous_point` option in `config.toml` can be a string with comma separated addresses, or a toml array of address strings.
## [0.12.1] - 2023-01-09
### Changed
- Swap: merge separate cancel/refund commands into one `cancel-and-refund` command for stuck swaps
## [0.12.0] - 2022-12-31
### Changed
- Update `bdk` library to latest version. This introduces an incompatability with previous versions due to different formats being used to exchange Bitcoin transactions
- Changed ASB to quote on Monero unlocked balance instead of total balance
- Allow `asb` to set a bitcoin address that is controlled by the asb itself to redeem/punish bitcoin to
### Added
- Allow asb config overrides using environment variables. See [1231](https://github.com/comit-network/xmr-btc-swap/pull/1231)
## [0.11.0] - 2022-08-11
### Changed
@ -356,12 +320,7 @@ It is possible to migrate critical data from the old db to the sqlite but there
- Fixed an issue where Alice would not verify if Bob's Bitcoin lock transaction is semantically correct, i.e. pays the agreed upon amount to an output owned by both of them.
Fixing this required a **breaking change** on the network layer and hence old versions are not compatible with this version.
[unreleased]: https://github.com/comit-network/xmr-btc-swap/compare/0.13.0...HEAD
[0.13.0]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.3...0.13.0
[0.12.3]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.2...0.12.3
[0.12.2]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.1...0.12.2
[0.12.1]: https://github.com/comit-network/xmr-btc-swap/compare/0.12.0...0.12.1
[0.12.0]: https://github.com/comit-network/xmr-btc-swap/compare/0.11.0...0.12.0
[Unreleased]: https://github.com/comit-network/xmr-btc-swap/compare/0.11.0...HEAD
[0.11.0]: https://github.com/comit-network/xmr-btc-swap/compare/0.10.2...0.11.0
[0.10.2]: https://github.com/comit-network/xmr-btc-swap/compare/0.10.1...0.10.2
[0.10.1]: https://github.com/comit-network/xmr-btc-swap/compare/0.10.0...0.10.1

2784
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -1,8 +1,5 @@
[workspace]
resolver = "2"
members = [ "monero-harness", "monero-rpc", "swap", "monero-wallet" ]
[patch.crates-io]
# patch until new release https://github.com/thomaseizinger/rust-jsonrpc-client/pull/51
jsonrpc_client = { git = "https://github.com/delta1/rust-jsonrpc-client.git", rev = "3b6081697cd616c952acb9c2f02d546357d35506" }
monero = { git = "https://github.com/comit-network/monero-rs", rev = "818f38b" }

@ -44,13 +44,13 @@ It is not recommended to bump fees when swapping because it can have unpredictab
## Contributing
We encourage community contributions whether it be a bug fix or an improvement to the documentation.
We are encourage community contributions whether it be a bug fix or an improvement to the documentation.
Please have a look at the [contribution guidelines](./CONTRIBUTING.md).
## Rust Version Support
Please note that only the latest stable Rust toolchain is supported.
All stable toolchains since 1.74 _should_ work.
All stable toolchains since 1.58 _should_ work.
## Contact

@ -0,0 +1,22 @@
status = [
"static_analysis",
"build (x86_64-unknown-linux-gnu, ubuntu-latest)",
"build (armv7-unknown-linux-gnueabihf, ubuntu-latest)",
"build (x86_64-apple-darwin, macos-latest)",
"build (x86_64-pc-windows-msvc, windows-latest)",
"test (ubuntu-latest)",
"test (macos-latest)",
"docker_tests (happy_path)",
"docker_tests (happy_path_restart_bob_after_xmr_locked)",
"docker_tests (happy_path_restart_alice_after_xmr_locked)",
"docker_tests (happy_path_restart_bob_before_xmr_locked)",
"docker_tests (alice_and_bob_refund_using_cancel_and_refund_command)",
"docker_tests (alice_and_bob_refund_using_cancel_and_refund_command_timelock_not_expired)",
"docker_tests (punish)",
"docker_tests (alice_punishes_after_restart_bob_dead)",
"docker_tests (alice_manually_punishes_after_bob_dead)",
"docker_tests (alice_refunds_after_restart_bob_refunded)",
"docker_tests (ensure_same_swap_id)",
"docker_tests (concurrent_bobs_before_xmr_lock_proof_sent)",
"docker_tests (alice_manually_redeems_after_enc_sig_learned)"
]

@ -42,16 +42,13 @@ Since the ASB is a long running task we specify the person running an ASB as ser
The ASB daemon supports the libp2p [rendezvous-protocol](https://github.com/libp2p/specs/tree/master/rendezvous).
Usage of the rendezvous functionality is entirely optional.
You can configure one or more rendezvous points in the `[network]` section of your config file.
You can configure a rendezvous point in the `[network]` section of your config file.
For the registration to be successful, you also need to configure the externally reachable addresses within the `[network]` section.
For example:
```toml
[network]
rendezvous_point = [
"/dns4/discover.unstoppableswap.net/tcp/8888/p2p/12D3KooWA6cnqJpVnreBVnoro8midDL9Lpzmg8oJPoAGi7YYaamE",
"/dns4/eratosthen.es/tcp/7798/p2p/12D3KooWAh7EXXa2ZyegzLGdjvj1W4G3EXrTGrf6trraoT1MEobs",
]
rendezvous_point = "/dns4/discover.unstoppableswap.net/tcp/8888/p2p/12D3KooWA6cnqJpVnreBVnoro8midDL9Lpzmg8oJPoAGi7YYaamE"
external_addresses = ["/dns4/example.com/tcp/9939"]
```
@ -164,38 +161,3 @@ May 01 01:32:05.018 INFO Tor found. Setting up hidden service.
May 01 01:32:07.475 INFO /onion3/z4findrdwtfbpoq64ayjtmxvr52vvxnsynerlenlfkmm52dqxsl4deyd:9939
May 01 01:32:07.476 INFO /onion3/z4findrdwtfbpoq64ayjtmxvr52vvxnsynerlenlfkmm52dqxsl4deyd:9940
```
### Exporting the Bitcoin wallet descriptor
First use `swap` or `asb` with the `export-bitcoin-wallet` subcommand.
Output example:
```json
{"descriptor":"wpkh(tprv8Zgredacted.../84'/1'/0'/0/*)","blockheight":2415616,"label":"asb-testnet"}
```
The wallet can theoretically be directly imported into
[bdk-cli](https://bitcoindevkit.org/bdk-cli/installation/) but it is easier to
use Sparrow Wallet.
Sparrow wallet import works as follows:
- File -> New wallet -> Give it a name
- Select "New or Imported Software Wallet"
- Click "Enter Private Key" for "Master Private Key (BIP32)"
- Enter the `xprv...` or `tprv...` part of the descriptor (example above is `tprv8Zgredacted...`:
![image](enter-master-private-key.png)
- Click "Import"
- Leave the derivation path as `m/84'/0'/0'` and click "Import Keystore" button
- Click "Apply" and then supply password
![image](import-keystore.png)
- Click Transactions tab
- ???
- Profit!
![image](transactions-tab.png)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

@ -3,16 +3,22 @@
"projectType": "openSource",
"incremental": true,
"markdown": {},
"exec": {
"associations": "**/*.{rs}",
"rustfmt": "rustfmt --edition 2021",
"rustfmt.associations": "**/*.rs"
"rustfmt": {
"edition": 2021,
"condense_wildcard_suffixes": true,
"format_macro_matchers": true,
"imports_granularity": "Module",
"use_field_init_shorthand": true,
"format_code_in_doc_comments": true,
"normalize_comments": true,
"wrap_comments": true,
"overflow_delimited_expr": true
},
"includes": ["**/*.{md}", "**/*.{toml}", "**/*.{rs}"],
"excludes": ["target/"],
"plugins": [
"https://plugins.dprint.dev/markdown-0.13.1.wasm",
"https://github.com/thomaseizinger/dprint-plugin-cargo-toml/releases/download/0.1.0/cargo-toml-0.1.0.wasm",
"https://plugins.dprint.dev/exec-0.3.5.json@d687dda57be0fe9a0088ccdaefa5147649ff24127d8b3ea227536c68ee7abeab"
"https://plugins.dprint.dev/rustfmt-0.6.1.exe-plugin@99b89a0599fd3a63e597e03436862157901f3facae2f0c2fbd0b9f656cdbc2a5"
]
}

@ -10,7 +10,8 @@ anyhow = "1"
futures = "0.3"
monero-rpc = { path = "../monero-rpc" }
rand = "0.7"
testcontainers = "0.15"
spectral = "0.6"
testcontainers = "0.12"
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "macros" ] }
tracing = "0.1"
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "tracing-log" ] }

@ -1,4 +1,6 @@
use testcontainers::{core::WaitFor, Image, ImageArgs};
use std::collections::HashMap;
use testcontainers::core::{Container, Docker, WaitForMessage};
use testcontainers::Image;
pub const MONEROD_DAEMON_CONTAINER_NAME: &str = "monerod";
pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
@ -11,22 +13,43 @@ pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
/// this doesn't matter.
pub const RPC_PORT: u16 = 18081;
#[derive(Clone, Copy, Debug, Default)]
pub struct Monerod;
#[derive(Debug, Default)]
pub struct Monerod {
args: MonerodArgs,
}
impl Image for Monerod {
type Args = MonerodArgs;
type EnvVars = HashMap<String, String>;
type Volumes = HashMap<String, String>;
type EntryPoint = str;
fn descriptor(&self) -> String {
"rinocommunity/monero:v0.18.0.0".to_owned()
}
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
container
.logs()
.stdout
.wait_for_message("RPC server started ok")
.unwrap();
}
fn args(&self) -> <Self as Image>::Args {
self.args.clone()
}
fn name(&self) -> String {
"rinocommunity/monero".into()
fn volumes(&self) -> Self::Volumes {
HashMap::new()
}
fn tag(&self) -> String {
"v0.18.1.2".into()
fn env_vars(&self) -> Self::EnvVars {
HashMap::new()
}
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("RPC server started ok")]
fn with_args(self, args: <Self as Image>::Args) -> Self {
Self { args }
}
fn entrypoint(&self) -> Option<String> {
@ -35,22 +58,43 @@ impl Image for Monerod {
}
}
#[derive(Clone, Copy, Debug)]
pub struct MoneroWalletRpc;
#[derive(Debug, Default)]
pub struct MoneroWalletRpc {
args: MoneroWalletRpcArgs,
}
impl Image for MoneroWalletRpc {
type Args = MoneroWalletRpcArgs;
type EnvVars = HashMap<String, String>;
type Volumes = HashMap<String, String>;
type EntryPoint = str;
fn descriptor(&self) -> String {
"rinocommunity/monero:v0.18.0.0".to_owned()
}
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
container
.logs()
.stdout
.wait_for_message("Run server thread name: RPC")
.unwrap();
}
fn args(&self) -> <Self as Image>::Args {
self.args.clone()
}
fn name(&self) -> String {
"rinocommunity/monero".into()
fn volumes(&self) -> Self::Volumes {
HashMap::new()
}
fn tag(&self) -> String {
"v0.18.1.2".into()
fn env_vars(&self) -> Self::EnvVars {
HashMap::new()
}
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("Run server thread name: RPC")]
fn with_args(self, args: <Self as Image>::Args) -> Self {
Self { args }
}
fn entrypoint(&self) -> Option<String> {
@ -60,9 +104,10 @@ impl Image for MoneroWalletRpc {
}
impl MoneroWalletRpc {
pub fn new(name: &str, daemon_address: String) -> (Self, MoneroWalletRpcArgs) {
let args = MoneroWalletRpcArgs::new(name, daemon_address);
(Self, args)
pub fn new(name: &str, daemon_address: String) -> Self {
Self {
args: MoneroWalletRpcArgs::new(name, daemon_address),
}
}
}
@ -146,12 +191,6 @@ impl IntoIterator for MonerodArgs {
}
}
impl ImageArgs for MonerodArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(self.into_iter())
}
}
#[derive(Debug, Clone)]
pub struct MoneroWalletRpcArgs {
pub disable_rpc_login: bool,
@ -161,6 +200,12 @@ pub struct MoneroWalletRpcArgs {
pub daemon_address: String,
}
impl Default for MoneroWalletRpcArgs {
fn default() -> Self {
unimplemented!("A default instance for `MoneroWalletRpc` doesn't make sense because we always need to connect to a node.")
}
}
impl MoneroWalletRpcArgs {
pub fn new(wallet_name: &str, daemon_address: String) -> Self {
Self {
@ -184,7 +229,6 @@ impl IntoIterator for MoneroWalletRpcArgs {
format!("--daemon-address={}", self.daemon_address),
format!("--rpc-bind-port={}", RPC_PORT),
"--log-level=4".to_string(),
"--allow-mismatched-daemon-version".to_string(), /* https://github.com/monero-project/monero/issues/8600 */
];
if self.disable_rpc_login {
@ -202,9 +246,3 @@ impl IntoIterator for MoneroWalletRpcArgs {
args.into_iter()
}
}
impl ImageArgs for MoneroWalletRpcArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(self.into_iter())
}
}

@ -20,20 +20,17 @@
//! every BLOCK_TIME_SECS seconds.
//!
//! Also provides standalone JSON RPC clients for monerod and monero-wallet-rpc.
use std::time::Duration;
pub mod image;
use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};
use anyhow::{anyhow, bail, Context, Result};
use testcontainers::clients::Cli;
use testcontainers::{Container, RunnableImage};
use tokio::time;
use monero_rpc::monerod;
use monero_rpc::monerod::MonerodRpc as _;
use monero_rpc::wallet::{self, GetAddress, MoneroWalletRpc as _, Refreshed, Transfer};
use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};
pub mod image;
use std::time::Duration;
use testcontainers::clients::Cli;
use testcontainers::{Container, Docker, RunArgs};
use tokio::time;
/// How often we mine a block.
const BLOCK_TIME_SECS: u64 = 1;
@ -59,8 +56,8 @@ impl<'c> Monero {
additional_wallets: Vec<&'static str>,
) -> Result<(
Self,
Container<'c, image::Monerod>,
Vec<Container<'c, image::MoneroWalletRpc>>,
Container<'c, Cli, image::Monerod>,
Vec<Container<'c, Cli, image::MoneroWalletRpc>>,
)> {
let prefix = format!("{}_", random_prefix());
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
@ -141,30 +138,15 @@ impl<'c> Monero {
let wallet = self.wallet(name)?;
let address = wallet.address().await?.address;
let mut expected_total = 0;
let mut expected_unlocked = 0;
let mut unlocked = 0;
for amount in amount_in_outputs {
if amount > 0 {
miner_wallet.transfer(&address, amount).await?;
expected_total += amount;
tracing::info!("Funded {} wallet with {}", wallet.name, amount);
// sanity checks for total/unlocked balance
let total = wallet.balance().await?;
assert_eq!(total, expected_total);
assert_eq!(unlocked, expected_unlocked);
monerod
.client()
.generateblocks(10, miner_address.clone())
.await?;
wallet.refresh().await?;
expected_unlocked += amount;
unlocked = wallet.unlocked_balance().await?;
assert_eq!(unlocked, expected_unlocked);
assert_eq!(total, expected_total);
}
}
@ -224,14 +206,15 @@ impl<'c> Monerod {
cli: &'c Cli,
name: String,
network: String,
) -> Result<(Self, Container<'c, image::Monerod>)> {
let image = image::Monerod;
let image: RunnableImage<image::Monerod> = RunnableImage::from(image)
.with_container_name(name.clone())
) -> Result<(Self, Container<'c, Cli, image::Monerod>)> {
let image = image::Monerod::default();
let run_args = RunArgs::default()
.with_name(name.clone())
.with_network(network.clone());
let container = cli.run(image);
let monerod_rpc_port = container.get_host_port_ipv4(RPC_PORT);
let container = cli.run_with_args(image, run_args);
let monerod_rpc_port = container
.get_host_port(RPC_PORT)
.context("port not exposed")?;
Ok((
Self {
@ -251,7 +234,7 @@ impl<'c> Monerod {
/// address
pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> {
let monerod = self.client().clone();
tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
Ok(())
}
}
@ -264,15 +247,19 @@ impl<'c> MoneroWalletRpc {
name: &str,
monerod: &Monerod,
prefix: String,
) -> Result<(Self, Container<'c, image::MoneroWalletRpc>)> {
) -> Result<(Self, Container<'c, Cli, image::MoneroWalletRpc>)> {
let daemon_address = format!("{}:{}", monerod.name, RPC_PORT);
let (image, args) = image::MoneroWalletRpc::new(name, daemon_address);
let image = RunnableImage::from((image, args))
.with_container_name(format!("{}{}", prefix, name))
.with_network(monerod.network.clone());
let image = image::MoneroWalletRpc::new(name, daemon_address);
let container = cli.run(image);
let wallet_rpc_port = container.get_host_port_ipv4(RPC_PORT);
let network = monerod.network.clone();
let run_args = RunArgs::default()
// prefix the container name so we can run multiple tests
.with_name(format!("{}{}", prefix, name))
.with_network(network.clone());
let container = cli.run_with_args(image, run_args);
let wallet_rpc_port = container
.get_host_port(RPC_PORT)
.context("port not exposed")?;
let client = wallet::Client::localhost(wallet_rpc_port)?;
@ -309,7 +296,7 @@ impl<'c> MoneroWalletRpc {
/// Sends amount to address
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
self.client().transfer_single(0, amount, address).await
Ok(self.client().transfer_single(0, amount, address).await?)
}
pub async fn address(&self) -> Result<GetAddress> {
@ -323,18 +310,10 @@ impl<'c> MoneroWalletRpc {
Ok(balance)
}
pub async fn unlocked_balance(&self) -> Result<u64> {
self.client().refresh().await?;
let balance = self.client().get_balance(0).await?.unlocked_balance;
Ok(balance)
}
pub async fn refresh(&self) -> Result<Refreshed> {
Ok(self.client().refresh().await?)
}
}
/// Mine a block ever BLOCK_TIME_SECS seconds.
async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
loop {

@ -1,5 +1,6 @@
use monero_harness::Monero;
use monero_rpc::monerod::MonerodRpc as _;
use spectral::prelude::*;
use std::time::Duration;
use testcontainers::clients::Cli;
use tokio::time;
@ -20,12 +21,12 @@ async fn init_miner_and_mine_to_miner_address() {
let miner_wallet = monero.wallet("miner").unwrap();
let got_miner_balance = miner_wallet.balance().await.unwrap();
assert!(got_miner_balance > 0);
assert_that!(got_miner_balance).is_greater_than(0);
time::sleep(Duration::from_millis(1010)).await;
// after a bit more than 1 sec another block should have been mined
let block_height = monerod.client().get_block_count().await.unwrap().count;
assert!(block_height > 70);
assert_that(&block_height).is_greater_than(70);
}

@ -1,5 +1,6 @@
use monero_harness::{Monero, MoneroWalletRpc};
use monero_rpc::wallet::MoneroWalletRpc as _;
use spectral::prelude::*;
use std::time::Duration;
use testcontainers::clients::Cli;
use tokio::time::sleep;
@ -28,7 +29,7 @@ async fn fund_transfer_and_check_tx_key() {
// check alice balance
let got_alice_balance = alice_wallet.balance().await.unwrap();
assert_eq!(got_alice_balance, fund_alice);
assert_that(&got_alice_balance).is_equal_to(fund_alice);
// transfer from alice to bob
let bob_address = bob_wallet.address().await.unwrap().address;
@ -40,7 +41,7 @@ async fn fund_transfer_and_check_tx_key() {
wait_for_wallet_to_catch_up(bob_wallet, send_to_bob).await;
let got_bob_balance = bob_wallet.balance().await.unwrap();
assert_eq!(got_bob_balance, send_to_bob);
assert_that(&got_bob_balance).is_equal_to(send_to_bob);
// check if tx was actually seen
let tx_id = transfer.tx_hash;
@ -51,7 +52,7 @@ async fn fund_transfer_and_check_tx_key() {
.await
.expect("failed to check tx by key");
assert_eq!(res.received, send_to_bob);
assert_that!(res.received).is_equal_to(send_to_bob);
}
async fn wait_for_wallet_to_catch_up(wallet: &MoneroWalletRpc, expected_balance: u64) {

@ -12,12 +12,11 @@ jsonrpc_client = { version = "0.7", features = [ "reqwest" ] }
monero = "0.12"
monero-epee-bin-serde = "1"
rand = "0.7"
reqwest = { version = "0.12", default-features = false, features = [ "json" ] }
rust_decimal = { version = "1", features = [ "serde-float" ] }
reqwest = { version = "0.11", default-features = false, features = [ "json" ] }
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
tracing = "0.1"
[dev-dependencies]
hex-literal = "0.4"
hex-literal = "0.3"
tokio = { version = "1", features = [ "full" ] }

@ -47,10 +47,9 @@ impl Client {
}
pub async fn get_o_indexes(&self, txid: Hash) -> Result<GetOIndexesResponse> {
self.binary_request(
self.get_o_indexes_bin_url.clone(),
GetOIndexesPayload { txid },
)
self.binary_request(self.get_o_indexes_bin_url.clone(), GetOIndexesPayload {
txid,
})
.await
}
@ -158,7 +157,7 @@ pub struct OutKey {
pub unlocked: bool,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct BaseResponse {
pub credits: u64,
pub status: Status,
@ -166,7 +165,7 @@ pub struct BaseResponse {
pub untrusted: bool,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GetOIndexesResponse {
#[serde(flatten)]
pub base: BaseResponse,
@ -174,7 +173,7 @@ pub struct GetOIndexesResponse {
pub o_indexes: Vec<u64>,
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
pub enum Status {
#[serde(rename = "OK")]
Ok,
@ -195,7 +194,7 @@ mod monero_serde_hex_block {
{
let hex = String::deserialize(deserializer)?;
let bytes = hex::decode(hex).map_err(D::Error::custom)?;
let bytes = hex::decode(&hex).map_err(D::Error::custom)?;
let mut cursor = Cursor::new(bytes);
let block = monero::Block::consensus_decode(&mut cursor).map_err(D::Error::custom)?;

@ -1,7 +1,4 @@
use std::fmt;
use anyhow::{Context, Result};
use rust_decimal::Decimal;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
@ -89,30 +86,10 @@ pub struct GetAddress {
#[derive(Deserialize, Debug, Clone, Copy)]
pub struct GetBalance {
pub balance: u64,
pub unlocked_balance: u64,
pub multisig_import_needed: bool,
pub blocks_to_unlock: u32,
pub multisig_import_needed: bool,
pub time_to_unlock: u32,
}
impl fmt::Display for GetBalance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut total = Decimal::from(self.balance);
total
.set_scale(12)
.expect("12 is smaller than max precision of 28");
let mut unlocked = Decimal::from(self.unlocked_balance);
unlocked
.set_scale(12)
.expect("12 is smaller than max precision of 28");
write!(
f,
"total balance: {}, unlocked balance: {}",
total, unlocked
)
}
pub unlocked_balance: u64,
}
#[derive(Deserialize, Debug, Clone)]
@ -157,7 +134,7 @@ pub struct Transfer {
pub unsigned_txset: String,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq)]
pub struct BlockHeight {
pub height: u32,
}

@ -14,6 +14,6 @@ rand = "0.7"
curve25519-dalek = "3"
monero-harness = { path = "../monero-harness" }
rand = "0.7"
testcontainers = "0.15"
testcontainers = "0.12"
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs" ] }
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "chrono", "tracing-log" ] }

@ -61,12 +61,13 @@ mod tests {
use monero_harness::image::Monerod;
use monero_rpc::monerod::{Client, GetOutputsOut};
use testcontainers::clients::Cli;
use testcontainers::Docker;
#[tokio::test]
async fn get_outs_for_key_offsets() {
let cli = Cli::default();
let container = cli.run(Monerod);
let rpc_client = Client::localhost(container.get_host_port_ipv4(18081)).unwrap();
let container = cli.run(Monerod::default());
let rpc_client = Client::localhost(container.get_host_port(18081).unwrap()).unwrap();
rpc_client.generateblocks(150, "498AVruCDWgP9Az9LjMm89VWjrBrSZ2W2K3HFBiyzzrRjUJWUcCVxvY1iitfuKoek2FdX6MKGAD9Qb1G1P8QgR5jPmmt3Vj".to_owned()).await.unwrap();
let wallet = Wallet {
client: rpc_client.clone(),

@ -1,4 +1,4 @@
[toolchain]
channel = "1.74" # also update this in the readme, changelog, and github actions
channel = "1.59"
components = ["clippy"]
targets = ["armv7-unknown-linux-gnueabihf"]

@ -1,6 +1,6 @@
[package]
name = "swap"
version = "0.13.0"
version = "0.11.0"
authors = [ "The COMIT guys <hello@comit.network>" ]
edition = "2021"
description = "XMR/BTC trustless atomic swaps."
@ -14,60 +14,57 @@ async-compression = { version = "0.3", features = [ "bzip2", "tokio" ] }
async-trait = "0.1"
atty = "0.2"
backoff = { version = "0.4", features = [ "tokio" ] }
base64 = "0.22"
bdk = "0.28"
base64 = "0.13"
bdk = "0.16"
big-bytes = "1"
bitcoin = { version = "0.29", features = [ "rand", "serde" ] }
bitcoin = { version = "0.27", features = [ "rand", "use-serde" ] }
bmrng = "0.5"
comfy-table = "7.1"
config = { version = "0.14", default-features = false, features = [ "toml" ] }
conquer-once = "0.4"
comfy-table = "5.0"
config = { version = "0.11", default-features = false, features = [ "toml" ] }
conquer-once = "0.3"
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" }
data-encoding = "2.6"
dialoguer = "0.11"
digest = "0.10.7"
data-encoding = "2.3"
dialoguer = "0.10"
directories-next = "2"
ecdsa_fun = { version = "0.10", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] }
ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde" ] }
ed25519-dalek = "1"
futures = { version = "0.3", default-features = false }
hex = "0.4"
itertools = "0.13"
jsonrpsee = { version = "0.16.2", features = [ "server" ] }
jsonrpsee-core = "0.16.2"
itertools = "0.10"
libp2p = { version = "0.42.2", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous", "identify" ] }
monero = { version = "0.12", features = [ "serde_support" ] }
monero-rpc = { path = "../monero-rpc" }
pem = "3.0"
pem = "1.1"
proptest = "1"
qrcode = "0.14"
qrcode = "0.12"
rand = "0.8"
rand_chacha = "0.3"
reqwest = { version = "0.12", features = [ "http2", "rustls-tls", "stream", "socks" ], default-features = false }
reqwest = { version = "0.11", features = [ "rustls-tls", "stream", "socks" ], default-features = false }
rust_decimal = { version = "1", features = [ "serde-float" ] }
rust_decimal_macros = "1"
serde = { version = "1", features = [ "derive" ] }
serde_cbor = "0.11"
serde_json = "1"
serde_with = { version = "1", features = [ "macros" ] }
sha2 = "0.10"
sigma_fun = { version = "0.7", default-features = false, features = [ "ed25519", "serde", "secp256k1", "alloc" ] }
sqlx = { version = "0.6.3", features = [ "sqlite", "runtime-tokio-rustls", "offline" ] }
sha2 = "0.9"
sigma_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "ed25519", "serde" ] }
sqlx = { version = "0.5", features = [ "sqlite", "runtime-tokio-rustls", "offline" ] }
structopt = "0.3"
strum = { version = "0.26", features = [ "derive" ] }
strum = { version = "0.24", features = [ "derive" ] }
thiserror = "1"
time = "0.3"
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net", "parking_lot" ] }
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] }
tokio-socks = "0.5"
tokio-tungstenite = { version = "0.15", features = [ "rustls-tls" ] }
tokio-util = { version = "0.7", features = [ "io", "codec" ] }
toml = "0.8"
toml = "0.5"
torut = { version = "0.2", default-features = false, features = [ "v3", "control" ] }
tracing = { version = "0.1", features = [ "attributes" ] }
tracing-appender = "0.2"
tracing-futures = { version = "0.2", features = [ "std-future", "futures-03" ] }
tracing-subscriber = { version = "0.3", default-features = false, features = [ "fmt", "ansi", "env-filter", "time", "tracing-log", "json" ] }
url = { version = "2", features = [ "serde" ] }
uuid = { version = "1.8", features = [ "serde", "v4" ] }
uuid = { version = "1.1", features = [ "serde", "v4" ] }
void = "1"
[target.'cfg(not(windows))'.dependencies]
@ -77,20 +74,17 @@ tokio-tar = "0.3"
zip = "0.5"
[dev-dependencies]
bitcoin-harness = { git = "https://github.com/delta1/bitcoin-harness-rs.git", rev = "80cc8d05db2610d8531011be505b7bee2b5cdf9f" }
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs" }
get-port = "3"
hyper = "1.3"
jsonrpsee = { version = "0.16.2", features = [ "ws-client" ] }
mockito = "1.3.0"
hyper = "0.14"
monero-harness = { path = "../monero-harness" }
port_check = "0.2"
port_check = "0.1"
proptest = "1"
sequential-test = "0.2.4"
serde_cbor = "0.11"
serial_test = "3.0"
spectral = "0.6"
tempfile = "3"
testcontainers = "0.15"
testcontainers = "0.12"
[build-dependencies]
vergen = { version = "7", default-features = false, features = [ "git", "build" ] }
anyhow = "1"
vergen = { version = "8.3", default-features = false, features = [ "build", "git", "git2" ] }

@ -1,9 +1,9 @@
use anyhow::Result;
use vergen::EmitBuilder;
use vergen::{vergen, Config, SemverKind};
fn main() -> Result<()> {
EmitBuilder::builder()
.git_describe(true, true, None)
.emit()?;
Ok(())
let mut config = Config::default();
*config.git_mut().semver_kind_mut() = SemverKind::Lightweight;
vergen(config)
}

@ -1,15 +1,6 @@
#!/bin/bash
# run this script from the swap dir
# make sure you have sqlx-cli installed: cargo install --version 0.6.3 sqlx-cli
# it's advised for the sqlx-cli to be the same version as specified in cargo.toml
# this script creates a temporary sqlite database
# then runs the migration scripts to create the tables (migrations folder)
# then it prepares the offline sqlx-data.json rust mappings
# crated temporary DB
# run the migration scripts to create the tables
# prepare the sqlx-data.json rust mappings
DATABASE_URL=sqlite:tempdb cargo sqlx database create
DATABASE_URL=sqlite:tempdb cargo sqlx migrate run
# needs the absolute path here
# https://github.com/launchbadge/sqlx/issues/1399
DB_PATH=$(readlink -f tempdb)
DATABASE_URL="sqlite:$DB_PATH" cargo sqlx prepare -- --bin swap
DATABASE_URL=sqlite:./swap/tempdb cargo sqlx prepare -- --bin swap

@ -1,6 +1,7 @@
{
"db": "SQLite",
"081c729a0f1ad6e4ff3e13d6702c946bc4d37d50f40670b4f51d2efcce595aa6": {
"query": "\n SELECT peer_id\n FROM peers\n WHERE swap_id = ?\n ",
"describe": {
"columns": [
{
@ -9,44 +10,26 @@
"type_info": "Text"
}
],
"nullable": [
false
],
"parameters": {
"Right": 1
}
},
"query": "\n SELECT peer_id\n FROM peers\n WHERE swap_id = ?\n "
},
"nullable": [
false
]
}
},
"0ab84c094964968e96a3f2bf590d9ae92227d057386921e0e57165b887de3c75": {
"query": "\n insert into peer_addresses (\n peer_id,\n address\n ) values (?, ?);\n ",
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n insert into peer_addresses (\n peer_id,\n address\n ) values (?, ?);\n "
},
"0d465a17ebbb5761421def759c73cad023c30705d5b41a1399ef79d8d2571d7c": {
"describe": {
"columns": [
{
"name": "start_date",
"ordinal": 0,
"type_info": "Text"
}
],
"nullable": [
true
],
"parameters": {
"Right": 1
}
},
"query": "\n SELECT min(entered_at) as start_date\n FROM swap_states\n WHERE swap_id = ?\n "
},
"nullable": []
}
},
"1ec38c85e7679b2eb42b3df75d9098772ce44fdb8db3012d3c2410d828b74157": {
"query": "\n SELECT swap_id, state\n FROM (\n SELECT max(id), swap_id, state\n FROM swap_states\n GROUP BY swap_id\n )\n ",
"describe": {
"columns": [
{
@ -60,61 +43,37 @@
"type_info": "Text"
}
],
"parameters": {
"Right": 0
},
"nullable": [
false,
false
],
"parameters": {
"Right": 0
}
},
"query": "\n SELECT swap_id, state\n FROM (\n SELECT max(id), swap_id, state\n FROM swap_states\n GROUP BY swap_id\n )\n "
]
}
},
"2a356078a41b321234adf2aa385b501749f907f7c422945a8bdda2b6274f5225": {
"query": "\n insert into peers (\n swap_id,\n peer_id\n ) values (?, ?);\n ",
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n insert into peers (\n swap_id,\n peer_id\n ) values (?, ?);\n "
},
"3f2bfdd2d134586ccad22171cd85a465800fc5c4fdaf191d206974e530240c87": {
"describe": {
"columns": [
{
"name": "swap_id",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "state",
"ordinal": 1,
"type_info": "Text"
}
],
"nullable": [
false,
false
],
"parameters": {
"Right": 0
}
},
"query": "\n SELECT swap_id, state\n FROM swap_states\n "
},
"nullable": []
}
},
"50a5764546f69c118fa0b64120da50f51073d36257d49768de99ff863e3511e0": {
"query": "\n insert into monero_addresses (\n swap_id,\n address\n ) values (?, ?);\n ",
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n insert into monero_addresses (\n swap_id,\n address\n ) values (?, ?);\n "
},
"nullable": []
}
},
"88f761a4f7a0429cad1df0b1bebb1c0a27b2a45656549b23076d7542cfa21ecf": {
"query": "\n SELECT state\n FROM swap_states\n WHERE swap_id = ?\n ORDER BY id desc\n LIMIT 1;\n\n ",
"describe": {
"columns": [
{
@ -123,26 +82,16 @@
"type_info": "Text"
}
],
"nullable": [
false
],
"parameters": {
"Right": 1
}
},
"query": "\n SELECT state\n FROM swap_states\n WHERE swap_id = ?\n ORDER BY id desc\n LIMIT 1;\n\n "
},
"b703032b4ddc627a1124817477e7a8e5014bdc694c36a14053ef3bb2fc0c69b0": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 3
}
},
"query": "\n insert into swap_states (\n swap_id,\n entered_at,\n state\n ) values (?, ?, ?);\n "
},
"nullable": [
false
]
}
},
"ce270dd4a4b9615695a79864240c5401e2122077365e5e5a19408c068c7f9454": {
"a0eb85d04ee3842c52291dad4d225941d1141af735922fcbc665868997fce304": {
"query": "\n SELECT address\n FROM peer_addresses\n WHERE peer_id = ?\n ",
"describe": {
"columns": [
{
@ -151,16 +100,26 @@
"type_info": "Text"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false
],
]
}
},
"b703032b4ddc627a1124817477e7a8e5014bdc694c36a14053ef3bb2fc0c69b0": {
"query": "\n insert into swap_states (\n swap_id,\n entered_at,\n state\n ) values (?, ?, ?);\n ",
"describe": {
"columns": [],
"parameters": {
"Right": 1
}
},
"query": "\n SELECT address\n FROM monero_addresses\n WHERE swap_id = ?\n "
"Right": 3
},
"nullable": []
}
},
"d78acba5eb8563826dd190e0886aa665aae3c6f1e312ee444e65df1c95afe8b2": {
"ce270dd4a4b9615695a79864240c5401e2122077365e5e5a19408c068c7f9454": {
"query": "\n SELECT address\n FROM monero_addresses\n WHERE swap_id = ?\n ",
"describe": {
"columns": [
{
@ -169,31 +128,12 @@
"type_info": "Text"
}
],
"nullable": [
false
],
"parameters": {
"Right": 1
}
},
"query": "\n SELECT DISTINCT address\n FROM peer_addresses\n WHERE peer_id = ?\n "
},
"e05620f420f8c1022971eeb66a803323a8cf258cbebb2834e3f7cf8f812fa646": {
"describe": {
"columns": [
{
"name": "state",
"ordinal": 0,
"type_info": "Text"
}
],
},
"nullable": [
false
],
"parameters": {
"Right": 1
}
},
"query": "\n SELECT state\n FROM swap_states\n WHERE swap_id = ?\n "
]
}
}
}

@ -1,460 +0,0 @@
pub mod request;
use crate::cli::command::{Bitcoin, Monero, Tor};
use crate::database::open_db;
use crate::env::{Config as EnvConfig, GetConfig, Mainnet, Testnet};
use crate::fs::system_data_dir;
use crate::network::rendezvous::XmrBtcNamespace;
use crate::protocol::Database;
use crate::seed::Seed;
use crate::{bitcoin, cli, monero};
use anyhow::{bail, Context as AnyContext, Error, Result};
use futures::future::try_join_all;
use std::fmt;
use std::future::Future;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::{Arc, Once};
use tokio::sync::{broadcast, broadcast::Sender, Mutex, RwLock};
use tokio::task::JoinHandle;
use url::Url;
static START: Once = Once::new();
#[derive(Clone, PartialEq, Debug)]
pub struct Config {
tor_socks5_port: u16,
namespace: XmrBtcNamespace,
server_address: Option<SocketAddr>,
pub env_config: EnvConfig,
seed: Option<Seed>,
debug: bool,
json: bool,
data_dir: PathBuf,
is_testnet: bool,
}
use uuid::Uuid;
#[derive(Default)]
pub struct PendingTaskList(Mutex<Vec<JoinHandle<()>>>);
impl PendingTaskList {
pub async fn spawn<F, T>(&self, future: F)
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let handle = tokio::spawn(async move {
let _ = future.await;
});
self.0.lock().await.push(handle);
}
pub async fn wait_for_tasks(&self) -> Result<()> {
let tasks = {
// Scope for the lock, to avoid holding it for the entire duration of the async block
let mut guard = self.0.lock().await;
guard.drain(..).collect::<Vec<_>>()
};
try_join_all(tasks).await?;
Ok(())
}
}
pub struct SwapLock {
current_swap: RwLock<Option<Uuid>>,
suspension_trigger: Sender<()>,
}
impl SwapLock {
pub fn new() -> Self {
let (suspension_trigger, _) = broadcast::channel(10);
SwapLock {
current_swap: RwLock::new(None),
suspension_trigger,
}
}
pub async fn listen_for_swap_force_suspension(&self) -> Result<(), Error> {
let mut listener = self.suspension_trigger.subscribe();
let event = listener.recv().await;
match event {
Ok(_) => Ok(()),
Err(e) => {
tracing::error!("Error receiving swap suspension signal: {}", e);
bail!(e)
}
}
}
pub async fn acquire_swap_lock(&self, swap_id: Uuid) -> Result<(), Error> {
let mut current_swap = self.current_swap.write().await;
if current_swap.is_some() {
bail!("There already exists an active swap lock");
}
tracing::debug!(swap_id = %swap_id, "Acquiring swap lock");
*current_swap = Some(swap_id);
Ok(())
}
pub async fn get_current_swap_id(&self) -> Option<Uuid> {
*self.current_swap.read().await
}
/// Sends a signal to suspend all ongoing swap processes.
///
/// This function performs the following steps:
/// 1. Triggers the suspension by sending a unit `()` signal to all listeners via `self.suspension_trigger`.
/// 2. Polls the `current_swap` state every 50 milliseconds to check if it has been set to `None`, indicating that the swap processes have been suspended and the lock released.
/// 3. If the lock is not released within 10 seconds, the function returns an error.
///
/// If we send a suspend signal while no swap is in progress, the function will not fail, but will return immediately.
///
/// # Returns
/// - `Ok(())` if the swap lock is successfully released.
/// - `Err(Error)` if the function times out waiting for the swap lock to be released.
///
/// # Notes
/// The 50ms polling interval is considered negligible overhead compared to the typical time required to suspend ongoing swap processes.
pub async fn send_suspend_signal(&self) -> Result<(), Error> {
const TIMEOUT: u64 = 10_000;
const INTERVAL: u64 = 50;
let _ = self.suspension_trigger.send(())?;
for _ in 0..(TIMEOUT / INTERVAL) {
if self.get_current_swap_id().await.is_none() {
return Ok(());
}
tokio::time::sleep(tokio::time::Duration::from_millis(INTERVAL)).await;
}
bail!("Timed out waiting for swap lock to be released");
}
pub async fn release_swap_lock(&self) -> Result<Uuid, Error> {
let mut current_swap = self.current_swap.write().await;
if let Some(swap_id) = current_swap.as_ref() {
tracing::debug!(swap_id = %swap_id, "Releasing swap lock");
let prev_swap_id = *swap_id;
*current_swap = None;
drop(current_swap);
Ok(prev_swap_id)
} else {
bail!("There is no current swap lock to release");
}
}
}
impl Default for SwapLock {
fn default() -> Self {
Self::new()
}
}
// workaround for warning over monero_rpc_process which we must own but not read
#[allow(dead_code)]
pub struct Context {
pub db: Arc<dyn Database + Send + Sync>,
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
monero_wallet: Option<Arc<monero::Wallet>>,
monero_rpc_process: Option<monero::WalletRpcProcess>,
pub swap_lock: Arc<SwapLock>,
pub config: Config,
pub tasks: Arc<PendingTaskList>,
}
#[allow(clippy::too_many_arguments)]
impl Context {
pub async fn build(
bitcoin: Option<Bitcoin>,
monero: Option<Monero>,
tor: Option<Tor>,
data: Option<PathBuf>,
is_testnet: bool,
debug: bool,
json: bool,
server_address: Option<SocketAddr>,
) -> Result<Context> {
let data_dir = data::data_dir_from(data, is_testnet)?;
let env_config = env_config_from(is_testnet);
START.call_once(|| {
let _ = cli::tracing::init(debug, json, data_dir.join("logs"));
});
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read seed in file")?;
let bitcoin_wallet = {
if let Some(bitcoin) = bitcoin {
let (bitcoin_electrum_rpc_url, bitcoin_target_block) =
bitcoin.apply_defaults(is_testnet)?;
Some(Arc::new(
init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?,
))
} else {
None
}
};
let (monero_wallet, monero_rpc_process) = {
if let Some(monero) = monero {
let monero_daemon_address = monero.apply_defaults(is_testnet);
let (wlt, prc) =
init_monero_wallet(data_dir.clone(), monero_daemon_address, env_config).await?;
(Some(Arc::new(wlt)), Some(prc))
} else {
(None, None)
}
};
let tor_socks5_port = tor.map_or(9050, |tor| tor.tor_socks5_port);
let context = Context {
db: open_db(data_dir.join("sqlite")).await?,
bitcoin_wallet,
monero_wallet,
monero_rpc_process,
config: Config {
tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
env_config,
seed: Some(seed),
server_address,
debug,
json,
is_testnet,
data_dir,
},
swap_lock: Arc::new(SwapLock::new()),
tasks: Arc::new(PendingTaskList::default()),
};
Ok(context)
}
pub async fn for_harness(
seed: Seed,
env_config: EnvConfig,
db_path: PathBuf,
bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
bob_monero_wallet: Arc<monero::Wallet>,
) -> Self {
let config = Config::for_harness(seed, env_config);
Self {
bitcoin_wallet: Some(bob_bitcoin_wallet),
monero_wallet: Some(bob_monero_wallet),
config,
db: open_db(db_path)
.await
.expect("Could not open sqlite database"),
monero_rpc_process: None,
swap_lock: Arc::new(SwapLock::new()),
tasks: Arc::new(PendingTaskList::default()),
}
}
}
impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "")
}
}
async fn init_bitcoin_wallet(
electrum_rpc_url: Url,
seed: &Seed,
data_dir: PathBuf,
env_config: EnvConfig,
bitcoin_target_block: usize,
) -> Result<bitcoin::Wallet> {
let wallet_dir = data_dir.join("wallet");
let wallet = bitcoin::Wallet::new(
electrum_rpc_url.clone(),
&wallet_dir,
seed.derive_extended_private_key(env_config.bitcoin_network)?,
env_config,
bitcoin_target_block,
)
.await
.context("Failed to initialize Bitcoin wallet")?;
wallet.sync().await?;
Ok(wallet)
}
async fn init_monero_wallet(
data_dir: PathBuf,
monero_daemon_address: String,
env_config: EnvConfig,
) -> Result<(monero::Wallet, monero::WalletRpcProcess)> {
let network = env_config.monero_network;
const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-monitoring-wallet";
let monero_wallet_rpc = monero::WalletRpc::new(data_dir.join("monero")).await?;
let monero_wallet_rpc_process = monero_wallet_rpc
.run(network, Some(monero_daemon_address))
.await?;
let monero_wallet = monero::Wallet::open_or_create(
monero_wallet_rpc_process.endpoint(),
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
env_config,
)
.await?;
Ok((monero_wallet, monero_wallet_rpc_process))
}
mod data {
use super::*;
pub fn data_dir_from(arg_dir: Option<PathBuf>, testnet: bool) -> Result<PathBuf> {
let base_dir = match arg_dir {
Some(custom_base_dir) => custom_base_dir,
None => os_default()?,
};
let sub_directory = if testnet { "testnet" } else { "mainnet" };
Ok(base_dir.join(sub_directory))
}
fn os_default() -> Result<PathBuf> {
Ok(system_data_dir()?.join("cli"))
}
}
fn env_config_from(testnet: bool) -> EnvConfig {
if testnet {
Testnet::get_config()
} else {
Mainnet::get_config()
}
}
impl Config {
pub fn for_harness(seed: Seed, env_config: EnvConfig) -> Self {
let data_dir = data::data_dir_from(None, false).expect("Could not find data directory");
Self {
tor_socks5_port: 9050,
namespace: XmrBtcNamespace::from_is_testnet(false),
server_address: None,
env_config,
seed: Some(seed),
debug: false,
json: false,
is_testnet: false,
data_dir,
}
}
}
#[cfg(test)]
pub mod api_test {
use super::*;
use crate::api::request::{Method, Request};
use libp2p::Multiaddr;
use std::str::FromStr;
use uuid::Uuid;
pub const MULTI_ADDRESS: &str =
"/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
pub const MONERO_STAGENET_ADDRESS: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
pub const BITCOIN_TESTNET_ADDRESS: &str = "tb1qr3em6k3gfnyl8r7q0v7t4tlnyxzgxma3lressv";
pub const MONERO_MAINNET_ADDRESS: &str = "44Ato7HveWidJYUAVw5QffEcEtSH1DwzSP3FPPkHxNAS4LX9CqgucphTisH978FLHE34YNEx7FcbBfQLQUU8m3NUC4VqsRa";
pub const BITCOIN_MAINNET_ADDRESS: &str = "bc1qe4epnfklcaa0mun26yz5g8k24em5u9f92hy325";
pub const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
impl Config {
pub fn default(
is_testnet: bool,
data_dir: Option<PathBuf>,
debug: bool,
json: bool,
) -> Self {
let data_dir = data::data_dir_from(data_dir, is_testnet).unwrap();
let seed = Seed::from_file_or_generate(data_dir.as_path()).unwrap();
let env_config = env_config_from(is_testnet);
Self {
tor_socks5_port: 9050,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
server_address: None,
env_config,
seed: Some(seed),
debug,
json,
is_testnet,
data_dir,
}
}
}
impl Request {
pub fn buy_xmr(is_testnet: bool) -> Request {
let seller = Multiaddr::from_str(MULTI_ADDRESS).unwrap();
let bitcoin_change_address = {
if is_testnet {
bitcoin::Address::from_str(BITCOIN_TESTNET_ADDRESS).unwrap()
} else {
bitcoin::Address::from_str(BITCOIN_MAINNET_ADDRESS).unwrap()
}
};
let monero_receive_address = {
if is_testnet {
monero::Address::from_str(MONERO_STAGENET_ADDRESS).unwrap()
} else {
monero::Address::from_str(MONERO_MAINNET_ADDRESS).unwrap()
}
};
Request::new(Method::BuyXmr {
seller,
bitcoin_change_address,
monero_receive_address,
swap_id: Uuid::new_v4(),
})
}
pub fn resume() -> Request {
Request::new(Method::Resume {
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
})
}
pub fn cancel() -> Request {
Request::new(Method::CancelAndRefund {
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
})
}
pub fn refund() -> Request {
Request::new(Method::CancelAndRefund {
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
})
}
}
}

@ -1,930 +0,0 @@
use crate::api::Context;
use crate::bitcoin::{Amount, ExpiredTimelocks, TxLock};
use crate::cli::{list_sellers, EventLoop, SellerStatus};
use crate::libp2p_ext::MultiAddrExt;
use crate::network::quote::{BidQuote, ZeroQuoteReceived};
use crate::network::swarm;
use crate::protocol::bob::{BobState, Swap};
use crate::protocol::{bob, State};
use crate::{bitcoin, cli, monero, rpc};
use anyhow::{bail, Context as AnyContext, Result};
use libp2p::core::Multiaddr;
use qrcode::render::unicode;
use qrcode::QrCode;
use serde_json::json;
use std::cmp::min;
use std::convert::TryInto;
use std::future::Future;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug_span, field, Instrument, Span};
use uuid::Uuid;
#[derive(PartialEq, Debug)]
pub struct Request {
pub cmd: Method,
pub log_reference: Option<String>,
}
#[derive(Debug, PartialEq)]
pub enum Method {
BuyXmr {
seller: Multiaddr,
bitcoin_change_address: bitcoin::Address,
monero_receive_address: monero::Address,
swap_id: Uuid,
},
Resume {
swap_id: Uuid,
},
CancelAndRefund {
swap_id: Uuid,
},
MoneroRecovery {
swap_id: Uuid,
},
History,
Config,
WithdrawBtc {
amount: Option<Amount>,
address: bitcoin::Address,
},
Balance {
force_refresh: bool,
},
ListSellers {
rendezvous_point: Multiaddr,
},
ExportBitcoinWallet,
SuspendCurrentSwap,
StartDaemon {
server_address: Option<SocketAddr>,
},
GetCurrentSwap,
GetSwapInfo {
swap_id: Uuid,
},
GetRawStates,
}
impl Method {
fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span {
let span = match self {
Method::Balance { .. } => {
debug_span!(
"method",
method_name = "Balance",
log_reference_id = field::Empty
)
}
Method::BuyXmr { swap_id, .. } => {
debug_span!("method", method_name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::CancelAndRefund { swap_id } => {
debug_span!("method", method_name="CancelAndRefund", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::Resume { swap_id } => {
debug_span!("method", method_name="Resume", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::Config => {
debug_span!(
"method",
method_name = "Config",
log_reference_id = field::Empty
)
}
Method::ExportBitcoinWallet => {
debug_span!(
"method",
method_name = "ExportBitcoinWallet",
log_reference_id = field::Empty
)
}
Method::GetCurrentSwap => {
debug_span!(
"method",
method_name = "GetCurrentSwap",
log_reference_id = field::Empty
)
}
Method::GetSwapInfo { .. } => {
debug_span!(
"method",
method_name = "GetSwapInfo",
log_reference_id = field::Empty
)
}
Method::History => {
debug_span!(
"method",
method_name = "History",
log_reference_id = field::Empty
)
}
Method::ListSellers { .. } => {
debug_span!(
"method",
method_name = "ListSellers",
log_reference_id = field::Empty
)
}
Method::MoneroRecovery { .. } => {
debug_span!(
"method",
method_name = "MoneroRecovery",
log_reference_id = field::Empty
)
}
Method::GetRawStates => debug_span!(
"method",
method_name = "RawHistory",
log_reference_id = field::Empty
),
Method::StartDaemon { .. } => {
debug_span!(
"method",
method_name = "StartDaemon",
log_reference_id = field::Empty
)
}
Method::SuspendCurrentSwap => {
debug_span!(
"method",
method_name = "SuspendCurrentSwap",
log_reference_id = field::Empty
)
}
Method::WithdrawBtc { .. } => {
debug_span!(
"method",
method_name = "WithdrawBtc",
log_reference_id = field::Empty
)
}
};
if let Some(log_reference_id) = log_reference_id {
span.record("log_reference_id", log_reference_id.as_str());
}
span
}
}
impl Request {
pub fn new(cmd: Method) -> Request {
Request {
cmd,
log_reference: None,
}
}
pub fn with_id(cmd: Method, id: Option<String>) -> Request {
Request {
cmd,
log_reference: id,
}
}
async fn handle_cmd(self, context: Arc<Context>) -> Result<serde_json::Value> {
match self.cmd {
Method::SuspendCurrentSwap => {
let swap_id = context.swap_lock.get_current_swap_id().await;
if let Some(id_value) = swap_id {
context.swap_lock.send_suspend_signal().await?;
Ok(json!({ "swapId": id_value }))
} else {
bail!("No swap is currently running")
}
}
Method::GetSwapInfo { swap_id } => {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?;
let state = context.db.get_state(swap_id).await?;
let is_completed = state.swap_finished();
let peerId = context
.db
.get_peer_id(swap_id)
.await
.with_context(|| "Could not get PeerID")?;
let addresses = context
.db
.get_addresses(peerId)
.await
.with_context(|| "Could not get addressess")?;
let start_date = context.db.get_swap_start_date(swap_id).await?;
let swap_state: BobState = state.try_into()?;
let state_name = format!("{}", swap_state);
let (
xmr_amount,
btc_amount,
tx_lock_id,
tx_cancel_fee,
tx_refund_fee,
tx_lock_fee,
btc_refund_address,
cancel_timelock,
punish_timelock,
) = context
.db
.get_states(swap_id)
.await?
.iter()
.find_map(|state| {
if let State::Bob(BobState::SwapSetupCompleted(state2)) = state {
let xmr_amount = state2.xmr;
let btc_amount = state2.tx_lock.lock_amount().to_sat();
let tx_cancel_fee = state2.tx_cancel_fee.to_sat();
let tx_refund_fee = state2.tx_refund_fee.to_sat();
let tx_lock_id = state2.tx_lock.txid();
let btc_refund_address = state2.refund_address.to_string();
if let Ok(tx_lock_fee) = state2.tx_lock.fee() {
let tx_lock_fee = tx_lock_fee.to_sat();
Some((
xmr_amount,
btc_amount,
tx_lock_id,
tx_cancel_fee,
tx_refund_fee,
tx_lock_fee,
btc_refund_address,
state2.cancel_timelock,
state2.punish_timelock,
))
} else {
None
}
} else {
None
}
})
.with_context(|| "Did not find SwapSetupCompleted state for swap")?;
let timelock = match swap_state {
BobState::Started { .. }
| BobState::SafelyAborted
| BobState::SwapSetupCompleted(_) => None,
BobState::BtcLocked { state3: state, .. }
| BobState::XmrLockProofReceived { state, .. } => {
Some(state.expired_timelock(bitcoin_wallet).await)
}
BobState::XmrLocked(state) | BobState::EncSigSent(state) => {
Some(state.expired_timelock(bitcoin_wallet).await)
}
BobState::CancelTimelockExpired(state) | BobState::BtcCancelled(state) => {
Some(state.expired_timelock(bitcoin_wallet).await)
}
BobState::BtcPunished { .. } => Some(Ok(ExpiredTimelocks::Punish)),
BobState::BtcRefunded(_)
| BobState::BtcRedeemed(_)
| BobState::XmrRedeemed { .. } => None,
};
Ok(json!({
"swapId": swap_id,
"seller": {
"peerId": peerId.to_string(),
"addresses": addresses
},
"completed": is_completed,
"startDate": start_date,
"stateName": state_name,
"xmrAmount": xmr_amount,
"btcAmount": btc_amount,
"txLockId": tx_lock_id,
"txCancelFee": tx_cancel_fee,
"txRefundFee": tx_refund_fee,
"txLockFee": tx_lock_fee,
"btcRefundAddress": btc_refund_address.to_string(),
"cancelTimelock": cancel_timelock,
"punishTimelock": punish_timelock,
// If the timelock is None, it means that the swap is in a state where the timelock is not accessible to us.
// If that is the case, we return null. Otherwise, we return the timelock.
"timelock": timelock.map(|tl| tl.map(|tl| json!(tl)).unwrap_or(json!(null))).unwrap_or(json!(null)),
}))
}
Method::BuyXmr {
seller,
bitcoin_change_address,
monero_receive_address,
swap_id,
} => {
let bitcoin_wallet = Arc::clone(
context
.bitcoin_wallet
.as_ref()
.expect("Could not find Bitcoin wallet"),
);
let monero_wallet = Arc::clone(
context
.monero_wallet
.as_ref()
.context("Could not get Monero wallet")?,
);
let env_config = context.config.env_config;
let seed = context.config.seed.clone().context("Could not get seed")?;
let seller_peer_id = seller
.extract_peer_id()
.context("Seller address must contain peer ID")?;
context
.db
.insert_address(seller_peer_id, seller.clone())
.await?;
let behaviour = cli::Behaviour::new(
seller_peer_id,
env_config,
bitcoin_wallet.clone(),
(seed.derive_libp2p_identity(), context.config.namespace),
);
let mut swarm = swarm::cli(
seed.derive_libp2p_identity(),
context.config.tor_socks5_port,
behaviour,
)
.await?;
swarm.behaviour_mut().add_address(seller_peer_id, seller);
context
.db
.insert_monero_address(swap_id, monero_receive_address)
.await?;
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
context.swap_lock.acquire_swap_lock(swap_id).await?;
let initialize_swap = tokio::select! {
biased;
_ = context.swap_lock.listen_for_swap_force_suspension() => {
tracing::debug!("Shutdown signal received, exiting");
context.swap_lock.release_swap_lock().await.expect("Shutdown signal received but failed to release swap lock. The swap process has been terminated but the swap lock is still active.");
bail!("Shutdown signal received");
},
result = async {
let (event_loop, mut event_loop_handle) =
EventLoop::new(swap_id, swarm, seller_peer_id)?;
let event_loop = tokio::spawn(event_loop.run().in_current_span());
let bid_quote = event_loop_handle.request_quote().await?;
Ok::<_, anyhow::Error>((event_loop, event_loop_handle, bid_quote))
} => {
result
},
};
let (event_loop, event_loop_handle, bid_quote) = match initialize_swap {
Ok(result) => result,
Err(error) => {
tracing::error!(%swap_id, "Swap initialization failed: {:#}", error);
context
.swap_lock
.release_swap_lock()
.await
.expect("Could not release swap lock");
bail!(error);
}
};
context.tasks.clone().spawn(async move {
tokio::select! {
biased;
_ = context.swap_lock.listen_for_swap_force_suspension() => {
tracing::debug!("Shutdown signal received, exiting");
context.swap_lock.release_swap_lock().await.expect("Shutdown signal received but failed to release swap lock. The swap process has been terminated but the swap lock is still active.");
bail!("Shutdown signal received");
},
event_loop_result = event_loop => {
match event_loop_result {
Ok(_) => {
tracing::debug!(%swap_id, "EventLoop completed")
}
Err(error) => {
tracing::error!(%swap_id, "EventLoop failed: {:#}", error)
}
}
},
swap_result = async {
let max_givable = || bitcoin_wallet.max_giveable(TxLock::script_size());
let estimate_fee = |amount| bitcoin_wallet.estimate_fee(TxLock::weight(), amount);
let determine_amount = determine_btc_to_swap(
context.config.json,
bid_quote,
bitcoin_wallet.new_address(),
|| bitcoin_wallet.balance(),
max_givable,
|| bitcoin_wallet.sync(),
estimate_fee,
);
let (amount, fees) = match determine_amount.await {
Ok(val) => val,
Err(error) => match error.downcast::<ZeroQuoteReceived>() {
Ok(_) => {
bail!("Seller's XMR balance is currently too low to initiate a swap, please try again later")
}
Err(other) => bail!(other),
},
};
tracing::info!(%amount, %fees, "Determined swap amount");
context.db.insert_peer_id(swap_id, seller_peer_id).await?;
let swap = Swap::new(
Arc::clone(&context.db),
swap_id,
Arc::clone(&bitcoin_wallet),
monero_wallet,
env_config,
event_loop_handle,
monero_receive_address,
bitcoin_change_address,
amount,
);
bob::run(swap).await
} => {
match swap_result {
Ok(state) => {
tracing::debug!(%swap_id, state=%state, "Swap completed")
}
Err(error) => {
tracing::error!(%swap_id, "Failed to complete swap: {:#}", error)
}
}
},
};
tracing::debug!(%swap_id, "Swap completed");
context
.swap_lock
.release_swap_lock()
.await
.expect("Could not release swap lock");
Ok::<_, anyhow::Error>(())
}.in_current_span()).await;
Ok(json!({
"swapId": swap_id.to_string(),
"quote": bid_quote,
}))
}
Method::Resume { swap_id } => {
context.swap_lock.acquire_swap_lock(swap_id).await?;
let seller_peer_id = context.db.get_peer_id(swap_id).await?;
let seller_addresses = context.db.get_addresses(seller_peer_id).await?;
let seed = context
.config
.seed
.as_ref()
.context("Could not get seed")?
.derive_libp2p_identity();
let behaviour = cli::Behaviour::new(
seller_peer_id,
context.config.env_config,
Arc::clone(
context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?,
),
(seed.clone(), context.config.namespace),
);
let mut swarm =
swarm::cli(seed.clone(), context.config.tor_socks5_port, behaviour).await?;
let our_peer_id = swarm.local_peer_id();
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
for seller_address in seller_addresses {
swarm
.behaviour_mut()
.add_address(seller_peer_id, seller_address);
}
let (event_loop, event_loop_handle) =
EventLoop::new(swap_id, swarm, seller_peer_id)?;
let monero_receive_address = context.db.get_monero_address(swap_id).await?;
let swap = Swap::from_db(
Arc::clone(&context.db),
swap_id,
Arc::clone(
context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?,
),
Arc::clone(
context
.monero_wallet
.as_ref()
.context("Could not get Monero wallet")?,
),
context.config.env_config,
event_loop_handle,
monero_receive_address,
)
.await?;
context.tasks.clone().spawn(
async move {
let handle = tokio::spawn(event_loop.run().in_current_span());
tokio::select! {
biased;
_ = context.swap_lock.listen_for_swap_force_suspension() => {
tracing::debug!("Shutdown signal received, exiting");
context.swap_lock.release_swap_lock().await.expect("Shutdown signal received but failed to release swap lock. The swap process has been terminated but the swap lock is still active.");
bail!("Shutdown signal received");
},
event_loop_result = handle => {
match event_loop_result {
Ok(_) => {
tracing::debug!(%swap_id, "EventLoop completed during swap resume")
}
Err(error) => {
tracing::error!(%swap_id, "EventLoop failed during swap resume: {:#}", error)
}
}
},
swap_result = bob::run(swap) => {
match swap_result {
Ok(state) => {
tracing::debug!(%swap_id, state=%state, "Swap completed after resuming")
}
Err(error) => {
tracing::error!(%swap_id, "Failed to resume swap: {:#}", error)
}
}
}
}
context
.swap_lock
.release_swap_lock()
.await
.expect("Could not release swap lock");
Ok::<(), anyhow::Error>(())
}
.in_current_span(),
).await;
Ok(json!({
"result": "ok",
}))
}
Method::CancelAndRefund { swap_id } => {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?;
context.swap_lock.acquire_swap_lock(swap_id).await?;
let state = cli::cancel_and_refund(
swap_id,
Arc::clone(bitcoin_wallet),
Arc::clone(&context.db),
)
.await;
context
.swap_lock
.release_swap_lock()
.await
.expect("Could not release swap lock");
state.map(|state| {
json!({
"result": state,
})
})
}
Method::History => {
let swaps = context.db.all().await?;
let mut vec: Vec<(Uuid, String)> = Vec::new();
for (swap_id, state) in swaps {
let state: BobState = state.try_into()?;
vec.push((swap_id, state.to_string()));
}
Ok(json!({ "swaps": vec }))
}
Method::GetRawStates => {
let raw_history = context.db.raw_all().await?;
Ok(json!({ "raw_states": raw_history }))
}
Method::Config => {
let data_dir_display = context.config.data_dir.display();
tracing::info!(path=%data_dir_display, "Data directory");
tracing::info!(path=%format!("{}/logs", data_dir_display), "Log files directory");
tracing::info!(path=%format!("{}/sqlite", data_dir_display), "Sqlite file location");
tracing::info!(path=%format!("{}/seed.pem", data_dir_display), "Seed file location");
tracing::info!(path=%format!("{}/monero", data_dir_display), "Monero-wallet-rpc directory");
tracing::info!(path=%format!("{}/wallet", data_dir_display), "Internal bitcoin wallet directory");
Ok(json!({
"log_files": format!("{}/logs", data_dir_display),
"sqlite": format!("{}/sqlite", data_dir_display),
"seed": format!("{}/seed.pem", data_dir_display),
"monero-wallet-rpc": format!("{}/monero", data_dir_display),
"bitcoin_wallet": format!("{}/wallet", data_dir_display),
}))
}
Method::WithdrawBtc { address, amount } => {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?;
let amount = match amount {
Some(amount) => amount,
None => {
bitcoin_wallet
.max_giveable(address.script_pubkey().len())
.await?
}
};
let psbt = bitcoin_wallet
.send_to_address(address, amount, None)
.await?;
let signed_tx = bitcoin_wallet.sign_and_finalize(psbt).await?;
bitcoin_wallet
.broadcast(signed_tx.clone(), "withdraw")
.await?;
Ok(json!({
"signed_tx": signed_tx,
"amount": amount.to_sat(),
"txid": signed_tx.txid(),
}))
}
Method::StartDaemon { server_address } => {
// Default to 127.0.0.1:1234
let server_address = server_address.unwrap_or("127.0.0.1:1234".parse()?);
let (addr, server_handle) =
rpc::run_server(server_address, Arc::clone(&context)).await?;
tracing::info!(%addr, "Started RPC server");
server_handle.stopped().await;
tracing::info!("Stopped RPC server");
Ok(json!({}))
}
Method::Balance { force_refresh } => {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?;
if force_refresh {
bitcoin_wallet.sync().await?;
}
let bitcoin_balance = bitcoin_wallet.balance().await?;
if force_refresh {
tracing::info!(
balance = %bitcoin_balance,
"Checked Bitcoin balance",
);
} else {
tracing::debug!(
balance = %bitcoin_balance,
"Current Bitcoin balance as of last sync",
);
}
Ok(json!({
"balance": bitcoin_balance.to_sat()
}))
}
Method::ListSellers { rendezvous_point } => {
let rendezvous_node_peer_id = rendezvous_point
.extract_peer_id()
.context("Rendezvous node address must contain peer ID")?;
let identity = context
.config
.seed
.as_ref()
.context("Cannot extract seed")?
.derive_libp2p_identity();
let sellers = list_sellers(
rendezvous_node_peer_id,
rendezvous_point,
context.config.namespace,
context.config.tor_socks5_port,
identity,
)
.await?;
for seller in &sellers {
match seller.status {
SellerStatus::Online(quote) => {
tracing::info!(
price = %quote.price.to_string(),
min_quantity = %quote.min_quantity.to_string(),
max_quantity = %quote.max_quantity.to_string(),
status = "Online",
address = %seller.multiaddr.to_string(),
"Fetched peer status"
);
}
SellerStatus::Unreachable => {
tracing::info!(
status = "Unreachable",
address = %seller.multiaddr.to_string(),
"Fetched peer status"
);
}
}
}
Ok(json!({ "sellers": sellers }))
}
Method::ExportBitcoinWallet => {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?;
let wallet_export = bitcoin_wallet.wallet_export("cli").await?;
tracing::info!(descriptor=%wallet_export.to_string(), "Exported bitcoin wallet");
Ok(json!({
"descriptor": wallet_export.to_string(),
}))
}
Method::MoneroRecovery { swap_id } => {
let swap_state: BobState = context.db.get_state(swap_id).await?.try_into()?;
if let BobState::BtcRedeemed(state5) = swap_state {
let (spend_key, view_key) = state5.xmr_keys();
let restore_height = state5.monero_wallet_restore_blockheight.height;
let address = monero::Address::standard(
context.config.env_config.monero_network,
monero::PublicKey::from_private_key(&spend_key),
monero::PublicKey::from(view_key.public()),
);
tracing::info!(restore_height=%restore_height, address=%address, spend_key=%spend_key, view_key=%view_key, "Monero recovery information");
Ok(json!({
"address": address,
"spend_key": spend_key.to_string(),
"view_key": view_key.to_string(),
"restore_height": state5.monero_wallet_restore_blockheight.height,
}))
} else {
bail!(
"Cannot print monero recovery information in state {}, only possible for BtcRedeemed",
swap_state
)
}
}
Method::GetCurrentSwap => Ok(json!({
"swap_id": context.swap_lock.get_current_swap_id().await
})),
}
}
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
let method_span = self.cmd.get_tracing_span(self.log_reference.clone());
self.handle_cmd(context)
.instrument(method_span.clone())
.await
.map_err(|err| {
method_span.in_scope(|| {
tracing::debug!(err = format!("{:?}", err), "API call resulted in an error");
});
err
})
}
}
fn qr_code(value: &impl ToString) -> Result<String> {
let code = QrCode::new(value.to_string())?;
let qr_code = code
.render::<unicode::Dense1x2>()
.dark_color(unicode::Dense1x2::Light)
.light_color(unicode::Dense1x2::Dark)
.build();
Ok(qr_code)
}
pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
json: bool,
bid_quote: BidQuote,
get_new_address: impl Future<Output = Result<bitcoin::Address>>,
balance: FB,
max_giveable_fn: FMG,
sync: FS,
estimate_fee: FFE,
) -> Result<(Amount, Amount)>
where
TB: Future<Output = Result<Amount>>,
FB: Fn() -> TB,
TMG: Future<Output = Result<Amount>>,
FMG: Fn() -> TMG,
TS: Future<Output = Result<()>>,
FS: Fn() -> TS,
FFE: Fn(Amount) -> TFE,
TFE: Future<Output = Result<Amount>>,
{
if bid_quote.max_quantity == Amount::ZERO {
bail!(ZeroQuoteReceived)
}
tracing::info!(
price = %bid_quote.price,
minimum_amount = %bid_quote.min_quantity,
maximum_amount = %bid_quote.max_quantity,
"Received quote",
);
sync().await?;
let mut max_giveable = max_giveable_fn().await?;
if max_giveable == Amount::ZERO || max_giveable < bid_quote.min_quantity {
let deposit_address = get_new_address.await?;
let minimum_amount = bid_quote.min_quantity;
let maximum_amount = bid_quote.max_quantity;
if !json {
eprintln!("{}", qr_code(&deposit_address)?);
}
loop {
let min_outstanding = bid_quote.min_quantity - max_giveable;
let min_fee = estimate_fee(min_outstanding).await?;
let min_deposit = min_outstanding + min_fee;
tracing::info!(
"Deposit at least {} to cover the min quantity with fee!",
min_deposit
);
tracing::info!(
%deposit_address,
%min_deposit,
%max_giveable,
%minimum_amount,
%maximum_amount,
"Waiting for Bitcoin deposit",
);
max_giveable = loop {
sync().await?;
let new_max_givable = max_giveable_fn().await?;
if new_max_givable > max_giveable {
break new_max_givable;
}
tokio::time::sleep(Duration::from_secs(1)).await;
};
let new_balance = balance().await?;
tracing::info!(%new_balance, %max_giveable, "Received Bitcoin");
if max_giveable < bid_quote.min_quantity {
tracing::info!("Deposited amount is less than `min_quantity`");
continue;
}
break;
}
};
let balance = balance().await?;
let fees = balance - max_giveable;
let max_accepted = bid_quote.max_quantity;
let btc_swap_amount = min(max_giveable, max_accepted);
Ok((btc_swap_amount, fees))
}

@ -8,7 +8,6 @@ pub mod tracing;
pub use event_loop::{EventLoop, EventLoopHandle, FixedRate, KrakenRate, LatestRate};
pub use network::behaviour::{Behaviour, OutEvent};
pub use network::rendezvous::RendezvousNode;
pub use network::transport;
pub use rate::Rate;
pub use recovery::cancel::cancel;
@ -19,4 +18,4 @@ pub use recovery::safely_abort::safely_abort;
pub use recovery::{cancel, refund};
#[cfg(test)]
pub use network::rendezvous;
pub use network::rendezous;

@ -171,7 +171,7 @@ fn env_config(is_testnet: bool) -> env::Config {
}
}
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Serialize)]
#[error("Invalid Bitcoin address provided, expected address on network {expected:?} but address provided is on {actual:?}")]
pub struct BitcoinAddressNetworkMismatch {
#[serde(with = "crate::bitcoin::network")]
@ -180,7 +180,7 @@ pub struct BitcoinAddressNetworkMismatch {
actual: bitcoin::Network,
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
pub struct Arguments {
pub testnet: bool,
pub json: bool,
@ -190,7 +190,7 @@ pub struct Arguments {
pub cmd: Command,
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
pub enum Command {
Start {
resume_only: bool,
@ -226,7 +226,7 @@ pub enum Command {
name = "asb",
about = "Automated Swap Backend for swapping XMR for BTC",
author,
version = env!("VERGEN_GIT_DESCRIBE")
version = env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT")
)]
pub struct RawArguments {
#[structopt(long, help = "Swap on testnet")]

@ -84,7 +84,7 @@ const DEFAULT_MIN_BUY_AMOUNT: f64 = 0.002f64;
const DEFAULT_MAX_BUY_AMOUNT: f64 = 0.02f64;
const DEFAULT_SPREAD: f64 = 0.02f64;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub data: Data,
@ -102,86 +102,29 @@ impl Config {
{
let config_file = Path::new(&config_file);
let config = config::Config::builder()
.add_source(config::File::from(config_file))
.add_source(
config::Environment::with_prefix("ASB")
.separator("__")
.list_separator(","),
)
.build()?;
let mut config = config::Config::new();
config.merge(config::File::from(config_file))?;
config.try_into()
}
}
impl TryFrom<config::Config> for Config {
type Error = config::ConfigError;
fn try_from(value: config::Config) -> Result<Self, Self::Error> {
value.try_deserialize()
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Data {
pub dir: PathBuf,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Network {
#[serde(deserialize_with = "addr_list::deserialize")]
pub listen: Vec<Multiaddr>,
#[serde(default, deserialize_with = "addr_list::deserialize")]
pub rendezvous_point: Vec<Multiaddr>,
#[serde(default, deserialize_with = "addr_list::deserialize")]
#[serde(default)]
pub rendezvous_point: Option<Multiaddr>,
#[serde(default)]
pub external_addresses: Vec<Multiaddr>,
}
mod addr_list {
use libp2p::Multiaddr;
use serde::de::Unexpected;
use serde::{de, Deserialize, Deserializer};
use serde_json::Value;
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Multiaddr>, D::Error>
where
D: Deserializer<'de>,
{
let s = Value::deserialize(deserializer)?;
return match s {
Value::String(s) => {
let list: Result<Vec<_>, _> = s
.split(',')
.filter(|s| !s.is_empty())
.map(|s| s.trim().parse().map_err(de::Error::custom))
.collect();
Ok(list?)
}
Value::Array(a) => {
let list: Result<Vec<_>, _> = a
.iter()
.map(|v| {
if let Value::String(s) = v {
s.trim().parse().map_err(de::Error::custom)
} else {
Err(de::Error::custom("expected a string"))
}
})
.collect();
Ok(list?)
}
value => Err(de::Error::invalid_type(
Unexpected::Other(&value.to_string()),
&"a string or array",
)),
};
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Bitcoin {
pub electrum_rpc_url: Url,
@ -191,7 +134,7 @@ pub struct Bitcoin {
pub network: bitcoin::Network,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Monero {
pub wallet_rpc_url: Url,
@ -200,14 +143,14 @@ pub struct Monero {
pub network: monero::Network,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TorConf {
pub control_port: u16,
pub socks5_port: u16,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Maker {
#[serde(with = "::bitcoin::util::amount::serde::as_btc")]
@ -216,7 +159,6 @@ pub struct Maker {
pub max_buy_btc: bitcoin::Amount,
pub ask_spread: Decimal,
pub price_ticker_ws_url: Url,
pub external_bitcoin_redeem_address: Option<bitcoin::Address>,
}
impl Default for TorConf {
@ -230,7 +172,7 @@ impl Default for TorConf {
#[derive(thiserror::Error, Debug, Clone, Copy)]
#[error("config not initialized")]
pub struct ConfigNotInitialized;
pub struct ConfigNotInitialized {}
pub fn read_config(config_path: PathBuf) -> Result<Result<Config, ConfigNotInitialized>> {
if config_path.exists() {
@ -347,27 +289,10 @@ pub fn query_user_for_initial_config(testnet: bool) -> Result<Config> {
}
let ask_spread = Decimal::from_f64(ask_spread).context("Unable to parse spread")?;
let mut number = 1;
let mut done = false;
let mut rendezvous_points = Vec::new();
println!("ASB can register with multiple rendezvous nodes for discoverability. This can also be edited in the config file later.");
while !done {
let prompt = format!(
"Enter the address for rendezvous node ({number}). Or just hit Enter to continue."
);
let rendezvous_addr = Input::<Multiaddr>::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.allow_empty(true)
.interact_text()?;
if rendezvous_addr.is_empty() {
done = true;
} else if rendezvous_points.contains(&rendezvous_addr) {
println!("That rendezvous address is already in the list.");
} else {
rendezvous_points.push(rendezvous_addr);
number += 1;
}
}
let rendezvous_point = Input::<Multiaddr>::with_theme(&ColorfulTheme::default())
.with_prompt("Do you want to advertise your ASB instance with a rendezvous node? Enter an empty string if not.")
.allow_empty(true)
.interact_text()?;
println!();
@ -375,7 +300,11 @@ pub fn query_user_for_initial_config(testnet: bool) -> Result<Config> {
data: Data { dir: data_dir },
network: Network {
listen: listen_addresses,
rendezvous_point: rendezvous_points, // keeping the singular key name for backcompat
rendezvous_point: if rendezvous_point.is_empty() {
None
} else {
Some(rendezvous_point)
},
external_addresses: vec![],
},
bitcoin: Bitcoin {
@ -398,7 +327,6 @@ pub fn query_user_for_initial_config(testnet: bool) -> Result<Config> {
max_buy_btc: max_buy,
ask_spread,
price_ticker_ws_url: defaults.price_ticker_ws_url,
external_bitcoin_redeem_address: None,
},
})
}
@ -406,12 +334,9 @@ pub fn query_user_for_initial_config(testnet: bool) -> Result<Config> {
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use tempfile::tempdir;
// these tests are run serially since env vars affect the whole process
#[test]
#[serial]
fn config_roundtrip_testnet() {
let temp_dir = tempdir().unwrap().path().to_path_buf();
let config_path = Path::join(&temp_dir, "config.toml");
@ -430,9 +355,10 @@ mod tests {
},
network: Network {
listen: vec![defaults.listen_address_tcp, defaults.listen_address_ws],
rendezvous_point: vec![],
rendezvous_point: None,
external_addresses: vec![],
},
monero: Monero {
wallet_rpc_url: defaults.monero_wallet_rpc_url,
finality_confirmations: None,
@ -444,7 +370,6 @@ mod tests {
max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(),
ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(),
price_ticker_ws_url: defaults.price_ticker_ws_url,
external_bitcoin_redeem_address: None,
},
};
@ -455,7 +380,6 @@ mod tests {
}
#[test]
#[serial]
fn config_roundtrip_mainnet() {
let temp_dir = tempdir().unwrap().path().to_path_buf();
let config_path = Path::join(&temp_dir, "config.toml");
@ -474,63 +398,10 @@ mod tests {
},
network: Network {
listen: vec![defaults.listen_address_tcp, defaults.listen_address_ws],
rendezvous_point: vec![],
rendezvous_point: None,
external_addresses: vec![],
},
monero: Monero {
wallet_rpc_url: defaults.monero_wallet_rpc_url,
finality_confirmations: None,
network: monero::Network::Mainnet,
},
tor: Default::default(),
maker: Maker {
min_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MIN_BUY_AMOUNT).unwrap(),
max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(),
ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(),
price_ticker_ws_url: defaults.price_ticker_ws_url,
external_bitcoin_redeem_address: None,
},
};
initial_setup(config_path.clone(), expected.clone()).unwrap();
let actual = read_config(config_path).unwrap().unwrap();
assert_eq!(expected, actual);
}
#[test]
#[serial]
fn env_override() {
let temp_dir = tempfile::tempdir().unwrap().path().to_path_buf();
let config_path = Path::join(&temp_dir, "config.toml");
let defaults = Mainnet::getConfigFileDefaults().unwrap();
let dir = PathBuf::from("/tmp/dir");
std::env::set_var("ASB__DATA__DIR", dir.clone());
let addr1 = "/dns4/example.com/tcp/9939";
let addr2 = "/ip4/1.2.3.4/tcp/9940";
let external_addresses = vec![addr1.parse().unwrap(), addr2.parse().unwrap()];
let listen = external_addresses.clone();
std::env::set_var(
"ASB__NETWORK__EXTERNAL_ADDRESSES",
format!("{},{}", addr1, addr2),
);
std::env::set_var("ASB__NETWORK__LISTEN", format!("{},{}", addr1, addr2));
let expected = Config {
data: Data { dir },
bitcoin: Bitcoin {
electrum_rpc_url: defaults.electrum_rpc_url,
target_block: defaults.bitcoin_confirmation_target,
finality_confirmations: None,
network: bitcoin::Network::Bitcoin,
},
network: Network {
listen,
rendezvous_point: vec![],
external_addresses,
},
monero: Monero {
wallet_rpc_url: defaults.monero_wallet_rpc_url,
finality_confirmations: None,
@ -542,7 +413,6 @@ mod tests {
max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(),
ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(),
price_ticker_ws_url: defaults.price_ticker_ws_url,
external_bitcoin_redeem_address: None,
},
};
@ -550,8 +420,5 @@ mod tests {
let actual = read_config(config_path).unwrap().unwrap();
assert_eq!(expected, actual);
std::env::remove_var("ASB__DATA__DIR");
std::env::remove_var("ASB__NETWORK__EXTERNAL_ADDRESSES");
std::env::remove_var("ASB__NETWORK__LISTEN");
}
}

@ -1,5 +1,4 @@
use crate::asb::{Behaviour, OutEvent, Rate};
use crate::monero::Amount;
use crate::network::quote::BidQuote;
use crate::network::swap_setup::alice::WalletSnapshot;
use crate::network::transfer_proof;
@ -44,7 +43,6 @@ where
latest_rate: LR,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
external_redeem_address: Option<bitcoin::Address>,
swap_sender: mpsc::Sender<Swap>,
@ -77,7 +75,6 @@ where
latest_rate: LR,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
external_redeem_address: Option<bitcoin::Address>,
) -> Result<(Self, mpsc::Receiver<Swap>)> {
let swap_channel = MpscChannels::default();
@ -91,7 +88,6 @@ where
swap_sender: swap_channel.sender,
min_buy,
max_buy,
external_redeem_address,
recv_encrypted_signature: Default::default(),
inflight_encrypted_signatures: Default::default(),
send_transfer_proof: Default::default(),
@ -168,7 +164,7 @@ where
}
};
let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet, &self.external_redeem_address, btc).await {
let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet, btc).await {
Ok(wallet_snapshot) => wallet_snapshot,
Err(error) => {
tracing::error!("Swap request will be ignored because we were unable to create wallet snapshot for swap: {:#}", error);
@ -180,7 +176,7 @@ where
let _ = responder.respond(wallet_snapshot);
}
SwarmEvent::Behaviour(OutEvent::SwapSetupCompleted{peer_id, swap_id, state3}) => {
self.handle_execution_setup_done(peer_id, swap_id, state3).await;
let _ = self.handle_execution_setup_done(peer_id, swap_id, state3).await;
}
SwarmEvent::Behaviour(OutEvent::SwapDeclined { peer, error }) => {
tracing::warn!(%peer, "Ignoring spot price request: {}", error);
@ -253,8 +249,8 @@ where
channel
}.boxed());
}
SwarmEvent::Behaviour(OutEvent::Rendezvous(libp2p::rendezvous::client::Event::Registered { rendezvous_node, ttl, namespace })) => {
tracing::info!("Successfully registered with rendezvous node: {} with namespace: {} and TTL: {:?}", rendezvous_node, namespace, ttl);
SwarmEvent::Behaviour(OutEvent::Rendezvous(libp2p::rendezvous::client::Event::Registered { .. })) => {
tracing::info!("Successfully registered with rendezvous node");
}
SwarmEvent::Behaviour(OutEvent::Rendezvous(libp2p::rendezvous::client::Event::RegisterFailed(error))) => {
tracing::error!("Registration with rendezvous node failed: {:?}", error);
@ -279,10 +275,10 @@ where
SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => {
tracing::warn!(%address, "Failed to set up connection with peer: {:#}", error);
}
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: Some(error) } => {
SwarmEvent::ConnectionClosed { peer_id: peer, num_established, endpoint, cause: Some(error) } if num_established == 0 => {
tracing::debug!(%peer, address = %endpoint.get_remote_address(), "Lost connection to peer: {:#}", error);
}
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: None } => {
SwarmEvent::ConnectionClosed { peer_id: peer, num_established, endpoint, cause: None } if num_established == 0 => {
tracing::info!(%peer, address = %endpoint.get_remote_address(), "Successfully closed connection");
}
SwarmEvent::NewListenAddr{address, ..} => {
@ -296,7 +292,7 @@ where
Some(Ok((peer, transfer_proof, responder))) => {
if !self.swarm.behaviour_mut().transfer_proof.is_connected(&peer) {
tracing::warn!(%peer, "No active connection to peer, buffering transfer proof");
self.buffered_transfer_proofs.entry(peer).or_default().push((transfer_proof, responder));
self.buffered_transfer_proofs.entry(peer).or_insert_with(Vec::new).push((transfer_proof, responder));
continue;
}
@ -330,10 +326,7 @@ where
.ask()
.context("Failed to compute asking price")?;
let balance = self.monero_wallet.get_balance().await?;
// use unlocked monero balance for quote
let xmr = Amount::from_piconero(balance.unlocked_balance);
let xmr = self.monero_wallet.get_balance().await?;
let max_bitcoin_for_monero = xmr.max_bitcoin_for_price(ask_price).ok_or_else(|| {
anyhow::anyhow!("Bitcoin price ({}) x Monero ({}) overflow", ask_price, xmr)

@ -44,9 +44,7 @@ pub mod transport {
}
pub mod behaviour {
use libp2p::swarm::behaviour::toggle::Toggle;
use super::{rendezvous::RendezvousNode, *};
use super::*;
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
@ -110,7 +108,7 @@ pub mod behaviour {
where
LR: LatestRate + Send + 'static,
{
pub rendezvous: Toggle<rendezvous::Behaviour>,
pub rendezvous: libp2p::swarm::behaviour::toggle::Toggle<rendezous::Behaviour>,
pub quote: quote::Behaviour,
pub swap_setup: alice::Behaviour<LR>,
pub transfer_proof: transfer_proof::Behaviour,
@ -134,22 +132,25 @@ pub mod behaviour {
resume_only: bool,
env_config: env::Config,
identify_params: (identity::Keypair, XmrBtcNamespace),
rendezvous_nodes: Vec<RendezvousNode>,
rendezvous_params: Option<(identity::Keypair, PeerId, Multiaddr, XmrBtcNamespace)>,
) -> Self {
let (identity, namespace) = identify_params;
let agent_version = format!("asb/{} ({})", env!("CARGO_PKG_VERSION"), namespace);
let protocol_version = "/comit/xmr/btc/1.0.0".to_string();
let identifyConfig = IdentifyConfig::new(protocol_version, identity.public())
.with_agent_version(agent_version);
let behaviour = if rendezvous_nodes.is_empty() {
None
} else {
Some(rendezvous::Behaviour::new(identity, rendezvous_nodes))
};
let agentVersion = format!("asb/{} ({})", env!("CARGO_PKG_VERSION"), identify_params.1);
let protocolVersion = "/comit/xmr/btc/1.0.0".to_string();
let identifyConfig = IdentifyConfig::new(protocolVersion, identify_params.0.public())
.with_agent_version(agentVersion);
Self {
rendezvous: Toggle::from(behaviour),
rendezvous: libp2p::swarm::behaviour::toggle::Toggle::from(rendezvous_params.map(
|(identity, rendezvous_peer_id, rendezvous_address, namespace)| {
rendezous::Behaviour::new(
identity,
rendezvous_peer_id,
rendezvous_address,
namespace,
None, // use default ttl on rendezvous point
)
},
)),
quote: quote::asb(),
swap_setup: alice::Behaviour::new(
min_buy,
@ -185,14 +186,13 @@ pub mod behaviour {
}
}
pub mod rendezvous {
pub mod rendezous {
use super::*;
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::DialError;
use std::collections::VecDeque;
use std::pin::Pin;
#[derive(Clone, PartialEq)]
#[derive(PartialEq)]
enum ConnectionStatus {
Disconnected,
Dialling,
@ -209,59 +209,39 @@ pub mod rendezvous {
pub struct Behaviour {
inner: libp2p::rendezvous::client::Behaviour,
rendezvous_nodes: Vec<RendezvousNode>,
to_dial: VecDeque<PeerId>,
}
pub struct RendezvousNode {
pub address: Multiaddr,
connection_status: ConnectionStatus,
pub peer_id: PeerId,
rendezvous_point: Multiaddr,
rendezvous_peer_id: PeerId,
namespace: XmrBtcNamespace,
registration_status: RegistrationStatus,
pub registration_ttl: Option<u64>,
pub namespace: XmrBtcNamespace,
connection_status: ConnectionStatus,
registration_ttl: Option<u64>,
}
impl RendezvousNode {
impl Behaviour {
pub fn new(
address: &Multiaddr,
peer_id: PeerId,
identity: identity::Keypair,
rendezvous_peer_id: PeerId,
rendezvous_address: Multiaddr,
namespace: XmrBtcNamespace,
registration_ttl: Option<u64>,
) -> Self {
Self {
address: address.to_owned(),
connection_status: ConnectionStatus::Disconnected,
inner: libp2p::rendezvous::client::Behaviour::new(identity),
rendezvous_point: rendezvous_address,
rendezvous_peer_id,
namespace,
peer_id,
registration_status: RegistrationStatus::RegisterOnNextConnection,
connection_status: ConnectionStatus::Disconnected,
registration_ttl,
}
}
fn set_connection(&mut self, status: ConnectionStatus) {
self.connection_status = status;
}
fn set_registration(&mut self, status: RegistrationStatus) {
self.registration_status = status;
}
}
impl Behaviour {
pub fn new(identity: identity::Keypair, rendezvous_nodes: Vec<RendezvousNode>) -> Self {
Self {
inner: libp2p::rendezvous::client::Behaviour::new(identity),
rendezvous_nodes,
to_dial: VecDeque::new(),
}
}
/// Calls the rendezvous register method of the node at node_index in the Vec of rendezvous nodes
fn register(&mut self, node_index: usize) {
let node = &self.rendezvous_nodes[node_index];
self.inner
.register(node.namespace.into(), node.peer_id, node.registration_ttl);
fn register(&mut self) {
self.inner.register(
self.namespace.into(),
self.rendezvous_peer_id,
self.registration_ttl,
);
}
}
@ -275,37 +255,31 @@ pub mod rendezvous {
}
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
for node in self.rendezvous_nodes.iter() {
if peer_id == &node.peer_id {
return vec![node.address.clone()];
}
if peer_id == &self.rendezvous_peer_id {
return vec![self.rendezvous_point.clone()];
}
vec![]
}
fn inject_connected(&mut self, peer_id: &PeerId) {
for i in 0..self.rendezvous_nodes.len() {
if peer_id == &self.rendezvous_nodes[i].peer_id {
self.rendezvous_nodes[i].set_connection(ConnectionStatus::Connected);
match &self.rendezvous_nodes[i].registration_status {
RegistrationStatus::RegisterOnNextConnection => {
self.register(i);
self.rendezvous_nodes[i].set_registration(RegistrationStatus::Pending);
}
RegistrationStatus::Registered { .. } => {}
RegistrationStatus::Pending => {}
if peer_id == &self.rendezvous_peer_id {
self.connection_status = ConnectionStatus::Connected;
match &self.registration_status {
RegistrationStatus::RegisterOnNextConnection => {
self.register();
self.registration_status = RegistrationStatus::Pending;
}
RegistrationStatus::Registered { .. } => {}
RegistrationStatus::Pending => {}
}
}
}
fn inject_disconnected(&mut self, peer_id: &PeerId) {
for i in 0..self.rendezvous_nodes.len() {
let node = &mut self.rendezvous_nodes[i];
if peer_id == &node.peer_id {
node.connection_status = ConnectionStatus::Disconnected;
}
if peer_id == &self.rendezvous_peer_id {
self.connection_status = ConnectionStatus::Disconnected;
}
}
@ -324,12 +298,9 @@ pub mod rendezvous {
_handler: Self::ProtocolsHandler,
_error: &DialError,
) {
for i in 0..self.rendezvous_nodes.len() {
let node = &mut self.rendezvous_nodes[i];
if let Some(id) = peer_id {
if id == node.peer_id {
node.connection_status = ConnectionStatus::Disconnected;
}
if let Some(id) = peer_id {
if id == self.rendezvous_peer_id {
self.connection_status = ConnectionStatus::Disconnected;
}
}
}
@ -340,73 +311,62 @@ pub mod rendezvous {
cx: &mut std::task::Context<'_>,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
if let Some(peer_id) = self.to_dial.pop_front() {
return Poll::Ready(NetworkBehaviourAction::Dial {
opts: DialOpts::peer_id(peer_id)
.condition(PeerCondition::Disconnected)
.build(),
handler: Self::ProtocolsHandler::new(Duration::from_secs(30)),
});
}
// check the status of each rendezvous node
for i in 0..self.rendezvous_nodes.len() {
let connection_status = self.rendezvous_nodes[i].connection_status.clone();
match &mut self.rendezvous_nodes[i].registration_status {
RegistrationStatus::RegisterOnNextConnection => match connection_status {
ConnectionStatus::Disconnected => {
self.rendezvous_nodes[i].set_connection(ConnectionStatus::Dialling);
self.to_dial.push_back(self.rendezvous_nodes[i].peer_id);
}
ConnectionStatus::Dialling => {}
ConnectionStatus::Connected => {
self.rendezvous_nodes[i].set_registration(RegistrationStatus::Pending);
self.register(i);
}
},
RegistrationStatus::Registered { re_register_in } => {
if let Poll::Ready(()) = re_register_in.poll_unpin(cx) {
match connection_status {
ConnectionStatus::Connected => {
self.rendezvous_nodes[i]
.set_registration(RegistrationStatus::Pending);
self.register(i);
}
ConnectionStatus::Disconnected => {
self.rendezvous_nodes[i].set_registration(
RegistrationStatus::RegisterOnNextConnection,
);
self.to_dial.push_back(self.rendezvous_nodes[i].peer_id);
}
ConnectionStatus::Dialling => {}
match &mut self.registration_status {
RegistrationStatus::RegisterOnNextConnection => match self.connection_status {
ConnectionStatus::Disconnected => {
self.connection_status = ConnectionStatus::Dialling;
return Poll::Ready(NetworkBehaviourAction::Dial {
opts: DialOpts::peer_id(self.rendezvous_peer_id)
.condition(PeerCondition::Disconnected)
.build(),
handler: Self::ProtocolsHandler::new(Duration::from_secs(30)),
});
}
ConnectionStatus::Dialling => {}
ConnectionStatus::Connected => {
self.registration_status = RegistrationStatus::Pending;
self.register();
}
},
RegistrationStatus::Registered { re_register_in } => {
if let Poll::Ready(()) = re_register_in.poll_unpin(cx) {
match self.connection_status {
ConnectionStatus::Connected => {
self.registration_status = RegistrationStatus::Pending;
self.register();
}
ConnectionStatus::Disconnected => {
self.registration_status =
RegistrationStatus::RegisterOnNextConnection;
return Poll::Ready(NetworkBehaviourAction::Dial {
opts: DialOpts::peer_id(self.rendezvous_peer_id)
.condition(PeerCondition::Disconnected)
.build(),
handler: Self::ProtocolsHandler::new(Duration::from_secs(30)),
});
}
ConnectionStatus::Dialling => {}
}
}
RegistrationStatus::Pending => {}
}
RegistrationStatus::Pending => {}
}
let inner_poll = self.inner.poll(cx, params);
// reset the timer for the specific rendezvous node if we successfully registered
// reset the timer if we successfully registered
if let Poll::Ready(NetworkBehaviourAction::GenerateEvent(
libp2p::rendezvous::client::Event::Registered {
ttl,
rendezvous_node,
..
},
libp2p::rendezvous::client::Event::Registered { ttl, .. },
)) = &inner_poll
{
if let Some(i) = self
.rendezvous_nodes
.iter()
.position(|n| &n.peer_id == rendezvous_node)
{
let half_of_ttl = Duration::from_secs(*ttl) / 2;
let re_register_in = Box::pin(tokio::time::sleep(half_of_ttl));
let status = RegistrationStatus::Registered { re_register_in };
self.rendezvous_nodes[i].set_registration(status);
}
let half_of_ttl = Duration::from_secs(*ttl) / 2;
self.registration_status = RegistrationStatus::Registered {
re_register_in: Box::pin(tokio::time::sleep(half_of_ttl)),
};
}
inner_poll
@ -420,7 +380,6 @@ pub mod rendezvous {
use futures::StreamExt;
use libp2p::rendezvous;
use libp2p::swarm::SwarmEvent;
use std::collections::HashMap;
#[tokio::test]
async fn given_no_initial_connection_when_constructed_asb_connects_and_registers_with_rendezvous_node(
@ -428,16 +387,16 @@ pub mod rendezvous {
let mut rendezvous_node = new_swarm(|_, _| {
rendezvous::server::Behaviour::new(rendezvous::server::Config::default())
});
let address = rendezvous_node.listen_on_random_memory_address().await;
let rendezvous_point = RendezvousNode::new(
&address,
rendezvous_node.local_peer_id().to_owned(),
XmrBtcNamespace::Testnet,
None,
);
let rendezvous_address = rendezvous_node.listen_on_random_memory_address().await;
let mut asb = new_swarm(|_, identity| {
super::rendezvous::Behaviour::new(identity, vec![rendezvous_point])
rendezous::Behaviour::new(
identity,
*rendezvous_node.local_peer_id(),
rendezvous_address,
XmrBtcNamespace::Testnet,
None,
)
});
asb.listen_on_random_memory_address().await; // this adds an external address
@ -469,16 +428,16 @@ pub mod rendezvous {
rendezvous::server::Config::default().with_min_ttl(2),
)
});
let address = rendezvous_node.listen_on_random_memory_address().await;
let rendezvous_point = RendezvousNode::new(
&address,
rendezvous_node.local_peer_id().to_owned(),
XmrBtcNamespace::Testnet,
Some(5),
);
let rendezvous_address = rendezvous_node.listen_on_random_memory_address().await;
let mut asb = new_swarm(|_, identity| {
super::rendezvous::Behaviour::new(identity, vec![rendezvous_point])
rendezous::Behaviour::new(
identity,
*rendezvous_node.local_peer_id(),
rendezvous_address,
XmrBtcNamespace::Testnet,
Some(5),
)
});
asb.listen_on_random_memory_address().await; // this adds an external address
@ -508,62 +467,5 @@ pub mod rendezvous {
.unwrap()
.unwrap();
}
#[tokio::test]
async fn asb_registers_multiple() {
let registration_ttl = Some(10);
let mut rendezvous_nodes = Vec::new();
let mut registrations = HashMap::new();
// register with 5 rendezvous nodes
for _ in 0..5 {
let mut rendezvous = new_swarm(|_, _| {
rendezvous::server::Behaviour::new(
rendezvous::server::Config::default().with_min_ttl(2),
)
});
let address = rendezvous.listen_on_random_memory_address().await;
let id = *rendezvous.local_peer_id();
registrations.insert(id, 0);
rendezvous_nodes.push(RendezvousNode::new(
&address,
*rendezvous.local_peer_id(),
XmrBtcNamespace::Testnet,
registration_ttl,
));
tokio::spawn(async move {
loop {
rendezvous.next().await;
}
});
}
let mut asb = new_swarm(|_, identity| {
super::rendezvous::Behaviour::new(identity, rendezvous_nodes)
});
asb.listen_on_random_memory_address().await; // this adds an external address
let handle = tokio::spawn(async move {
loop {
if let SwarmEvent::Behaviour(rendezvous::client::Event::Registered {
rendezvous_node,
..
}) = asb.select_next_some().await
{
registrations
.entry(rendezvous_node)
.and_modify(|counter| *counter += 1);
}
if registrations.iter().all(|(_, &count)| count >= 4) {
break;
}
}
});
tokio::time::timeout(Duration::from_secs(30), handle)
.await
.unwrap()
.unwrap();
}
}
}

@ -5,7 +5,7 @@ use rust_decimal::Decimal;
use std::fmt::{Debug, Display, Formatter};
/// Represents the rate at which we are willing to trade 1 XMR.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rate {
/// Represents the asking price from the market.
ask: bitcoin::Amount,
@ -29,7 +29,7 @@ impl Rate {
///
/// This applies the spread to the market asking price.
pub fn ask(&self) -> Result<bitcoin::Amount> {
let sats = self.ask.to_sat();
let sats = self.ask.as_sat();
let sats = Decimal::from(sats);
let additional_sats = sats * self.ask_spread;
@ -51,13 +51,13 @@ impl Rate {
// quote (btc) = rate * base (xmr)
// base = quote / rate
let quote_in_sats = quote.to_sat();
let quote_in_sats = quote.as_sat();
let quote_in_btc = Decimal::from(quote_in_sats)
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.to_sat()))
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.as_sat()))
.context("Division overflow")?;
let rate_in_btc = Decimal::from(rate.to_sat())
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.to_sat()))
let rate_in_btc = Decimal::from(rate.as_sat())
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.as_sat()))
.context("Division overflow")?;
let base_in_xmr = quote_in_btc
@ -105,7 +105,7 @@ mod tests {
let amount = rate.ask().unwrap();
assert_eq!(amount.to_sat(), 102);
assert_eq!(amount.as_sat(), 102);
}
#[test]

@ -31,6 +31,7 @@ use swap::asb::config::{
use swap::asb::{cancel, punish, redeem, refund, safely_abort, EventLoop, Finality, KrakenRate};
use swap::common::check_latest_version;
use swap::database::open_db;
use swap::monero::Amount;
use swap::network::rendezvous::XmrBtcNamespace;
use swap::network::swarm;
use swap::protocol::alice::{run, AliceState};
@ -102,47 +103,23 @@ async fn main() -> Result<()> {
match cmd {
Command::Start { resume_only } => {
// check and warn for duplicate rendezvous points
let mut rendezvous_addrs = config.network.rendezvous_point.clone();
let prev_len = rendezvous_addrs.len();
rendezvous_addrs.sort();
rendezvous_addrs.dedup();
let new_len = rendezvous_addrs.len();
if new_len < prev_len {
tracing::warn!(
"`rendezvous_point` config has {} duplicate entries, they are being ignored.",
prev_len - new_len
);
}
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let monero_wallet = init_monero_wallet(&config, env_config).await?;
let monero_address = monero_wallet.get_main_address();
tracing::info!(%monero_address, "Monero wallet address");
let monero = monero_wallet.get_balance().await?;
match (monero.balance, monero.unlocked_balance) {
(0, _) => {
tracing::warn!(
%monero_address,
"The Monero balance is 0, make sure to deposit funds at",
)
}
(total, 0) => {
let total = monero::Amount::from_piconero(total);
tracing::warn!(
%total,
"Unlocked Monero balance is 0, total balance is",
)
}
(total, unlocked) => {
let total = monero::Amount::from_piconero(total);
let unlocked = monero::Amount::from_piconero(unlocked);
tracing::info!(%total, %unlocked, "Monero wallet balance");
}
}
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
tracing::info!(%bitcoin_balance, "Bitcoin wallet balance");
tracing::info!(%bitcoin_balance, "Initialized Bitcoin wallet");
let monero_balance = monero_wallet.get_balance().await?;
if monero_balance == Amount::ZERO {
let monero_address = monero_wallet.get_main_address();
tracing::warn!(
%monero_address,
"The Monero balance is 0, make sure to deposit funds at",
)
} else {
tracing::info!(%monero_balance, "Initialized Monero wallet");
}
let kraken_price_updates = kraken::connect(config.maker.price_ticker_ws_url.clone())?;
@ -174,7 +151,7 @@ async fn main() -> Result<()> {
resume_only,
env_config,
namespace,
&rendezvous_addrs,
config.network.rendezvous_point,
)?;
for listen in config.network.listen.clone() {
@ -201,7 +178,6 @@ async fn main() -> Result<()> {
kraken_rate.clone(),
config.maker.min_buy_btc,
config.maker.max_buy_btc,
config.maker.external_bitcoin_redeem_address,
)
.unwrap();
@ -260,14 +236,16 @@ async fn main() -> Result<()> {
bitcoin_wallet.broadcast(signed_tx, "withdraw").await?;
}
Command::Balance => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let monero_wallet = init_monero_wallet(&config, env_config).await?;
let monero_balance = monero_wallet.get_balance().await?;
tracing::info!(%monero_balance);
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
tracing::info!(%bitcoin_balance);
tracing::info!(%bitcoin_balance, %monero_balance, "Current balance");
let monero_balance = monero_wallet.get_balance().await?;
tracing::info!(
%bitcoin_balance,
%monero_balance,
"Current balance");
}
Command::Cancel { swap_id } => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
@ -334,10 +312,11 @@ async fn init_bitcoin_wallet(
env_config: swap::env::Config,
) -> Result<bitcoin::Wallet> {
tracing::debug!("Opening Bitcoin wallet");
let data_dir = &config.data.dir;
let wallet_dir = config.data.dir.join("wallet");
let wallet = bitcoin::Wallet::new(
config.bitcoin.electrum_rpc_url.clone(),
data_dir,
&wallet_dir,
seed.derive_extended_private_key(env_config.bitcoin_network)?,
env_config,
config.bitcoin.target_block,

@ -12,15 +12,43 @@
#![forbid(unsafe_code)]
#![allow(non_snake_case)]
use anyhow::Result;
use anyhow::{bail, Context, Result};
use comfy_table::Table;
use qrcode::render::unicode;
use qrcode::QrCode;
use std::cmp::min;
use std::convert::TryInto;
use std::env;
use swap::cli::command::{parse_args_and_apply_defaults, ParseResult};
use std::future::Future;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use swap::bitcoin::TxLock;
use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command, ParseResult};
use swap::cli::{list_sellers, EventLoop, SellerStatus};
use swap::common::check_latest_version;
use swap::database::open_db;
use swap::env::Config;
use swap::libp2p_ext::MultiAddrExt;
use swap::network::quote::{BidQuote, ZeroQuoteReceived};
use swap::network::swarm;
use swap::protocol::bob;
use swap::protocol::bob::{BobState, Swap};
use swap::seed::Seed;
use swap::{bitcoin, cli, monero};
use url::Url;
use uuid::Uuid;
#[tokio::main]
async fn main() -> Result<()> {
let (context, request) = match parse_args_and_apply_defaults(env::args_os()).await? {
ParseResult::Context(context, request) => (context, request),
let Arguments {
env_config,
data_dir,
debug,
json,
cmd,
} = match parse_args_and_apply_defaults(env::args_os())? {
ParseResult::Arguments(args) => *args,
ParseResult::PrintAndExitZero { message } => {
println!("{}", message);
std::process::exit(0);
@ -30,19 +58,622 @@ async fn main() -> Result<()> {
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
eprintln!("{}", e);
}
request.call(context.clone()).await?;
context.tasks.wait_for_tasks().await?;
match cmd {
Command::BuyXmr {
seller,
bitcoin_electrum_rpc_url,
bitcoin_target_block,
bitcoin_change_address,
monero_receive_address,
monero_daemon_address,
tor_socks5_port,
namespace,
} => {
let swap_id = Uuid::new_v4();
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
let db = open_db(data_dir.join("sqlite")).await?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let (monero_wallet, _process) =
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet);
let seller_peer_id = seller
.extract_peer_id()
.context("Seller address must contain peer ID")?;
db.insert_address(seller_peer_id, seller.clone()).await?;
let behaviour = cli::Behaviour::new(
seller_peer_id,
env_config,
bitcoin_wallet.clone(),
(seed.derive_libp2p_identity(), namespace),
);
let mut swarm =
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
swarm.behaviour_mut().add_address(seller_peer_id, seller);
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
let (event_loop, mut event_loop_handle) =
EventLoop::new(swap_id, swarm, seller_peer_id)?;
let event_loop = tokio::spawn(event_loop.run());
let max_givable = || bitcoin_wallet.max_giveable(TxLock::script_size());
let estimate_fee = |amount| bitcoin_wallet.estimate_fee(TxLock::weight(), amount);
let (amount, fees) = match determine_btc_to_swap(
json,
event_loop_handle.request_quote(),
bitcoin_wallet.new_address(),
|| bitcoin_wallet.balance(),
max_givable,
|| bitcoin_wallet.sync(),
estimate_fee,
)
.await
{
Ok(val) => val,
Err(error) => match error.downcast::<ZeroQuoteReceived>() {
Ok(_) => {
bail!("Seller's XMR balance is currently too low to initiate a swap, please try again later")
}
Err(other) => bail!(other),
},
};
tracing::info!(%amount, %fees, "Determined swap amount");
db.insert_peer_id(swap_id, seller_peer_id).await?;
db.insert_monero_address(swap_id, monero_receive_address)
.await?;
let swap = Swap::new(
db,
swap_id,
bitcoin_wallet,
Arc::new(monero_wallet),
env_config,
event_loop_handle,
monero_receive_address,
bitcoin_change_address,
amount,
);
tokio::select! {
result = event_loop => {
result
.context("EventLoop panicked")?;
},
result = bob::run(swap) => {
result.context("Failed to complete swap")?;
}
}
}
Command::History => {
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let db = open_db(data_dir.join("sqlite")).await?;
let swaps = db.all().await?;
if json {
for (swap_id, state) in swaps {
let state: BobState = state.try_into()?;
tracing::info!(swap_id=%swap_id.to_string(), state=%state.to_string(), "Read swap state from database");
}
} else {
let mut table = Table::new();
table.set_header(vec!["SWAP ID", "STATE"]);
for (swap_id, state) in swaps {
let state: BobState = state.try_into()?;
table.add_row(vec![swap_id.to_string(), state.to_string()]);
}
println!("{}", table);
}
}
Command::Config => {
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
tracing::info!(path=%data_dir.display(), "Data directory");
tracing::info!(path=%format!("{}/logs", data_dir.display()), "Log files directory");
tracing::info!(path=%format!("{}/sqlite", data_dir.display()), "Sqlite file location");
tracing::info!(path=%format!("{}/seed.pem", data_dir.display()), "Seed file location");
tracing::info!(path=%format!("{}/monero", data_dir.display()), "Monero-wallet-rpc directory");
tracing::info!(path=%format!("{}/wallet", data_dir.display()), "Internal bitcoin wallet directory");
}
Command::WithdrawBtc {
bitcoin_electrum_rpc_url,
bitcoin_target_block,
amount,
address,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let amount = match amount {
Some(amount) => amount,
None => {
bitcoin_wallet
.max_giveable(address.script_pubkey().len())
.await?
}
};
let psbt = bitcoin_wallet
.send_to_address(address, amount, None)
.await?;
let signed_tx = bitcoin_wallet.sign_and_finalize(psbt).await?;
bitcoin_wallet.broadcast(signed_tx, "withdraw").await?;
}
Command::Balance {
bitcoin_electrum_rpc_url,
bitcoin_target_block,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
tracing::info!(
balance = %bitcoin_balance,
"Checked Bitcoin balance",
);
}
Command::Resume {
swap_id,
bitcoin_electrum_rpc_url,
bitcoin_target_block,
monero_daemon_address,
tor_socks5_port,
namespace,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
let db = open_db(data_dir.join("sqlite")).await?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let (monero_wallet, _process) =
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet);
let seller_peer_id = db.get_peer_id(swap_id).await?;
let seller_addresses = db.get_addresses(seller_peer_id).await?;
let behaviour = cli::Behaviour::new(
seller_peer_id,
env_config,
bitcoin_wallet.clone(),
(seed.derive_libp2p_identity(), namespace),
);
let mut swarm =
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
let our_peer_id = swarm.local_peer_id();
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
for seller_address in seller_addresses {
swarm
.behaviour_mut()
.add_address(seller_peer_id, seller_address);
}
let (event_loop, event_loop_handle) = EventLoop::new(swap_id, swarm, seller_peer_id)?;
let handle = tokio::spawn(event_loop.run());
let monero_receive_address = db.get_monero_address(swap_id).await?;
let swap = Swap::from_db(
db,
swap_id,
bitcoin_wallet,
Arc::new(monero_wallet),
env_config,
event_loop_handle,
monero_receive_address,
)
.await?;
tokio::select! {
event_loop_result = handle => {
event_loop_result?;
},
swap_result = bob::run(swap) => {
swap_result?;
}
}
}
Command::Cancel {
swap_id,
bitcoin_electrum_rpc_url,
bitcoin_target_block,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
let db = open_db(data_dir.join("sqlite")).await?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir,
env_config,
bitcoin_target_block,
)
.await?;
let (txid, _) = cli::cancel(swap_id, Arc::new(bitcoin_wallet), db).await?;
tracing::debug!("Cancel transaction successfully published with id {}", txid);
}
Command::Refund {
swap_id,
bitcoin_electrum_rpc_url,
bitcoin_target_block,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
let db = open_db(data_dir.join("sqlite")).await?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir,
env_config,
bitcoin_target_block,
)
.await?;
cli::refund(swap_id, Arc::new(bitcoin_wallet), db).await?;
}
Command::ListSellers {
rendezvous_point,
namespace,
tor_socks5_port,
} => {
let rendezvous_node_peer_id = rendezvous_point
.extract_peer_id()
.context("Rendezvous node address must contain peer ID")?;
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let identity = seed.derive_libp2p_identity();
let sellers = list_sellers(
rendezvous_node_peer_id,
rendezvous_point,
namespace,
tor_socks5_port,
identity,
)
.await?;
if json {
for seller in sellers {
match seller.status {
SellerStatus::Online(quote) => {
tracing::info!(
price = %quote.price.to_string(),
min_quantity = %quote.min_quantity.to_string(),
max_quantity = %quote.max_quantity.to_string(),
status = "Online",
address = %seller.multiaddr.to_string(),
"Fetched peer status"
);
}
SellerStatus::Unreachable => {
tracing::info!(
status = "Unreachable",
address = %seller.multiaddr.to_string(),
"Fetched peer status"
);
}
}
}
} else {
let mut table = Table::new();
table.set_header(vec![
"PRICE",
"MIN_QUANTITY",
"MAX_QUANTITY",
"STATUS",
"ADDRESS",
]);
for seller in sellers {
let row = match seller.status {
SellerStatus::Online(quote) => {
vec![
quote.price.to_string(),
quote.min_quantity.to_string(),
quote.max_quantity.to_string(),
"Online".to_owned(),
seller.multiaddr.to_string(),
]
}
SellerStatus::Unreachable => {
vec![
"???".to_owned(),
"???".to_owned(),
"???".to_owned(),
"Unreachable".to_owned(),
seller.multiaddr.to_string(),
]
}
};
table.add_row(row);
}
println!("{}", table);
}
}
Command::ExportBitcoinWallet {
bitcoin_electrum_rpc_url,
bitcoin_target_block,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read in seed file")?;
let bitcoin_wallet = init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let wallet_export = bitcoin_wallet.wallet_export("cli").await?;
tracing::info!(descriptor=%wallet_export.to_string(), "Exported bitcoin wallet");
}
Command::MoneroRecovery { swap_id } => {
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
let db = open_db(data_dir.join("sqlite")).await?;
let swap_state: BobState = db.get_state(swap_id).await?.try_into()?;
match swap_state {
BobState::Started { .. }
| BobState::SwapSetupCompleted(_)
| BobState::BtcLocked { .. }
| BobState::XmrLockProofReceived { .. }
| BobState::XmrLocked(_)
| BobState::EncSigSent(_)
| BobState::CancelTimelockExpired(_)
| BobState::BtcCancelled(_)
| BobState::BtcRefunded(_)
| BobState::BtcPunished { .. }
| BobState::SafelyAborted
| BobState::XmrRedeemed { .. } => {
bail!("Cannot print monero recovery information in state {}, only possible for BtcRedeemed", swap_state)
}
BobState::BtcRedeemed(state5) => {
let (spend_key, view_key) = state5.xmr_keys();
let address = monero::Address::standard(
env_config.monero_network,
monero::PublicKey::from_private_key(&spend_key),
monero::PublicKey::from(view_key.public()),
);
tracing::info!("Wallet address: {}", address.to_string());
let view_key = serde_json::to_string(&view_key)?;
println!("View key: {}", view_key);
println!("Spend key: {}", spend_key);
}
}
}
};
Ok(())
}
async fn init_bitcoin_wallet(
electrum_rpc_url: Url,
seed: &Seed,
data_dir: PathBuf,
env_config: Config,
bitcoin_target_block: usize,
) -> Result<bitcoin::Wallet> {
let wallet_dir = data_dir.join("wallet");
let wallet = bitcoin::Wallet::new(
electrum_rpc_url.clone(),
&wallet_dir,
seed.derive_extended_private_key(env_config.bitcoin_network)?,
env_config,
bitcoin_target_block,
)
.await
.context("Failed to initialize Bitcoin wallet")?;
wallet.sync().await?;
Ok(wallet)
}
async fn init_monero_wallet(
data_dir: PathBuf,
monero_daemon_address: String,
env_config: Config,
) -> Result<(monero::Wallet, monero::WalletRpcProcess)> {
let network = env_config.monero_network;
const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-monitoring-wallet";
let monero_wallet_rpc = monero::WalletRpc::new(data_dir.join("monero")).await?;
let monero_wallet_rpc_process = monero_wallet_rpc
.run(network, monero_daemon_address.as_str())
.await?;
let monero_wallet = monero::Wallet::open_or_create(
monero_wallet_rpc_process.endpoint(),
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
env_config,
)
.await?;
Ok((monero_wallet, monero_wallet_rpc_process))
}
fn qr_code(value: &impl ToString) -> Result<String> {
let code = QrCode::new(value.to_string())?;
let qr_code = code
.render::<unicode::Dense1x2>()
.dark_color(unicode::Dense1x2::Light)
.light_color(unicode::Dense1x2::Dark)
.build();
Ok(qr_code)
}
async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
json: bool,
bid_quote: impl Future<Output = Result<BidQuote>>,
get_new_address: impl Future<Output = Result<bitcoin::Address>>,
balance: FB,
max_giveable_fn: FMG,
sync: FS,
estimate_fee: FFE,
) -> Result<(bitcoin::Amount, bitcoin::Amount)>
where
TB: Future<Output = Result<bitcoin::Amount>>,
FB: Fn() -> TB,
TMG: Future<Output = Result<bitcoin::Amount>>,
FMG: Fn() -> TMG,
TS: Future<Output = Result<()>>,
FS: Fn() -> TS,
FFE: Fn(bitcoin::Amount) -> TFE,
TFE: Future<Output = Result<bitcoin::Amount>>,
{
tracing::debug!("Requesting quote");
let bid_quote = bid_quote.await?;
if bid_quote.max_quantity == bitcoin::Amount::ZERO {
bail!(ZeroQuoteReceived)
}
tracing::info!(
price = %bid_quote.price,
minimum_amount = %bid_quote.min_quantity,
maximum_amount = %bid_quote.max_quantity,
"Received quote",
);
let mut max_giveable = max_giveable_fn().await?;
if max_giveable == bitcoin::Amount::ZERO || max_giveable < bid_quote.min_quantity {
let deposit_address = get_new_address.await?;
let minimum_amount = bid_quote.min_quantity;
let maximum_amount = bid_quote.max_quantity;
if !json {
eprintln!("{}", qr_code(&deposit_address)?);
}
loop {
let min_outstanding = bid_quote.min_quantity - max_giveable;
let min_fee = estimate_fee(min_outstanding).await?;
let min_deposit = min_outstanding + min_fee;
tracing::info!(
"Deposit at least {} to cover the min quantity with fee!",
min_deposit
);
tracing::info!(
%deposit_address,
%min_deposit,
%max_giveable,
%minimum_amount,
%maximum_amount,
"Waiting for Bitcoin deposit",
);
max_giveable = loop {
sync().await?;
let new_max_givable = max_giveable_fn().await?;
if new_max_givable > max_giveable {
break new_max_givable;
}
tokio::time::sleep(Duration::from_secs(1)).await;
};
let new_balance = balance().await?;
tracing::info!(%new_balance, %max_giveable, "Received Bitcoin");
if max_giveable < bid_quote.min_quantity {
tracing::info!("Deposited amount is less than `min_quantity`");
continue;
}
break;
}
};
let balance = balance().await?;
let fees = balance - max_giveable;
let max_accepted = bid_quote.max_quantity;
let btc_swap_amount = min(max_giveable, max_accepted);
Ok((btc_swap_amount, fees))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::determine_btc_to_swap;
use ::bitcoin::Amount;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use swap::api::request::determine_btc_to_swap;
use swap::network::quote::BidQuote;
use std::sync::Mutex;
use swap::tracing_ext::capture_logs;
use tracing::level_filters::LevelFilter;
@ -56,7 +687,7 @@ mod tests {
let (amount, fees) = determine_btc_to_swap(
true,
quote_with_max(0.01),
async { Ok(quote_with_max(0.01)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.001)?) },
|| async {
@ -75,10 +706,10 @@ mod tests {
assert_eq!((amount, fees), (expected_amount, expected_fees));
assert_eq!(
writer.captured(),
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
INFO swap::api::request: Deposit at least 0.00001 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001 BTC max_giveable=0 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
INFO swap::api::request: Received Bitcoin new_balance=0.001 BTC max_giveable=0.0009 BTC
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
INFO swap: Deposit at least 0.00001000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
INFO swap: Received Bitcoin new_balance=0.00100000 BTC max_giveable=0.00090000 BTC
"
);
}
@ -93,7 +724,7 @@ mod tests {
let (amount, fees) = determine_btc_to_swap(
true,
quote_with_max(0.01),
async { Ok(quote_with_max(0.01)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.1001)?) },
|| async {
@ -112,10 +743,10 @@ mod tests {
assert_eq!((amount, fees), (expected_amount, expected_fees));
assert_eq!(
writer.captured(),
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
INFO swap::api::request: Deposit at least 0.00001 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001 BTC max_giveable=0 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC
INFO swap::api::request: Received Bitcoin new_balance=0.1001 BTC max_giveable=0.1 BTC
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
INFO swap: Deposit at least 0.00001000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
INFO swap: Received Bitcoin new_balance=0.10010000 BTC max_giveable=0.10000000 BTC
"
);
}
@ -130,7 +761,7 @@ mod tests {
let (amount, fees) = determine_btc_to_swap(
true,
quote_with_max(0.01),
async { Ok(quote_with_max(0.01)) },
async { panic!("should not request new address when initial balance is > 0") },
|| async { Ok(Amount::from_btc(0.005)?) },
|| async {
@ -149,7 +780,7 @@ mod tests {
assert_eq!((amount, fees), (expected_amount, expected_fees));
assert_eq!(
writer.captured(),
" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC\n"
" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC\n"
);
}
@ -163,7 +794,7 @@ mod tests {
let (amount, fees) = determine_btc_to_swap(
true,
quote_with_max(0.01),
async { Ok(quote_with_max(0.01)) },
async { panic!("should not request new address when initial balance is > 0") },
|| async { Ok(Amount::from_btc(0.1001)?) },
|| async {
@ -182,7 +813,7 @@ mod tests {
assert_eq!((amount, fees), (expected_amount, expected_fees));
assert_eq!(
writer.captured(),
" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0 BTC maximum_amount=0.01 BTC\n"
" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC\n"
);
}
@ -196,7 +827,7 @@ mod tests {
let (amount, fees) = determine_btc_to_swap(
true,
quote_with_min(0.01),
async { Ok(quote_with_min(0.01)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.0101)?) },
|| async {
@ -215,10 +846,10 @@ mod tests {
assert_eq!((amount, fees), (expected_amount, expected_fees));
assert_eq!(
writer.captured(),
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Deposit at least 0.01001 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.01001 BTC max_giveable=0 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Received Bitcoin new_balance=0.0101 BTC max_giveable=0.01 BTC
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Deposit at least 0.01001000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.01001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
"
);
}
@ -233,7 +864,7 @@ mod tests {
let (amount, fees) = determine_btc_to_swap(
true,
quote_with_min(0.01),
async { Ok(quote_with_min(0.01)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.0101)?) },
|| async {
@ -252,10 +883,10 @@ mod tests {
assert_eq!((amount, fees), (expected_amount, expected_fees));
assert_eq!(
writer.captured(),
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Deposit at least 0.00991 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00991 BTC max_giveable=0.0001 BTC minimum_amount=0.01 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Received Bitcoin new_balance=0.0101 BTC max_giveable=0.01 BTC
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Deposit at least 0.00991000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.00991000 BTC max_giveable=0.00010000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
"
);
}
@ -275,7 +906,7 @@ mod tests {
Duration::from_secs(1),
determine_btc_to_swap(
true,
quote_with_min(0.1),
async { Ok(quote_with_min(0.1)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.0101)?) },
|| async {
@ -292,13 +923,13 @@ mod tests {
assert!(matches!(error, tokio::time::error::Elapsed { .. }));
assert_eq!(
writer.captured(),
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Deposit at least 0.10001 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001 BTC max_giveable=0 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Received Bitcoin new_balance=0.0101 BTC max_giveable=0.01 BTC
INFO swap::api::request: Deposited amount is less than `min_quantity`
INFO swap::api::request: Deposit at least 0.09001 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.09001 BTC max_giveable=0.01 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Deposit at least 0.10001000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
INFO swap: Deposited amount is less than `min_quantity`
INFO swap: Deposit at least 0.09001000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.09001000 BTC max_giveable=0.01000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
"
);
}
@ -323,7 +954,7 @@ mod tests {
Duration::from_secs(10),
determine_btc_to_swap(
true,
quote_with_min(0.1),
async { Ok(quote_with_min(0.1)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.21)?) },
|| async {
@ -341,10 +972,10 @@ mod tests {
assert_eq!(
writer.captured(),
r" INFO swap::api::request: Received quote price=0.001 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Deposit at least 0.10001 BTC to cover the min quantity with fee!
INFO swap::api::request: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001 BTC max_giveable=0 BTC minimum_amount=0.1 BTC maximum_amount=184467440737.09551615 BTC
INFO swap::api::request: Received Bitcoin new_balance=0.21 BTC max_giveable=0.2 BTC
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Deposit at least 0.10001000 BTC to cover the min quantity with fee!
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 min_deposit=0.10001000 BTC max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
INFO swap: Received Bitcoin new_balance=0.21000000 BTC max_giveable=0.20000000 BTC
"
);
}
@ -358,7 +989,7 @@ mod tests {
let determination_error = determine_btc_to_swap(
true,
quote_with_max(0.00),
async { Ok(quote_with_max(0.00)) },
get_dummy_address(),
|| async { Ok(Amount::from_btc(0.0101)?) },
|| async {

@ -15,8 +15,7 @@ pub use crate::bitcoin::refund::TxRefund;
pub use crate::bitcoin::timelocks::{BlockHeight, ExpiredTimelocks};
pub use ::bitcoin::util::amount::Amount;
pub use ::bitcoin::util::psbt::PartiallySignedTransaction;
pub use ::bitcoin::{Address, AddressType, Network, Transaction, Txid};
use bitcoin::secp256k1::ecdsa;
pub use ::bitcoin::{Address, Network, Transaction, Txid};
pub use ecdsa_fun::adaptor::EncryptedSignature;
pub use ecdsa_fun::fun::Scalar;
pub use ecdsa_fun::Signature;
@ -26,8 +25,9 @@ pub use wallet::Wallet;
pub use wallet::WalletBuilder;
use crate::bitcoin::wallet::ScriptStatus;
use ::bitcoin::hashes::hex::ToHex;
use ::bitcoin::hashes::Hash;
use ::bitcoin::Sighash;
use ::bitcoin::{secp256k1, SigHash};
use anyhow::{bail, Context, Result};
use bdk::miniscript::descriptor::Wsh;
use bdk::miniscript::{Descriptor, Segwitv0};
@ -78,7 +78,7 @@ impl SecretKey {
self.inner.to_bytes()
}
pub fn sign(&self, digest: Sighash) -> Signature {
pub fn sign(&self, digest: SigHash) -> Signature {
let ecdsa = ECDSA::<Deterministic<Sha256>>::default();
ecdsa.sign(&self.inner, &digest.into_inner())
@ -98,7 +98,7 @@ impl SecretKey {
// alice now has s_a and s_b and can refund monero
// self = a, Y = S_b, digest = tx_refund
pub fn encsign(&self, Y: PublicKey, digest: Sighash) -> EncryptedSignature {
pub fn encsign(&self, Y: PublicKey, digest: SigHash) -> EncryptedSignature {
let adaptor = Adaptor::<
HashTranscript<Sha256, rand_chacha::ChaCha20Rng>,
Deterministic<Sha256>,
@ -108,7 +108,7 @@ impl SecretKey {
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
pub struct PublicKey(Point);
impl PublicKey {
@ -124,12 +124,12 @@ impl From<PublicKey> for Point {
}
}
impl TryFrom<PublicKey> for bitcoin::PublicKey {
type Error = bitcoin::util::key::Error;
fn try_from(pubkey: PublicKey) -> Result<Self, Self::Error> {
let bytes = pubkey.0.to_bytes();
bitcoin::PublicKey::from_slice(&bytes)
impl From<PublicKey> for ::bitcoin::PublicKey {
fn from(from: PublicKey) -> Self {
::bitcoin::PublicKey {
compressed: true,
key: from.0.into(),
}
}
}
@ -166,7 +166,7 @@ impl From<Scalar> for PublicKey {
pub fn verify_sig(
verification_key: &PublicKey,
transaction_sighash: &Sighash,
transaction_sighash: &SigHash,
sig: &Signature,
) -> Result<()> {
let ecdsa = ECDSA::verify_only();
@ -185,7 +185,7 @@ pub struct InvalidSignature;
pub fn verify_encsig(
verification_key: PublicKey,
encryption_key: PublicKey,
digest: &Sighash,
digest: &SigHash,
encsig: &EncryptedSignature,
) -> Result<()> {
let adaptor = Adaptor::<HashTranscript<Sha256>, Deterministic<Sha256>>::default();
@ -206,21 +206,20 @@ pub fn verify_encsig(
#[error("encrypted signature is invalid")]
pub struct InvalidEncryptedSignature;
pub fn build_shared_output_descriptor(
A: Point,
B: Point,
) -> Result<Descriptor<bitcoin::PublicKey>> {
pub fn build_shared_output_descriptor(A: Point, B: Point) -> Descriptor<bitcoin::PublicKey> {
const MINISCRIPT_TEMPLATE: &str = "c:and_v(v:pk(A),pk_k(B))";
let miniscript = MINISCRIPT_TEMPLATE
.replace('A', &A.to_string())
.replace('B', &B.to_string());
// NOTE: This shouldn't be a source of error, but maybe it is
let A = ToHex::to_hex(&secp256k1::PublicKey::from(A));
let B = ToHex::to_hex(&secp256k1::PublicKey::from(B));
let miniscript = MINISCRIPT_TEMPLATE.replace('A', &A).replace('B', &B);
let miniscript =
bdk::miniscript::Miniscript::<bitcoin::PublicKey, Segwitv0>::from_str(&miniscript)
.expect("a valid miniscript");
Ok(Descriptor::Wsh(Wsh::new(miniscript)?))
Descriptor::Wsh(Wsh::new(miniscript).expect("a valid descriptor"))
}
pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result<SecretKey> {
@ -245,72 +244,10 @@ pub fn current_epoch(
}
if tx_lock_status.is_confirmed_with(cancel_timelock) {
return ExpiredTimelocks::Cancel {
blocks_left: tx_cancel_status.blocks_left_until(punish_timelock),
};
}
ExpiredTimelocks::None {
blocks_left: tx_lock_status.blocks_left_until(cancel_timelock),
}
}
pub mod bitcoin_address {
use anyhow::{bail, Result};
use serde::Serialize;
use std::str::FromStr;
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Serialize)]
#[error("Invalid Bitcoin address provided, expected address on network {expected:?} but address provided is on {actual:?}")]
pub struct BitcoinAddressNetworkMismatch {
#[serde(with = "crate::bitcoin::network")]
expected: bitcoin::Network,
#[serde(with = "crate::bitcoin::network")]
actual: bitcoin::Network,
}
pub fn parse(addr_str: &str) -> Result<bitcoin::Address> {
let address = bitcoin::Address::from_str(addr_str)?;
if address.address_type() != Some(bitcoin::AddressType::P2wpkh) {
anyhow::bail!("Invalid Bitcoin address provided, only bech32 format is supported!")
}
Ok(address)
return ExpiredTimelocks::Cancel;
}
pub fn validate(
address: bitcoin::Address,
expected_network: bitcoin::Network,
) -> Result<bitcoin::Address> {
if address.network != expected_network {
bail!(BitcoinAddressNetworkMismatch {
expected: expected_network,
actual: address.network
});
}
Ok(address)
}
pub fn validate_is_testnet(
address: bitcoin::Address,
is_testnet: bool,
) -> Result<bitcoin::Address> {
let expected_network = if is_testnet {
bitcoin::Network::Testnet
} else {
bitcoin::Network::Bitcoin
};
validate(address, expected_network)
}
}
// Transform the ecdsa der signature bytes into a secp256kfun ecdsa signature type.
pub fn extract_ecdsa_sig(sig: &[u8]) -> Result<Signature> {
let data = &sig[..sig.len() - 1];
let sig = ecdsa::Signature::from_der(data)?.serialize_compact();
Signature::from_bytes(sig).ok_or(anyhow::anyhow!("invalid signature"))
ExpiredTimelocks::None
}
/// Bitcoin error codes: https://github.com/bitcoin/bitcoin/blob/97d3500601c1d28642347d014a6de1e38f53ae4e/src/rpc/protocol.h#L23
@ -386,10 +323,7 @@ mod tests {
use super::*;
use crate::env::{GetConfig, Regtest};
use crate::protocol::{alice, bob};
use bitcoin::secp256k1;
use ecdsa_fun::fun::marker::{NonZero, Public};
use rand::rngs::OsRng;
use std::matches;
use uuid::Uuid;
#[test]
@ -404,7 +338,7 @@ mod tests {
tx_cancel_status,
);
assert!(matches!(expired_timelock, ExpiredTimelocks::None { .. }));
assert_eq!(expired_timelock, ExpiredTimelocks::None)
}
#[test]
@ -419,7 +353,7 @@ mod tests {
tx_cancel_status,
);
assert!(matches!(expired_timelock, ExpiredTimelocks::Cancel { .. }));
assert_eq!(expired_timelock, ExpiredTimelocks::Cancel)
}
#[test]
@ -439,8 +373,8 @@ mod tests {
#[tokio::test]
async fn calculate_transaction_weights() {
let alice_wallet = WalletBuilder::new(Amount::ONE_BTC.to_sat()).build();
let bob_wallet = WalletBuilder::new(Amount::ONE_BTC.to_sat()).build();
let alice_wallet = WalletBuilder::new(Amount::ONE_BTC.as_sat()).build();
let bob_wallet = WalletBuilder::new(Amount::ONE_BTC.as_sat()).build();
let spending_fee = Amount::from_sat(1_000);
let btc_amount = Amount::from_sat(500_000);
let xmr_amount = crate::monero::Amount::from_piconero(10000);
@ -523,7 +457,7 @@ mod tests {
// transactions have 2 signatures the weight can be up to 8 bytes less than
// the static weight (4 bytes per signature).
fn assert_weight(transaction: Transaction, expected_weight: usize, tx_name: &str) {
let is_weight = transaction.weight();
let is_weight = transaction.get_weight();
assert!(
expected_weight - is_weight <= 8,
@ -534,16 +468,4 @@ mod tests {
transaction
)
}
#[test]
fn compare_point_hex() {
// secp256kfun Point and secp256k1 PublicKey should have the same bytes and hex representation
let secp = secp256k1::Secp256k1::default();
let keypair = secp256k1::KeyPair::new(&secp, &mut OsRng);
let pubkey = keypair.public_key();
let point: Point<_, Public, NonZero> = Point::from_bytes(pubkey.serialize()).unwrap();
assert_eq!(pubkey.to_string(), point.to_string());
}
}

@ -3,13 +3,10 @@ use crate::bitcoin::wallet::Watchable;
use crate::bitcoin::{
build_shared_output_descriptor, Address, Amount, BlockHeight, PublicKey, Transaction, TxLock,
};
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{
secp256k1, EcdsaSighashType, OutPoint, PackedLockTime, Script, Sequence, Sighash, TxIn, TxOut,
Txid,
};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{OutPoint, Script, SigHash, SigHashType, TxIn, TxOut, Txid};
use anyhow::Result;
use bdk::miniscript::Descriptor;
use bdk::miniscript::{Descriptor, DescriptorTrait};
use ecdsa_fun::Signature;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
@ -25,12 +22,6 @@ use std::ops::Add;
#[serde(transparent)]
pub struct CancelTimelock(u32);
impl From<CancelTimelock> for u32 {
fn from(cancel_timelock: CancelTimelock) -> Self {
cancel_timelock.0
}
}
impl CancelTimelock {
pub const fn new(number_of_blocks: u32) -> Self {
Self(number_of_blocks)
@ -71,12 +62,6 @@ impl fmt::Display for CancelTimelock {
#[serde(transparent)]
pub struct PunishTimelock(u32);
impl From<PunishTimelock> for u32 {
fn from(punish_timelock: PunishTimelock) -> Self {
punish_timelock.0
}
}
impl PunishTimelock {
pub const fn new(number_of_blocks: u32) -> Self {
Self(number_of_blocks)
@ -106,7 +91,7 @@ impl PartialEq<PunishTimelock> for u32 {
#[derive(Debug)]
pub struct TxCancel {
inner: Transaction,
digest: Sighash,
digest: SigHash,
pub(in crate::bitcoin) output_descriptor: Descriptor<::bitcoin::PublicKey>,
lock_output_descriptor: Descriptor<::bitcoin::PublicKey>,
}
@ -118,50 +103,48 @@ impl TxCancel {
A: PublicKey,
B: PublicKey,
spending_fee: Amount,
) -> Result<Self> {
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0)?;
) -> Self {
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0);
let tx_in = TxIn {
previous_output: tx_lock.as_outpoint(),
script_sig: Default::default(),
sequence: Sequence(cancel_timelock.0),
witness: Default::default(),
sequence: cancel_timelock.0,
witness: Vec::new(),
};
let tx_out = TxOut {
value: tx_lock.lock_amount().to_sat() - spending_fee.to_sat(),
value: tx_lock.lock_amount().as_sat() - spending_fee.as_sat(),
script_pubkey: cancel_output_descriptor.script_pubkey(),
};
let transaction = Transaction {
version: 2,
lock_time: PackedLockTime(0),
lock_time: 0,
input: vec![tx_in],
output: vec![tx_out],
};
let digest = SighashCache::new(&transaction)
.segwit_signature_hash(
0, // Only one input: lock_input (lock transaction)
&tx_lock.output_descriptor.script_code().expect("scriptcode"),
tx_lock.lock_amount().to_sat(),
EcdsaSighashType::All,
)
.expect("sighash");
let digest = SigHashCache::new(&transaction).signature_hash(
0, // Only one input: lock_input (lock transaction)
&tx_lock.output_descriptor.script_code(),
tx_lock.lock_amount().as_sat(),
SigHashType::All,
);
Ok(Self {
Self {
inner: transaction,
digest,
output_descriptor: cancel_output_descriptor,
lock_output_descriptor: tx_lock.output_descriptor.clone(),
})
}
}
pub fn txid(&self) -> Txid {
self.inner.txid()
}
pub fn digest(&self) -> Sighash {
pub fn digest(&self) -> SigHash {
self.digest
}
@ -215,30 +198,16 @@ impl TxCancel {
let A = ::bitcoin::PublicKey {
compressed: true,
inner: secp256k1::PublicKey::from_slice(&A.0.to_bytes())?,
key: A.0.into(),
};
let B = ::bitcoin::PublicKey {
compressed: true,
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
key: B.0.into(),
};
// The order in which these are inserted doesn't matter
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
satisfier.insert(B, (sig_b.into(), ::bitcoin::SigHashType::All));
satisfier
};
@ -258,22 +227,21 @@ impl TxCancel {
) -> Transaction {
let previous_output = self.as_outpoint();
let sequence = Sequence(sequence.map(|seq| seq.0).unwrap_or(0xFFFF_FFFF));
let tx_in = TxIn {
previous_output,
script_sig: Default::default(),
sequence,
witness: Default::default(),
sequence: sequence.map(|seq| seq.0).unwrap_or(0xFFFF_FFFF),
witness: Vec::new(),
};
let tx_out = TxOut {
value: self.amount().to_sat() - spending_fee.to_sat(),
value: self.amount().as_sat() - spending_fee.as_sat(),
script_pubkey: spend_address.script_pubkey(),
};
Transaction {
version: 2,
lock_time: PackedLockTime(0),
lock_time: 0,
input: vec![tx_in],
output: vec![tx_out],
}

@ -4,25 +4,24 @@ use crate::bitcoin::{
};
use ::bitcoin::util::psbt::PartiallySignedTransaction;
use ::bitcoin::{OutPoint, TxIn, TxOut, Txid};
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use bdk::database::BatchDatabase;
use bdk::miniscript::Descriptor;
use bdk::psbt::PsbtUtils;
use bitcoin::{PackedLockTime, Script, Sequence};
use bdk::miniscript::{Descriptor, DescriptorTrait};
use bitcoin::Script;
use serde::{Deserialize, Serialize};
const SCRIPT_SIZE: usize = 34;
const TX_LOCK_WEIGHT: usize = 485;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TxLock {
inner: PartiallySignedTransaction,
pub(in crate::bitcoin) output_descriptor: Descriptor<::bitcoin::PublicKey>,
}
impl TxLock {
pub async fn new<D, C>(
wallet: &Wallet<D, C>,
pub async fn new<B, D, C>(
wallet: &Wallet<B, D, C>,
amount: Amount,
A: PublicKey,
B: PublicKey,
@ -32,7 +31,7 @@ impl TxLock {
C: EstimateFeeRate,
D: BatchDatabase,
{
let lock_output_descriptor = build_shared_output_descriptor(A.0, B.0)?;
let lock_output_descriptor = build_shared_output_descriptor(A.0, B.0);
let address = lock_output_descriptor
.address(wallet.get_network())
.expect("can derive address from descriptor");
@ -58,15 +57,15 @@ impl TxLock {
B: PublicKey,
btc: Amount,
) -> Result<Self> {
let shared_output_candidate = match psbt.unsigned_tx.output.as_slice() {
[shared_output_candidate, _] if shared_output_candidate.value == btc.to_sat() => {
let shared_output_candidate = match psbt.global.unsigned_tx.output.as_slice() {
[shared_output_candidate, _] if shared_output_candidate.value == btc.as_sat() => {
shared_output_candidate
}
[_, shared_output_candidate] if shared_output_candidate.value == btc.to_sat() => {
[_, shared_output_candidate] if shared_output_candidate.value == btc.as_sat() => {
shared_output_candidate
}
// A single output is possible if Bob funds without any change necessary
[shared_output_candidate] if shared_output_candidate.value == btc.to_sat() => {
[shared_output_candidate] if shared_output_candidate.value == btc.as_sat() => {
shared_output_candidate
}
[_, _] => {
@ -84,7 +83,7 @@ impl TxLock {
}
};
let descriptor = build_shared_output_descriptor(A.0, B.0)?;
let descriptor = build_shared_output_descriptor(A.0, B.0);
let legit_shared_output_script = descriptor.script_pubkey();
if shared_output_candidate.script_pubkey != legit_shared_output_script {
@ -101,15 +100,6 @@ impl TxLock {
Amount::from_sat(self.inner.clone().extract_tx().output[self.lock_output_vout()].value)
}
pub fn fee(&self) -> Result<Amount> {
Ok(Amount::from_sat(
self.inner
.clone()
.fee_amount()
.context("The PSBT is missing a TxOut for an input")?,
))
}
pub fn txid(&self) -> Txid {
self.inner.clone().extract_tx().txid()
}
@ -150,15 +140,14 @@ impl TxLock {
) -> Transaction {
let previous_output = self.as_outpoint();
let sequence = Sequence(sequence.unwrap_or(0xFFFF_FFFF));
let tx_in = TxIn {
previous_output,
script_sig: Default::default(),
sequence,
witness: Default::default(),
sequence: sequence.unwrap_or(0xFFFF_FFFF),
witness: Vec::new(),
};
let fee = spending_fee.to_sat();
let fee = spending_fee.as_sat();
let tx_out = TxOut {
value: self.inner.clone().extract_tx().output[self.lock_output_vout()].value - fee,
script_pubkey: spend_address.script_pubkey(),
@ -168,7 +157,7 @@ impl TxLock {
Transaction {
version: 2,
lock_time: PackedLockTime(0),
lock_time: 0,
input: vec![tx_in],
output: vec![tx_out],
}
@ -218,12 +207,12 @@ mod tests {
let (A, B) = alice_and_bob();
let fees = 300;
let agreed_amount = Amount::from_sat(10000);
let amount = agreed_amount.to_sat() + fees;
let amount = agreed_amount.as_sat() + fees;
let wallet = WalletBuilder::new(amount).build();
let psbt = bob_make_psbt(A, B, &wallet, agreed_amount).await;
assert_eq!(
psbt.unsigned_tx.output.len(),
psbt.global.unsigned_tx.output.len(),
1,
"psbt should only have a single output"
);
@ -263,7 +252,7 @@ mod tests {
fn estimated_tx_lock_script_size_never_changes(a in crate::proptest::ecdsa_fun::point(), b in crate::proptest::ecdsa_fun::point()) {
proptest::prop_assume!(a != b);
let computed_size = build_shared_output_descriptor(a, b).unwrap().script_pubkey().len();
let computed_size = build_shared_output_descriptor(a, b).script_pubkey().len();
assert_eq!(computed_size, SCRIPT_SIZE);
}
@ -275,7 +264,7 @@ mod tests {
async fn bob_make_psbt(
A: PublicKey,
B: PublicKey,
wallet: &Wallet<bdk::database::MemoryDatabase, StaticFeeRate>,
wallet: &Wallet<(), bdk::database::MemoryDatabase, StaticFeeRate>,
amount: Amount,
) -> PartiallySignedTransaction {
let change = wallet.new_address().await.unwrap();

@ -1,16 +1,16 @@
use crate::bitcoin::wallet::Watchable;
use crate::bitcoin::{self, Address, Amount, PunishTimelock, Transaction, TxCancel, Txid};
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{secp256k1, EcdsaSighashType, Sighash};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{SigHash, SigHashType};
use anyhow::{Context, Result};
use bdk::bitcoin::Script;
use bdk::miniscript::Descriptor;
use bdk::miniscript::{Descriptor, DescriptorTrait};
use std::collections::HashMap;
#[derive(Debug)]
pub struct TxPunish {
inner: Transaction,
digest: Sighash,
digest: SigHash,
cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>,
watch_script: Script,
}
@ -25,17 +25,12 @@ impl TxPunish {
let tx_punish =
tx_cancel.build_spend_transaction(punish_address, Some(punish_timelock), spending_fee);
let digest = SighashCache::new(&tx_punish)
.segwit_signature_hash(
0, // Only one input: cancel transaction
&tx_cancel
.output_descriptor
.script_code()
.expect("scriptcode"),
tx_cancel.amount().to_sat(),
EcdsaSighashType::All,
)
.expect("sighash");
let digest = SigHashCache::new(&tx_punish).signature_hash(
0, // Only one input: cancel transaction
&tx_cancel.output_descriptor.script_code(),
tx_cancel.amount().as_sat(),
SigHashType::All,
);
Self {
inner: tx_punish,
@ -45,7 +40,7 @@ impl TxPunish {
}
}
pub fn digest(&self) -> Sighash {
pub fn digest(&self) -> SigHash {
self.digest
}
@ -61,26 +56,12 @@ impl TxPunish {
let satisfier = {
let mut satisfier = HashMap::with_capacity(2);
let A = a.public().try_into()?;
let B = B.try_into()?;
let A = a.public().into();
let B = B.into();
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
// The order in which these are inserted doesn't matter
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
satisfier.insert(B, (sig_b.into(), ::bitcoin::SigHashType::All));
satisfier
};

@ -3,12 +3,11 @@ use crate::bitcoin::{
verify_encsig, verify_sig, Address, Amount, EmptyWitnessStack, EncryptedSignature, NoInputs,
NotThreeWitnesses, PublicKey, SecretKey, TooManyInputs, Transaction, TxLock,
};
use ::bitcoin::{Sighash, Txid};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{SigHash, SigHashType, Txid};
use anyhow::{bail, Context, Result};
use bdk::miniscript::Descriptor;
use bitcoin::secp256k1;
use bitcoin::util::sighash::SighashCache;
use bitcoin::{EcdsaSighashType, Script};
use bdk::miniscript::{Descriptor, DescriptorTrait};
use bitcoin::Script;
use ecdsa_fun::adaptor::{Adaptor, HashTranscript};
use ecdsa_fun::fun::Scalar;
use ecdsa_fun::nonce::Deterministic;
@ -16,12 +15,10 @@ use ecdsa_fun::Signature;
use sha2::Sha256;
use std::collections::HashMap;
use super::extract_ecdsa_sig;
#[derive(Clone, Debug)]
pub struct TxRedeem {
inner: Transaction,
digest: Sighash,
digest: SigHash,
lock_output_descriptor: Descriptor<::bitcoin::PublicKey>,
watch_script: Script,
}
@ -32,14 +29,12 @@ impl TxRedeem {
// redeem transaction
let tx_redeem = tx_lock.build_spend_transaction(redeem_address, None, spending_fee);
let digest = SighashCache::new(&tx_redeem)
.segwit_signature_hash(
0, // Only one input: lock_input (lock transaction)
&tx_lock.output_descriptor.script_code().expect("scriptcode"),
tx_lock.lock_amount().to_sat(),
EcdsaSighashType::All,
)
.expect("sighash");
let digest = SigHashCache::new(&tx_redeem).signature_hash(
0, // Only one input: lock_input (lock transaction)
&tx_lock.output_descriptor.script_code(),
tx_lock.lock_amount().as_sat(),
SigHashType::All,
);
Self {
inner: tx_redeem,
@ -53,7 +48,7 @@ impl TxRedeem {
self.inner.txid()
}
pub fn digest(&self) -> Sighash {
pub fn digest(&self) -> SigHash {
self.digest
}
@ -66,7 +61,7 @@ impl TxRedeem {
) -> Result<Transaction> {
verify_encsig(
B,
PublicKey::from(s_a),
PublicKey::from(s_a.clone()),
&self.digest(),
&encrypted_signature,
)
@ -81,30 +76,16 @@ impl TxRedeem {
let A = ::bitcoin::PublicKey {
compressed: true,
inner: secp256k1::PublicKey::from_slice(&a.public.to_bytes())?,
key: a.public.into(),
};
let B = ::bitcoin::PublicKey {
compressed: true,
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
key: B.0.into(),
};
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
// The order in which these are inserted doesn't matter
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
satisfier.insert(B, (sig_b.into(), ::bitcoin::SigHashType::All));
satisfier
};
@ -124,16 +105,25 @@ impl TxRedeem {
let input = match candidate_transaction.input.as_slice() {
[input] => input,
[] => bail!(NoInputs),
inputs => bail!(TooManyInputs(inputs.len())),
[inputs @ ..] => bail!(TooManyInputs(inputs.len())),
};
let sigs = match input.witness.to_vec().as_slice() {
let sigs = match input
.witness
.iter()
.map(|vec| vec.as_slice())
.collect::<Vec<_>>()
.as_slice()
{
[sig_1, sig_2, _script] => [sig_1, sig_2]
.into_iter()
.map(|sig| extract_ecdsa_sig(sig))
.collect::<Result<Vec<_>, _>>(),
.iter()
.map(|sig| {
bitcoin::secp256k1::Signature::from_der(&sig[..sig.len() - 1])
.map(Signature::from)
})
.collect::<std::result::Result<Vec<_>, _>>(),
[] => bail!(EmptyWitnessStack),
witnesses => bail!(NotThreeWitnesses(witnesses.len())),
[witnesses @ ..] => bail!(NotThreeWitnesses(witnesses.len())),
}?;
let sig = sigs

@ -4,20 +4,17 @@ use crate::bitcoin::{
TooManyInputs, Transaction, TxCancel,
};
use crate::{bitcoin, monero};
use ::bitcoin::secp256k1;
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{EcdsaSighashType, Script, Sighash, Txid};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{Script, SigHash, SigHashType, Txid};
use anyhow::{bail, Context, Result};
use bdk::miniscript::Descriptor;
use bdk::miniscript::{Descriptor, DescriptorTrait};
use ecdsa_fun::Signature;
use std::collections::HashMap;
use super::extract_ecdsa_sig;
#[derive(Debug)]
pub struct TxRefund {
inner: Transaction,
digest: Sighash,
digest: SigHash,
cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>,
watch_script: Script,
}
@ -26,17 +23,12 @@ impl TxRefund {
pub fn new(tx_cancel: &TxCancel, refund_address: &Address, spending_fee: Amount) -> Self {
let tx_refund = tx_cancel.build_spend_transaction(refund_address, None, spending_fee);
let digest = SighashCache::new(&tx_refund)
.segwit_signature_hash(
0, // Only one input: cancel transaction
&tx_cancel
.output_descriptor
.script_code()
.expect("scriptcode"),
tx_cancel.amount().to_sat(),
EcdsaSighashType::All,
)
.expect("sighash");
let digest = SigHashCache::new(&tx_refund).signature_hash(
0, // Only one input: cancel transaction
&tx_cancel.output_descriptor.script_code(),
tx_cancel.amount().as_sat(),
SigHashType::All,
);
Self {
inner: tx_refund,
@ -50,7 +42,7 @@ impl TxRefund {
self.inner.txid()
}
pub fn digest(&self) -> Sighash {
pub fn digest(&self) -> SigHash {
self.digest
}
@ -64,30 +56,16 @@ impl TxRefund {
let A = ::bitcoin::PublicKey {
compressed: true,
inner: secp256k1::PublicKey::from_slice(&A.0.to_bytes())?,
key: A.0.into(),
};
let B = ::bitcoin::PublicKey {
compressed: true,
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
key: B.0.into(),
};
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
// The order in which these are inserted doesn't matter
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
satisfier.insert(B, (sig_b.into(), ::bitcoin::SigHashType::All));
satisfier
};
@ -131,16 +109,25 @@ impl TxRefund {
let input = match candidate_transaction.input.as_slice() {
[input] => input,
[] => bail!(NoInputs),
inputs => bail!(TooManyInputs(inputs.len())),
[inputs @ ..] => bail!(TooManyInputs(inputs.len())),
};
let sigs = match input.witness.to_vec().as_slice() {
let sigs = match input
.witness
.iter()
.map(|vec| vec.as_slice())
.collect::<Vec<_>>()
.as_slice()
{
[sig_1, sig_2, _script] => [sig_1, sig_2]
.into_iter()
.map(|sig| extract_ecdsa_sig(sig))
.collect::<Result<Vec<_>, _>>(),
.iter()
.map(|sig| {
bitcoin::secp256k1::Signature::from_der(&sig[..sig.len() - 1])
.map(Signature::from)
})
.collect::<std::result::Result<Vec<_>, _>>(),
[] => bail!(EmptyWitnessStack),
witnesses => bail!(NotThreeWitnesses(witnesses.len())),
[witnesses @ ..] => bail!(NotThreeWitnesses(witnesses.len())),
}?;
let sig = sigs

@ -37,9 +37,9 @@ impl Add<u32> for BlockHeight {
}
}
#[derive(Serialize, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExpiredTimelocks {
None { blocks_left: u32 },
Cancel { blocks_left: u32 },
None,
Cancel,
Punish,
}

@ -4,14 +4,14 @@ use crate::env;
use ::bitcoin::util::psbt::PartiallySignedTransaction;
use ::bitcoin::Txid;
use anyhow::{bail, Context, Result};
use bdk::blockchain::{Blockchain, ElectrumBlockchain, GetTx};
use bdk::blockchain::{noop_progress, Blockchain, ElectrumBlockchain};
use bdk::database::BatchDatabase;
use bdk::descriptor::Segwitv0;
use bdk::electrum_client::{ElectrumApi, GetHistoryRes};
use bdk::sled::Tree;
use bdk::wallet::export::FullyNodedExport;
use bdk::keys::DerivableKey;
use bdk::wallet::export::WalletExport;
use bdk::wallet::AddressIndex;
use bdk::{FeeRate, KeychainKind, SignOptions, SyncOptions};
use bitcoin::util::bip32::ExtendedPrivKey;
use bdk::{FeeRate, KeychainKind, SignOptions};
use bitcoin::{Network, Script};
use reqwest::Url;
use rust_decimal::prelude::*;
@ -24,7 +24,6 @@ use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{watch, Mutex};
use tracing::{debug_span, Instrument};
const SLED_TREE_NAME: &str = "default_tree";
@ -34,12 +33,9 @@ const MAX_RELATIVE_TX_FEE: Decimal = dec!(0.03);
const MAX_ABSOLUTE_TX_FEE: Decimal = dec!(100_000);
const DUST_AMOUNT: u64 = 546;
const WALLET: &str = "wallet";
const WALLET_OLD: &str = "wallet-old";
pub struct Wallet<D = Tree, C = Client> {
pub struct Wallet<B = ElectrumBlockchain, D = bdk::sled::Tree, C = Client> {
client: Arc<Mutex<C>>,
wallet: Arc<Mutex<bdk::Wallet<D>>>,
wallet: Arc<Mutex<bdk::Wallet<B, D>>>,
finality_confirmations: u32,
network: Network,
target_block: usize,
@ -48,33 +44,37 @@ pub struct Wallet<D = Tree, C = Client> {
impl Wallet {
pub async fn new(
electrum_rpc_url: Url,
data_dir: impl AsRef<Path>,
xprivkey: ExtendedPrivKey,
wallet_dir: &Path,
key: impl DerivableKey<Segwitv0> + Clone,
env_config: env::Config,
target_block: usize,
) -> Result<Self> {
let data_dir = data_dir.as_ref();
let wallet_dir = data_dir.join(WALLET);
let database = bdk::sled::open(wallet_dir)?.open_tree(SLED_TREE_NAME)?;
let network = env_config.bitcoin_network;
let wallet = match bdk::Wallet::new(
bdk::template::Bip84(xprivkey, KeychainKind::External),
Some(bdk::template::Bip84(xprivkey, KeychainKind::Internal)),
network,
database,
) {
Ok(w) => w,
Err(bdk::Error::ChecksumMismatch) => Self::migrate(data_dir, xprivkey, network)?,
err => err?,
};
let config = bdk::electrum_client::ConfigBuilder::default()
.retry(5)
.build();
let client = bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config)
.context("Failed to initialize Electrum RPC client")?;
let db = bdk::sled::open(wallet_dir)?.open_tree(SLED_TREE_NAME)?;
let wallet = bdk::Wallet::new(
bdk::template::Bip84(key.clone(), KeychainKind::External),
Some(bdk::template::Bip84(key, KeychainKind::Internal)),
env_config.bitcoin_network,
db,
ElectrumBlockchain::from(client),
)?;
let client = Client::new(electrum_rpc_url, env_config.bitcoin_sync_interval())?;
let electrum = bdk::electrum_client::Client::new(electrum_rpc_url.as_str())
.context("Failed to initialize Electrum RPC client")?;
let network = wallet.network();
Ok(Self {
client: Arc::new(Mutex::new(client)),
client: Arc::new(Mutex::new(Client::new(
electrum,
env_config.bitcoin_sync_interval(),
)?)),
wallet: Arc::new(Mutex::new(wallet)),
finality_confirmations: env_config.bitcoin_finality_confirmations,
network,
@ -82,32 +82,6 @@ impl Wallet {
})
}
/// Create a new database for the wallet and rename the old one.
/// This is necessary when getting a ChecksumMismatch from a wallet
/// created with an older version of BDK. Only affected Testnet wallets.
// https://github.com/comit-network/xmr-btc-swap/issues/1182
fn migrate(
data_dir: &Path,
xprivkey: ExtendedPrivKey,
network: bitcoin::Network,
) -> Result<bdk::Wallet<Tree>> {
let from = data_dir.join(WALLET);
let to = data_dir.join(WALLET_OLD);
std::fs::rename(from, to)?;
let wallet_dir = data_dir.join(WALLET);
let database = bdk::sled::open(wallet_dir)?.open_tree(SLED_TREE_NAME)?;
let wallet = bdk::Wallet::new(
bdk::template::Bip84(xprivkey, KeychainKind::External),
Some(bdk::template::Bip84(xprivkey, KeychainKind::Internal)),
network,
database,
)?;
Ok(wallet)
}
/// Broadcast the given transaction to the network and emit a log statement
/// if done so successfully.
///
@ -125,12 +99,13 @@ impl Wallet {
.subscribe_to((txid, transaction.output[0].script_pubkey.clone()))
.await;
let client = self.client.lock().await;
let blockchain = client.blockchain();
blockchain.broadcast(&transaction).with_context(|| {
format!("Failed to broadcast Bitcoin {} transaction {}", kind, txid)
})?;
self.wallet
.lock()
.await
.broadcast(&transaction)
.with_context(|| {
format!("Failed to broadcast Bitcoin {} transaction {}", kind, txid)
})?;
tracing::info!(%txid, %kind, "Published Bitcoin transaction");
@ -168,6 +143,8 @@ impl Wallet {
let mut last_status = None;
loop {
tokio::time::sleep(Duration::from_secs(5)).await;
let new_status = match client.lock().await.status_of_script(&tx) {
Ok(new_status) => new_status,
Err(error) => {
@ -188,10 +165,8 @@ impl Wallet {
return;
}
}
tokio::time::sleep(Duration::from_secs(5)).await;
}
}.instrument(debug_span!("BitcoinWalletSubscription")));
});
Subscription {
receiver,
@ -204,9 +179,9 @@ impl Wallet {
sub
}
pub async fn wallet_export(&self, role: &str) -> Result<FullyNodedExport> {
pub async fn wallet_export(&self, role: &str) -> Result<WalletExport> {
let wallet = self.wallet.lock().await;
match bdk::wallet::export::FullyNodedExport::export_wallet(
match bdk::wallet::export::WalletExport::export_wallet(
&wallet,
&format!("{}-{}", role, self.network),
true,
@ -273,7 +248,7 @@ impl Subscription {
pub async fn wait_until_confirmed_with<T>(&self, target: T) -> Result<()>
where
T: Into<u32>,
u32: PartialOrd<T>,
T: Copy,
{
self.wait_until(|status| status.is_confirmed_with(target))
@ -294,7 +269,7 @@ impl Subscription {
}
}
impl<D, C> Wallet<D, C>
impl<B, D, C> Wallet<B, D, C>
where
C: EstimateFeeRate,
D: BatchDatabase,
@ -318,7 +293,6 @@ where
Ok(tx)
}
/// Returns the total Bitcoin balance, which includes pending funds
pub async fn balance(&self) -> Result<Amount> {
let balance = self
.wallet
@ -327,7 +301,7 @@ where
.get_balance()
.context("Failed to calculate Bitcoin balance")?;
Ok(Amount::from_sat(balance.get_total()))
Ok(Amount::from_sat(balance))
}
pub async fn new_address(&self) -> Result<Address> {
@ -383,16 +357,16 @@ where
let script = address.script_pubkey();
let mut tx_builder = wallet.build_tx();
tx_builder.add_recipient(script.clone(), amount.to_sat());
tx_builder.add_recipient(script.clone(), amount.as_sat());
tx_builder.fee_rate(fee_rate);
let (psbt, _details) = tx_builder.finish()?;
let mut psbt: PartiallySignedTransaction = psbt;
match psbt.unsigned_tx.output.as_mut_slice() {
match psbt.global.unsigned_tx.output.as_mut_slice() {
// our primary output is the 2nd one? reverse the vectors
[_, second_txout] if second_txout.script_pubkey == script => {
psbt.outputs.reverse();
psbt.unsigned_tx.output.reverse();
psbt.global.unsigned_tx.output.reverse();
}
[first_txout, _] if first_txout.script_pubkey == script => {
// no need to do anything
@ -404,7 +378,7 @@ where
}
if let ([_, change], [_, psbt_output], Some(change_override)) = (
&mut psbt.unsigned_tx.output.as_mut_slice(),
&mut psbt.global.unsigned_tx.output.as_mut_slice(),
&mut psbt.outputs.as_mut_slice(),
change_override,
) {
@ -425,13 +399,13 @@ where
pub async fn max_giveable(&self, locking_script_size: usize) -> Result<Amount> {
let wallet = self.wallet.lock().await;
let balance = wallet.get_balance()?;
if balance.get_total() < DUST_AMOUNT {
if balance < DUST_AMOUNT {
return Ok(Amount::ZERO);
}
let client = self.client.lock().await;
let min_relay_fee = client.min_relay_fee()?.to_sat();
let min_relay_fee = client.min_relay_fee()?.as_sat();
if balance.get_total() < min_relay_fee {
if balance < min_relay_fee {
return Ok(Amount::ZERO);
}
@ -480,18 +454,18 @@ fn estimate_fee(
fee_rate: FeeRate,
min_relay_fee: Amount,
) -> Result<Amount> {
if transfer_amount.to_sat() <= 546 {
if transfer_amount.as_sat() <= 546 {
bail!("Amounts needs to be greater than Bitcoin dust amount.")
}
let fee_rate_svb = fee_rate.as_sat_per_vb();
let fee_rate_svb = fee_rate.as_sat_vb();
if fee_rate_svb <= 0.0 {
bail!("Fee rate needs to be > 0")
}
if fee_rate_svb > 100_000_000.0 || min_relay_fee.to_sat() > 100_000_000 {
if fee_rate_svb > 100_000_000.0 || min_relay_fee.as_sat() > 100_000_000 {
bail!("A fee_rate or min_relay_fee of > 1BTC does not make sense")
}
let min_relay_fee = if min_relay_fee.to_sat() == 0 {
let min_relay_fee = if min_relay_fee.as_sat() == 0 {
// if min_relay_fee is 0 we don't fail, we just set it to 1 satoshi;
Amount::ONE_SAT
} else {
@ -511,9 +485,9 @@ fn estimate_fee(
"Estimated fee for transaction",
);
let transfer_amount = Decimal::from(transfer_amount.to_sat());
let transfer_amount = Decimal::from(transfer_amount.as_sat());
let max_allowed_fee = transfer_amount * MAX_RELATIVE_TX_FEE;
let min_relay_fee = Decimal::from(min_relay_fee.to_sat());
let min_relay_fee = Decimal::from(min_relay_fee.as_sat());
let recommended_fee = if sats_per_vbyte < min_relay_fee {
tracing::warn!(
@ -544,32 +518,29 @@ fn estimate_fee(
Ok(amount)
}
impl<D> Wallet<D>
impl<B, D, C> Wallet<B, D, C>
where
B: Blockchain,
D: BatchDatabase,
{
pub async fn get_tx(&self, txid: Txid) -> Result<Option<Transaction>> {
let client = self.client.lock().await;
let tx = client.get_tx(&txid)?;
let tx = self.wallet.lock().await.client().get_tx(&txid)?;
Ok(tx)
}
pub async fn sync(&self) -> Result<()> {
let client = self.client.lock().await;
let blockchain = client.blockchain();
let sync_opts = SyncOptions::default();
self.wallet
.lock()
.await
.sync(blockchain, sync_opts)
.sync(noop_progress(), None)
.context("Failed to sync balance of Bitcoin wallet")?;
Ok(())
}
}
impl<D, C> Wallet<D, C> {
impl<B, D, C> Wallet<B, D, C> {
// TODO: Get rid of this by changing bounds on bdk::Wallet
pub fn get_network(&self) -> bitcoin::Network {
self.network
@ -599,7 +570,6 @@ impl EstimateFeeRate for StaticFeeRate {
}
#[cfg(test)]
#[derive(Debug)]
pub struct WalletBuilder {
utxo_amount: u64,
sats_per_vb: f32,
@ -651,9 +621,9 @@ impl WalletBuilder {
}
}
pub fn build(self) -> Wallet<bdk::database::MemoryDatabase, StaticFeeRate> {
use bdk::database::{BatchOperations, MemoryDatabase, SyncTime};
use bdk::{testutils, BlockTime};
pub fn build(self) -> Wallet<(), bdk::database::MemoryDatabase, StaticFeeRate> {
use bdk::database::MemoryDatabase;
use bdk::testutils;
let descriptors = testutils!(@descriptors (&format!("wpkh({}/*)", self.key)));
@ -668,14 +638,9 @@ impl WalletBuilder {
Some(100)
);
}
let block_time = bdk::BlockTime {
height: 100,
timestamp: 0,
};
let sync_time = SyncTime { block_time };
database.set_sync_time(sync_time).unwrap();
let wallet = bdk::Wallet::new(&descriptors.0, None, Network::Regtest, database).unwrap();
let wallet =
bdk::Wallet::new_offline(&descriptors.0, None, Network::Regtest, database).unwrap();
Wallet {
client: Arc::new(Mutex::new(StaticFeeRate {
@ -713,7 +678,6 @@ impl Watchable for (Txid, Script) {
pub struct Client {
electrum: bdk::electrum_client::Client,
blockchain: ElectrumBlockchain,
latest_block_height: BlockHeight,
last_sync: Instant,
sync_interval: Duration,
@ -722,48 +686,26 @@ pub struct Client {
}
impl Client {
fn new(electrum_rpc_url: Url, interval: Duration) -> Result<Self> {
let config = bdk::electrum_client::ConfigBuilder::default()
.retry(5)
.build();
let electrum = bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config)
.context("Failed to initialize Electrum RPC client")?;
fn new(electrum: bdk::electrum_client::Client, interval: Duration) -> Result<Self> {
// Initially fetch the latest block for storing the height.
// We do not act on this subscription after this call.
let latest_block = electrum
.block_headers_subscribe()
.context("Failed to subscribe to header notifications")?;
let client = bdk::electrum_client::Client::new(electrum_rpc_url.as_str())
.context("Failed to initialize Electrum RPC client")?;
let blockchain = ElectrumBlockchain::from(client);
let last_sync = Instant::now()
.checked_sub(interval)
.expect("no underflow since block time is only 600 secs");
Ok(Self {
electrum,
blockchain,
latest_block_height: BlockHeight::try_from(latest_block)?,
last_sync,
last_sync: Instant::now(),
sync_interval: interval,
script_history: Default::default(),
subscriptions: Default::default(),
})
}
fn blockchain(&self) -> &ElectrumBlockchain {
&self.blockchain
}
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, bdk::Error> {
self.blockchain.get_tx(txid)
}
fn update_state(&mut self, force_sync: bool) -> Result<()> {
fn update_state(&mut self) -> Result<()> {
let now = Instant::now();
if !force_sync && now < self.last_sync + self.sync_interval {
if now < self.last_sync + self.sync_interval {
return Ok(());
}
@ -783,15 +725,10 @@ impl Client {
if !self.script_history.contains_key(&script) {
self.script_history.insert(script.clone(), vec![]);
// When we first subscribe to a script we want to immediately fetch its status
// Otherwise we would have to wait for the next sync interval, which can take a minute
// This would result in potentially inaccurate status updates until that next sync interval is hit
self.update_state(true)?;
} else {
self.update_state(false)?;
}
self.update_state()?;
let history = self.script_history.entry(script).or_default();
let history_of_tx = history
@ -884,7 +821,7 @@ impl EstimateFeeRate for Client {
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ScriptStatus {
Unseen,
InMempool,
@ -901,7 +838,7 @@ impl ScriptStatus {
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Confirmed {
/// The depth of this transaction within the blockchain.
///
@ -932,20 +869,9 @@ impl Confirmed {
pub fn meets_target<T>(&self, target: T) -> bool
where
T: Into<u32>,
{
self.confirmations() >= target.into()
}
pub fn blocks_left_until<T>(&self, target: T) -> u32
where
T: Into<u32> + Copy,
u32: PartialOrd<T>,
{
if self.meets_target(target) {
0
} else {
target.into() - self.confirmations()
}
self.confirmations() >= target
}
}
@ -958,7 +884,7 @@ impl ScriptStatus {
/// Check if the script has met the given confirmation target.
pub fn is_confirmed_with<T>(&self, target: T) -> bool
where
T: Into<u32>,
u32: PartialOrd<T>,
{
match self {
ScriptStatus::Confirmed(inner) => inner.meets_target(target),
@ -966,17 +892,6 @@ impl ScriptStatus {
}
}
// Calculate the number of blocks left until the target is met.
pub fn blocks_left_until<T>(&self, target: T) -> u32
where
T: Into<u32> + Copy,
{
match self {
ScriptStatus::Confirmed(inner) => inner.blocks_left_until(target),
_ => target.into(),
}
}
pub fn has_been_seen(&self) -> bool {
matches!(self, ScriptStatus::InMempool | ScriptStatus::Confirmed(_))
}
@ -1000,7 +915,6 @@ mod tests {
use super::*;
use crate::bitcoin::{PublicKey, TxLock};
use crate::tracing_ext::capture_logs;
use bitcoin::hashes::Hash;
use proptest::prelude::*;
use tracing::level_filters::LevelFilter;
@ -1008,7 +922,7 @@ mod tests {
fn given_depth_0_should_meet_confirmation_target_one() {
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
let confirmed = script.is_confirmed_with(1_u32);
let confirmed = script.is_confirmed_with(1);
assert!(confirmed)
}
@ -1017,7 +931,7 @@ mod tests {
fn given_confirmations_1_should_meet_confirmation_target_one() {
let script = ScriptStatus::from_confirmations(1);
let confirmed = script.is_confirmed_with(1_u32);
let confirmed = script.is_confirmed_with(1);
assert!(confirmed)
}
@ -1032,33 +946,6 @@ mod tests {
assert_eq!(confirmed.depth, 0)
}
#[test]
fn given_depth_0_should_return_0_blocks_left_until_1() {
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
let blocks_left = script.blocks_left_until(1_u32);
assert_eq!(blocks_left, 0)
}
#[test]
fn given_depth_1_should_return_0_blocks_left_until_1() {
let script = ScriptStatus::Confirmed(Confirmed { depth: 1 });
let blocks_left = script.blocks_left_until(1_u32);
assert_eq!(blocks_left, 0)
}
#[test]
fn given_depth_0_should_return_1_blocks_left_until_2() {
let script = ScriptStatus::Confirmed(Confirmed { depth: 0 });
let blocks_left = script.blocks_left_until(2_u32);
assert_eq!(blocks_left, 1)
}
#[test]
fn given_one_BTC_and_100k_sats_per_vb_fees_should_not_hit_max() {
// 400 weight = 100 vbyte
@ -1127,7 +1014,7 @@ mod tests {
// weight / 4.0 * sat_per_vb would be greater than 3% hence we take total
// max allowed fee.
assert_eq!(is_fee.to_sat(), MAX_ABSOLUTE_TX_FEE.to_u64().unwrap());
assert_eq!(is_fee.as_sat(), MAX_ABSOLUTE_TX_FEE.to_u64().unwrap());
}
proptest! {
@ -1163,7 +1050,7 @@ mod tests {
let is_fee = estimate_fee(weight, amount, fee_rate, relay_fee).unwrap();
// weight / 4 * 1_000 is always lower than MAX_ABSOLUTE_TX_FEE
assert!(is_fee.to_sat() < MAX_ABSOLUTE_TX_FEE.to_u64().unwrap());
assert!(is_fee.as_sat() < MAX_ABSOLUTE_TX_FEE.to_u64().unwrap());
}
}
@ -1182,7 +1069,7 @@ mod tests {
let is_fee = estimate_fee(weight, amount, fee_rate, relay_fee).unwrap();
// weight / 4 * 1_000 is always higher than MAX_ABSOLUTE_TX_FEE
assert!(is_fee.to_sat() >= MAX_ABSOLUTE_TX_FEE.to_u64().unwrap());
assert!(is_fee.as_sat() >= MAX_ABSOLUTE_TX_FEE.to_u64().unwrap());
}
}
@ -1238,7 +1125,7 @@ mod tests {
let wallet = WalletBuilder::new(10_000).build();
let amount = wallet.max_giveable(TxLock::script_size()).await.unwrap();
assert!(amount.to_sat() > 0);
assert!(amount.as_sat() > 0);
}
/// This test ensures that the relevant script output of the transaction
@ -1308,8 +1195,7 @@ mod tests {
fn printing_status_change_doesnt_spam_on_same_status() {
let writer = capture_logs(LevelFilter::DEBUG);
let inner = bitcoin::hashes::sha256d::Hash::all_zeros();
let tx = Txid::from_hash(inner);
let tx = Txid::default();
let mut old = None;
old = Some(print_status_change(tx, old, ScriptStatus::Unseen));
old = Some(print_status_change(tx, old, ScriptStatus::InMempool));

@ -1,21 +1,22 @@
mod behaviour;
pub mod cancel_and_refund;
pub mod cancel;
pub mod command;
mod event_loop;
mod list_sellers;
pub mod refund;
pub mod tracing;
pub mod transport;
pub use behaviour::{Behaviour, OutEvent};
pub use cancel_and_refund::{cancel, cancel_and_refund, refund};
pub use cancel::cancel;
pub use event_loop::{EventLoop, EventLoopHandle};
pub use list_sellers::{list_sellers, Seller, Status as SellerStatus};
pub use refund::refund;
#[cfg(test)]
mod tests {
use super::*;
use crate::asb;
use crate::asb::rendezvous::RendezvousNode;
use crate::cli::list_sellers::{Seller, Status};
use crate::network::quote;
use crate::network::quote::BidQuote;
@ -34,8 +35,10 @@ mod tests {
async fn list_sellers_should_report_all_registered_asbs_with_a_quote() {
let namespace = XmrBtcNamespace::Mainnet;
let (rendezvous_address, rendezvous_peer_id) = setup_rendezvous_point().await;
let expected_seller_1 = setup_asb(rendezvous_peer_id, &rendezvous_address, namespace).await;
let expected_seller_2 = setup_asb(rendezvous_peer_id, &rendezvous_address, namespace).await;
let expected_seller_1 =
setup_asb(rendezvous_peer_id, rendezvous_address.clone(), namespace).await;
let expected_seller_2 =
setup_asb(rendezvous_peer_id, rendezvous_address.clone(), namespace).await;
let list_sellers = list_sellers(
rendezvous_peer_id,
@ -71,7 +74,7 @@ mod tests {
async fn setup_asb(
rendezvous_peer_id: PeerId,
rendezvous_address: &Multiaddr,
rendezvous_address: Multiaddr,
namespace: XmrBtcNamespace,
) -> Seller {
let static_quote = BidQuote {
@ -80,18 +83,18 @@ mod tests {
max_quantity: bitcoin::Amount::from_sat(9001),
};
let mut asb = new_swarm(|_, identity| {
let rendezvous_node =
RendezvousNode::new(rendezvous_address, rendezvous_peer_id, namespace, None);
let rendezvous = asb::rendezvous::Behaviour::new(identity, vec![rendezvous_node]);
StaticQuoteAsbBehaviour {
rendezvous,
ping: Default::default(),
quote: quote::asb(),
static_quote,
registered: false,
}
let mut asb = new_swarm(|_, identity| StaticQuoteAsbBehaviour {
rendezvous: asb::rendezous::Behaviour::new(
identity,
rendezvous_peer_id,
rendezvous_address,
namespace,
None,
),
ping: Default::default(),
quote: quote::asb(),
static_quote,
registered: false,
});
let asb_address = asb.listen_on_tcp_localhost().await;
@ -120,7 +123,7 @@ mod tests {
#[derive(libp2p::NetworkBehaviour)]
#[behaviour(event_process = true)]
struct StaticQuoteAsbBehaviour {
rendezvous: asb::rendezvous::Behaviour,
rendezvous: asb::rendezous::Behaviour,
// Support `Ping` as a workaround until https://github.com/libp2p/rust-libp2p/issues/2109 is fixed.
ping: libp2p::ping::Ping,
quote: quote::Behaviour,

@ -0,0 +1,56 @@
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Txid, Wallet};
use crate::protocol::bob::BobState;
use crate::protocol::Database;
use anyhow::{bail, Result};
use std::convert::TryInto;
use std::sync::Arc;
use uuid::Uuid;
pub async fn cancel(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: Arc<dyn Database>,
) -> Result<(Txid, BobState)> {
let state = db.get_state(swap_id).await?.try_into()?;
let state6 = match state {
BobState::BtcLocked { state3, .. } => state3.cancel(),
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
BobState::XmrLocked(state4) => state4.cancel(),
BobState::EncSigSent(state4) => state4.cancel(),
BobState::CancelTimelockExpired(state6) => state6,
BobState::BtcRefunded(state6) => state6,
BobState::BtcCancelled(state6) => state6,
BobState::Started { .. }
| BobState::SwapSetupCompleted(_)
| BobState::BtcRedeemed(_)
| BobState::XmrRedeemed { .. }
| BobState::BtcPunished { .. }
| BobState::SafelyAborted => bail!(
"Cannot cancel swap {} because it is in state {} which is not refundable.",
swap_id,
state
),
};
tracing::info!(%swap_id, "Manually cancelling swap");
let txid = match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
Ok(txid) => txid,
Err(err) => {
if let Ok(code) = parse_rpc_error_code(&err) {
if code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
tracing::info!("Cancel transaction has already been confirmed on chain")
}
}
bail!(err);
}
};
let state = BobState::BtcCancelled(state6);
db.insert_latest_state(swap_id, state.clone().into())
.await?;
Ok((txid, state))
}

@ -1,115 +0,0 @@
use crate::bitcoin::wallet::Subscription;
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Wallet};
use crate::protocol::bob::BobState;
use crate::protocol::Database;
use anyhow::{bail, Result};
use bitcoin::Txid;
use std::sync::Arc;
use uuid::Uuid;
pub async fn cancel_and_refund(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: Arc<dyn Database + Send + Sync>,
) -> Result<BobState> {
if let Err(err) = cancel(swap_id, bitcoin_wallet.clone(), db.clone()).await {
tracing::info!(%err, "Could not submit cancel transaction");
};
let state = match refund(swap_id, bitcoin_wallet, db).await {
Ok(s) => s,
Err(e) => bail!(e),
};
tracing::info!("Refund transaction submitted");
Ok(state)
}
pub async fn cancel(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: Arc<dyn Database + Send + Sync>,
) -> Result<(Txid, Subscription, BobState)> {
let state = db.get_state(swap_id).await?.try_into()?;
let state6 = match state {
BobState::BtcLocked { state3, .. } => state3.cancel(),
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
BobState::XmrLocked(state4) => state4.cancel(),
BobState::EncSigSent(state4) => state4.cancel(),
BobState::CancelTimelockExpired(state6) => state6,
BobState::BtcRefunded(state6) => state6,
BobState::BtcCancelled(state6) => state6,
BobState::Started { .. }
| BobState::SwapSetupCompleted(_)
| BobState::BtcRedeemed(_)
| BobState::XmrRedeemed { .. }
| BobState::BtcPunished { .. }
| BobState::SafelyAborted => bail!(
"Cannot cancel swap {} because it is in state {} which is not refundable.",
swap_id,
state
),
};
tracing::info!(%swap_id, "Manually cancelling swap");
let (txid, subscription) = match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
Ok(txid) => txid,
Err(err) => {
if let Ok(error_code) = parse_rpc_error_code(&err) {
tracing::debug!(%error_code, "parse rpc error");
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
tracing::info!("Cancel transaction has already been confirmed on chain");
} else if error_code == i64::from(RpcErrorCode::RpcVerifyError) {
tracing::info!("General error trying to submit cancel transaction");
}
}
bail!(err);
}
};
let state = BobState::BtcCancelled(state6);
db.insert_latest_state(swap_id, state.clone().into())
.await?;
Ok((txid, subscription, state))
}
pub async fn refund(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: Arc<dyn Database + Send + Sync>,
) -> Result<BobState> {
let state = db.get_state(swap_id).await?.try_into()?;
let state6 = match state {
BobState::BtcLocked { state3, .. } => state3.cancel(),
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
BobState::XmrLocked(state4) => state4.cancel(),
BobState::EncSigSent(state4) => state4.cancel(),
BobState::CancelTimelockExpired(state6) => state6,
BobState::BtcCancelled(state6) => state6,
BobState::Started { .. }
| BobState::SwapSetupCompleted(_)
| BobState::BtcRedeemed(_)
| BobState::BtcRefunded(_)
| BobState::XmrRedeemed { .. }
| BobState::BtcPunished { .. }
| BobState::SafelyAborted => bail!(
"Cannot refund swap {} because it is in state {} which is not refundable.",
swap_id,
state
),
};
tracing::info!(%swap_id, "Manually refunding swap");
state6.publish_refund_btc(bitcoin_wallet.as_ref()).await?;
let state = BobState::BtcRefunded(state6);
db.insert_latest_state(swap_id, state.clone().into())
.await?;
Ok(state)
}

File diff suppressed because it is too large Load Diff

@ -151,28 +151,28 @@ impl EventLoop {
return;
}
SwarmEvent::Behaviour(OutEvent::Failure { peer, error }) => {
tracing::warn!(%peer, err = %error, "Communication error");
tracing::warn!(%peer, "Communication error: {:#}", error);
return;
}
SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } if peer_id == self.alice_peer_id => {
tracing::info!(peer_id = %endpoint.get_remote_address(), "Connected to Alice");
tracing::info!("Connected to Alice at {}", endpoint.get_remote_address());
}
SwarmEvent::Dialing(peer_id) if peer_id == self.alice_peer_id => {
tracing::debug!(%peer_id, "Dialling Alice");
tracing::debug!("Dialling Alice at {}", peer_id);
}
SwarmEvent::ConnectionClosed { peer_id, endpoint, num_established, cause: Some(error) } if peer_id == self.alice_peer_id && num_established == 0 => {
tracing::warn!(peer_id = %endpoint.get_remote_address(), cause = %error, "Lost connection to Alice");
tracing::warn!("Lost connection to Alice at {}, cause: {}", endpoint.get_remote_address(), error);
}
SwarmEvent::ConnectionClosed { peer_id, num_established, cause: None, .. } if peer_id == self.alice_peer_id && num_established == 0 => {
// no error means the disconnection was requested
tracing::info!("Successfully closed connection to Alice");
return;
}
SwarmEvent::OutgoingConnectionError { peer_id: Some(alice_peer_id), error } if alice_peer_id == self.alice_peer_id => {
tracing::warn!(%error, "Failed to dial Alice");
SwarmEvent::OutgoingConnectionError { peer_id, error } if matches!(peer_id, Some(alice_peer_id) if alice_peer_id == self.alice_peer_id) => {
tracing::warn!( "Failed to dial Alice: {}", error);
if let Some(duration) = self.swarm.behaviour_mut().redial.until_next_redial() {
tracing::info!(seconds_until_next_redial = %duration.as_secs(), "Waiting for next redial attempt");
tracing::info!("Next redial attempt in {}s", duration.as_secs());
}
}
@ -241,7 +241,6 @@ impl EventLoopHandle {
}
pub async fn request_quote(&mut self) -> Result<BidQuote> {
tracing::debug!("Requesting quote");
Ok(self.quote.send_receive(()).await?)
}
@ -249,8 +248,9 @@ impl EventLoopHandle {
&mut self,
tx_redeem_encsig: EncryptedSignature,
) -> Result<(), bmrng::error::RequestError<EncryptedSignature>> {
self.encrypted_signature
Ok(self
.encrypted_signature
.send_receive(tx_redeem_encsig)
.await
.await?)
}
}

@ -350,26 +350,23 @@ mod tests {
list.sort();
assert_eq!(
list,
vec![
Seller {
multiaddr: "/ip4/127.0.0.1/tcp/5678".parse().unwrap(),
status: Status::Online(BidQuote {
price: Default::default(),
min_quantity: Default::default(),
max_quantity: Default::default(),
})
},
Seller {
multiaddr: Multiaddr::empty(),
status: Status::Unreachable
},
Seller {
multiaddr: "/ip4/127.0.0.1/tcp/1234".parse().unwrap(),
status: Status::Unreachable
},
]
)
assert_eq!(list, vec![
Seller {
multiaddr: "/ip4/127.0.0.1/tcp/5678".parse().unwrap(),
status: Status::Online(BidQuote {
price: Default::default(),
min_quantity: Default::default(),
max_quantity: Default::default(),
})
},
Seller {
multiaddr: Multiaddr::empty(),
status: Status::Unreachable
},
Seller {
multiaddr: "/ip4/127.0.0.1/tcp/1234".parse().unwrap(),
status: Status::Unreachable
},
])
}
}

@ -0,0 +1,43 @@
use crate::bitcoin::Wallet;
use crate::protocol::bob::BobState;
use crate::protocol::Database;
use anyhow::{bail, Result};
use std::convert::TryInto;
use std::sync::Arc;
use uuid::Uuid;
pub async fn refund(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: Arc<dyn Database>,
) -> Result<BobState> {
let state = db.get_state(swap_id).await?.try_into()?;
let state6 = match state {
BobState::BtcLocked { state3, .. } => state3.cancel(),
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
BobState::XmrLocked(state4) => state4.cancel(),
BobState::EncSigSent(state4) => state4.cancel(),
BobState::CancelTimelockExpired(state6) => state6,
BobState::BtcCancelled(state6) => state6,
BobState::Started { .. }
| BobState::SwapSetupCompleted(_)
| BobState::BtcRedeemed(_)
| BobState::BtcRefunded(_)
| BobState::XmrRedeemed { .. }
| BobState::BtcPunished { .. }
| BobState::SafelyAborted => bail!(
"Cannot refund swap {} because it is in state {} which is not refundable.",
swap_id,
state
),
};
state6.publish_refund_btc(bitcoin_wallet.as_ref()).await?;
let state = BobState::BtcRefunded(state6);
db.insert_latest_state(swap_id, state.clone().into())
.await?;
Ok(state)
}

@ -1,4 +1,5 @@
use anyhow::Result;
use std::option::Option::Some;
use std::path::Path;
use time::format_description::well_known::Rfc3339;
use tracing::subscriber::set_global_default;
@ -6,31 +7,55 @@ use tracing::{Event, Level, Subscriber};
use tracing_subscriber::fmt::format::{DefaultFields, Format, JsonFields};
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::layer::{Context, SubscriberExt};
use tracing_subscriber::{fmt, EnvFilter, Layer, Registry};
use tracing_subscriber::{fmt, EnvFilter, FmtSubscriber, Layer, Registry};
use uuid::Uuid;
pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>) -> Result<()> {
let level_filter = EnvFilter::try_new("swap=debug")?;
let registry = Registry::default().with(level_filter);
pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>, swap_id: Option<Uuid>) -> Result<()> {
if let Some(swap_id) = swap_id {
let level_filter = EnvFilter::try_new("swap=debug")?;
let appender = tracing_appender::rolling::never(dir.as_ref(), "swap-all.log");
let registry = Registry::default().with(level_filter);
let file_logger = registry.with(
fmt::layer()
.with_ansi(false)
.with_target(false)
.json()
.with_writer(appender),
);
if json && debug {
set_global_default(file_logger.with(debug_json_terminal_printer()))?;
} else if json && !debug {
set_global_default(file_logger.with(info_json_terminal_printer()))?;
} else if !json && debug {
set_global_default(file_logger.with(debug_terminal_printer()))?;
let appender =
tracing_appender::rolling::never(dir.as_ref(), format!("swap-{}.log", swap_id));
let (appender, guard) = tracing_appender::non_blocking(appender);
std::mem::forget(guard);
let file_logger = registry.with(
fmt::layer()
.with_ansi(false)
.with_target(false)
.json()
.with_writer(appender),
);
if json && debug {
set_global_default(file_logger.with(debug_json_terminal_printer()))?;
} else if json && !debug {
set_global_default(file_logger.with(info_json_terminal_printer()))?;
} else if !json && debug {
set_global_default(file_logger.with(debug_terminal_printer()))?;
} else {
set_global_default(file_logger.with(info_terminal_printer()))?;
}
} else {
set_global_default(file_logger.with(info_terminal_printer()))?;
}
let level = if debug { Level::DEBUG } else { Level::INFO };
let is_terminal = atty::is(atty::Stream::Stderr);
let builder = FmtSubscriber::builder()
.with_env_filter(format!("swap={}", level))
.with_writer(std::io::stderr)
.with_ansi(is_terminal)
.with_timer(UtcTime::rfc_3339())
.with_target(false);
if json {
builder.json().init();
} else {
builder.init();
}
};
tracing::info!("Logging initialized to {}", dir.as_ref().display());
Ok(())
@ -41,11 +66,19 @@ pub struct StdErrPrinter<L> {
level: Level,
}
type StdErrLayer<S, T> =
fmt::Layer<S, DefaultFields, Format<fmt::format::Full, T>, fn() -> std::io::Stderr>;
type StdErrLayer<S, T> = tracing_subscriber::fmt::Layer<
S,
DefaultFields,
Format<tracing_subscriber::fmt::format::Full, T>,
fn() -> std::io::Stderr,
>;
type StdErrJsonLayer<S, T> =
fmt::Layer<S, JsonFields, Format<fmt::format::Json, T>, fn() -> std::io::Stderr>;
type StdErrJsonLayer<S, T> = tracing_subscriber::fmt::Layer<
S,
JsonFields,
Format<tracing_subscriber::fmt::format::Json, T>,
fn() -> std::io::Stderr,
>;
fn debug_terminal_printer<S>() -> StdErrPrinter<StdErrLayer<S, UtcTime<Rfc3339>>> {
let is_terminal = atty::is(atty::Stream::Stderr);

@ -2,25 +2,26 @@ use anyhow::anyhow;
const LATEST_RELEASE_URL: &str = "https://github.com/comit-network/xmr-btc-swap/releases/latest";
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq)]
pub enum Version {
Current,
Available,
}
/// Check the latest release from GitHub API.
pub async fn check_latest_version(current_version: &str) -> anyhow::Result<Version> {
pub async fn check_latest_version(current: &str) -> anyhow::Result<Version> {
let response = reqwest::get(LATEST_RELEASE_URL).await?;
let e = "Failed to get latest release.";
let download_url = response.url();
let segments = download_url.path_segments().ok_or_else(|| anyhow!(e))?;
let latest_version = segments.last().ok_or_else(|| anyhow!(e))?;
let url = response.url();
let segments = url.path_segments().ok_or_else(|| anyhow!(e))?;
let latest = segments.last().ok_or_else(|| anyhow!(e))?;
let result = if is_latest_version(current_version, latest_version) {
let result = if is_latest_version(current, latest) {
Version::Current
} else {
tracing::warn!(%current_version, %latest_version, %download_url,
"You are not on the latest version",
println!(
"You are not on the latest version: {} is available. \n{}",
latest, url
);
Version::Available
};
@ -47,10 +48,10 @@ mod test {
#[tokio::test]
#[ignore = "For local testing, makes http requests to github."]
async fn it_compares_with_github() {
let result = check_latest_version("0.11.0").await.unwrap();
let result = check_latest_version("0.10.1").await.unwrap();
assert_eq!(result, Version::Available);
let result = check_latest_version("0.11.1").await.unwrap();
let result = check_latest_version("0.10.2").await.unwrap();
assert_eq!(result, Version::Current);
}
}

@ -70,7 +70,7 @@ pub enum Alice {
Done(AliceEndState),
}
#[derive(Copy, Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Copy, Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq)]
pub enum AliceEndState {
SafelyAborted,
BtcRedeemed,

@ -1,12 +1,11 @@
use crate::database::Swap;
use crate::monero::Address;
use crate::protocol::{Database, State};
use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use async_trait::async_trait;
use libp2p::{Multiaddr, PeerId};
use sqlx::sqlite::Sqlite;
use sqlx::{Pool, SqlitePool};
use std::collections::HashMap;
use std::path::Path;
use std::str::FromStr;
use time::OffsetDateTime;
@ -150,7 +149,7 @@ impl Database for SqliteDatabase {
let rows = sqlx::query!(
r#"
SELECT DISTINCT address
SELECT address
FROM peer_addresses
WHERE peer_id = ?
"#,
@ -170,25 +169,6 @@ impl Database for SqliteDatabase {
addresses
}
async fn get_swap_start_date(&self, swap_id: Uuid) -> Result<String> {
let mut conn = self.pool.acquire().await?;
let swap_id = swap_id.to_string();
let row = sqlx::query!(
r#"
SELECT min(entered_at) as start_date
FROM swap_states
WHERE swap_id = ?
"#,
swap_id
)
.fetch_one(&mut conn)
.await?;
row.start_date
.ok_or_else(|| anyhow!("Could not get swap start date"))
}
async fn insert_latest_state(&self, swap_id: Uuid, state: State) -> Result<()> {
let mut conn = self.pool.acquire().await?;
let entered_at = OffsetDateTime::now_utc();
@ -269,69 +249,6 @@ impl Database for SqliteDatabase {
result
}
async fn get_states(&self, swap_id: Uuid) -> Result<Vec<State>> {
let mut conn = self.pool.acquire().await?;
let swap_id = swap_id.to_string();
// TODO: We should use query! instead of query here to allow for at-compile-time validation
// I didn't manage to generate the mappings for the query! macro because of problems with sqlx-cli
let rows = sqlx::query!(
r#"
SELECT state
FROM swap_states
WHERE swap_id = ?
"#,
swap_id
)
.fetch_all(&mut conn)
.await?;
let result = rows
.iter()
.map(|row| {
let state_str: &str = &row.state;
let state = match serde_json::from_str::<Swap>(state_str) {
Ok(a) => Ok(State::from(a)),
Err(e) => Err(e),
}?;
Ok(state)
})
.collect::<Result<Vec<State>>>();
result
}
async fn raw_all(&self) -> Result<HashMap<Uuid, Vec<serde_json::Value>>> {
let mut conn = self.pool.acquire().await?;
let rows = sqlx::query!(
r#"
SELECT swap_id, state
FROM swap_states
"#
)
.fetch_all(&mut conn)
.await?;
let mut swaps: HashMap<Uuid, Vec<serde_json::Value>> = HashMap::new();
for row in &rows {
let swap_id = Uuid::from_str(&row.swap_id)?;
let state = serde_json::from_str(&row.state)?;
if let std::collections::hash_map::Entry::Vacant(e) = swaps.entry(swap_id) {
e.insert(vec![state]);
} else {
swaps
.get_mut(&swap_id)
.ok_or_else(|| anyhow!("Error while retrieving the swap"))?
.push(state);
}
}
Ok(swaps)
}
}
#[cfg(test)]

@ -5,7 +5,7 @@ use std::cmp::max;
use std::time::Duration;
use time::ext::NumericalStdDuration;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Copy, Clone, PartialEq, Serialize)]
pub struct Config {
pub bitcoin_lock_mempool_timeout: Duration,
pub bitcoin_lock_confirmed_timeout: Duration,
@ -46,7 +46,7 @@ pub struct Regtest;
impl GetConfig for Mainnet {
fn get_config() -> Config {
Config {
bitcoin_lock_mempool_timeout: 10.std_minutes(),
bitcoin_lock_mempool_timeout: 3.std_minutes(),
bitcoin_lock_confirmed_timeout: 2.std_hours(),
bitcoin_finality_confirmations: 1,
bitcoin_avg_block_time: 10.std_minutes(),
@ -63,7 +63,7 @@ impl GetConfig for Mainnet {
impl GetConfig for Testnet {
fn get_config() -> Config {
Config {
bitcoin_lock_mempool_timeout: 10.std_minutes(),
bitcoin_lock_mempool_timeout: 3.std_minutes(),
bitcoin_lock_confirmed_timeout: 1.std_hours(),
bitcoin_finality_confirmations: 1,
bitcoin_avg_block_time: 10.std_minutes(),

@ -230,7 +230,7 @@ mod wire {
use bitcoin::util::amount::ParseAmountError;
use serde_json::Value;
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "event")]
pub enum Event {
#[serde(rename = "systemStatus")]

@ -16,7 +16,6 @@
missing_copy_implementations
)]
pub mod api;
pub mod asb;
pub mod bitcoin;
pub mod cli;
@ -29,7 +28,6 @@ pub mod libp2p_ext;
pub mod monero;
pub mod network;
pub mod protocol;
pub mod rpc;
pub mod seed;
pub mod tor;
pub mod tracing_ext;

@ -39,16 +39,9 @@ pub fn private_key_from_secp256k1_scalar(scalar: bitcoin::Scalar) -> PrivateKey
PrivateKey::from_scalar(Scalar::from_bytes_mod_order(bytes))
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub struct PrivateViewKey(#[serde(with = "monero_private_key")] PrivateKey);
impl fmt::Display for PrivateViewKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Delegate to the Display implementation of PrivateKey
write!(f, "{}", self.0)
}
}
impl PrivateViewKey {
pub fn new_random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let scalar = Scalar::random(rng);
@ -85,7 +78,7 @@ impl From<PublicViewKey> for PublicKey {
#[derive(Clone, Copy, Debug)]
pub struct PublicViewKey(PublicKey);
#[derive(Debug, Copy, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd)]
#[derive(Debug, Copy, Clone, Deserialize, Serialize, PartialEq, PartialOrd)]
pub struct Amount(u64);
// Median tx fees on Monero as found here: https://www.monero.how/monero-transaction-fees, XMR 0.000_008 * 2 (to be on the safe side)
@ -117,7 +110,7 @@ impl Amount {
}
// safely convert the BTC/XMR rate to sat/pico
let ask_sats = Decimal::from(ask_price.to_sat());
let ask_sats = Decimal::from(ask_price.as_sat());
let pico_per_xmr = Decimal::from(PICONERO_OFFSET);
let ask_sats_per_pico = ask_sats / pico_per_xmr;
@ -192,7 +185,7 @@ impl fmt::Display for Amount {
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct TransferProof {
tx_hash: TxHash,
#[serde(with = "monero_private_key")]
@ -212,7 +205,7 @@ impl TransferProof {
}
// TODO: add constructor/ change String to fixed length byte array
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct TxHash(pub String);
impl From<TxHash> for String {
@ -234,7 +227,7 @@ pub struct InsufficientFunds {
pub actual: Amount,
}
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
#[derive(thiserror::Error, Debug, Clone, PartialEq)]
#[error("Overflow, cannot convert {0} to u64")]
pub struct OverflowError(pub String);
@ -327,52 +320,6 @@ pub mod monero_amount {
}
}
pub mod monero_address {
use anyhow::{bail, Context, Result};
use std::str::FromStr;
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq)]
#[error("Invalid monero address provided, expected address on network {expected:?} but address provided is on {actual:?}")]
pub struct MoneroAddressNetworkMismatch {
pub expected: monero::Network,
pub actual: monero::Network,
}
pub fn parse(s: &str) -> Result<monero::Address> {
monero::Address::from_str(s).with_context(|| {
format!(
"Failed to parse {} as a monero address, please make sure it is a valid address",
s
)
})
}
pub fn validate(
address: monero::Address,
expected_network: monero::Network,
) -> Result<monero::Address> {
if address.network != expected_network {
bail!(MoneroAddressNetworkMismatch {
expected: expected_network,
actual: address.network,
});
}
Ok(address)
}
pub fn validate_is_testnet(
address: monero::Address,
is_testnet: bool,
) -> Result<monero::Address> {
let expected_network = if is_testnet {
monero::Network::Stagenet
} else {
monero::Network::Mainnet
};
validate(address, expected_network)
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -523,10 +470,10 @@ mod tests {
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct MoneroPrivateKey(#[serde(with = "monero_private_key")] crate::monero::PrivateKey);
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct MoneroAmount(#[serde(with = "monero_amount")] crate::monero::Amount);
#[test]

@ -26,16 +26,15 @@ impl Wallet {
pub async fn open_or_create(url: Url, name: String, env_config: Config) -> Result<Self> {
let client = wallet::Client::new(url)?;
match client.open_wallet(name.clone()).await {
Err(error) => {
tracing::debug!(%error, "Open wallet response error");
client.create_wallet(name.clone(), "English".to_owned()).await.context(
"Unable to create Monero wallet, please ensure that the monero-wallet-rpc is available",
)?;
tracing::debug!(monero_wallet_name = %name, "Created Monero wallet");
}
Ok(_) => tracing::debug!(monero_wallet_name = %name, "Opened Monero wallet"),
let open_wallet_response = client.open_wallet(name.clone()).await;
if open_wallet_response.is_err() {
client.create_wallet(name.clone(), "English".to_owned()).await.context(
"Unable to create Monero wallet, please ensure that the monero-wallet-rpc is available",
)?;
tracing::debug!(monero_wallet_name = %name, "Created Monero wallet");
} else {
tracing::debug!(monero_wallet_name = %name, "Opened Monero wallet");
}
Self::connect(client, name, env_config).await
@ -174,6 +173,11 @@ impl Wallet {
pub async fn transfer(&self, request: TransferRequest) -> Result<TransferProof> {
let inner = self.inner.lock().await;
inner
.open_wallet(self.name.clone())
.await
.with_context(|| format!("Failed to open wallet {}", self.name))?;
let TransferRequest {
public_spend_key,
public_view_key,
@ -249,8 +253,10 @@ impl Wallet {
}
/// Get the balance of the primary account.
pub async fn get_balance(&self) -> Result<wallet::GetBalance> {
Ok(self.inner.lock().await.get_balance(0).await?)
pub async fn get_balance(&self) -> Result<Amount> {
let amount = self.inner.lock().await.get_balance(0).await?.balance;
Ok(Amount::from_piconero(amount))
}
pub async fn block_height(&self) -> Result<BlockHeight> {

@ -1,75 +1,37 @@
use ::monero::Network;
use anyhow::{bail, Context, Error, Result};
use anyhow::{Context, Result};
use big_bytes::BigByte;
use data_encoding::HEXLOWER;
use futures::{StreamExt, TryStreamExt};
use monero_rpc::wallet::{Client, MoneroWalletRpc as _};
use reqwest::header::CONTENT_LENGTH;
use reqwest::Url;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;
use tokio::fs::{remove_file, OpenOptions};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, Command};
use tokio_util::codec::{BytesCodec, FramedRead};
use tokio_util::io::StreamReader;
// See: https://www.moneroworld.com/#nodes, https://monero.fail
// We don't need any testnet nodes because we don't support testnet at all
const MONERO_DAEMONS: [MoneroDaemon; 17] = [
MoneroDaemon::new("xmr-node.cakewallet.com", 18081, Network::Mainnet),
MoneroDaemon::new("nodex.monerujo.io", 18081, Network::Mainnet),
MoneroDaemon::new("node.moneroworld.com", 18089, Network::Mainnet),
MoneroDaemon::new("nodes.hashvault.pro", 18081, Network::Mainnet),
MoneroDaemon::new("p2pmd.xmrvsbeast.com", 18081, Network::Mainnet),
MoneroDaemon::new("node.monerodevs.org", 18089, Network::Mainnet),
MoneroDaemon::new("xmr-node-usa-east.cakewallet.com", 18081, Network::Mainnet),
MoneroDaemon::new("xmr-node-uk.cakewallet.com", 18081, Network::Mainnet),
MoneroDaemon::new("node.community.rino.io", 18081, Network::Mainnet),
MoneroDaemon::new("testingjohnross.com", 20031, Network::Mainnet),
MoneroDaemon::new("xmr.litepay.ch", 18081, Network::Mainnet),
MoneroDaemon::new("node.trocador.app", 18089, Network::Mainnet),
MoneroDaemon::new("stagenet.xmr-tw.org", 38081, Network::Stagenet),
MoneroDaemon::new("node.monerodevs.org", 38089, Network::Stagenet),
MoneroDaemon::new("singapore.node.xmr.pm", 38081, Network::Stagenet),
MoneroDaemon::new("xmr-lux.boldsuck.org", 38081, Network::Stagenet),
MoneroDaemon::new("stagenet.community.rino.io", 38081, Network::Stagenet),
];
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
compile_error!("unsupported operating system");
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.3.1.tar.bz2";
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
const DOWNLOAD_HASH: &str = "7f8bd9364ef16482b418aa802a65be0e4cc660c794bb5d77b2d17bc84427883a";
const DOWNLOAD_URL: &str = "http://downloads.getmonero.org/cli/monero-mac-x64-v0.18.0.0.tar.bz2";
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.3.1.tar.bz2";
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
const DOWNLOAD_HASH: &str = "915288b023cb5811e626e10052adc6ac5323dd283c5a25b91059b0fb86a21fb6";
#[cfg(all(target_os = "macos", target_arch = "arm"))]
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.0.0.tar.bz2";
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.3.1.tar.bz2";
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
const DOWNLOAD_HASH: &str = "23af572fdfe3459b9ab97e2e9aa7e3c11021c955d6064b801a27d7e8c21ae09d";
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.0.0.tar.bz2";
#[cfg(all(target_os = "linux", target_arch = "arm"))]
const DOWNLOAD_URL: &str =
"https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2";
#[cfg(all(target_os = "linux", target_arch = "arm"))]
const DOWNLOAD_HASH: &str = "2ea2c8898cbab88f49423f4f6c15f2a94046cb4bbe827493dd061edc0fd5f1ca";
"https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.0.0.tar.bz2";
#[cfg(target_os = "windows")]
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.3.1.zip";
#[cfg(target_os = "windows")]
const DOWNLOAD_HASH: &str = "35dcc4bee4caad3442659d37837e0119e4649a77f2e3b5e80dd6d9b8fc4fb6ad";
const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.0.0.zip";
#[cfg(any(target_os = "macos", target_os = "linux"))]
const PACKED_FILE: &str = "monero-wallet-rpc";
@ -77,7 +39,7 @@ const PACKED_FILE: &str = "monero-wallet-rpc";
#[cfg(target_os = "windows")]
const PACKED_FILE: &str = "monero-wallet-rpc.exe";
const WALLET_RPC_VERSION: &str = "v0.18.3.1";
const CODENAME: &str = "Fluorine Fermi";
#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("monero wallet rpc executable not found in downloaded archive")]
@ -88,91 +50,6 @@ pub struct WalletRpcProcess {
port: u16,
}
struct MoneroDaemon {
address: &'static str,
port: u16,
network: Network,
}
impl MoneroDaemon {
const fn new(address: &'static str, port: u16, network: Network) -> Self {
Self {
address,
port,
network,
}
}
/// Checks if the Monero daemon is available by sending a request to its `get_info` endpoint.
async fn is_available(&self, client: &reqwest::Client) -> Result<bool, Error> {
let url = format!("http://{}:{}/get_info", self.address, self.port);
let res = client
.get(url)
.send()
.await
.context("Failed to send request to get_info endpoint")?;
let json: MoneroDaemonGetInfoResponse = res
.json()
.await
.context("Failed to deserialize daemon get_info response")?;
let is_status_ok = json.status == "OK";
let is_synchronized = json.synchronized;
let is_correct_network = match self.network {
Network::Mainnet => json.mainnet,
Network::Stagenet => json.stagenet,
Network::Testnet => json.testnet,
};
Ok(is_status_ok && is_synchronized && is_correct_network)
}
}
impl Display for MoneroDaemon {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.address, self.port)
}
}
#[derive(Deserialize)]
struct MoneroDaemonGetInfoResponse {
status: String,
synchronized: bool,
mainnet: bool,
stagenet: bool,
testnet: bool,
}
/// Chooses an available Monero daemon based on the specified network.
async fn choose_monero_daemon(network: Network) -> Result<&'static MoneroDaemon, Error> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.https_only(false)
.build()?;
// We only want to check for daemons that match the specified network
let network_matching_daemons = MONERO_DAEMONS
.iter()
.filter(|daemon| daemon.network == network);
for daemon in network_matching_daemons {
match daemon.is_available(&client).await {
Ok(true) => {
tracing::debug!(%daemon, "Found available Monero daemon");
return Ok(daemon);
}
Err(err) => {
tracing::debug!(%err, %daemon, "Failed to connect to Monero daemon");
continue;
}
Ok(false) => continue,
}
}
bail!("No Monero daemon could be found. Please specify one manually or try again later.")
}
impl WalletRpcProcess {
pub fn endpoint(&self) -> Url {
Url::parse(&format!("http://127.0.0.1:{}/json_rpc", self.port))
@ -209,7 +86,7 @@ impl WalletRpc {
let version = String::from_utf8_lossy(&output.stdout);
tracing::debug!("RPC version output: {}", version);
if !version.contains(WALLET_RPC_VERSION) {
if !version.contains(CODENAME) {
tracing::info!("Removing old version of monero-wallet-rpc");
tokio::fs::remove_file(exec_path).await?;
}
@ -233,20 +110,13 @@ impl WalletRpc {
.parse::<u64>()?;
tracing::info!(
progress="0%",
size=%content_length.big_byte(2),
download_url=DOWNLOAD_URL,
"Downloading monero-wallet-rpc",
"Downloading monero-wallet-rpc ({}) from {}",
content_length.big_byte(2),
DOWNLOAD_URL
);
let mut hasher = Sha256::new();
let byte_stream = response
.bytes_stream()
.map_ok(|bytes| {
hasher.update(&bytes);
bytes
})
.map_err(|err| std::io::Error::new(ErrorKind::Other, err));
#[cfg(not(target_os = "windows"))]
@ -269,34 +139,10 @@ impl WalletRpc {
let total = 3 * content_length;
let percent = 100 * received as u64 / total;
if percent != notified && percent % 10 == 0 {
tracing::info!(
progress=format!("{}%", percent),
size=%content_length.big_byte(2),
download_url=DOWNLOAD_URL,
"Downloading monero-wallet-rpc",
);
tracing::debug!("{}%", percent);
notified = percent;
}
file.write_all(&bytes).await?;
}
tracing::info!(
progress="100%",
size=%content_length.big_byte(2),
download_url=DOWNLOAD_URL,
"Downloading monero-wallet-rpc",
);
let result = hasher.finalize();
let result_hash = HEXLOWER.encode(result.as_ref());
if result_hash != DOWNLOAD_HASH {
bail!(
"SHA256 of download ({}) does not match expected ({})!",
result_hash,
DOWNLOAD_HASH
);
} else {
tracing::debug!("Hashes match");
file.write(&bytes).await?;
}
file.flush().await?;
@ -307,23 +153,13 @@ impl WalletRpc {
Ok(monero_wallet_rpc)
}
pub async fn run(
&self,
network: Network,
daemon_address: Option<String>,
) -> Result<WalletRpcProcess> {
pub async fn run(&self, network: Network, daemon_address: &str) -> Result<WalletRpcProcess> {
let port = tokio::net::TcpListener::bind("127.0.0.1:0")
.await?
.local_addr()?
.port();
let daemon_address = match daemon_address {
Some(daemon_address) => daemon_address,
None => choose_monero_daemon(network).await?.to_string(),
};
tracing::debug!(
%daemon_address,
%port,
"Starting monero-wallet-rpc"
);
@ -396,6 +232,7 @@ impl WalletRpc {
#[cfg(not(target_os = "windows"))]
async fn extract_archive(monero_wallet_rpc: &Self) -> Result<()> {
use anyhow::bail;
use tokio_tar::Archive;
let mut options = OpenOptions::new();
@ -460,123 +297,3 @@ impl WalletRpc {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn extract_host_and_port(address: String) -> (&'static str, u16) {
let parts: Vec<&str> = address.split(':').collect();
if parts.len() == 2 {
let host = parts[0].to_string();
let port = parts[1].parse::<u16>().unwrap();
let static_str_host: &'static str = Box::leak(host.into_boxed_str());
return (static_str_host, port);
}
panic!("Could not extract host and port from address: {}", address)
}
#[tokio::test]
async fn test_is_daemon_available_success() {
let mut server = mockito::Server::new();
let _ = server
.mock("GET", "/get_info")
.with_status(200)
.with_body(
r#"
{
"status": "OK",
"synchronized": true,
"mainnet": true,
"stagenet": false,
"testnet": false
}
"#,
)
.create();
let (host, port) = extract_host_and_port(server.host_with_port());
let client = reqwest::Client::new();
let result = MoneroDaemon::new(host, port, Network::Mainnet)
.is_available(&client)
.await;
assert!(result.is_ok());
assert!(result.unwrap());
}
#[tokio::test]
async fn test_is_daemon_available_wrong_network_failure() {
let mut server = mockito::Server::new();
let _ = server
.mock("GET", "/get_info")
.with_status(200)
.with_body(
r#"
{
"status": "OK",
"synchronized": true,
"mainnet": true,
"stagenet": false,
"testnet": false
}
"#,
)
.create();
let (host, port) = extract_host_and_port(server.host_with_port());
let client = reqwest::Client::new();
let result = MoneroDaemon::new(host, port, Network::Stagenet)
.is_available(&client)
.await;
assert!(result.is_ok());
assert!(!result.unwrap());
}
#[tokio::test]
async fn test_is_daemon_available_not_synced_failure() {
let mut server = mockito::Server::new();
let _ = server
.mock("GET", "/get_info")
.with_status(200)
.with_body(
r#"
{
"status": "OK",
"synchronized": false,
"mainnet": true,
"stagenet": false,
"testnet": false
}
"#,
)
.create();
let (host, port) = extract_host_and_port(server.host_with_port());
let client = reqwest::Client::new();
let result = MoneroDaemon::new(host, port, Network::Mainnet)
.is_available(&client)
.await;
assert!(result.is_ok());
assert!(!result.unwrap());
}
#[tokio::test]
async fn test_is_daemon_available_network_error_failure() {
let client = reqwest::Client::new();
let result = MoneroDaemon::new("does.not.exist.com", 18081, Network::Mainnet)
.is_available(&client)
.await;
assert!(result.is_err());
}
}

@ -1,5 +1,5 @@
use crate::bitcoin::Scalar;
use ecdsa_fun::fun::marker::{NonZero, Secret};
use ecdsa_fun::fun::marker::{Mark, NonZero, Secret};
pub trait ScalarExt {
fn to_secpfun_scalar(&self) -> ecdsa_fun::fun::Scalar;
@ -14,7 +14,7 @@ impl ScalarExt for crate::monero::Scalar {
ecdsa_fun::fun::Scalar::from_bytes(big_endian_bytes)
.expect("valid scalar")
.non_zero()
.mark::<NonZero>()
.expect("non-zero scalar")
}
}

@ -19,7 +19,7 @@ pub struct CborCodec<P, Req, Res> {
impl<P, Req, Res> Default for CborCodec<P, Req, Res> {
fn default() -> Self {
Self {
phantom: PhantomData,
phantom: PhantomData::default(),
}
}
}

@ -25,7 +25,7 @@ pub struct JsonPullCodec<P, Res> {
impl<P, Res> Default for JsonPullCodec<P, Res> {
fn default() -> Self {
Self {
phantom: PhantomData,
phantom: PhantomData::default(),
}
}
}

@ -1,7 +1,7 @@
use libp2p::rendezvous::Namespace;
use std::fmt;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum XmrBtcNamespace {
Mainnet,
Testnet,

@ -37,7 +37,7 @@ pub mod protocol {
>;
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub struct BlockchainNetwork {
#[serde(with = "crate::bitcoin::network")]
pub bitcoin: bitcoin::Network,

@ -1,5 +1,4 @@
use crate::asb::LatestRate;
use crate::monero::Amount;
use crate::network::swap_setup;
use crate::network::swap_setup::{
protocol, BlockchainNetwork, SpotPriceError, SpotPriceRequest, SpotPriceResponse,
@ -43,7 +42,7 @@ pub enum OutEvent {
#[derive(Debug)]
pub struct WalletSnapshot {
balance: monero_rpc::wallet::GetBalance,
balance: monero::Amount,
lock_fee: monero::Amount,
// TODO: Consider using the same address for punish and redeem (they are mutually exclusive, so
@ -59,17 +58,11 @@ impl WalletSnapshot {
pub async fn capture(
bitcoin_wallet: &bitcoin::Wallet,
monero_wallet: &monero::Wallet,
external_redeem_address: &Option<bitcoin::Address>,
transfer_amount: bitcoin::Amount,
) -> Result<Self> {
let balance = monero_wallet.get_balance().await?;
let redeem_address = external_redeem_address
.clone()
.unwrap_or(bitcoin_wallet.new_address().await?);
let punish_address = external_redeem_address
.clone()
.unwrap_or(bitcoin_wallet.new_address().await?);
let redeem_address = bitcoin_wallet.new_address().await?;
let punish_address = bitcoin_wallet.new_address().await?;
let redeem_fee = bitcoin_wallet
.estimate_fee(bitcoin::TxRedeem::weight(), transfer_amount)
.await?;
@ -330,8 +323,7 @@ where
.sell_quote(btc)
.map_err(Error::SellQuoteCalculationFailed)?;
let unlocked = Amount::from_piconero(wallet_snapshot.balance.unlocked_balance);
if unlocked < xmr + wallet_snapshot.lock_fee {
if wallet_snapshot.balance < xmr + wallet_snapshot.lock_fee {
return Err(Error::BalanceTooLow {
balance: wallet_snapshot.balance,
buy: btc,
@ -487,9 +479,9 @@ pub enum Error {
max: bitcoin::Amount,
buy: bitcoin::Amount,
},
#[error("Unlocked balance ({balance}) too low to fulfill swapping {buy}")]
#[error("Balance {balance} too low to fulfill swapping {buy}")]
BalanceTooLow {
balance: monero_rpc::wallet::GetBalance,
balance: monero::Amount,
buy: bitcoin::Amount,
},
#[error("Failed to fetch latest rate")]

@ -155,16 +155,13 @@ impl ProtocolsHandler for Handler {
let env_config = self.env_config;
let protocol = tokio::time::timeout(self.timeout, async move {
write_cbor_message(
&mut substream,
SpotPriceRequest {
btc: info.btc,
blockchain_network: BlockchainNetwork {
bitcoin: env_config.bitcoin_network,
monero: env_config.monero_network,
},
write_cbor_message(&mut substream, SpotPriceRequest {
btc: info.btc,
blockchain_network: BlockchainNetwork {
bitcoin: env_config.bitcoin_network,
monero: env_config.monero_network,
},
)
})
.await?;
let xmr = Result::from(read_cbor_message::<SpotPriceResponse>(&mut substream).await?)?;
@ -261,7 +258,7 @@ impl From<SpotPriceResponse> for Result<monero::Amount, Error> {
}
}
#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
#[derive(Clone, Debug, thiserror::Error, PartialEq)]
pub enum Error {
#[error("Seller currently does not accept incoming swap requests, please try again later")]
NoSwapsAccepted,

@ -1,9 +1,9 @@
use crate::asb::{LatestRate, RendezvousNode};
use crate::asb::LatestRate;
use crate::libp2p_ext::MultiAddrExt;
use crate::network::rendezvous::XmrBtcNamespace;
use crate::seed::Seed;
use crate::{asb, bitcoin, cli, env, tor};
use anyhow::Result;
use anyhow::{Context, Result};
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder};
use libp2p::{identity, Multiaddr, Swarm};
use std::fmt::Debug;
@ -17,23 +17,22 @@ pub fn asb<LR>(
resume_only: bool,
env_config: env::Config,
namespace: XmrBtcNamespace,
rendezvous_addrs: &[Multiaddr],
rendezvous_point: Option<Multiaddr>,
) -> Result<Swarm<asb::Behaviour<LR>>>
where
LR: LatestRate + Send + 'static + Debug + Clone,
{
let identity = seed.derive_libp2p_identity();
let rendezvous_nodes = rendezvous_addrs
.iter()
.map(|addr| {
let peer_id = addr
.extract_peer_id()
.expect("Rendezvous node address must contain peer ID");
let rendezvous_params = if let Some(address) = rendezvous_point {
let peer_id = address
.extract_peer_id()
.context("Rendezvous node address must contain peer ID")?;
RendezvousNode::new(addr, peer_id, namespace, None)
})
.collect();
Some((identity.clone(), peer_id, address, namespace))
} else {
None
};
let behaviour = asb::Behaviour::new(
min_buy,
@ -42,7 +41,7 @@ where
resume_only,
env_config,
(identity.clone(), namespace),
rendezvous_nodes,
rendezvous_params,
);
let transport = asb::transport::new(&identity)?;

@ -21,7 +21,7 @@ struct GlobalSpawnTokioExecutor;
impl Executor for GlobalSpawnTokioExecutor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
tokio::spawn(future);
let _ = tokio::spawn(future);
}
}
@ -40,7 +40,7 @@ where
.expect("failed to create dh_keys");
let noise = NoiseConfig::xx(dh_keys).into_authenticated();
let transport = MemoryTransport
let transport = MemoryTransport::default()
.or_transport(TokioTcpConfig::new())
.upgrade(Version::V1)
.authenticate(noise)

@ -2,15 +2,16 @@ use proptest::prelude::*;
pub mod ecdsa_fun {
use super::*;
use ::ecdsa_fun::fun::marker::{Mark, NonZero, Normal};
use ::ecdsa_fun::fun::{Point, Scalar, G};
pub fn point() -> impl Strategy<Value = Point> {
scalar().prop_map(|mut scalar| Point::even_y_from_scalar_mul(G, &mut scalar).normalize())
scalar().prop_map(|mut scalar| Point::from_scalar_mul(G, &mut scalar).mark::<Normal>())
}
pub fn scalar() -> impl Strategy<Value = Scalar> {
prop::array::uniform32(0..255u8).prop_filter_map("generated the 0 element", |bytes| {
Scalar::from_bytes_mod_order(bytes).non_zero()
Scalar::from_bytes_mod_order(bytes).mark::<NonZero>()
})
}
}

@ -6,12 +6,12 @@ use crate::{bitcoin, monero};
use anyhow::Result;
use async_trait::async_trait;
use conquer_once::Lazy;
use ecdsa_fun::fun::marker::Mark;
use libp2p::{Multiaddr, PeerId};
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use sigma_fun::ext::dl_secp256k1_ed25519_eq::{CrossCurveDLEQ, CrossCurveDLEQProof};
use sigma_fun::HashTranscript;
use std::collections::HashMap;
use std::convert::TryInto;
use uuid::Uuid;
@ -22,7 +22,7 @@ pub static CROSS_CURVE_PROOF_SYSTEM: Lazy<
CrossCurveDLEQ<HashTranscript<Sha256, rand_chacha::ChaCha20Rng>>,
> = Lazy::new(|| {
CrossCurveDLEQ::<HashTranscript<Sha256, rand_chacha::ChaCha20Rng>>::new(
(*ecdsa_fun::fun::G).normalize(),
(*ecdsa_fun::fun::G).mark::<ecdsa_fun::fun::marker::Normal>(),
curve25519_dalek::constants::ED25519_BASEPOINT_POINT,
)
});
@ -102,11 +102,11 @@ impl From<BobState> for State {
}
}
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq)]
#[error("Not in the role of Alice")]
pub struct NotAlice;
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq)]
#[error("Not in the role of Bob")]
pub struct NotBob;
@ -140,10 +140,7 @@ pub trait Database {
async fn get_monero_address(&self, swap_id: Uuid) -> Result<monero::Address>;
async fn insert_address(&self, peer_id: PeerId, address: Multiaddr) -> Result<()>;
async fn get_addresses(&self, peer_id: PeerId) -> Result<Vec<Multiaddr>>;
async fn get_swap_start_date(&self, swap_id: Uuid) -> Result<String>;
async fn insert_latest_state(&self, swap_id: Uuid, state: State) -> Result<()>;
async fn get_state(&self, swap_id: Uuid) -> Result<State>;
async fn get_states(&self, swap_id: Uuid) -> Result<Vec<State>>;
async fn all(&self) -> Result<Vec<(Uuid, State)>>;
async fn raw_all(&self) -> Result<HashMap<Uuid, Vec<serde_json::Value>>>;
}

@ -184,32 +184,29 @@ impl State0 {
let v = self.v_a + msg.v_b;
Ok((
msg.swap_id,
State1 {
a: self.a,
B: msg.B,
s_a: self.s_a,
S_a_monero: self.S_a_monero,
S_a_bitcoin: self.S_a_bitcoin,
S_b_monero: msg.S_b_monero,
S_b_bitcoin: msg.S_b_bitcoin,
v,
v_a: self.v_a,
dleq_proof_s_a: self.dleq_proof_s_a,
btc: self.btc,
xmr: self.xmr,
cancel_timelock: self.cancel_timelock,
punish_timelock: self.punish_timelock,
refund_address: msg.refund_address,
redeem_address: self.redeem_address,
punish_address: self.punish_address,
tx_redeem_fee: self.tx_redeem_fee,
tx_punish_fee: self.tx_punish_fee,
tx_refund_fee: msg.tx_refund_fee,
tx_cancel_fee: msg.tx_cancel_fee,
},
))
Ok((msg.swap_id, State1 {
a: self.a,
B: msg.B,
s_a: self.s_a,
S_a_monero: self.S_a_monero,
S_a_bitcoin: self.S_a_bitcoin,
S_b_monero: msg.S_b_monero,
S_b_bitcoin: msg.S_b_bitcoin,
v,
v_a: self.v_a,
dleq_proof_s_a: self.dleq_proof_s_a,
btc: self.btc,
xmr: self.xmr,
cancel_timelock: self.cancel_timelock,
punish_timelock: self.punish_timelock,
refund_address: msg.refund_address,
redeem_address: self.redeem_address,
punish_address: self.punish_address,
tx_redeem_fee: self.tx_redeem_fee,
tx_punish_fee: self.tx_punish_fee,
tx_refund_fee: msg.tx_refund_fee,
tx_cancel_fee: msg.tx_cancel_fee,
}))
}
}
@ -310,8 +307,7 @@ impl State2 {
self.a.public(),
self.B,
self.tx_cancel_fee,
)
.expect("valid cancel tx");
);
let tx_refund =
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
@ -336,7 +332,7 @@ impl State2 {
self.a.public(),
self.B,
self.tx_cancel_fee,
)?;
);
bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig)
.context("Failed to verify cancel transaction")?;
let tx_punish = bitcoin::TxPunish::new(
@ -459,7 +455,6 @@ impl State3 {
self.B,
self.tx_cancel_fee,
)
.expect("valid cancel tx")
}
pub fn tx_refund(&self) -> TxRefund {

@ -112,7 +112,7 @@ where
}
AliceState::BtcLocked { state3 } => {
match state3.expired_timelocks(bitcoin_wallet).await? {
ExpiredTimelocks::None { .. } => {
ExpiredTimelocks::None => {
// Record the current monero wallet block height so we don't have to scan from
// block 0 for scenarios where we create a refund wallet.
let monero_wallet_restore_blockheight = monero_wallet.block_height().await?;
@ -135,7 +135,7 @@ where
transfer_proof,
state3,
} => match state3.expired_timelocks(bitcoin_wallet).await? {
ExpiredTimelocks::None { .. } => {
ExpiredTimelocks::None => {
monero_wallet
.watch_for_transfer(state3.lock_xmr_watch_request(transfer_proof.clone(), 1))
.await
@ -176,7 +176,7 @@ where
}
},
result = tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock) => {
result?;
let _ = result?;
AliceState::CancelTimelockExpired {
monero_wallet_restore_blockheight,
transfer_proof,
@ -196,7 +196,7 @@ where
biased; // make sure the cancel timelock expiry future is polled first
result = tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock) => {
result?;
let _ = result?;
AliceState::CancelTimelockExpired {
monero_wallet_restore_blockheight,
transfer_proof,
@ -221,7 +221,7 @@ where
encrypted_signature,
state3,
} => match state3.expired_timelocks(bitcoin_wallet).await? {
ExpiredTimelocks::None { .. } => {
ExpiredTimelocks::None => {
let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await;
match state3.signed_redeem_transaction(*encrypted_signature) {
Ok(tx) => match bitcoin_wallet.broadcast(tx, "redeem").await {
@ -326,7 +326,7 @@ where
}
}
result = tx_cancel_status.wait_until_confirmed_with(state3.punish_timelock) => {
result?;
let _ = result?;
AliceState::BtcPunishable {
monero_wallet_restore_blockheight,

@ -1,4 +1,4 @@
use crate::bitcoin::wallet::{EstimateFeeRate, Subscription};
use crate::bitcoin::wallet::EstimateFeeRate;
use crate::bitcoin::{
self, current_epoch, CancelTimelock, ExpiredTimelocks, PunishTimelock, Transaction, TxCancel,
TxLock, Txid,
@ -21,10 +21,9 @@ use sigma_fun::ext::dl_secp256k1_ed25519_eq::CrossCurveDLEQProof;
use std::fmt;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq)]
pub enum BobState {
Started {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc_amount: bitcoin::Amount,
change_address: bitcoin::Address,
},
@ -150,9 +149,9 @@ impl State0 {
}
}
pub async fn receive<D, C>(
pub async fn receive<B, D, C>(
self,
wallet: &bitcoin::Wallet<D, C>,
wallet: &bitcoin::Wallet<B, D, C>,
msg: Message1,
) -> Result<State1>
where
@ -243,7 +242,7 @@ impl State1 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
);
let tx_refund =
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
@ -288,13 +287,13 @@ pub struct State2 {
S_a_monero: monero::PublicKey,
S_a_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey,
pub xmr: monero::Amount,
pub cancel_timelock: CancelTimelock,
pub punish_timelock: PunishTimelock,
pub refund_address: bitcoin::Address,
xmr: monero::Amount,
cancel_timelock: CancelTimelock,
punish_timelock: PunishTimelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
pub tx_lock: bitcoin::TxLock,
tx_lock: bitcoin::TxLock,
tx_cancel_sig_a: Signature,
tx_refund_encsig: bitcoin::EncryptedSignature,
min_monero_confirmations: u64,
@ -303,9 +302,9 @@ pub struct State2 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_punish_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub tx_refund_fee: bitcoin::Amount,
tx_refund_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub tx_cancel_fee: bitcoin::Amount,
tx_cancel_fee: bitcoin::Amount,
}
impl State2 {
@ -316,8 +315,7 @@ impl State2 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)
.expect("valid cancel tx");
);
let tx_cancel_sig = self.b.sign(tx_cancel.digest());
let tx_punish = bitcoin::TxPunish::new(
&tx_cancel,
@ -441,7 +439,7 @@ impl State3 {
self.tx_lock.txid()
}
pub async fn expired_timelock(
pub async fn current_epoch(
&self,
bitcoin_wallet: &bitcoin::Wallet,
) -> Result<ExpiredTimelocks> {
@ -451,7 +449,7 @@ impl State3 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
);
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
@ -532,7 +530,7 @@ impl State4 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
);
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
@ -562,7 +560,7 @@ impl State4 {
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct State5 {
#[serde(with = "monero_private_key")]
s_a: monero::PrivateKey,
@ -613,7 +611,7 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
);
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
@ -636,30 +634,27 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
);
let tx = bitcoin_wallet.get_raw_transaction(tx_cancel.txid()).await?;
Ok(tx)
}
pub async fn submit_tx_cancel(
&self,
bitcoin_wallet: &bitcoin::Wallet,
) -> Result<(Txid, Subscription)> {
pub async fn submit_tx_cancel(&self, bitcoin_wallet: &bitcoin::Wallet) -> Result<Txid> {
let transaction = bitcoin::TxCancel::new(
&self.tx_lock,
self.cancel_timelock,
self.A,
self.b.public(),
self.tx_cancel_fee,
)?
)
.complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone())
.context("Failed to complete Bitcoin cancel transaction")?;
let (tx_id, subscription) = bitcoin_wallet.broadcast(transaction, "cancel").await?;
let (tx_id, _) = bitcoin_wallet.broadcast(transaction, "cancel").await?;
Ok((tx_id, subscription))
Ok(tx_id)
}
pub async fn publish_refund_btc(&self, bitcoin_wallet: &bitcoin::Wallet) -> Result<()> {
@ -676,7 +671,7 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
);
let tx_refund =
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);

@ -117,7 +117,7 @@ async fn next_state(
} => {
let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await;
if let ExpiredTimelocks::None { .. } = state3.expired_timelock(bitcoin_wallet).await? {
if let ExpiredTimelocks::None = state3.current_epoch(bitcoin_wallet).await? {
let transfer_proof_watcher = event_loop_handle.recv_transfer_proof();
let cancel_timelock_expires =
tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock);
@ -137,7 +137,7 @@ async fn next_state(
}
},
result = cancel_timelock_expires => {
result?;
let _ = result?;
tracing::info!("Alice took too long to lock Monero, cancelling the swap");
let state4 = state3.cancel();
@ -156,7 +156,7 @@ async fn next_state(
} => {
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
if let ExpiredTimelocks::None { .. } = state.expired_timelock(bitcoin_wallet).await? {
if let ExpiredTimelocks::None = state.current_epoch(bitcoin_wallet).await? {
let watch_request = state.lock_xmr_watch_request(lock_transfer_proof);
select! {
@ -174,7 +174,7 @@ async fn next_state(
}
}
result = tx_lock_status.wait_until_confirmed_with(state.cancel_timelock) => {
result?;
let _ = result?;
BobState::CancelTimelockExpired(state.cancel())
}
}
@ -185,7 +185,7 @@ async fn next_state(
BobState::XmrLocked(state) => {
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
if let ExpiredTimelocks::None { .. } = state.expired_timelock(bitcoin_wallet).await? {
if let ExpiredTimelocks::None = state.expired_timelock(bitcoin_wallet).await? {
// Alice has locked Xmr
// Bob sends Alice his key
@ -198,7 +198,7 @@ async fn next_state(
}
},
result = tx_lock_status.wait_until_confirmed_with(state.cancel_timelock) => {
result?;
let _ = result?;
BobState::CancelTimelockExpired(state.cancel())
}
}
@ -209,13 +209,13 @@ async fn next_state(
BobState::EncSigSent(state) => {
let tx_lock_status = bitcoin_wallet.subscribe_to(state.tx_lock.clone()).await;
if let ExpiredTimelocks::None { .. } = state.expired_timelock(bitcoin_wallet).await? {
if let ExpiredTimelocks::None = state.expired_timelock(bitcoin_wallet).await? {
select! {
state5 = state.watch_for_redeem_btc(bitcoin_wallet) => {
BobState::BtcRedeemed(state5?)
},
result = tx_lock_status.wait_until_confirmed_with(state.cancel_timelock) => {
result?;
let _ = result?;
BobState::CancelTimelockExpired(state.cancel())
}
}
@ -227,9 +227,6 @@ async fn next_state(
let (spend_key, view_key) = state.xmr_keys();
let wallet_file_name = swap_id.to_string();
tracing::info!(%wallet_file_name, "Generating and opening Monero wallet from the extracted keys to redeem the Monero");
if let Err(e) = monero_wallet
.create_from_and_load(
wallet_file_name.clone(),
@ -272,12 +269,12 @@ async fn next_state(
BobState::BtcCancelled(state) => {
// Bob has cancelled the swap
match state.expired_timelock(bitcoin_wallet).await? {
ExpiredTimelocks::None { .. } => {
ExpiredTimelocks::None => {
bail!(
"Internal error: canceled state reached before cancel timelock was expired"
);
}
ExpiredTimelocks::Cancel { .. } => {
ExpiredTimelocks::Cancel => {
state.publish_refund_btc(bitcoin_wallet).await?;
BobState::BtcRefunded(state)
}

@ -1,31 +0,0 @@
use crate::api::Context;
use jsonrpsee::server::{RpcModule, ServerBuilder, ServerHandle};
use std::net::SocketAddr;
use std::sync::Arc;
use thiserror::Error;
pub mod methods;
#[derive(Debug, Error)]
pub enum Error {
#[error("Could not parse key value from params")]
ParseError,
}
pub async fn run_server(
server_address: SocketAddr,
context: Arc<Context>,
) -> anyhow::Result<(SocketAddr, ServerHandle)> {
let server = ServerBuilder::default().build(server_address).await?;
let mut modules = RpcModule::new(());
{
modules
.merge(methods::register_modules(Arc::clone(&context))?)
.expect("Could not register RPC modules")
}
let addr = server.local_addr()?;
let server_handle = server.start(modules)?;
Ok((addr, server_handle))
}

@ -1,236 +0,0 @@
use crate::api::request::{Method, Request};
use crate::api::Context;
use crate::bitcoin::bitcoin_address;
use crate::monero::monero_address;
use crate::{bitcoin, monero};
use anyhow::Result;
use jsonrpsee::server::RpcModule;
use jsonrpsee::types::Params;
use libp2p::core::Multiaddr;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use uuid::Uuid;
pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>> {
let mut module = RpcModule::new(context);
module.register_async_method("suspend_current_swap", |params, context| async move {
execute_request(params, Method::SuspendCurrentSwap, &context).await
})?;
module.register_async_method("get_swap_info", |params_raw, context| async move {
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
let swap_id = params
.get("swap_id")
.ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?;
let swap_id = as_uuid(swap_id)
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
execute_request(params_raw, Method::GetSwapInfo { swap_id }, &context).await
})?;
module.register_async_method("get_bitcoin_balance", |params_raw, context| async move {
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
let force_refresh = params
.get("force_refresh")
.ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain force_refresh".to_string())
})?
.as_bool()
.ok_or_else(|| {
jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string())
})?;
execute_request(params_raw, Method::Balance { force_refresh }, &context).await
})?;
module.register_async_method("get_history", |params, context| async move {
execute_request(params, Method::History, &context).await
})?;
module.register_async_method("get_raw_states", |params, context| async move {
execute_request(params, Method::GetRawStates, &context).await
})?;
module.register_async_method("resume_swap", |params_raw, context| async move {
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
let swap_id = params
.get("swap_id")
.ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?;
let swap_id = as_uuid(swap_id)
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
execute_request(params_raw, Method::Resume { swap_id }, &context).await
})?;
module.register_async_method("cancel_refund_swap", |params_raw, context| async move {
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
let swap_id = params
.get("swap_id")
.ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?;
let swap_id = as_uuid(swap_id)
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
execute_request(params_raw, Method::CancelAndRefund { swap_id }, &context).await
})?;
module.register_async_method(
"get_monero_recovery_info",
|params_raw, context| async move {
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
let swap_id = params.get("swap_id").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
})?;
let swap_id = as_uuid(swap_id).ok_or_else(|| {
jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string())
})?;
execute_request(params_raw, Method::MoneroRecovery { swap_id }, &context).await
},
)?;
module.register_async_method("withdraw_btc", |params_raw, context| async move {
let params: HashMap<String, String> = params_raw.parse()?;
let amount = if let Some(amount_str) = params.get("amount") {
Some(
::bitcoin::Amount::from_str_in(amount_str, ::bitcoin::Denomination::Bitcoin)
.map_err(|_| {
jsonrpsee_core::Error::Custom("Unable to parse amount".to_string())
})?,
)
} else {
None
};
let withdraw_address =
bitcoin::Address::from_str(params.get("address").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain address".to_string())
})?)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
let withdraw_address =
bitcoin_address::validate(withdraw_address, context.config.env_config.bitcoin_network)?;
execute_request(
params_raw,
Method::WithdrawBtc {
amount,
address: withdraw_address,
},
&context,
)
.await
})?;
module.register_async_method("buy_xmr", |params_raw, context| async move {
let params: HashMap<String, String> = params_raw.parse()?;
let bitcoin_change_address =
bitcoin::Address::from_str(params.get("bitcoin_change_address").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain bitcoin_change_address".to_string())
})?)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
let bitcoin_change_address = bitcoin_address::validate(
bitcoin_change_address,
context.config.env_config.bitcoin_network,
)?;
let monero_receive_address =
monero::Address::from_str(params.get("monero_receive_address").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain monero_receiveaddress".to_string())
})?)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
let monero_receive_address = monero_address::validate(
monero_receive_address,
context.config.env_config.monero_network,
)?;
let seller =
Multiaddr::from_str(params.get("seller").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain seller".to_string())
})?)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
execute_request(
params_raw,
Method::BuyXmr {
bitcoin_change_address,
monero_receive_address,
seller,
swap_id: Uuid::new_v4(),
},
&context,
)
.await
})?;
module.register_async_method("list_sellers", |params_raw, context| async move {
let params: HashMap<String, serde_json::Value> = params_raw.parse()?;
let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string())
})?;
let rendezvous_point = rendezvous_point
.as_str()
.and_then(|addr_str| Multiaddr::from_str(addr_str).ok())
.ok_or_else(|| {
jsonrpsee_core::Error::Custom("Could not parse valid multiaddr".to_string())
})?;
execute_request(
params_raw,
Method::ListSellers {
rendezvous_point: rendezvous_point.clone(),
},
&context,
)
.await
})?;
module.register_async_method("get_current_swap", |params, context| async move {
execute_request(params, Method::GetCurrentSwap, &context).await
})?;
Ok(module)
}
fn as_uuid(json_value: &serde_json::Value) -> Option<Uuid> {
if let Some(uuid_str) = json_value.as_str() {
Uuid::parse_str(uuid_str).ok()
} else {
None
}
}
async fn execute_request(
params: Params<'static>,
cmd: Method,
context: &Arc<Context>,
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
// If we fail to parse the params as a String HashMap, it's most likely because its an empty object
// In that case, we want to make sure not to fail the request, so we set the log_reference_id to None
// and swallow the error
let reference_id = params
.parse::<HashMap<String, serde_json::Value>>()
.ok()
.and_then(|params_parsed| params_parsed.get("log_reference_id").cloned());
let request = Request::with_id(cmd, reference_id.map(|log_ref| log_ref.to_string()));
request
.call(Arc::clone(context))
.await
.map_err(|err| jsonrpsee_core::Error::Custom(format!("{:#}", err)))
}

@ -16,7 +16,7 @@ use torut::onion::TorSecretKeyV3;
pub const SEED_LENGTH: usize = 32;
#[derive(Clone, Eq, PartialEq)]
#[derive(Eq, PartialEq)]
pub struct Seed([u8; SEED_LENGTH]);
impl Seed {
@ -61,7 +61,7 @@ impl Seed {
let file_path = Path::new(&file_path_buf);
if file_path.exists() {
return Self::from_file(file_path);
return Self::from_file(&file_path);
}
tracing::debug!("No seed file found, creating at {}", file_path.display());
@ -106,12 +106,11 @@ impl Seed {
}
fn from_pem(pem: pem::Pem) -> Result<Self, Error> {
let contents = pem.contents();
if contents.len() != SEED_LENGTH {
Err(Error::IncorrectLength(contents.len()))
if pem.contents.len() != SEED_LENGTH {
Err(Error::IncorrectLength(pem.contents.len()))
} else {
let mut array = [0; SEED_LENGTH];
for (i, b) in contents.iter().enumerate() {
for (i, b) in pem.contents.iter().enumerate() {
array[i] = *b;
}
@ -123,7 +122,10 @@ impl Seed {
ensure_directory_exists(&seed_file)?;
let data = self.bytes();
let pem = Pem::new("SEED", data);
let pem = Pem {
tag: String::from("SEED"),
contents: data.to_vec(),
};
let pem_string = encode(&pem);
@ -185,9 +187,6 @@ mod tests {
#[test]
fn seed_from_pem_works() {
use base64::engine::general_purpose;
use base64::Engine;
let payload: &str = "syl9wSYaruvgxg9P5Q1qkZaq5YkM6GvXkxe+VYrL/XM=";
// 32 bytes base64 encoded.
@ -196,7 +195,7 @@ syl9wSYaruvgxg9P5Q1qkZaq5YkM6GvXkxe+VYrL/XM=
-----END SEED-----
";
let want = general_purpose::STANDARD.decode(payload).unwrap();
let want = base64::decode(payload).unwrap();
let pem = pem::parse(pem_string).unwrap();
let got = Seed::from_pem(pem).unwrap();
@ -222,20 +221,19 @@ VnZUNFZ4dlY=
}
#[test]
#[should_panic]
fn seed_from_pem_fails_for_long_seed() {
let long = "-----BEGIN SEED-----
MIIBPQIBAAJBAOsfi5AGYhdRs/x6q5H7kScxA0Kzzqe6WI6gf6+tc6IvKQJo5rQc
dWWSQ0nRGt2hOPDO+35NKhQEjBQxPh/v7n0CAwEAAQJBAOGaBAyuw0ICyENy5NsO
mbKANv2qKGmNVg1qtquj6Hx1pFPelpqOfE2JaJJAMEg1FlFhNRNlFlE=
mbKANv2qKGmNVg1qtquj6Hx1pFPelpqOfE2JaJJAMEg1FlFhNRNlFlE=
-----END SEED-----
";
let pem = pem::parse(long).unwrap();
assert_eq!(pem.contents().len(), 96);
match Seed::from_pem(pem) {
Ok(_) => panic!("should fail for long payload"),
Err(e) => {
match e {
Error::IncorrectLength(len) => assert_eq!(len, 96), // pass
Error::IncorrectLength(_) => {} // pass
_ => panic!("should fail with IncorrectLength error"),
}
}

@ -50,7 +50,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
// Bob manually cancels
bob_join_handle.abort();
let (_, _, state) = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
let (_, state) = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
assert!(matches!(state, BobState::BtcCancelled { .. }));
let (bob_swap, bob_join_handle) = ctx
@ -64,7 +64,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
ctx.assert_bob_refunded(bob_state).await;
// manually refund Alice's swap
// manually refund ALice's swap
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;
let alice_state = asb::refund(

@ -1,74 +0,0 @@
pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::bob_run_until::is_btc_locked;
use harness::FastCancelConfig;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};
use swap::{asb, cli};
#[tokio::test]
async fn given_alice_and_bob_manually_cancel_and_refund_after_funds_locked_both_refund() {
harness::setup_test(FastCancelConfig, |mut ctx| async move {
let (bob_swap, bob_join_handle) = ctx.bob_swap().await;
let bob_swap_id = bob_swap.id;
let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
let alice_swap = ctx.alice_next_swap().await;
let alice_swap = tokio::spawn(alice::run_until(
alice_swap,
is_xmr_lock_transaction_sent,
FixedRate::default(),
));
let bob_state = bob_swap.await??;
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
let alice_state = alice_swap.await??;
assert!(matches!(
alice_state,
AliceState::XmrLockTransactionSent { .. }
));
let (bob_swap, bob_join_handle) = ctx
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
.await;
// Ensure cancel timelock is expired
if let BobState::BtcLocked { state3, .. } = bob_swap.state.clone() {
bob_swap
.bitcoin_wallet
.subscribe_to(state3.tx_lock)
.await
.wait_until_confirmed_with(state3.cancel_timelock)
.await?;
} else {
panic!("Bob in unexpected state {}", bob_swap.state);
}
// Bob manually cancels and refunds
bob_join_handle.abort();
let bob_state =
cli::cancel_and_refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
ctx.assert_bob_refunded(bob_state).await;
// manually refund Alice's swap
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;
let alice_state = asb::refund(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.monero_wallet,
alice_swap.db,
)
.await?;
ctx.assert_alice_refunded(alice_state).await;
Ok(())
})
.await
}

@ -1,27 +0,0 @@
#!/bin/bash
set -euxo pipefail
VERSION=0.11.1
mkdir bdk
stat ./target/debug/swap || exit 1
cp ./target/debug/swap bdk/swap-current
pushd bdk
echo "download swap $VERSION"
curl -L "https://github.com/comit-network/xmr-btc-swap/releases/download/${VERSION}/swap_${VERSION}_Linux_x86_64.tar" | tar xv
echo "create testnet wallet with $VERSION"
./swap --testnet --data-base-dir . --debug balance || exit 1
echo "check testnet wallet with this version"
./swap-current --testnet --data-base-dir . --debug balance || exit 1
echo "create mainnet wallet with $VERSION"
./swap --version || exit 1
./swap --data-base-dir . --debug balance || exit 1
echo "check mainnet wallet with this version"
./swap-current --version || exit 1
./swap-current --data-base-dir . --debug balance || exit 1
exit 0

@ -8,7 +8,7 @@ async fn ensure_same_swap_id_for_alice_and_bob() {
harness::setup_test(SlowCancelConfig, |mut ctx| async move {
let (bob_swap, _) = ctx.bob_swap().await;
let bob_swap_id = bob_swap.id;
tokio::spawn(bob::run(bob_swap));
let _ = tokio::spawn(bob::run(bob_swap));
// once Bob's swap is spawned we can retrieve Alice's swap and assert on the
// swap ID

@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use testcontainers::{core::WaitFor, Image, ImageArgs};
use std::collections::HashMap;
use testcontainers::core::{Container, Docker, WaitForMessage};
use testcontainers::Image;
pub const RPC_USER: &str = "admin";
pub const RPC_PASSWORD: &str = "123";
@ -9,27 +10,57 @@ pub const DATADIR: &str = "/home/bdk";
#[derive(Debug)]
pub struct Bitcoind {
args: BitcoindArgs,
entrypoint: Option<String>,
volumes: BTreeMap<String, String>,
volume: Option<String>,
}
impl Image for Bitcoind {
type Args = BitcoindArgs;
type EnvVars = HashMap<String, String>;
type Volumes = HashMap<String, String>;
type EntryPoint = str;
fn name(&self) -> String {
"coblox/bitcoin-core".into()
fn descriptor(&self) -> String {
"coblox/bitcoin-core:0.21.0".to_string()
}
fn tag(&self) -> String {
"0.21.0".into()
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
container
.logs()
.stdout
.wait_for_message("init message: Done loading")
.unwrap();
}
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("init message: Done loading")]
fn args(&self) -> <Self as Image>::Args {
self.args.clone()
}
fn volumes(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(self.volumes.iter())
fn volumes(&self) -> Self::Volumes {
let mut volumes = HashMap::new();
match self.volume.clone() {
None => {}
Some(volume) => {
volumes.insert(volume, DATADIR.to_string());
}
}
volumes
}
fn env_vars(&self) -> Self::EnvVars {
HashMap::new()
}
fn with_args(self, args: <Self as Image>::Args) -> Self {
Bitcoind { args, ..self }
}
fn with_entrypoint(self, entrypoint: &Self::EntryPoint) -> Self {
Self {
entrypoint: Some(entrypoint.to_string()),
..self
}
}
fn entrypoint(&self) -> Option<String> {
@ -40,15 +71,16 @@ impl Image for Bitcoind {
impl Default for Bitcoind {
fn default() -> Self {
Bitcoind {
args: BitcoindArgs::default(),
entrypoint: Some("/usr/bin/bitcoind".into()),
volumes: BTreeMap::default(),
volume: None,
}
}
}
impl Bitcoind {
pub fn with_volume(mut self, volume: String) -> Self {
self.volumes.insert(volume, DATADIR.to_string());
self.volume = Some(volume);
self
}
}
@ -77,6 +109,7 @@ impl IntoIterator for BitcoindArgs {
format!("-rpcuser={}", RPC_USER),
format!("-rpcpassword={}", RPC_PASSWORD),
"-printtoconsole".to_string(),
"-rest".to_string(),
"-fallbackfee=0.0002".to_string(),
format!("-datadir={}", DATADIR),
format!("-rpcport={}", RPC_PORT),
@ -87,9 +120,3 @@ impl IntoIterator for BitcoindArgs {
args.into_iter()
}
}
impl ImageArgs for BitcoindArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(self.into_iter())
}
}

@ -1,8 +1,8 @@
use std::collections::BTreeMap;
use crate::harness::bitcoind;
use bitcoin::Network;
use testcontainers::{core::WaitFor, Image, ImageArgs};
use std::collections::HashMap;
use testcontainers::core::{Container, Docker, WaitForMessage};
use testcontainers::Image;
pub const HTTP_PORT: u16 = 60401;
pub const RPC_PORT: u16 = 3002;
@ -13,25 +13,50 @@ pub struct Electrs {
args: ElectrsArgs,
entrypoint: Option<String>,
wait_for_message: String,
volumes: BTreeMap<String, String>,
volume: String,
}
impl Image for Electrs {
type Args = ElectrsArgs;
fn name(&self) -> String {
"vulpemventures/electrs".into()
type EnvVars = HashMap<String, String>;
type Volumes = HashMap<String, String>;
type EntryPoint = str;
fn descriptor(&self) -> String {
format!("vulpemventures/electrs:{}", self.tag)
}
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
container
.logs()
.stderr
.wait_for_message(&self.wait_for_message)
.unwrap();
}
fn tag(&self) -> String {
self.tag.clone()
fn args(&self) -> <Self as Image>::Args {
self.args.clone()
}
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stderr(self.wait_for_message.clone())]
fn volumes(&self) -> Self::Volumes {
let mut volumes = HashMap::new();
volumes.insert(self.volume.clone(), bitcoind::DATADIR.to_string());
volumes
}
fn volumes(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(self.volumes.iter())
fn env_vars(&self) -> Self::EnvVars {
HashMap::new()
}
fn with_args(self, args: <Self as Image>::Args) -> Self {
Electrs { args, ..self }
}
fn with_entrypoint(self, entrypoint: &Self::EntryPoint) -> Self {
Self {
entrypoint: Some(entrypoint.to_string()),
..self
}
}
fn entrypoint(&self) -> Option<String> {
@ -46,7 +71,7 @@ impl Default for Electrs {
args: ElectrsArgs::default(),
entrypoint: Some("/build/electrs".into()),
wait_for_message: "Running accept thread".to_string(),
volumes: BTreeMap::default(),
volume: uuid::Uuid::new_v4().to_string(),
}
}
}
@ -60,7 +85,7 @@ impl Electrs {
}
pub fn with_volume(mut self, volume: String) -> Self {
self.volumes.insert(volume, bitcoind::DATADIR.to_string());
self.volume = volume;
self
}
@ -68,11 +93,6 @@ impl Electrs {
self.args.daemon_rpc_addr = name;
self
}
pub fn self_and_args(self) -> (Self, ElectrsArgs) {
let args = self.args.clone();
(self, args)
}
}
#[derive(Debug, Clone)]
@ -117,7 +137,7 @@ impl IntoIterator for ElectrsArgs {
}
args.push("-vvvvv".to_string());
args.push(format!("--daemon-dir={}", self.daemon_dir.as_str()));
args.push(format!("--daemon-dir=={}", self.daemon_dir.as_str()));
args.push(format!("--daemon-rpc-addr={}", self.daemon_rpc_addr));
args.push(format!("--cookie={}", self.cookie));
args.push(format!("--http-addr={}", self.http_addr));
@ -127,9 +147,3 @@ impl IntoIterator for ElectrsArgs {
args.into_iter()
}
}
impl ImageArgs for ElectrsArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(self.into_iter())
}
}

@ -23,12 +23,12 @@ use swap::network::rendezvous::XmrBtcNamespace;
use swap::network::swarm;
use swap::protocol::alice::{AliceState, Swap};
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob, Database};
use swap::protocol::{alice, bob};
use swap::seed::Seed;
use swap::{api, asb, bitcoin, cli, env, monero};
use swap::{asb, bitcoin, cli, env, monero};
use tempfile::{tempdir, NamedTempFile};
use testcontainers::clients::Cli;
use testcontainers::{Container, RunnableImage};
use testcontainers::{Container, Docker, RunArgs};
use tokio::sync::mpsc;
use tokio::sync::mpsc::Receiver;
use tokio::task::JoinHandle;
@ -56,12 +56,15 @@ where
monero.init_miner().await.unwrap();
let btc_amount = bitcoin::Amount::from_sat(1_000_000);
let xmr_amount = monero::Amount::from_monero(btc_amount.to_btc() / FixedRate::RATE).unwrap();
let xmr_amount = monero::Amount::from_monero(btc_amount.as_btc() / FixedRate::RATE).unwrap();
let alice_starting_balances =
StartingBalances::new(bitcoin::Amount::ZERO, xmr_amount, Some(10));
let electrs_rpc_port = containers.electrs.get_host_port_ipv4(electrs::RPC_PORT);
let electrs_rpc_port = containers
.electrs
.get_host_port(electrs::RPC_PORT)
.expect("Could not map electrs rpc port");
let alice_seed = Seed::random().unwrap();
let (alice_bitcoin_wallet, alice_monero_wallet) = init_test_wallets(
@ -143,28 +146,25 @@ where
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
let prefix = random_prefix();
let bitcoind_name = format!("{}_{}", prefix, "bitcoind");
let (_bitcoind, bitcoind_url, mapped_port) =
let (bitcoind, bitcoind_url) =
init_bitcoind_container(cli, prefix.clone(), bitcoind_name.clone(), prefix.clone())
.await
.expect("could not init bitcoind");
let electrs = init_electrs_container(cli, prefix.clone(), bitcoind_name, prefix, mapped_port)
let electrs = init_electrs_container(cli, prefix.clone(), bitcoind_name, prefix)
.await
.expect("could not init electrs");
let (monero, _monerod_container, _monero_wallet_rpc_containers) =
let (monero, monerod_container, monero_wallet_rpc_containers) =
Monero::new(cli, vec![MONERO_WALLET_NAME_ALICE, MONERO_WALLET_NAME_BOB])
.await
.unwrap();
(
monero,
Containers {
bitcoind_url,
_bitcoind,
_monerod_container,
_monero_wallet_rpc_containers,
electrs,
},
)
(monero, Containers {
bitcoind_url,
bitcoind,
monerod_container,
monero_wallet_rpc_containers,
electrs,
})
}
async fn init_bitcoind_container(
@ -172,28 +172,29 @@ async fn init_bitcoind_container(
volume: String,
name: String,
network: String,
) -> Result<(Container<'_, bitcoind::Bitcoind>, Url, u16)> {
) -> Result<(Container<'_, Cli, bitcoind::Bitcoind>, Url)> {
let image = bitcoind::Bitcoind::default().with_volume(volume);
let image = RunnableImage::from(image)
.with_container_name(name)
.with_network(network);
let docker = cli.run(image);
let port = docker.get_host_port_ipv4(bitcoind::RPC_PORT);
let run_args = RunArgs::default().with_name(name).with_network(network);
let docker = cli.run_with_args(image, run_args);
let a = docker
.get_host_port(bitcoind::RPC_PORT)
.context("Could not map bitcoind rpc port")?;
let bitcoind_url = {
let input = format!(
"http://{}:{}@localhost:{}",
bitcoind::RPC_USER,
bitcoind::RPC_PASSWORD,
port
a
);
Url::parse(&input).unwrap()
};
init_bitcoind(bitcoind_url.clone(), 5).await?;
Ok((docker, bitcoind_url.clone(), bitcoind::RPC_PORT))
Ok((docker, bitcoind_url.clone()))
}
pub async fn init_electrs_container(
@ -201,18 +202,16 @@ pub async fn init_electrs_container(
volume: String,
bitcoind_container_name: String,
network: String,
port: u16,
) -> Result<Container<'_, electrs::Electrs>> {
let bitcoind_rpc_addr = format!("{}:{}", bitcoind_container_name, port);
) -> Result<Container<'_, Cli, electrs::Electrs>> {
let bitcoind_rpc_addr = format!("{}:{}", bitcoind_container_name, bitcoind::RPC_PORT);
let image = electrs::Electrs::default()
.with_volume(volume)
.with_daemon_rpc_addr(bitcoind_rpc_addr)
.with_tag("latest");
let image = RunnableImage::from(image.self_and_args())
.with_network(network.clone())
.with_container_name(format!("{}_electrs", network));
let docker = cli.run(image);
let run_args = RunArgs::default().with_network(network);
let docker = cli.run_with_args(image, run_args);
Ok(docker)
}
@ -246,7 +245,7 @@ async fn start_alice(
resume_only,
env_config,
XmrBtcNamespace::Testnet,
&[],
None,
)
.unwrap();
swarm.listen_on(listen_address).unwrap();
@ -260,7 +259,6 @@ async fn start_alice(
FixedRate::default(),
min_buy,
max_buy,
None,
)
.unwrap();
@ -400,7 +398,7 @@ impl StartingBalances {
}
}
pub struct BobParams {
struct BobParams {
seed: Seed,
db_path: PathBuf,
bitcoin_wallet: Arc<bitcoin::Wallet>,
@ -411,21 +409,6 @@ pub struct BobParams {
}
impl BobParams {
pub fn get_concentenated_alice_address(&self) -> String {
format!(
"{}/p2p/{}",
self.alice_address.clone(),
self.alice_peer_id.clone().to_base58()
)
}
pub async fn get_change_receive_addresses(&self) -> (bitcoin::Address, monero::Address) {
(
self.bitcoin_wallet.new_address().await.unwrap(),
self.monero_wallet.get_main_address(),
)
}
pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, cli::EventLoop)> {
let (event_loop, handle) = self.new_eventloop(swap_id).await?;
@ -467,8 +450,6 @@ impl BobParams {
}
let db = Arc::new(SqliteDatabase::open(&self.db_path).await?);
db.insert_peer_id(swap_id, self.alice_peer_id).await?;
let swap = bob::Swap::new(
db,
swap_id,
@ -542,24 +523,13 @@ pub struct TestContext {
alice_swap_handle: mpsc::Receiver<Swap>,
alice_handle: AliceApplicationHandle,
pub bob_params: BobParams,
bob_params: BobParams,
bob_starting_balances: StartingBalances,
bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
bob_monero_wallet: Arc<monero::Wallet>,
}
impl TestContext {
pub async fn get_bob_context(self) -> api::Context {
api::Context::for_harness(
self.bob_params.seed,
self.env_config,
self.bob_params.db_path,
self.bob_bitcoin_wallet,
self.bob_monero_wallet,
)
.await
}
pub async fn restart_alice(&mut self) {
self.alice_handle.abort();
@ -897,9 +867,7 @@ impl Wallet for monero::Wallet {
}
async fn get_balance(&self) -> Result<Self::Amount> {
let total = self.get_balance().await?;
let balance = Self::Amount::from_piconero(total.balance);
Ok(balance)
self.get_balance().await
}
}
@ -934,7 +902,7 @@ async fn mine(bitcoind_client: Client, reward_address: bitcoin::Address) -> Resu
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
bitcoind_client
.generatetoaddress(1, reward_address.clone())
.generatetoaddress(1, reward_address.clone(), None)
.await?;
}
}
@ -952,9 +920,9 @@ async fn init_bitcoind(node_url: Url, spendable_quantity: u32) -> Result<Client>
.await?;
bitcoind_client
.generatetoaddress(101 + spendable_quantity, reward_address.clone())
.generatetoaddress(101 + spendable_quantity, reward_address.clone(), None)
.await?;
tokio::spawn(mine(bitcoind_client.clone(), reward_address));
let _ = tokio::spawn(mine(bitcoind_client.clone(), reward_address));
Ok(bitcoind_client)
}
@ -972,18 +940,21 @@ pub async fn mint(node_url: Url, address: bitcoin::Address, amount: bitcoin::Amo
.with_wallet(BITCOIN_TEST_WALLET_NAME)?
.getnewaddress(None, None)
.await?;
bitcoind_client.generatetoaddress(1, reward_address).await?;
bitcoind_client
.generatetoaddress(1, reward_address, None)
.await?;
Ok(())
}
// This is just to keep the containers alive
#[allow(dead_code)]
struct Containers<'a> {
bitcoind_url: Url,
_bitcoind: Container<'a, bitcoind::Bitcoind>,
_monerod_container: Container<'a, image::Monerod>,
_monero_wallet_rpc_containers: Vec<Container<'a, image::MoneroWalletRpc>>,
electrs: Container<'a, electrs::Electrs>,
bitcoind: Container<'a, Cli, bitcoind::Bitcoind>,
monerod_container: Container<'a, Cli, image::Monerod>,
monero_wallet_rpc_containers: Vec<Container<'a, Cli, image::MoneroWalletRpc>>,
electrs: Container<'a, Cli, electrs::Electrs>,
}
pub mod alice_run_until {

@ -1,436 +0,0 @@
pub mod harness;
#[cfg(test)]
mod test {
use anyhow::Result;
use jsonrpsee::ws_client::WsClientBuilder;
use jsonrpsee_core::client::{Client, ClientT};
use jsonrpsee_core::params::ObjectParams;
use serial_test::serial;
use serde_json::Value;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use swap::api::request::{Method, Request};
use swap::api::Context;
use crate::harness::alice_run_until::is_xmr_lock_transaction_sent;
use crate::harness::bob_run_until::is_btc_locked;
use crate::harness::{setup_test, SlowCancelConfig, TestContext};
use swap::asb::FixedRate;
use swap::protocol::{alice, bob};
use swap::tracing_ext::{capture_logs, MakeCapturingWriter};
use tracing_subscriber::filter::LevelFilter;
use uuid::Uuid;
const SERVER_ADDRESS: &str = "127.0.0.1:1234";
const SERVER_START_TIMEOUT_SECS: u64 = 50;
const BITCOIN_ADDR: &str = "bcrt1qahvhjfc7vx5857zf8knxs8yp5lkm26jgyt0k76";
const MONERO_ADDR: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
const SELLER: &str =
"/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
pub async fn setup_daemon(
harness_ctx: TestContext,
) -> (Client, MakeCapturingWriter, Arc<Context>) {
let writer = capture_logs(LevelFilter::DEBUG);
let server_address: SocketAddr = SERVER_ADDRESS.parse().unwrap();
let request = Request::new(Method::StartDaemon {
server_address: Some(server_address),
});
let context = Arc::new(harness_ctx.get_bob_context().await);
let context_clone = context.clone();
tokio::spawn(async move {
if let Err(err) = request.call(context_clone).await {
println!("Failed to initialize daemon for testing: {}", err);
}
});
for _ in 0..SERVER_START_TIMEOUT_SECS {
if writer.captured().contains("Started RPC server") {
let url = format!("ws://{}", SERVER_ADDRESS);
let client = WsClientBuilder::default().build(&url).await.unwrap();
return (client, writer, context);
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
panic!(
"Failed to start RPC server after {} seconds",
SERVER_START_TIMEOUT_SECS
);
}
fn assert_has_keys_serde(map: &serde_json::Map<String, Value>, keys: &[&str]) {
for &key in keys {
assert!(map.contains_key(key), "Key {} is missing", key);
}
}
// Helper function for HashMap
fn assert_has_keys_hashmap<T>(map: &HashMap<String, T>, keys: &[&str]) {
for &key in keys {
assert!(map.contains_key(key), "Key {} is missing", key);
}
}
#[tokio::test]
#[serial]
pub async fn get_swap_info() {
setup_test(SlowCancelConfig, |mut harness_ctx| async move {
// Start a swap and wait for xmr lock transaction to be published (XmrLockTransactionSent)
let (bob_swap, _) = harness_ctx.bob_swap().await;
let bob_swap_id = bob_swap.id;
tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
let alice_swap = harness_ctx.alice_next_swap().await;
alice::run_until(
alice_swap,
is_xmr_lock_transaction_sent,
FixedRate::default(),
)
.await?;
let (client, _, _) = setup_daemon(harness_ctx).await;
let response: HashMap<String, Vec<(Uuid, String)>> = client
.request("get_history", ObjectParams::new())
.await
.unwrap();
let swaps: Vec<(Uuid, String)> = vec![(bob_swap_id, "btc is locked".to_string())];
assert_eq!(response, HashMap::from([("swaps".to_string(), swaps)]));
let response: HashMap<String, HashMap<Uuid, Vec<Value>>> = client
.request("get_raw_states", ObjectParams::new())
.await
.unwrap();
let response_raw_states = response.get("raw_states").unwrap();
assert!(response_raw_states.contains_key(&bob_swap_id));
assert_eq!(response_raw_states.get(&bob_swap_id).unwrap().len(), 2);
let mut params = ObjectParams::new();
params.insert("swap_id", bob_swap_id).unwrap();
let response: HashMap<String, Value> =
client.request("get_swap_info", params).await.unwrap();
// Check primary keys in response
assert_has_keys_hashmap(
&response,
&[
"txRefundFee",
"swapId",
"cancelTimelock",
"timelock",
"punishTimelock",
"stateName",
"btcAmount",
"startDate",
"btcRefundAddress",
"txCancelFee",
"xmrAmount",
"completed",
"txLockId",
"seller",
],
);
// Assert specific fields
assert_eq!(response.get("swapId").unwrap(), &bob_swap_id.to_string());
assert_eq!(
response.get("stateName").unwrap(),
&"btc is locked".to_string()
);
assert_eq!(response.get("completed").unwrap(), &Value::Bool(false));
// Check seller object and its keys
let seller = response
.get("seller")
.expect("Field 'seller' is missing from response")
.as_object()
.expect("'seller' is not an object");
assert_has_keys_serde(seller, &["peerId"]);
// Check timelock object, nested 'None' object, and blocks_left
let timelock = response
.get("timelock")
.expect("Field 'timelock' is missing from response")
.as_object()
.expect("'timelock' is not an object");
let none_obj = timelock
.get("None")
.expect("Field 'None' is missing from 'timelock'")
.as_object()
.expect("'None' is not an object in 'timelock'");
let blocks_left = none_obj
.get("blocks_left")
.expect("Field 'blocks_left' is missing from 'None'")
.as_i64()
.expect("'blocks_left' is not an integer");
// Validate blocks_left
assert!(
blocks_left > 0 && blocks_left <= 180,
"Field 'blocks_left' should be > 0 and <= 180 but got {}",
blocks_left
);
Ok(())
})
.await;
}
#[tokio::test]
#[serial]
pub async fn test_rpc_calls() {
setup_test(SlowCancelConfig, |harness_ctx| async move {
let alice_addr = harness_ctx.bob_params.get_concentenated_alice_address();
let (change_address, receive_address) =
harness_ctx.bob_params.get_change_receive_addresses().await;
let (client, writer, _) = setup_daemon(harness_ctx).await;
assert!(client.is_connected());
let mut params = ObjectParams::new();
params.insert("force_refresh", false).unwrap();
let response: HashMap<String, i32> = client
.request("get_bitcoin_balance", params)
.await
.unwrap();
assert_eq!(response, HashMap::from([("balance".to_string(), 10000000)]));
let mut params = ObjectParams::new();
params.insert("log_reference_id", "test_ref_id").unwrap();
params.insert("force_refresh", false).unwrap();
let _: HashMap<String, i32> = client.request("get_bitcoin_balance", params).await.unwrap();
assert!(writer.captured().contains(
r#"method{method_name="Balance" log_reference_id="\"test_ref_id\""}: swap::api::request: Current Bitcoin balance as of last sync balance=0.1 BTC"#
));
for method in ["get_swap_info", "resume_swap", "cancel_refund_swap"].iter() {
let mut params = ObjectParams::new();
params.insert("swap_id", "invalid_swap").unwrap();
let response: Result<HashMap<String, String>, _> =
client.request(method, params).await;
response.expect_err(&format!(
"Expected an error when swap_id is invalid for method {}",
method
));
let params = ObjectParams::new();
let response: Result<HashMap<String, String>, _> =
client.request(method, params).await;
response.expect_err(&format!(
"Expected an error when swap_id is missing for method {}",
method
));
}
let params = ObjectParams::new();
let result: Result<HashMap<String, String>, _> =
client.request("list_sellers", params).await;
result.expect_err("Expected an error when rendezvous_point is missing");
let params = ObjectParams::new();
let result: Result<HashMap<String, String>, _> =
client.request("list_sellers", params).await;
result.expect_err("Expected an error when rendezvous_point is missing");
let params = ObjectParams::new();
let response: Result<HashMap<String, String>, _> =
client.request("withdraw_btc", params).await;
response.expect_err("Expected an error when withdraw_address is missing");
let mut params = ObjectParams::new();
params.insert("address", "invalid_address").unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("withdraw_btc", params).await;
response.expect_err("Expected an error when withdraw_address is malformed");
let mut params = ObjectParams::new();
params.insert("address", BITCOIN_ADDR).unwrap();
params.insert("amount", "0").unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("withdraw_btc", params).await;
response.expect_err("Expected an error when amount is 0");
let mut params = ObjectParams::new();
params
.insert("address", BITCOIN_ADDR)
.unwrap();
params.insert("amount", "0.01").unwrap();
let response: HashMap<String, Value> = client
.request("withdraw_btc", params)
.await
.expect("Expected a valid response");
assert_has_keys_hashmap(&response, &["signed_tx", "amount", "txid"]);
assert_eq!(
response.get("amount").unwrap().as_u64().unwrap(),
1_000_000
);
let params = ObjectParams::new();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when no params are given");
let mut params = ObjectParams::new();
params
.insert("bitcoin_change_address", BITCOIN_ADDR)
.unwrap();
params
.insert("monero_receive_address", MONERO_ADDR)
.unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when seller is missing");
let mut params = ObjectParams::new();
params
.insert("bitcoin_change_address", BITCOIN_ADDR)
.unwrap();
params.insert("seller", SELLER).unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when monero_receive_address is missing");
let mut params = ObjectParams::new();
params
.insert("monero_receive_address", MONERO_ADDR)
.unwrap();
params.insert("seller", SELLER).unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when bitcoin_change_address is missing");
let mut params = ObjectParams::new();
params
.insert("bitcoin_change_address", "invalid_address")
.unwrap();
params
.insert("monero_receive_address", MONERO_ADDR)
.unwrap();
params.insert("seller", SELLER).unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when bitcoin_change_address is malformed");
let mut params = ObjectParams::new();
params
.insert("bitcoin_change_address", BITCOIN_ADDR)
.unwrap();
params
.insert("monero_receive_address", "invalid_address")
.unwrap();
params.insert("seller", SELLER).unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when monero_receive_address is malformed");
let mut params = ObjectParams::new();
params
.insert("bitcoin_change_address", BITCOIN_ADDR)
.unwrap();
params
.insert("monero_receive_address", MONERO_ADDR)
.unwrap();
params.insert("seller", "invalid_seller").unwrap();
let response: Result<HashMap<String, String>, _> =
client.request("buy_xmr", params).await;
response.expect_err("Expected an error when seller is malformed");
let response: Result<HashMap<String, String>, _> = client
.request("suspend_current_swap", ObjectParams::new())
.await;
response.expect_err("Expected an error when no swap is running");
let mut params = ObjectParams::new();
params
.insert("bitcoin_change_address", change_address)
.unwrap();
params
.insert("monero_receive_address", receive_address)
.unwrap();
params.insert("seller", alice_addr).unwrap();
let response: HashMap<String, Value> = client
.request("buy_xmr", params)
.await
.expect("Expected a HashMap, got an error");
assert_has_keys_hashmap(&response, &["swapId"]);
Ok(())
})
.await;
}
#[tokio::test]
#[serial]
pub async fn suspend_current_swap_swap_running() {
setup_test(SlowCancelConfig, |harness_ctx| async move {
let (client, _, ctx) = setup_daemon(harness_ctx).await;
ctx.swap_lock
.acquire_swap_lock(Uuid::parse_str(SWAP_ID).unwrap())
.await
.unwrap();
let cloned_ctx = ctx.clone();
tokio::spawn(async move {
// Immediately release lock when suspend signal is received. Mocks a running swap that is then cancelled.
ctx.swap_lock
.listen_for_swap_force_suspension()
.await
.unwrap();
ctx.swap_lock.release_swap_lock().await.unwrap();
});
let response: HashMap<String, String> = client
.request("suspend_current_swap", ObjectParams::new())
.await
.unwrap();
assert_eq!(
response,
HashMap::from([("swapId".to_string(), SWAP_ID.to_string())])
);
cloned_ctx
.swap_lock
.acquire_swap_lock(Uuid::parse_str(SWAP_ID).unwrap())
.await
.unwrap();
let response: Result<HashMap<String, String>, _> = client
.request("suspend_current_swap", ObjectParams::new())
.await;
response.expect_err("Expected an error when suspend signal times out");
Ok(())
})
.await;
}
}
Loading…
Cancel
Save