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

Receive payjoin #820

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
766 changes: 531 additions & 235 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lightning-rapid-gossip-sync = { version = "0.0.121" }
lightning-background-processor = { version = "0.0.121", features = ["futures"] }
lightning-transaction-sync = { version = "0.0.121", default-features = false, features = ["esplora-async-https"] }
lightning-liquidity = "0.1.0-alpha.2"
chrono = "0.4.22"
chrono = "0.4.33"
futures-util = { version = "0.3", default-features = false }
reqwest = { version = "0.11", default-features = false, features = ["multipart", "json"] }
async-trait = "0.1.68"
Expand All @@ -44,7 +44,12 @@ 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.13.0", features = ["send", "base64"] }
once_cell = "1.18.0"
gloo-net = { version = "0.5.0", features = ["io-util"] }
payjoin = { version = "0.15.0", features = ["v2", "send", "receive", "base64"] }
futures-rustls = { version = "0.25.1" }
rustls-pki-types = { version = "1.4.0", features = ["web"] }
webpki-roots = "0.26.1"
bincode = "1.3.3"
hex-conservative = "0.1.1"
async-lock = "3.2.0"
Expand Down Expand Up @@ -84,6 +89,8 @@ wasm-bindgen-futures = { version = "0.4.38" }
gloo-net = { version = "0.4.0" }
web-time = "1.1"
gloo-timers = { version = "0.3.0", features = ["futures"] }
web-sys = { version = "0.3.65", features = ["console"] }
js-sys = "0.3.65"
getrandom = { version = "0.2", features = ["js"] }
# add nip07 feature for wasm32
nostr = { version = "0.29.0", default-features = false, features = ["nip04", "nip05", "nip07", "nip47", "nip57"] }
Expand Down
9 changes: 9 additions & 0 deletions mutiny-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub enum MutinyError {
/// Cannot change password to the same password
#[error("Cannot change password to the same password.")]
SamePassword,
/// Error with payjoin
#[error("Payjoin error: {0}")]
Payjoin(crate::payjoin::Error),
/// Payjoin request creation failed.
#[error("Failed to create payjoin request.")]
PayjoinCreateRequest,
Expand Down Expand Up @@ -574,6 +577,12 @@ impl From<nostr_sdk::signer::Error> for MutinyError {
}
}

impl From<crate::payjoin::Error> for MutinyError {
fn from(e: crate::payjoin::Error) -> Self {
Self::Payjoin(e)
}
}

impl From<payjoin::send::CreateRequestError> for MutinyError {
fn from(_e: payjoin::send::CreateRequestError) -> Self {
Self::PayjoinCreateRequest
Expand Down
28 changes: 28 additions & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
type_alias_bounds
)]
extern crate core;
extern crate payjoin as pj;

pub mod auth;
pub mod blindauth;
Expand All @@ -33,6 +34,7 @@ mod node;
pub mod nodemanager;
pub mod nostr;
mod onchain;
mod payjoin;
mod peermanager;
pub mod scorer;
pub mod storage;
Expand All @@ -47,6 +49,7 @@ pub use crate::gossip::{GOSSIP_SYNC_TIME_KEY, NETWORK_GRAPH_KEY, PROB_SCORER_KEY
pub use crate::keymanager::generate_seed;
pub use crate::ldkstorage::{CHANNEL_CLOSURE_PREFIX, CHANNEL_MANAGER_KEY, MONITORS_PREFIX_KEY};
use crate::nostr::primal::{PrimalApi, PrimalClient};
use crate::payjoin::PayjoinStorage;
use crate::storage::{
get_payment_hash_from_key, list_payment_info, persist_payment_info, update_nostr_contact_list,
IndexItem, MutinyStorage, DEVICE_ID_KEY, EXPECTED_NETWORK_KEY, NEED_FULL_SYNC_KEY,
Expand Down Expand Up @@ -1186,6 +1189,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
// when we restart, gen a new session id
self.node_manager = Arc::new(nm_builder.build().await?);
NodeManager::start_sync(self.node_manager.clone());
NodeManager::resume_payjoins(self.node_manager.clone());

Ok(())
}
Expand Down Expand Up @@ -1573,11 +1577,35 @@ impl<S: MutinyStorage> MutinyWallet<S> {
return Err(MutinyError::WalletOperationFailed);
};

let (pj, ohttp) = match self.node_manager.start_payjoin_session().await {
Ok((enrolled, ohttp_keys)) => {
let session = self
.node_manager
.storage
.store_new_recv_session(enrolled.clone())?;
let pj_uri = session.enrolled.fallback_target();
self.node_manager.spawn_payjoin_receiver(session);
let ohttp = base64::encode_config(
ohttp_keys
.encode()
.map_err(|_| MutinyError::PayjoinConfigError)?,
base64::URL_SAFE_NO_PAD,
);
(Some(pj_uri), Some(ohttp))
}
Err(e) => {
log_error!(self.logger, "Error enrolling payjoin: {e}");
(None, None)
}
};

Ok(MutinyBip21RawMaterials {
address,
invoice,
btc_amount: amount.map(|amount| bitcoin::Amount::from_sat(amount).to_btc().to_string()),
labels,
pj,
ohttp,
})
}

Expand Down
Loading
Loading