Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle payjoin errors according to BIP 78 #940

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cbc = { version = "0.1", features = ["alloc"] }
aes = { version = "0.8" }
jwt-compact = { version = "0.8.0-beta.1", features = ["es256k"] }
argon2 = { version = "0.5.0", features = ["password-hash", "alloc"] }
payjoin = { version = "0.10.0", features = ["send", "base64"] }
payjoin = { version = "0.13.0", features = ["send", "base64"] }
gluesql = { version = "0.15", default-features = false, features = ["memory-storage"] }
gluesql-core = "0.15.0"
bincode = "1.3.3"
Expand Down
12 changes: 6 additions & 6 deletions mutiny-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ pub enum MutinyError {
/// Payjoin request creation failed.
#[error("Failed to create payjoin request.")]
PayjoinCreateRequest,
/// Payjoin response validation failed.
#[error("Failed to validate payjoin response.")]
PayjoinValidateResponse(payjoin::send::ValidationError),
/// Payjoin request failed.
#[error("Payjoin response error: {0}")]
PayjoinResponse(payjoin::send::ResponseError),
/// Payjoin configuration error
#[error("Payjoin configuration failed.")]
PayjoinConfigError,
Expand Down Expand Up @@ -471,8 +471,8 @@ impl From<payjoin::send::CreateRequestError> for MutinyError {
}
}

impl From<payjoin::send::ValidationError> for MutinyError {
fn from(e: payjoin::send::ValidationError) -> Self {
Self::PayjoinValidateResponse(e)
impl From<payjoin::send::ResponseError> for MutinyError {
fn from(e: payjoin::send::ResponseError) -> Self {
Self::PayjoinResponse(e)
}
}
25 changes: 13 additions & 12 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use lightning::util::logger::*;
use lightning::{log_debug, log_error, log_info, log_warn};
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription};
use lightning_transaction_sync::EsploraSyncClient;
use payjoin::{PjUri, PjUriExt};
use payjoin::Uri;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -777,16 +777,15 @@ impl<S: MutinyStorage> NodeManager<S> {

pub async fn send_payjoin(
&self,
uri: PjUri<'_>,
uri: Uri<'_, payjoin::bitcoin::address::NetworkChecked>,
amount: u64,
labels: Vec<String>,
fee_rate: Option<f32>,
) -> Result<Txid, MutinyError> {
let address = Address::from_str(&uri.address.to_string())
.map_err(|_| MutinyError::PayjoinConfigError)?;
.map_err(|_| MutinyError::InvalidArgumentsError)?;
let original_psbt = self.wallet.create_signed_psbt(address, amount, fee_rate)?;

let payout_scripts = std::iter::once(uri.address.script_pubkey());
let fee_rate = if let Some(rate) = fee_rate {
FeeRate::from_sat_per_vb(rate)
} else {
Expand All @@ -797,17 +796,18 @@ impl<S: MutinyStorage> NodeManager<S> {
let original_psbt = payjoin::bitcoin::psbt::PartiallySignedTransaction::from_str(
&original_psbt.to_string(),
)
.map_err(|_| MutinyError::PayjoinConfigError)?;
let pj_params =
payjoin::send::Configuration::recommended(&original_psbt, payout_scripts, fee_rate)
.map_err(|_| MutinyError::PayjoinConfigError)?;

.map_err(|_| MutinyError::WalletOperationFailed)?;
log_debug!(self.logger, "Creating payjoin request");
let (req, ctx) = uri.create_pj_request(original_psbt.clone(), pj_params)?;
let (req, ctx) =
payjoin::send::RequestBuilder::from_psbt_and_uri(original_psbt.clone(), uri)
.unwrap()
.build_recommended(fee_rate)
.map_err(|_| MutinyError::PayjoinCreateRequest)?
.extract_v1()?;

let client = Client::builder()
.build()
.map_err(|_| MutinyError::PayjoinConfigError)?;
.map_err(|e| MutinyError::Other(e.into()))?;

log_debug!(self.logger, "Sending payjoin request");
let res = client
Expand All @@ -825,7 +825,8 @@ impl<S: MutinyStorage> NodeManager<S> {

log_debug!(self.logger, "Processing payjoin response");
let proposal_psbt = ctx.process_response(&mut cursor).map_err(|e| {
log_error!(self.logger, "Error processing payjoin response: {e}");
// unrecognized error contents may only appear in debug logs and will not Display
log_debug!(self.logger, "Payjoin response error: {:?}", e);
e
})?;

Expand Down
2 changes: 1 addition & 1 deletion mutiny-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ getrandom = { version = "0.2", features = ["js"] }
futures = "0.3.25"
urlencoding = "2.1.2"
once_cell = "1.18.0"
payjoin = { version = "0.10.0", features = ["send", "base64"] }
payjoin = { version = "0.13.0", features = ["send", "base64"] }
fedimint-core = "0.2.1"

# The `console_error_panic_hook` crate provides better debugging of panics by
Expand Down
8 changes: 4 additions & 4 deletions mutiny-wasm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ pub enum MutinyJsError {
/// Payjoin request creation failed.
#[error("Failed to create payjoin request.")]
PayjoinCreateRequest,
/// Payjoin response validation failed.
#[error("Failed to validate payjoin response.")]
PayjoinValidateResponse,
// Payjoin request failed.
#[error("Payjoin response error: {0}")]
PayjoinResponse(String),
/// Payjoin configuration error
#[error("Payjoin configuration failed.")]
PayjoinConfigError,
Expand Down Expand Up @@ -209,7 +209,7 @@ impl From<MutinyError> for MutinyJsError {
MutinyError::NetworkMismatch => MutinyJsError::NetworkMismatch,
MutinyError::PayjoinConfigError => MutinyJsError::PayjoinConfigError,
MutinyError::PayjoinCreateRequest => MutinyJsError::PayjoinCreateRequest,
MutinyError::PayjoinValidateResponse(_) => MutinyJsError::PayjoinValidateResponse,
MutinyError::PayjoinResponse(e) => MutinyJsError::PayjoinResponse(e.to_string()),
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use mutiny_core::{
nodemanager::{create_lsp_config, NodeManager},
};
use mutiny_core::{logging::MutinyLogger, nostr::ProfileType};
use payjoin::UriExt;
use std::str::FromStr;
use std::sync::Arc;
use std::{
Expand Down Expand Up @@ -487,9 +486,7 @@ impl MutinyWallet {
// I know walia parses `pj=` and `pjos=` but payjoin::Uri parses the whole bip21 uri
let pj_uri = payjoin::Uri::try_from(payjoin_uri.as_str())
.map_err(|_| MutinyJsError::InvalidArgumentsError)?
.assume_checked()
.check_pj_supported()
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
.assume_checked();
Ok(self
.inner
.node_manager
Expand Down
Loading