Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3616c11
commit 9efdee7
Showing
2 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{collections::VecDeque, ffi::OsString, fmt, num::ParseIntError}; | ||
|
||
use crate::{ScheduledChannel, ScheduledPayJoin}; | ||
|
||
/// CLI argument errors. | ||
#[derive(Debug)] | ||
pub(crate) enum ArgError { | ||
/// Argument not UTF-8 | ||
NotUTF8(OsString), | ||
/// Parse feerate error | ||
FeeRateError(ParseIntError), | ||
/// Parse node address error | ||
InvalidNodeAddress(ln_types::p2p_address::ParseError), | ||
/// Parse bitcoin amount error | ||
InvalidBitcoinAmount(bitcoin::util::amount::ParseAmountError), | ||
/// Wallet amount error | ||
InvalidWalletAmount(bitcoin::util::amount::ParseAmountError), | ||
} | ||
|
||
impl fmt::Display for ArgError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
// TODO: Do this properly. | ||
write!(f, "invalid arguments: {:?}", self) | ||
} | ||
} | ||
|
||
impl std::error::Error for ArgError {} | ||
|
||
/// Parses arguments in `[fee_rate] [(<p2p_addr>, <sats_amount>)...] [wallet_amount]` | ||
pub(crate) fn parse_args<A>(args: A) -> Result<Option<ScheduledPayJoin>, ArgError> | ||
where | ||
A: Iterator<Item = OsString>, | ||
{ | ||
// ensure all args are utf8 | ||
let args = args | ||
.map(|arg| arg.into_string()) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map_err(ArgError::NotUTF8)?; | ||
|
||
// first argument is fee rate | ||
let fee_rate = match args.get(0) { | ||
Some(fee_rate_str) => fee_rate_str.parse::<u64>().map_err(ArgError::FeeRateError)?, | ||
None => return Ok(None), | ||
}; | ||
|
||
// return None if no remaining args. | ||
let mut args = match args.get(1..) { | ||
Some(args) => args.iter(), | ||
None => return Ok(None), | ||
}; | ||
|
||
// parse scheduled channel arguments: pairs of (addr, amount) | ||
let mut channels = VecDeque::with_capacity(args.len() / 2); | ||
|
||
// the remaining single argument is the wallet amount in satoshis (if any) | ||
let wallet_amount = loop { | ||
match (args.next(), args.next()) { | ||
// we have a pair of arguments, interpret it as a scheduled channel (p2p addr, amount) | ||
(Some(addr), Some(amount)) => { | ||
channels.push_back(ScheduledChannel::from_args(addr, amount)?); | ||
} | ||
// if there is a remaining single argument, it is the wallet amount | ||
(Some(amount), None) => { | ||
break bitcoin::Amount::from_str_in(amount, bitcoin::Denomination::Satoshi) | ||
.map_err(ArgError::InvalidWalletAmount)? | ||
} | ||
// if there is no remaining single argument, the wallet amount is 0 | ||
_ => break bitcoin::Amount::ZERO, | ||
} | ||
}; | ||
|
||
Ok(Some(ScheduledPayJoin { wallet_amount, channels, fee_rate })) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters