diff --git a/bip78/Cargo.toml b/bip78/Cargo.toml index 901fd8a2..ef9644ab 100644 --- a/bip78/Cargo.toml +++ b/bip78/Cargo.toml @@ -23,8 +23,8 @@ base64 = "0.13.0" rand = { version = "0.8.4", optional = true } bip21 = "0.1.1" url = "2.2.2" +log = { version = "0.4.14", optional = true } [dev-dependencies] bitcoind = { version = "0.18.0", features = ["0_21_1"] } env_logger = "0.9.0" -log = "0.4.14" diff --git a/bip78/src/lib.rs b/bip78/src/lib.rs index 82798b34..11cf6752 100644 --- a/bip78/src/lib.rs +++ b/bip78/src/lib.rs @@ -37,3 +37,15 @@ pub(crate) mod params; pub(crate) mod psbt; pub use uri::{Uri, PjUri, UriExt, PjUriExt, PjParseError}; + +#[cfg(feature = "log")] +macro_rules! log_warn { + ($($args:tt)*) => { log::warn!($($args)*) } +} + +#[cfg(not(feature = "log"))] +macro_rules! log_warn { + ($($args:tt)*) => {} +} +pub(crate) use log_warn; + diff --git a/bip78/src/params.rs b/bip78/src/params.rs index 2a30de69..01eb8baf 100644 --- a/bip78/src/params.rs +++ b/bip78/src/params.rs @@ -1,6 +1,7 @@ use std::borrow::Borrow; use std::fmt; +use crate::log_warn; use crate::fee_rate::FeeRate; #[derive(Debug)] @@ -44,12 +45,22 @@ impl Params { return Err(ParamsError::UnknownVersion) }, ("additionalfeeoutputindex", index) => { - if let Ok(index) = index.parse::() { - additional_fee_output_index = Some(index); + additional_fee_output_index = match index.parse::() { + Ok(index) => Some(index), + Err(_error) => { + log_warn!("bad `additionalfeeoutputindex` query value '{}': {}", index, _error); + None + } } }, ("maxadditionalfeecontribution", fee) => { - max_additional_fee_contribution = bitcoin::Amount::from_str_in(&fee, bitcoin::Denomination::Bitcoin).ok(); + max_additional_fee_contribution = match bitcoin::Amount::from_str_in(&fee, bitcoin::Denomination::Bitcoin) { + Ok(contribution) => Some(contribution), + Err(_error) => { + log_warn!("bad `maxadditionalfeecontribution` query value '{}': {}", fee, _error); + None + } + } } ("minfeerate", feerate) => { params.min_feerate = match feerate.parse::() { @@ -57,12 +68,15 @@ impl Params { Err(e) => return Err(ParamsError::FeeRate(e)), } } - ("disableoutputsubstitution", v) => params.disable_output_substitution = v == "true", // existance is truthy + ("disableoutputsubstitution", v) => params.disable_output_substitution = v == "true", _ => (), } } - if let (Some(amount), Some(index)) = (max_additional_fee_contribution, additional_fee_output_index) { - params.additional_fee_contribution = Some((amount, index)); + + match (max_additional_fee_contribution, additional_fee_output_index) { + (Some(amount), Some(index)) => params.additional_fee_contribution = Some((amount, index)), + (Some(_), None) | (None, Some(_)) => { log_warn!("only one additional-fee parameter specified: {:?}", params); }, + _ => () } Ok(params)