Skip to content

Commit

Permalink
Cli: Implement sweep address call
Browse files Browse the repository at this point in the history
This can be used to salvage funds from funding addresses in case
something goes wrong.
  • Loading branch information
TheCharlatan committed Jul 1, 2022
1 parent 42ee892 commit 90f5ca0
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 16 deletions.
24 changes: 24 additions & 0 deletions shell/_swap-cli
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,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 +352,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 +427,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
16 changes: 16 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 @@ -290,6 +291,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
51 changes: 50 additions & 1 deletion 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 @@ -833,6 +836,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
25 changes: 25 additions & 0 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 @@ -252,6 +254,29 @@ 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::Farcasterd,
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()?;
}
}
}

Ok(())
Expand Down
9 changes: 9 additions & 0 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ 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, Error, From)]
Expand Down
33 changes: 23 additions & 10 deletions src/databased/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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 @@ -285,16 +286,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 Down
Loading

0 comments on commit 90f5ca0

Please sign in to comment.