mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Merge --seller-addr
and --seller-peer-id
into --seller
parameter
This simplifies the CLI's interface.
This commit is contained in:
parent
206c98d71b
commit
ec4234fbb9
@ -21,11 +21,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
Now, after sending a spot price the ASB will wait for one minute for the CLI's to trigger the execution setup, and three minutes to see the BTC lock transaction of the CLI in mempool after the swap started.
|
Now, after sending a spot price the ASB will wait for one minute for the CLI's to trigger the execution setup, and three minutes to see the BTC lock transaction of the CLI in mempool after the swap started.
|
||||||
If the first timeout is triggered the execution setup will be aborted, if the second timeout is triggered the swap will be safely aborted.
|
If the first timeout is triggered the execution setup will be aborted, if the second timeout is triggered the swap will be safely aborted.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- The commandline interface of the CLI to combine `--seller-addr` and `--seller-peer-id`.
|
||||||
|
These two parameters have been merged into a parameter `--seller` that accepts a single [multiaddress](https://docs.libp2p.io/concepts/addressing/).
|
||||||
|
The multiaddress must end with a `/p2p` protocol defining the seller's peer ID.
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
|
||||||
- The websocket transport from the CLI.
|
- The websocket transport from the CLI.
|
||||||
Websockets were only ever intended to be used for the ASB side to allow websites to retrieve quotes.
|
Websockets were only ever intended to be used for the ASB side to allow websites to retrieve quotes.
|
||||||
The CLI can use regular TCP connections and having both - TCP and websockets - causes problems and unnecessary overhead.
|
The CLI can use regular TCP connections and having both - TCP and websockets - causes problems and unnecessary overhead.
|
||||||
|
- The `--seller-addr` parameter from the CLI's `resume` command.
|
||||||
|
This information is now loaded from the database.
|
||||||
|
|
||||||
## [0.7.0] - 2021-05-28
|
## [0.7.0] - 2021-05-28
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command, Pars
|
|||||||
use swap::cli::EventLoop;
|
use swap::cli::EventLoop;
|
||||||
use swap::database::Database;
|
use swap::database::Database;
|
||||||
use swap::env::Config;
|
use swap::env::Config;
|
||||||
|
use swap::libp2p_ext::MultiAddrExt;
|
||||||
use swap::network::quote::BidQuote;
|
use swap::network::quote::BidQuote;
|
||||||
use swap::network::swarm;
|
use swap::network::swarm;
|
||||||
use swap::protocol::bob;
|
use swap::protocol::bob;
|
||||||
@ -58,8 +59,7 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
match cmd {
|
match cmd {
|
||||||
Command::BuyXmr {
|
Command::BuyXmr {
|
||||||
seller_peer_id,
|
seller,
|
||||||
seller_addr,
|
|
||||||
bitcoin_electrum_rpc_url,
|
bitcoin_electrum_rpc_url,
|
||||||
bitcoin_target_block,
|
bitcoin_target_block,
|
||||||
monero_receive_address,
|
monero_receive_address,
|
||||||
@ -86,6 +86,11 @@ async fn main() -> Result<()> {
|
|||||||
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
|
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
|
||||||
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
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 mut swarm = swarm::cli(
|
let mut swarm = swarm::cli(
|
||||||
&seed,
|
&seed,
|
||||||
seller_peer_id,
|
seller_peer_id,
|
||||||
@ -94,9 +99,7 @@ async fn main() -> Result<()> {
|
|||||||
bitcoin_wallet.clone(),
|
bitcoin_wallet.clone(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
swarm
|
swarm.behaviour_mut().add_address(seller_peer_id, seller);
|
||||||
.behaviour_mut()
|
|
||||||
.add_address(seller_peer_id, seller_addr);
|
|
||||||
|
|
||||||
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
|
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
|
||||||
|
|
||||||
@ -157,7 +160,6 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
Command::Resume {
|
Command::Resume {
|
||||||
swap_id,
|
swap_id,
|
||||||
seller_addr,
|
|
||||||
bitcoin_electrum_rpc_url,
|
bitcoin_electrum_rpc_url,
|
||||||
bitcoin_target_block,
|
bitcoin_target_block,
|
||||||
monero_receive_address,
|
monero_receive_address,
|
||||||
@ -187,6 +189,7 @@ async fn main() -> Result<()> {
|
|||||||
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
||||||
|
|
||||||
let seller_peer_id = db.get_peer_id(swap_id)?;
|
let seller_peer_id = db.get_peer_id(swap_id)?;
|
||||||
|
let seller_addresses = db.get_addresses(seller_peer_id)?;
|
||||||
|
|
||||||
let mut swarm = swarm::cli(
|
let mut swarm = swarm::cli(
|
||||||
&seed,
|
&seed,
|
||||||
@ -198,9 +201,12 @@ async fn main() -> Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
let our_peer_id = swarm.local_peer_id();
|
let our_peer_id = swarm.local_peer_id();
|
||||||
tracing::debug!(peer_id = %our_peer_id, "Initializing network module");
|
tracing::debug!(peer_id = %our_peer_id, "Initializing network module");
|
||||||
swarm
|
|
||||||
.behaviour_mut()
|
for seller_address in seller_addresses {
|
||||||
.add_address(seller_peer_id, seller_addr);
|
swarm
|
||||||
|
.behaviour_mut()
|
||||||
|
.add_address(seller_peer_id, seller_address);
|
||||||
|
}
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) =
|
let (event_loop, event_loop_handle) =
|
||||||
EventLoop::new(swap_id, swarm, seller_peer_id, env_config)?;
|
EventLoop::new(swap_id, swarm, seller_peer_id, env_config)?;
|
||||||
|
@ -3,7 +3,6 @@ use crate::fs::system_data_dir;
|
|||||||
use crate::{env, monero};
|
use crate::{env, monero};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use libp2p::core::Multiaddr;
|
use libp2p::core::Multiaddr;
|
||||||
use libp2p::PeerId;
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -68,8 +67,7 @@ where
|
|||||||
|
|
||||||
let arguments = match args.cmd {
|
let arguments = match args.cmd {
|
||||||
RawCommand::BuyXmr {
|
RawCommand::BuyXmr {
|
||||||
seller_peer_id,
|
seller: Seller { seller },
|
||||||
seller_addr: SellerAddr { seller_addr },
|
|
||||||
bitcoin:
|
bitcoin:
|
||||||
Bitcoin {
|
Bitcoin {
|
||||||
bitcoin_electrum_rpc_url,
|
bitcoin_electrum_rpc_url,
|
||||||
@ -87,8 +85,7 @@ where
|
|||||||
json,
|
json,
|
||||||
data_dir: data::data_dir_from(data, is_testnet)?,
|
data_dir: data::data_dir_from(data, is_testnet)?,
|
||||||
cmd: Command::BuyXmr {
|
cmd: Command::BuyXmr {
|
||||||
seller_peer_id,
|
seller,
|
||||||
seller_addr,
|
|
||||||
bitcoin_electrum_rpc_url: bitcoin_electrum_rpc_url_from(
|
bitcoin_electrum_rpc_url: bitcoin_electrum_rpc_url_from(
|
||||||
bitcoin_electrum_rpc_url,
|
bitcoin_electrum_rpc_url,
|
||||||
is_testnet,
|
is_testnet,
|
||||||
@ -114,7 +111,6 @@ where
|
|||||||
},
|
},
|
||||||
RawCommand::Resume {
|
RawCommand::Resume {
|
||||||
swap_id: SwapId { swap_id },
|
swap_id: SwapId { swap_id },
|
||||||
seller_addr: SellerAddr { seller_addr },
|
|
||||||
bitcoin:
|
bitcoin:
|
||||||
Bitcoin {
|
Bitcoin {
|
||||||
bitcoin_electrum_rpc_url,
|
bitcoin_electrum_rpc_url,
|
||||||
@ -133,7 +129,6 @@ where
|
|||||||
data_dir: data::data_dir_from(data, is_testnet)?,
|
data_dir: data::data_dir_from(data, is_testnet)?,
|
||||||
cmd: Command::Resume {
|
cmd: Command::Resume {
|
||||||
swap_id,
|
swap_id,
|
||||||
seller_addr,
|
|
||||||
bitcoin_electrum_rpc_url: bitcoin_electrum_rpc_url_from(
|
bitcoin_electrum_rpc_url: bitcoin_electrum_rpc_url_from(
|
||||||
bitcoin_electrum_rpc_url,
|
bitcoin_electrum_rpc_url,
|
||||||
is_testnet,
|
is_testnet,
|
||||||
@ -201,8 +196,7 @@ where
|
|||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
BuyXmr {
|
BuyXmr {
|
||||||
seller_peer_id: PeerId,
|
seller: Multiaddr,
|
||||||
seller_addr: Multiaddr,
|
|
||||||
bitcoin_electrum_rpc_url: Url,
|
bitcoin_electrum_rpc_url: Url,
|
||||||
bitcoin_target_block: usize,
|
bitcoin_target_block: usize,
|
||||||
monero_receive_address: monero::Address,
|
monero_receive_address: monero::Address,
|
||||||
@ -212,7 +206,6 @@ pub enum Command {
|
|||||||
History,
|
History,
|
||||||
Resume {
|
Resume {
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
seller_addr: Multiaddr,
|
|
||||||
bitcoin_electrum_rpc_url: Url,
|
bitcoin_electrum_rpc_url: Url,
|
||||||
bitcoin_target_block: usize,
|
bitcoin_target_block: usize,
|
||||||
monero_receive_address: monero::Address,
|
monero_receive_address: monero::Address,
|
||||||
@ -268,11 +261,8 @@ pub struct RawArguments {
|
|||||||
pub enum RawCommand {
|
pub enum RawCommand {
|
||||||
/// Start a XMR for BTC swap
|
/// Start a XMR for BTC swap
|
||||||
BuyXmr {
|
BuyXmr {
|
||||||
#[structopt(long = "seller-peer-id", help = "The seller's peer id")]
|
|
||||||
seller_peer_id: PeerId,
|
|
||||||
|
|
||||||
#[structopt(flatten)]
|
#[structopt(flatten)]
|
||||||
seller_addr: SellerAddr,
|
seller: Seller,
|
||||||
|
|
||||||
#[structopt(flatten)]
|
#[structopt(flatten)]
|
||||||
bitcoin: Bitcoin,
|
bitcoin: Bitcoin,
|
||||||
@ -290,9 +280,6 @@ pub enum RawCommand {
|
|||||||
#[structopt(flatten)]
|
#[structopt(flatten)]
|
||||||
swap_id: SwapId,
|
swap_id: SwapId,
|
||||||
|
|
||||||
#[structopt(flatten)]
|
|
||||||
seller_addr: SellerAddr,
|
|
||||||
|
|
||||||
#[structopt(flatten)]
|
#[structopt(flatten)]
|
||||||
bitcoin: Bitcoin,
|
bitcoin: Bitcoin,
|
||||||
|
|
||||||
@ -373,9 +360,12 @@ pub struct SwapId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(structopt::StructOpt, Debug)]
|
#[derive(structopt::StructOpt, Debug)]
|
||||||
pub struct SellerAddr {
|
pub struct Seller {
|
||||||
#[structopt(long = "seller-addr", help = "The seller's multiaddress")]
|
#[structopt(
|
||||||
pub seller_addr: Multiaddr,
|
long,
|
||||||
|
help = "The seller's address. Must include a peer ID part, i.e. `/p2p/`"
|
||||||
|
)]
|
||||||
|
pub seller: Multiaddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
mod data {
|
mod data {
|
||||||
@ -492,8 +482,8 @@ mod tests {
|
|||||||
|
|
||||||
const MONERO_STAGENET_ADDRESS: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
|
const MONERO_STAGENET_ADDRESS: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
|
||||||
const MONERO_MAINNET_ADDRESS: &str = "44Ato7HveWidJYUAVw5QffEcEtSH1DwzSP3FPPkHxNAS4LX9CqgucphTisH978FLHE34YNEx7FcbBfQLQUU8m3NUC4VqsRa";
|
const MONERO_MAINNET_ADDRESS: &str = "44Ato7HveWidJYUAVw5QffEcEtSH1DwzSP3FPPkHxNAS4LX9CqgucphTisH978FLHE34YNEx7FcbBfQLQUU8m3NUC4VqsRa";
|
||||||
const MUTLI_ADDRESS: &str = "/ip4/127.0.0.1/tcp/9939";
|
const MULTI_ADDRESS: &str =
|
||||||
const PEER_ID: &str = "12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
|
"/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
|
||||||
const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
|
const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -503,10 +493,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let expected_args = ParseResult::Arguments(Arguments::buy_xmr_mainnet_defaults());
|
let expected_args = ParseResult::Arguments(Arguments::buy_xmr_mainnet_defaults());
|
||||||
@ -523,10 +511,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -544,10 +530,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let err = parse_args_and_apply_defaults(raw_ars).unwrap_err();
|
let err = parse_args_and_apply_defaults(raw_ars).unwrap_err();
|
||||||
@ -569,10 +553,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let err = parse_args_and_apply_defaults(raw_ars).unwrap_err();
|
let err = parse_args_and_apply_defaults(raw_ars).unwrap_err();
|
||||||
@ -595,8 +577,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -617,8 +597,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -688,10 +666,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -712,10 +688,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -737,8 +711,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -761,8 +733,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -784,10 +754,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -803,10 +771,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -823,8 +789,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -842,8 +806,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -861,10 +823,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -880,10 +840,8 @@ mod tests {
|
|||||||
"buy-xmr",
|
"buy-xmr",
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
"--seller",
|
||||||
MUTLI_ADDRESS,
|
MULTI_ADDRESS,
|
||||||
"--seller-peer-id",
|
|
||||||
PEER_ID,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -900,8 +858,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_MAINNET_ADDRESS,
|
MONERO_MAINNET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -919,8 +875,6 @@ mod tests {
|
|||||||
SWAP_ID,
|
SWAP_ID,
|
||||||
"--receive-address",
|
"--receive-address",
|
||||||
MONERO_STAGENET_ADDRESS,
|
MONERO_STAGENET_ADDRESS,
|
||||||
"--seller-addr",
|
|
||||||
MUTLI_ADDRESS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
let args = parse_args_and_apply_defaults(raw_ars).unwrap();
|
||||||
@ -938,8 +892,7 @@ mod tests {
|
|||||||
json: false,
|
json: false,
|
||||||
data_dir: data_dir_path_cli().join(TESTNET),
|
data_dir: data_dir_path_cli().join(TESTNET),
|
||||||
cmd: Command::BuyXmr {
|
cmd: Command::BuyXmr {
|
||||||
seller_peer_id: PeerId::from_str(PEER_ID).unwrap(),
|
seller: Multiaddr::from_str(MULTI_ADDRESS).unwrap(),
|
||||||
seller_addr: Multiaddr::from_str(MUTLI_ADDRESS).unwrap(),
|
|
||||||
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL_TESTNET)
|
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL_TESTNET)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET,
|
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET,
|
||||||
@ -958,8 +911,7 @@ mod tests {
|
|||||||
json: false,
|
json: false,
|
||||||
data_dir: data_dir_path_cli().join(MAINNET),
|
data_dir: data_dir_path_cli().join(MAINNET),
|
||||||
cmd: Command::BuyXmr {
|
cmd: Command::BuyXmr {
|
||||||
seller_peer_id: PeerId::from_str(PEER_ID).unwrap(),
|
seller: Multiaddr::from_str(MULTI_ADDRESS).unwrap(),
|
||||||
seller_addr: Multiaddr::from_str(MUTLI_ADDRESS).unwrap(),
|
|
||||||
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL).unwrap(),
|
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL).unwrap(),
|
||||||
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET,
|
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET,
|
||||||
monero_receive_address: monero::Address::from_str(MONERO_MAINNET_ADDRESS)
|
monero_receive_address: monero::Address::from_str(MONERO_MAINNET_ADDRESS)
|
||||||
@ -978,7 +930,6 @@ mod tests {
|
|||||||
data_dir: data_dir_path_cli().join(TESTNET),
|
data_dir: data_dir_path_cli().join(TESTNET),
|
||||||
cmd: Command::Resume {
|
cmd: Command::Resume {
|
||||||
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
||||||
seller_addr: Multiaddr::from_str(MUTLI_ADDRESS).unwrap(),
|
|
||||||
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL_TESTNET)
|
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL_TESTNET)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET,
|
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET,
|
||||||
@ -998,7 +949,6 @@ mod tests {
|
|||||||
data_dir: data_dir_path_cli().join(MAINNET),
|
data_dir: data_dir_path_cli().join(MAINNET),
|
||||||
cmd: Command::Resume {
|
cmd: Command::Resume {
|
||||||
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
swap_id: Uuid::from_str(SWAP_ID).unwrap(),
|
||||||
seller_addr: Multiaddr::from_str(MUTLI_ADDRESS).unwrap(),
|
|
||||||
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL).unwrap(),
|
bitcoin_electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL).unwrap(),
|
||||||
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET,
|
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET,
|
||||||
monero_receive_address: monero::Address::from_str(MONERO_MAINNET_ADDRESS)
|
monero_receive_address: monero::Address::from_str(MONERO_MAINNET_ADDRESS)
|
||||||
|
@ -3,7 +3,7 @@ pub use bob::Bob;
|
|||||||
|
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use libp2p::PeerId;
|
use libp2p::{Multiaddr, PeerId};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
@ -68,6 +68,7 @@ impl Swap {
|
|||||||
pub struct Database {
|
pub struct Database {
|
||||||
swaps: sled::Tree,
|
swaps: sled::Tree,
|
||||||
peers: sled::Tree,
|
peers: sled::Tree,
|
||||||
|
addresses: sled::Tree,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
@ -79,8 +80,13 @@ impl Database {
|
|||||||
|
|
||||||
let swaps = db.open_tree("swaps")?;
|
let swaps = db.open_tree("swaps")?;
|
||||||
let peers = db.open_tree("peers")?;
|
let peers = db.open_tree("peers")?;
|
||||||
|
let addresses = db.open_tree("addresses")?;
|
||||||
|
|
||||||
Ok(Database { swaps, peers })
|
Ok(Database {
|
||||||
|
swaps,
|
||||||
|
peers,
|
||||||
|
addresses,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn insert_peer_id(&self, swap_id: Uuid, peer_id: PeerId) -> Result<()> {
|
pub async fn insert_peer_id(&self, swap_id: Uuid, peer_id: PeerId) -> Result<()> {
|
||||||
@ -110,6 +116,46 @@ impl Database {
|
|||||||
Ok(PeerId::from_str(peer_id.as_str())?)
|
Ok(PeerId::from_str(peer_id.as_str())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn insert_address(&self, peer_id: PeerId, address: Multiaddr) -> Result<()> {
|
||||||
|
let key = peer_id.to_bytes();
|
||||||
|
|
||||||
|
let existing_addresses = self.addresses.get(&key)?;
|
||||||
|
|
||||||
|
let new_addresses = {
|
||||||
|
let existing_addresses = existing_addresses.clone();
|
||||||
|
|
||||||
|
Some(match existing_addresses {
|
||||||
|
Some(encoded) => {
|
||||||
|
let mut addresses = deserialize::<Vec<Multiaddr>>(&encoded)?;
|
||||||
|
addresses.push(address);
|
||||||
|
|
||||||
|
serialize(&addresses)?
|
||||||
|
}
|
||||||
|
None => serialize(&[address])?,
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
self.addresses
|
||||||
|
.compare_and_swap(key, existing_addresses, new_addresses)??;
|
||||||
|
|
||||||
|
self.addresses
|
||||||
|
.flush_async()
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.context("Could not flush db")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_addresses(&self, peer_id: PeerId) -> Result<Vec<Multiaddr>> {
|
||||||
|
let key = peer_id.to_bytes();
|
||||||
|
|
||||||
|
let addresses = match self.addresses.get(&key)? {
|
||||||
|
Some(encoded) => deserialize(&encoded).context("Failed to deserialize addresses")?,
|
||||||
|
None => vec![],
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(addresses)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn insert_latest_state(&self, swap_id: Uuid, state: Swap) -> Result<()> {
|
pub async fn insert_latest_state(&self, swap_id: Uuid, state: Swap) -> Result<()> {
|
||||||
let key = serialize(&swap_id)?;
|
let key = serialize(&swap_id)?;
|
||||||
let new_value = serialize(&state).context("Could not serialize new state value")?;
|
let new_value = serialize(&state).context("Could not serialize new state value")?;
|
||||||
|
@ -23,6 +23,7 @@ pub mod database;
|
|||||||
pub mod env;
|
pub mod env;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
pub mod kraken;
|
pub mod kraken;
|
||||||
|
pub mod libp2p_ext;
|
||||||
pub mod monero;
|
pub mod monero;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
|
15
swap/src/libp2p_ext.rs
Normal file
15
swap/src/libp2p_ext.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
use libp2p::multiaddr::Protocol;
|
||||||
|
use libp2p::{Multiaddr, PeerId};
|
||||||
|
|
||||||
|
pub trait MultiAddrExt {
|
||||||
|
fn extract_peer_id(&self) -> Option<PeerId>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MultiAddrExt for Multiaddr {
|
||||||
|
fn extract_peer_id(&self) -> Option<PeerId> {
|
||||||
|
match self.iter().last()? {
|
||||||
|
Protocol::P2p(multihash) => PeerId::from_multihash(multihash).ok(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user