Skip to content

Commit

Permalink
Change send_spontaneous_payment to msat and wrap in request/response …
Browse files Browse the repository at this point in the history
…structs
  • Loading branch information
dangeross committed Oct 19, 2023
1 parent abeb31f commit 32ff026
Show file tree
Hide file tree
Showing 23 changed files with 1,623 additions and 849 deletions.
19 changes: 17 additions & 2 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ dictionary LnUrlErrorData {
string reason;
};

dictionary LnUrlPayRequest {
LnUrlPayRequestData req_data;
u64 amount_msat;
string? comment;
};

dictionary LnUrlPayRequestData {
string callback;
u64 min_sendable;
Expand Down Expand Up @@ -533,6 +539,15 @@ dictionary SweepResponse {
sequence<u8> txid;
};

dictionary SendPaymentResponse {
Payment payment;
};

dictionary SendSpontaneousPaymentRequest {
string node_id;
u64 amount_msat;
};

dictionary SendOnchainRequest {
u64 amount_sat;
string onchain_recipient_address;
Expand Down Expand Up @@ -563,13 +578,13 @@ interface BlockingBreezServices {
Payment send_payment(string bolt11, u64? amount_msat);

[Throws=SdkError]
Payment send_spontaneous_payment(string node_id, u64 amount_sats);
SendPaymentResponse send_spontaneous_payment(SendSpontaneousPaymentRequest req);

[Throws=SdkError]
ReceivePaymentResponse receive_payment(ReceivePaymentRequest req);

[Throws=SdkError]
LnUrlPayResult pay_lnurl(LnUrlPayRequestData req_data, u64 amount_sats, string? comment);
LnUrlPayResult pay_lnurl(LnUrlPayRequest req);

[Throws=SdkError]
LnUrlWithdrawResult withdraw_lnurl(LnUrlWithdrawRequestData req_data, u64 amount_sats, string? description);
Expand Down
33 changes: 11 additions & 22 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ use breez_sdk_core::{
ClosedChannelPaymentDetails, Config, CurrencyInfo, EnvironmentType, EventListener,
FeeratePreset, FiatCurrency, GreenlightCredentials, GreenlightNodeConfig, InputType,
InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails, LnUrlAuthRequestData,
LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayRequestData, LnUrlPayResult,
LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayRequest, LnUrlPayRequestData, LnUrlPayResult,
LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData, LocaleOverrides,
LocalizedName, LogEntry, LogStream, LspInformation, MessageSuccessActionData, MetadataItem,
Network, NodeConfig, NodeState, OpenChannelFeeRequest, OpenChannelFeeResponse,
OpeningFeeParams, OpeningFeeParamsMenu, Payment, PaymentDetails, PaymentFailedData,
PaymentStatus, PaymentType, PaymentTypeFilter, Rate, ReceiveOnchainRequest,
ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees, RefundRequest, RefundResponse,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint,
RouteHintHop, SendOnchainRequest, SendOnchainResponse, SignMessageRequest, SignMessageResponse,
StaticBackupRequest, StaticBackupResponse, SuccessActionProcessed, SwapInfo, SwapStatus,
SweepRequest, SweepResponse, Symbol, UnspentTransactionOutput, UrlSuccessActionData,
RouteHintHop, SendOnchainRequest, SendOnchainResponse, SendPaymentResponse,
SendSpontaneousPaymentRequest, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SuccessActionProcessed, SwapInfo, SwapStatus, SweepRequest,
SweepResponse, Symbol, UnspentTransactionOutput, UrlSuccessActionData,
};
static RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
static LOG_INIT: OnceCell<bool> = OnceCell::new();
Expand Down Expand Up @@ -118,13 +119,9 @@ impl BlockingBreezServices {

pub fn send_spontaneous_payment(
&self,
node_id: String,
amount_sats: u64,
) -> SdkResult<Payment> {
rt().block_on(
self.breez_services
.send_spontaneous_payment(node_id, amount_sats),
)
req: SendSpontaneousPaymentRequest,
) -> SdkResult<SendPaymentResponse> {
rt().block_on(self.breez_services.send_spontaneous_payment(req))
}

pub fn receive_payment(&self, req: ReceivePaymentRequest) -> SdkResult<ReceivePaymentResponse> {
Expand Down Expand Up @@ -163,17 +160,9 @@ impl BlockingBreezServices {
.map_err(|e| e.into())
}

pub fn pay_lnurl(
&self,
req_data: LnUrlPayRequestData,
amount_sats: u64,
comment: Option<String>,
) -> SdkResult<LnUrlPayResult> {
rt().block_on(
self.breez_services
.lnurl_pay(req_data, amount_sats, comment),
)
.map_err(|e| e.into())
pub fn pay_lnurl(&self, req: LnUrlPayRequest) -> SdkResult<LnUrlPayResult> {
rt().block_on(self.breez_services.lnurl_pay(req))
.map_err(|e| e.into())
}

pub fn withdraw_lnurl(
Expand Down
33 changes: 12 additions & 21 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ use crate::breez_services::{self, BreezEvent, BreezServices, EventListener};
use crate::chain::RecommendedFees;
use crate::error::SdkError;
use crate::fiat::{FiatCurrency, Rate};
use crate::input_parser::{
self, InputType, LnUrlAuthRequestData, LnUrlPayRequestData, LnUrlWithdrawRequestData,
};
use crate::input_parser::{self, InputType, LnUrlAuthRequestData, LnUrlWithdrawRequestData};
use crate::invoice::{self, LNInvoice};
use crate::lnurl::pay::model::LnUrlPayResult;
use crate::lsp::LspInformation;
use crate::models::{Config, LogEntry, NodeState, Payment, SwapInfo};
use crate::{
BackupStatus, BuyBitcoinRequest, BuyBitcoinResponse, CheckMessageRequest, CheckMessageResponse,
EnvironmentType, ListPaymentsRequest, LnUrlCallbackStatus, LnUrlWithdrawResult, NodeConfig,
OpenChannelFeeRequest, OpenChannelFeeResponse, ReceiveOnchainRequest, ReceivePaymentRequest,
ReceivePaymentResponse, RefundRequest, RefundResponse, ReverseSwapFeesRequest, ReverseSwapInfo,
ReverseSwapPairInfo, SendOnchainRequest, SendOnchainResponse, SignMessageRequest,
SignMessageResponse, StaticBackupRequest, StaticBackupResponse, SweepRequest, SweepResponse,
EnvironmentType, ListPaymentsRequest, LnUrlCallbackStatus, LnUrlPayRequest,
LnUrlWithdrawResult, NodeConfig, OpenChannelFeeRequest, OpenChannelFeeResponse,
ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RefundRequest,
RefundResponse, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo,
SendOnchainRequest, SendOnchainResponse, SendPaymentResponse, SendSpontaneousPaymentRequest,
SignMessageRequest, SignMessageResponse, StaticBackupRequest, StaticBackupResponse,
SweepRequest, SweepResponse,
};

/*
Expand Down Expand Up @@ -238,11 +238,11 @@ pub fn send_payment(bolt11: String, amount_msat: Option<u64>) -> Result<Payment>
}

/// See [BreezServices::send_spontaneous_payment]
pub fn send_spontaneous_payment(node_id: String, amount_sats: u64) -> Result<Payment> {
pub fn send_spontaneous_payment(req: SendSpontaneousPaymentRequest) -> Result<SendPaymentResponse> {
block_on(async {
get_breez_services()
.await?
.send_spontaneous_payment(node_id, amount_sats)
.send_spontaneous_payment(req)
.await
})
.map_err(anyhow::Error::new::<SdkError>)
Expand All @@ -257,17 +257,8 @@ pub fn receive_payment(req: ReceivePaymentRequest) -> Result<ReceivePaymentRespo
/* LNURL API's */

/// See [BreezServices::lnurl_pay]
pub fn lnurl_pay(
req_data: LnUrlPayRequestData,
user_amount_sat: u64,
comment: Option<String>,
) -> Result<LnUrlPayResult> {
block_on(async {
get_breez_services()
.await?
.lnurl_pay(req_data, user_amount_sat, comment)
.await
})
pub fn lnurl_pay(req: LnUrlPayRequest) -> Result<LnUrlPayResult> {
block_on(async { get_breez_services().await?.lnurl_pay(req).await })
}

/// See [BreezServices::lnurl_withdraw]
Expand Down
35 changes: 17 additions & 18 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::grpc::information_client::InformationClient;
use crate::grpc::signer_client::SignerClient;
use crate::grpc::swapper_client::SwapperClient;
use crate::grpc::PaymentInformation;
use crate::input_parser::LnUrlPayRequestData;
use crate::invoice::{add_lsp_routing_hints, parse_invoice, LNInvoice, RouteHint, RouteHintHop};
use crate::lnurl::auth::perform_lnurl_auth;
use crate::lnurl::pay::model::SuccessAction::Aes;
Expand Down Expand Up @@ -278,36 +277,36 @@ impl BreezServices {
///
/// # Arguments
///
/// * `node_id` - The destination node_id
/// * `amount_sats` - The amount to pay in satoshis
/// * `req` - Request parameters for sending a spontaneous payment
pub async fn send_spontaneous_payment(
&self,
node_id: String,
amount_sats: u64,
) -> SdkResult<Payment> {
req: SendSpontaneousPaymentRequest,
) -> SdkResult<SendPaymentResponse> {
self.start_node().await?;
let payment_res = self
.node_api
.send_spontaneous_payment(node_id.clone(), amount_sats)
.send_spontaneous_payment(req.node_id.clone(), req.amount_msat)
.await;
self.on_payment_completed(node_id, None, payment_res).await
let payment = self
.on_payment_completed(req.node_id, None, payment_res)
.await?;
Ok(SendPaymentResponse { payment })
}

/// Second step of LNURL-pay. The first step is `parse()`, which also validates the LNURL destination
/// and generates the `LnUrlPayRequestData` payload needed here.
///
/// This call will validate the given `user_amount_sat` and `comment` against the parameters
/// This call will validate the `amount_msat` and `comment` parameters of `req` against the parameters
/// of the LNURL endpoint (`req_data`). If they match the endpoint requirements, the LNURL payment
/// is made.
///
/// This method will return an [anyhow::Error] when any validation check fails.
pub async fn lnurl_pay(
&self,
req_data: LnUrlPayRequestData,
user_amount_sat: u64,
comment: Option<String>,
) -> Result<LnUrlPayResult> {
match validate_lnurl_pay(user_amount_sat, comment, req_data.clone()).await? {
///
/// # Arguments
///
/// * `req` - The [LnUrlPayRequest] request parameters for sending a LNURL payment
pub async fn lnurl_pay(&self, req: LnUrlPayRequest) -> Result<LnUrlPayResult> {
match validate_lnurl_pay(req.amount_msat, req.comment, req.req_data.clone()).await? {
ValidatedCallbackResponse::EndpointError { data: e } => {
Ok(LnUrlPayResult::EndpointError { data: e })
}
Expand Down Expand Up @@ -345,8 +344,8 @@ impl BreezServices {
self.persister.insert_lnurl_payment_external_info(
&details.payment_hash,
maybe_sa_processed.as_ref(),
Some(req_data.metadata_str),
req_data.ln_address,
Some(req.req_data.metadata_str),
req.req_data.ln_address,
None,
)?;

Expand Down
Loading

0 comments on commit 32ff026

Please sign in to comment.