Skip to content

Commit

Permalink
feat: WIP payjoin
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jul 19, 2024
1 parent b0debd0 commit 44ed262
Show file tree
Hide file tree
Showing 14 changed files with 779 additions and 85 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ cdk-sqlite = { version = "0.2", path = "./crates/cdk-sqlite", default-features =
cdk-redb = { version = "0.2", path = "./crates/cdk-redb", default-features = false }
cdk-cln = { version = "0.1", path = "./crates/cdk-cln", default-features = false }
cdk-axum = { version = "0.1", path = "./crates/cdk-axum", default-features = false }
cdk-bdk = { version = "0.1", path = "./crates/cdk-bdk", default-features = false }
cdk-fake-wallet = { version = "0.1", path = "./crates/cdk-fake-wallet", default-features = false }
payjoin = { version = "0.19.0", features = ["receive", "v2", "io"] }
tokio = { version = "1", default-features = false }
thiserror = "1"
tracing = { version = "0.1", default-features = false, features = ["attributes", "log"] }
Expand All @@ -44,6 +46,12 @@ web-sys = { version = "0.3.69", default-features = false, features = ["console"
uuid = { version = "1", features = ["v4"] }
lightning-invoice = { version = "0.31", features = ["serde"] }
home = "0.5.9"
reqwest = { version = "0.12", default-features = false, features = [
"json",
"rustls-tls",
"rustls-tls-native-roots",
"socks",
] }

[profile]

Expand Down
76 changes: 63 additions & 13 deletions crates/cdk-axum/src/router_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cdk::error::{Error, ErrorResponse};
use cdk::nuts::nut05::MeltBolt11Response;
use cdk::nuts::nut17::{
MintBtcOnchainRequest, MintBtcOnchainResponse, MintQuoteBtcOnchainRequest,
MintQuoteBtcOnchainResponse,
MintQuoteBtcOnchainResponse, PayjoinInfo,
};
use cdk::nuts::nut18::{MeltQuoteBtcOnchainRequest, MeltQuoteBtcOnchainResponse};
use cdk::nuts::{
Expand Down Expand Up @@ -106,39 +106,61 @@ pub async fn get_mint_onchain_quote(
State(state): State<MintState>,
Json(payload): Json<MintQuoteBtcOnchainRequest>,
) -> Result<Json<MintQuoteBtcOnchainResponse>, Response> {
tracing::debug!("Mint quote unit: {}", payload.unit);
let onchain = state
.onchain
.get(&LnKey::new(payload.unit, PaymentMethod::Bolt11))
.get(&LnKey::new(payload.unit, PaymentMethod::BtcOnChain))
.ok_or({
tracing::info!("Bolt11 mint request for unsupported unit");
tracing::info!("Onchain mint request for unsupported unit");

into_response(Error::UnsupportedUnit)
})?;

let quote_expiry = unix_time() + state.quote_ttl;

let address = onchain.new_address().await.map_err(|err| {
tracing::error!("Could not create invoice: {}", err);
tracing::error!("Could not get onchain address: {}", err);
into_response(Error::InvalidPaymentRequest)
})?;

let quote = state
.mint
.new_mint_quote(
state.mint_url.into(),
address.clone(),
address.address.clone(),
payload.unit,
payload.amount,
quote_expiry,
address,
address.address,
)
.await
.map_err(|err| {
tracing::error!("Could not create new mint quote: {}", err);
into_response(err)
})?;

Ok(Json(quote.into()))
let settings = onchain.get_settings();

let payjoin = match settings.payjoin_settings.receive_enabled {
true => match (settings.payjoin_settings.ohttp_relay, address.payjoin_url) {
(Some(ohttp_relay), Some(payjoin_directory)) => Some(PayjoinInfo {
origin: payjoin_directory,
ohttp_relay: Some(ohttp_relay),
pjos: false,
}),
_ => None,
},
false => None,
};

let mint_quote_response = MintQuoteBtcOnchainResponse {
quote: quote.id,
address: quote.request,
state: quote.state,
payjoin,
};

Ok(Json(mint_quote_response))
}

pub async fn get_check_mint_bolt11_quote(
Expand Down Expand Up @@ -171,14 +193,22 @@ pub async fn get_check_mint_onchain_quote(

let address = quote.request.clone();

tracing::debug!("{:?}", state.onchain.keys());

let onchain = state
.onchain
.get(&LnKey::new(quote.unit, PaymentMethod::BtcOnChain))
.ok_or({
tracing::info!("Bolt11 mint request for unsupported unit");
.get(&LnKey::new(CurrencyUnit::Sat, PaymentMethod::BtcOnChain));

into_response(Error::UnsupportedUnit)
})?;
let onchain = match onchain {
Some(onchain) => onchain,
None => {
tracing::info!("Checking quote for unsupported unit");

return Err(into_response(Error::UnsupportedUnit));
}
};

tracing::info!("Checking paid status for {}", quote_id);

let AddressPaidResponse {
amount,
Expand All @@ -194,10 +224,30 @@ pub async fn get_check_mint_onchain_quote(
state.mint.update_mint_quote(quote).await.unwrap();
}

let settings = onchain.get_settings();

let payjoin = match settings.payjoin_settings.receive_enabled {
true => {
match (
settings.payjoin_settings.ohttp_relay,
settings.payjoin_settings.payjoin_directory,
) {
(Some(ohttp_relay), Some(payjoin_directory)) => Some(PayjoinInfo {
origin: payjoin_directory,
ohttp_relay: Some(ohttp_relay),
pjos: false,
}),
_ => None,
}
}
false => None,
};

let res = MintQuoteBtcOnchainResponse {
quote: quote.id,
address: quote.request,
state: quote.state.clone(),
state: quote.state,
payjoin,
};

Ok(Json(res))
Expand Down
7 changes: 4 additions & 3 deletions crates/cdk-bdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ description = "BDK on chain wallet for mint"
[dependencies]
anyhow.workspace = true
async-trait.workspace = true
bitcoin = "0.32.0"
bdk_chain = "0.16.0"
bdk_wallet = { version = "1.0.0-alpha.13", default-features = false, features = ["keys-bip39"] }
bdk_chain = {version = "0.16.0", features = ["std"]}
bdk_wallet = { version = "1.0.0-alpha.13", default-features = false, features = ["keys-bip39", "std"] }
bdk_esplora = { version = "0.15.0", default-features = false, features = ["std", "async-https"] }
bdk_file_store = "0.13.0"
cdk = { workspace = true, default-features = false, features = ["mint"] }
payjoin.workspace = true
reqwest.workspace = true
futures.workspace = true
tokio.workspace = true
tracing.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/cdk-bdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum Error {
UnknownInvoice,
#[error(transparent)]
Anyhow(#[from] anyhow::Error),
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
/// Esplora client error
#[error(transparent)]
EsploraClient(#[from] bdk_esplora::esplora_client::Error),
Expand Down
Loading

0 comments on commit 44ed262

Please sign in to comment.