Skip to content

Commit

Permalink
Add prepare withdraw method (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar111190 authored Oct 25, 2023
1 parent a51d814 commit 89dd16a
Show file tree
Hide file tree
Showing 23 changed files with 650 additions and 75 deletions.
21 changes: 17 additions & 4 deletions libs/Cargo.lock

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

19 changes: 16 additions & 3 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ dictionary UnspentTransactionOutput {
u32 outnum;
u64 amount_millisatoshi;
string address;
boolean reserved;
boolean reserved;
};

dictionary NodeState {
Expand Down Expand Up @@ -536,6 +536,16 @@ enum BuyBitcoinProvider {
"Moonpay",
};

dictionary PrepareSweepRequest {
string to_address;
u64 sats_per_vbyte;
};

dictionary PrepareSweepResponse {
u64 sweep_tx_weight;
u64 sweep_tx_fee_sat;
};

dictionary SweepRequest {
string to_address;
u32 fee_rate_sats_per_vbyte;
Expand Down Expand Up @@ -580,7 +590,7 @@ dictionary RefundResponse {
string refund_tx_id;
};

interface BlockingBreezServices {
interface BlockingBreezServices {

[Throws=SdkError]
void disconnect();
Expand Down Expand Up @@ -686,6 +696,9 @@ interface BlockingBreezServices {

[Throws=SdkError]
BuyBitcoinResponse buy_bitcoin(BuyBitcoinRequest req);

[Throws=SdkError]
PrepareSweepResponse prepare_sweep(PrepareSweepRequest req);
};

namespace breez_sdk {
Expand All @@ -709,4 +722,4 @@ namespace breez_sdk {

[Throws=SdkError]
StaticBackupResponse static_backup(StaticBackupRequest req);
};
};
32 changes: 20 additions & 12 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};

use breez_sdk_core::{
error::*, mnemonic_to_seed as sdk_mnemonic_to_seed, parse as sdk_parse_input,
parse_invoice as sdk_parse_invoice, AesSuccessActionDataDecrypted, BackupFailedData,
Expand All @@ -17,14 +12,19 @@ use breez_sdk_core::{
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, SendPaymentRequest, SendPaymentResponse,
SendSpontaneousPaymentRequest, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SuccessActionProcessed, SwapInfo, SwapStatus, SweepRequest,
SweepResponse, Symbol, UnspentTransactionOutput, UrlSuccessActionData,
PaymentStatus, PaymentType, PaymentTypeFilter, PrepareSweepRequest, PrepareSweepResponse, Rate,
ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees,
RefundRequest, RefundResponse, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo,
ReverseSwapStatus, RouteHint, RouteHintHop, SendOnchainRequest, SendOnchainResponse,
SendPaymentRequest, SendPaymentResponse, SendSpontaneousPaymentRequest, SignMessageRequest,
SignMessageResponse, StaticBackupRequest, StaticBackupResponse, SuccessActionProcessed,
SwapInfo, SwapStatus, SweepRequest, SweepResponse, Symbol, UnspentTransactionOutput,
UrlSuccessActionData,
};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};
use std::sync::Arc;

static RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
static LOG_INIT: OnceCell<bool> = OnceCell::new();

Expand Down Expand Up @@ -286,6 +286,14 @@ impl BlockingBreezServices {
pub fn buy_bitcoin(&self, req: BuyBitcoinRequest) -> SdkResult<BuyBitcoinResponse> {
rt().block_on(self.breez_services.buy_bitcoin(req))
}

pub fn prepare_sweep(
&self,
req: PrepareSweepRequest,
) -> Result<PrepareSweepResponse, SdkError> {
rt().block_on(self.breez_services.prepare_sweep(req))
.map_err(|e| e.into())
}
}

pub fn parse_invoice(invoice: String) -> SdkResult<LNInvoice> {
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ base64 = "0.13.0"
chrono = "0.4"
ecies = { version = "0.2", default-features = false, features = ["pure"] }
env_logger = "0.10"
futures = "0.3.28"
ripemd = "0.1"
rand = "0.8"
tiny-bip39 = "1"
Expand Down
16 changes: 11 additions & 5 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ use crate::{
BackupStatus, BuyBitcoinRequest, BuyBitcoinResponse, CheckMessageRequest, CheckMessageResponse,
EnvironmentType, ListPaymentsRequest, LnUrlCallbackStatus, LnUrlPayRequest,
LnUrlWithdrawRequest, LnUrlWithdrawResult, NodeConfig, OpenChannelFeeRequest,
OpenChannelFeeResponse, ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse,
RefundRequest, RefundResponse, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo,
SendOnchainRequest, SendOnchainResponse, SendPaymentRequest, SendPaymentResponse,
SendSpontaneousPaymentRequest, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SweepRequest, SweepResponse,
OpenChannelFeeResponse, PrepareSweepRequest, PrepareSweepResponse, ReceiveOnchainRequest,
ReceivePaymentRequest, ReceivePaymentResponse, RefundRequest, RefundResponse,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, SendOnchainRequest,
SendOnchainResponse, SendPaymentRequest, SendPaymentResponse, SendSpontaneousPaymentRequest,
SignMessageRequest, SignMessageResponse, StaticBackupRequest, StaticBackupResponse,
SweepRequest, SweepResponse,
};

/*
Expand Down Expand Up @@ -301,6 +302,11 @@ pub fn sweep(req: SweepRequest) -> Result<SweepResponse> {
block_on(async { get_breez_services().await?.sweep(req).await })
}

/// See [BreezServices::prepare_sweep]
pub fn prepare_sweep(req: PrepareSweepRequest) -> Result<PrepareSweepResponse> {
block_on(async { get_breez_services().await?.prepare_sweep(req).await })
}

/* Refundables API's */

/// See [BreezServices::list_refundables]
Expand Down
16 changes: 11 additions & 5 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ impl BreezServices {
Ok(SweepResponse { txid })
}

pub async fn prepare_sweep(&self, req: PrepareSweepRequest) -> Result<PrepareSweepResponse> {
self.start_node().await?;
let response = self.node_api.prepare_sweep(req).await?;
Ok(response)
}

/// Fetch live rates of fiat currencies
pub async fn fetch_fiat_rates(&self) -> Result<Vec<Rate>> {
self.fiat_api.fetch_fiat_rates().await
Expand Down Expand Up @@ -1430,6 +1436,11 @@ impl BreezServicesBuilder {
.unwrap_or_else(|| Arc::new(SqliteStorage::new(self.config.working_dir.clone())));
persister.init()?;

// mempool space is used to monitor the chain
let chain_service = Arc::new(MempoolSpace::from_base_url(
self.config.mempoolspace_url.clone(),
));

let mut node_api = self.node_api.clone();
let mut backup_transport = self.backup_transport.clone();
if node_api.is_none() {
Expand Down Expand Up @@ -1500,11 +1511,6 @@ impl BreezServicesBuilder {
self.config.api_key.clone(),
));

// mempool space is used to monitor the chain
let chain_service = Arc::new(MempoolSpace::from_base_url(
self.config.mempoolspace_url.clone(),
));

let current_lsp_id = persister.get_lsp_id()?;
if current_lsp_id.is_none() && self.config.default_lsp_id.is_some() {
persister.set_lsp_id(self.config.default_lsp_id.clone().unwrap())?;
Expand Down
46 changes: 46 additions & 0 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ pub extern "C" fn wire_sweep(port_: i64, req: *mut wire_SweepRequest) {
wire_sweep_impl(port_, req)
}

#[no_mangle]
pub extern "C" fn wire_prepare_sweep(port_: i64, req: *mut wire_PrepareSweepRequest) {
wire_prepare_sweep_impl(port_, req)
}

#[no_mangle]
pub extern "C" fn wire_list_refundables(port_: i64) {
wire_list_refundables_impl(port_)
Expand Down Expand Up @@ -301,6 +306,11 @@ pub extern "C" fn new_box_autoadd_opening_fee_params_0() -> *mut wire_OpeningFee
support::new_leak_box_ptr(wire_OpeningFeeParams::new_with_null_ptr())
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_prepare_sweep_request_0() -> *mut wire_PrepareSweepRequest {
support::new_leak_box_ptr(wire_PrepareSweepRequest::new_with_null_ptr())
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_receive_onchain_request_0() -> *mut wire_ReceiveOnchainRequest {
support::new_leak_box_ptr(wire_ReceiveOnchainRequest::new_with_null_ptr())
Expand Down Expand Up @@ -474,6 +484,12 @@ impl Wire2Api<OpeningFeeParams> for *mut wire_OpeningFeeParams {
Wire2Api::<OpeningFeeParams>::wire2api(*wrap).into()
}
}
impl Wire2Api<PrepareSweepRequest> for *mut wire_PrepareSweepRequest {
fn wire2api(self) -> PrepareSweepRequest {
let wrap = unsafe { support::box_from_leak_ptr(self) };
Wire2Api::<PrepareSweepRequest>::wire2api(*wrap).into()
}
}
impl Wire2Api<ReceiveOnchainRequest> for *mut wire_ReceiveOnchainRequest {
fn wire2api(self) -> ReceiveOnchainRequest {
let wrap = unsafe { support::box_from_leak_ptr(self) };
Expand Down Expand Up @@ -705,6 +721,14 @@ impl Wire2Api<OpeningFeeParams> for wire_OpeningFeeParams {
}
}

impl Wire2Api<PrepareSweepRequest> for wire_PrepareSweepRequest {
fn wire2api(self) -> PrepareSweepRequest {
PrepareSweepRequest {
to_address: self.to_address.wire2api(),
sats_per_vbyte: self.sats_per_vbyte.wire2api(),
}
}
}
impl Wire2Api<ReceiveOnchainRequest> for wire_ReceiveOnchainRequest {
fn wire2api(self) -> ReceiveOnchainRequest {
ReceiveOnchainRequest {
Expand Down Expand Up @@ -927,6 +951,13 @@ pub struct wire_OpeningFeeParams {
promise: *mut wire_uint_8_list,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_PrepareSweepRequest {
to_address: *mut wire_uint_8_list,
sats_per_vbyte: u64,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_ReceiveOnchainRequest {
Expand Down Expand Up @@ -1286,6 +1317,21 @@ impl Default for wire_OpeningFeeParams {
}
}

impl NewWithNullPtr for wire_PrepareSweepRequest {
fn new_with_null_ptr() -> Self {
Self {
to_address: core::ptr::null_mut(),
sats_per_vbyte: Default::default(),
}
}
}

impl Default for wire_PrepareSweepRequest {
fn default() -> Self {
Self::new_with_null_ptr()
}
}

impl NewWithNullPtr for wire_ReceiveOnchainRequest {
fn new_with_null_ptr() -> Self {
Self {
Expand Down
29 changes: 29 additions & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ use crate::models::PaymentDetails;
use crate::models::PaymentStatus;
use crate::models::PaymentType;
use crate::models::PaymentTypeFilter;
use crate::models::PrepareSweepRequest;
use crate::models::PrepareSweepResponse;
use crate::models::ReceiveOnchainRequest;
use crate::models::ReceivePaymentRequest;
use crate::models::ReceivePaymentResponse;
Expand Down Expand Up @@ -565,6 +567,22 @@ fn wire_sweep_impl(port_: MessagePort, req: impl Wire2Api<SweepRequest> + Unwind
},
)
}
fn wire_prepare_sweep_impl(
port_: MessagePort,
req: impl Wire2Api<PrepareSweepRequest> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap(
WrapInfo {
debug_name: "prepare_sweep",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_req = req.wire2api();
move |task_callback| prepare_sweep(api_req)
},
)
}
fn wire_list_refundables_impl(port_: MessagePort) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap(
WrapInfo {
Expand Down Expand Up @@ -1261,6 +1279,17 @@ impl support::IntoDart for PaymentType {
}
}
impl support::IntoDartExceptPrimitive for PaymentType {}
impl support::IntoDart for PrepareSweepResponse {
fn into_dart(self) -> support::DartAbi {
vec![
self.sweep_tx_weight.into_dart(),
self.sweep_tx_fee_sat.into_dart(),
]
.into_dart()
}
}
impl support::IntoDartExceptPrimitive for PrepareSweepResponse {}

impl support::IntoDart for Rate {
fn into_dart(self) -> support::DartAbi {
vec![self.coin.into_dart(), self.value.into_dart()].into_dart()
Expand Down
Loading

0 comments on commit 89dd16a

Please sign in to comment.