Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into CheckpointOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Jul 3, 2022
2 parents f44aa83 + 6122362 commit 4173830
Show file tree
Hide file tree
Showing 13 changed files with 491 additions and 55 deletions.
26 changes: 26 additions & 0 deletions shell/_swap-cli
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ _arguments "${_arguments_options[@]}" \
;;
(list-offers)
_arguments "${_arguments_options[@]}" \
'-s+[]:SELECT:(open Open inprogress in_progress ended Ended all All)' \
'--select=[]:SELECT:(open Open inprogress in_progress ended Ended all All)' \
'-d+[Data directory path]:DATA_DIR:_files -/' \
'--data-dir=[Data directory path]:DATA_DIR:_files -/' \
'-T+[Use Tor]:TOR_PROXY:_hosts' \
Expand Down Expand Up @@ -297,6 +299,24 @@ _arguments "${_arguments_options[@]}" \
':coin -- The coin funding required needs to be checked against:' \
&& ret=0
;;
(sweep-address)
_arguments "${_arguments_options[@]}" \
'-d+[Data directory path]:DATA_DIR:_files -/' \
'--data-dir=[Data directory path]:DATA_DIR:_files -/' \
'-T+[Use Tor]:TOR_PROXY:_hosts' \
'--tor-proxy=[Use Tor]:TOR_PROXY:_hosts' \
'-m+[ZMQ socket name/address to forward all incoming protocol messages]:MSG_SOCKET:_files' \
'--msg-socket=[ZMQ socket name/address to forward all incoming protocol messages]:MSG_SOCKET:_files' \
'-x+[ZMQ socket name/address for daemon control interface]:CTL_SOCKET:_files' \
'--ctl-socket=[ZMQ socket name/address for daemon control interface]:CTL_SOCKET:_files' \
'-h[Print help information]' \
'--help[Print help information]' \
'*-v[Set verbosity level]' \
'*--verbose[Set verbosity level]' \
':source-address -- The source address to be swept:' \
':destination-address -- The destination address receiving the coins:' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
'-d+[Data directory path]:DATA_DIR:_files -/' \
Expand Down Expand Up @@ -334,6 +354,7 @@ _swap-cli_commands() {
'cancel-swap:Cancel a swap if it has not locked yet' \
'progress:Request swap progress report' \
'needs-funding:Returns addresses and amounts that require funding for coin' \
'sweep-address:Attempts to sweep any funds on a given funding address' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'swap-cli commands' commands "$@"
Expand Down Expand Up @@ -408,6 +429,11 @@ _swap-cli__revoke-offer_commands() {
local commands; commands=()
_describe -t commands 'swap-cli revoke-offer commands' commands "$@"
}
(( $+functions[_swap-cli__sweep-address_commands] )) ||
_swap-cli__sweep-address_commands() {
local commands; commands=()
_describe -t commands 'swap-cli sweep-address commands' commands "$@"
}
(( $+functions[_swap-cli__take_commands] )) ||
_swap-cli__take_commands() {
local commands; commands=()
Expand Down
18 changes: 18 additions & 0 deletions shell/_swap-cli.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Register-ArgumentCompleter -Native -CommandName 'swap-cli' -ScriptBlock {
[CompletionResult]::new('cancel-swap', 'cancel-swap', [CompletionResultType]::ParameterValue, 'Cancel a swap if it has not locked yet')
[CompletionResult]::new('progress', 'progress', [CompletionResultType]::ParameterValue, 'Request swap progress report')
[CompletionResult]::new('needs-funding', 'needs-funding', [CompletionResultType]::ParameterValue, 'Returns addresses and amounts that require funding for coin')
[CompletionResult]::new('sweep-address', 'sweep-address', [CompletionResultType]::ParameterValue, 'Attempts to sweep any funds on a given funding address')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
Expand Down Expand Up @@ -98,6 +99,8 @@ Register-ArgumentCompleter -Native -CommandName 'swap-cli' -ScriptBlock {
break
}
'swap-cli;list-offers' {
[CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 's')
[CompletionResult]::new('--select', 'select', [CompletionResultType]::ParameterName, 'select')
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('--data-dir', 'data-dir', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('-T', 'T', [CompletionResultType]::ParameterName, 'Use Tor')
Expand Down Expand Up @@ -290,6 +293,21 @@ Register-ArgumentCompleter -Native -CommandName 'swap-cli' -ScriptBlock {
[CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Set verbosity level')
break
}
'swap-cli;sweep-address' {
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('--data-dir', 'data-dir', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('-T', 'T', [CompletionResultType]::ParameterName, 'Use Tor')
[CompletionResult]::new('--tor-proxy', 'tor-proxy', [CompletionResultType]::ParameterName, 'Use Tor')
[CompletionResult]::new('-m', 'm', [CompletionResultType]::ParameterName, 'ZMQ socket name/address to forward all incoming protocol messages')
[CompletionResult]::new('--msg-socket', 'msg-socket', [CompletionResultType]::ParameterName, 'ZMQ socket name/address to forward all incoming protocol messages')
[CompletionResult]::new('-x', 'x', [CompletionResultType]::ParameterName, 'ZMQ socket name/address for daemon control interface')
[CompletionResult]::new('--ctl-socket', 'ctl-socket', [CompletionResultType]::ParameterName, 'ZMQ socket name/address for daemon control interface')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'Set verbosity level')
[CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Set verbosity level')
break
}
'swap-cli;help' {
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'Data directory path')
[CompletionResult]::new('--data-dir', 'data-dir', [CompletionResultType]::ParameterName, 'Data directory path')
Expand Down
61 changes: 59 additions & 2 deletions shell/swap-cli.bash
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ _swap-cli() {
revoke-offer)
cmd+="__revoke__offer"
;;
sweep-address)
cmd+="__sweep__address"
;;
take)
cmd+="__take"
;;
Expand All @@ -64,7 +67,7 @@ _swap-cli() {

case "${cmd}" in
swap__cli)
opts="-h -V -d -v -T -m -x --help --version --data-dir --verbose --tor-proxy --msg-socket --ctl-socket info peers list-swaps list-offers offer-info list-listens list-checkpoints restore-checkpoint make take revoke-offer cancel-swap progress needs-funding help"
opts="-h -V -d -v -T -m -x --help --version --data-dir --verbose --tor-proxy --msg-socket --ctl-socket info peers list-swaps list-offers offer-info list-listens list-checkpoints restore-checkpoint make take revoke-offer cancel-swap progress needs-funding sweep-address help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -340,12 +343,20 @@ _swap-cli() {
return 0
;;
swap__cli__list__offers)
opts="-h -d -v -T -m -x --help --data-dir --verbose --tor-proxy --msg-socket --ctl-socket"
opts="-s -h -d -v -T -m -x --select --help --data-dir --verbose --tor-proxy --msg-socket --ctl-socket"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--select)
COMPREPLY=($(compgen -W "open Open inprogress in_progress ended Ended all All" -- "${cur}"))
return 0
;;
-s)
COMPREPLY=($(compgen -W "open Open inprogress in_progress ended Ended all All" -- "${cur}"))
return 0
;;
--data-dir)
COMPREPLY=($(compgen -f "${cur}"))
return 0
Expand Down Expand Up @@ -833,6 +844,52 @@ _swap-cli() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
swap__cli__sweep__address)
opts="-h -d -v -T -m -x --help --data-dir --verbose --tor-proxy --msg-socket --ctl-socket <SOURCE_ADDRESS> <DESTINATION_ADDRESS>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--data-dir)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-d)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--tor-proxy)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-T)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--msg-socket)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-m)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--ctl-socket)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-x)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
swap__cli__take)
opts="-o -w -h -d -v -T -m -x --btc-addr --xmr-addr --offer --without-validation --help --data-dir --verbose --tor-proxy --msg-socket --ctl-socket"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
Expand Down
31 changes: 29 additions & 2 deletions src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use crate::rpc::request::AddressSecretKey;
use crate::syncerd::SweepBitcoinAddress;
use farcaster_core::negotiation::Offer;
use farcaster_core::swap::btcxmr::BtcXmr;
use std::{
Expand Down Expand Up @@ -89,8 +91,8 @@ impl Exec for Command {
}

// TODO: only list offers matching list of OfferIds
Command::ListOffers => {
runtime.request(ServiceId::Farcasterd, Request::ListOffers)?;
Command::ListOffers { select } => {
runtime.request(ServiceId::Farcasterd, Request::ListOffers(select.into()))?;
runtime.report_response_or_fail()?;
}

Expand Down Expand Up @@ -252,6 +254,31 @@ impl Exec for Command {
runtime.request(ServiceId::Farcasterd, Request::NeedsFunding(coin))?;
runtime.report_response_or_fail()?;
}

Command::SweepAddress {
source_address,
destination_address,
} => {
runtime.request(
ServiceId::Database,
Request::GetAddressSecretKey(source_address.clone()),
)?;
if let Request::AddressSecretKey(AddressSecretKey { secret_key, .. }) =
runtime.report_failure()?
{
runtime.request(
ServiceId::Farcasterd,
Request::SweepBitcoinAddress(SweepBitcoinAddress {
source_address,
source_private_key: secret_key,
destination_address,
}),
)?;
runtime.report_response_or_fail()?;
} else {
return Err(Error::Farcaster("Received unexpected response".to_string()));
}
}
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
mod command;
mod opts;

pub use opts::{Command, Opts};
pub use opts::{Command, OfferSelector, Opts};
52 changes: 50 additions & 2 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ pub enum Command {
#[clap(aliases = &["ls"])]
ListSwaps,

// TODO: only list offers matching list of OfferIds
/// Lists public offers created by daemon
#[clap(aliases = &["lo"])]
ListOffers,
ListOffers {
#[clap(
short,
long,
default_value = "open",
possible_values = &["open", "Open", "inprogress", "in_progress", "ended", "Ended", "all", "All"],
)]
select: OfferSelector,
},

/// Gives information on an open offer
#[clap(aliases = &["oi"])]
Expand Down Expand Up @@ -234,6 +241,47 @@ pub enum Command {
/// The coin funding required needs to be checked against.
coin: Coin,
},

/// Attempts to sweep any funds on a given funding address
#[display("sweep-address<{source_address} {destination_address}>")]
SweepAddress {
/// The source address to be swept.
source_address: BtcAddress,
/// The destination address receiving the coins.
destination_address: BtcAddress,
},
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, From)]
pub enum OfferSelector {
#[display("Open")]
Open,
#[display("In Progress")]
InProgress,
#[display("Ended")]
Ended,
#[display("All")]
All,
}

impl FromStr for OfferSelector {
type Err = OfferSelectorParseError;
fn from_str(input: &str) -> Result<OfferSelector, Self::Err> {
match input {
"open" | "Open" => Ok(OfferSelector::Open),
"in_progress" | "inprogress" => Ok(OfferSelector::InProgress),
"ended" | "Ended" => Ok(OfferSelector::Ended),
"all" | "All" => Ok(OfferSelector::All),
_ => Err(OfferSelectorParseError::Invalid),
}
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum OfferSelectorParseError {
/// The provided value can't be parsed as an offer selector
Invalid,
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, Error, From)]
Expand Down
35 changes: 24 additions & 11 deletions src/databased/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use farcaster_core::negotiation::PublicOffer;
use farcaster_core::swap::btcxmr::BtcXmr;
use farcaster_core::swap::SwapId;
use lmdb::{Cursor, Transaction as LMDBTransaction};
use microservices::rpc::Failure;
use std::path::PathBuf;
use std::{
any::Any,
Expand Down Expand Up @@ -288,16 +289,28 @@ impl Runtime {
}

Request::GetAddressSecretKey(address) => {
let secret_key = self.database.get_address_secret_key(&address)?;
endpoints.send_to(
ServiceBus::Ctl,
ServiceId::Database,
source,
Request::AddressSecretKey(request::AddressSecretKey {
address,
secret_key: secret_key.secret_bytes(),
}),
)?;
match self.database.get_address_secret_key(&address) {
Err(_) => endpoints.send_to(
ServiceBus::Ctl,
ServiceId::Database,
source,
Request::Failure(Failure {
code: 1,
info: format!("Could not retrieve secret key for address {}", address),
}),
)?,
Ok(secret_key) => {
endpoints.send_to(
ServiceBus::Ctl,
ServiceId::Database,
source,
Request::AddressSecretKey(request::AddressSecretKey {
address,
secret_key: secret_key.secret_bytes(),
}),
)?;
}
}
}

Request::GetAddresses => {
Expand All @@ -314,7 +327,7 @@ impl Runtime {
self.database.set_offer_status(&offer, &status)?;
}

Request::RetrieveOffers(selector) => {
Request::ListOffers(selector) => {
let offer_status_pairs = self.database.get_offers(selector)?;
endpoints.send_to(
ServiceBus::Ctl,
Expand Down
Loading

0 comments on commit 4173830

Please sign in to comment.