Skip to content

Commit 08ae4bf

Browse files
committed
Integrate ln addr for spark
1 parent f9ac3f1 commit 08ae4bf

6 files changed

Lines changed: 107 additions & 7 deletions

File tree

orange-sdk/src/ffi/orange/wallet.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,16 @@ impl Wallet {
251251
pub fn event_handled(&self) -> bool {
252252
self.inner.event_handled().is_ok()
253253
}
254+
255+
/// Gets the lightning address for this wallet, if one is set.
256+
pub async fn get_lightning_address(&self) -> Result<Option<String>, WalletError> {
257+
let result = self.inner.get_lightning_address().await?;
258+
Ok(result)
259+
}
260+
261+
/// Attempts to register the lightning address for this wallet.
262+
pub async fn register_lightning_address(&self, name: String) -> Result<(), WalletError> {
263+
self.inner.register_lightning_address(name).await?;
264+
Ok(())
265+
}
254266
}

orange-sdk/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl Wallet {
533533
ExtraConfig::Spark(sp) => Arc::new(Box::new(
534534
Spark::init(
535535
&config,
536-
*sp,
536+
sp.clone(),
537537
Arc::clone(&store),
538538
Arc::clone(&event_queue),
539539
tx_metadata.clone(),
@@ -1345,6 +1345,16 @@ impl Wallet {
13451345
res
13461346
}
13471347

1348+
/// Gets the lightning address for this wallet, if one is set.
1349+
pub async fn get_lightning_address(&self) -> Result<Option<String>, WalletError> {
1350+
Ok(self.inner.trusted.get_lightning_address().await?)
1351+
}
1352+
1353+
/// Attempts to register the lightning address for this wallet.
1354+
pub async fn register_lightning_address(&self, name: String) -> Result<(), WalletError> {
1355+
Ok(self.inner.trusted.register_lightning_address(name).await?)
1356+
}
1357+
13481358
/// Stops the wallet, which will stop the underlying LDK node and any background tasks.
13491359
/// This will ensure that any critical tasks have completed before stopping.
13501360
pub async fn stop(&self) {

orange-sdk/src/trusted_wallet/cashu/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ impl TrustedWalletInterface for Cashu {
469469
})
470470
}
471471

472+
fn get_lightning_address(
473+
&self,
474+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>> {
475+
Box::pin(async { Ok(None) })
476+
}
477+
478+
fn register_lightning_address(
479+
&self, _name: String,
480+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>> {
481+
Box::pin(async {
482+
Err(TrustedError::UnsupportedOperation(
483+
"register_lightning_address is not supported in Cashu Wallet".to_string(),
484+
))
485+
})
486+
}
487+
472488
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
473489
Box::pin(async move {
474490
log_info!(self.logger, "Stopping Cashu wallet");

orange-sdk/src/trusted_wallet/dummy.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,22 @@ impl TrustedWalletInterface for DummyTrustedWallet {
411411
})
412412
}
413413

414+
fn get_lightning_address(
415+
&self,
416+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>> {
417+
Box::pin(async { Ok(None) })
418+
}
419+
420+
fn register_lightning_address(
421+
&self, _name: String,
422+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>> {
423+
Box::pin(async {
424+
Err(TrustedError::UnsupportedOperation(
425+
"register_lightning_address is not supported in DummyTrustedWallet".to_string(),
426+
))
427+
})
428+
}
429+
414430
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
415431
Box::pin(async move {
416432
let _ = self.ldk_node.stop();

orange-sdk/src/trusted_wallet/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ pub trait TrustedWalletInterface: Send + Sync + private::Sealed {
8989
&self, payment_hash: [u8; 32],
9090
) -> Pin<Box<dyn Future<Output = Option<ReceivedLightningPayment>> + Send + '_>>;
9191

92+
/// Gets the lightning address for this wallet, if one is set.
93+
fn get_lightning_address(
94+
&self,
95+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>>;
96+
97+
/// Attempts to register the lightning address for this wallet.
98+
fn register_lightning_address(
99+
&self, name: String,
100+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>>;
101+
92102
/// Stops the wallet, cleaning up any resources.
93103
/// This is typically used to gracefully shut down the wallet.
94104
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;

orange-sdk/src/trusted_wallet/spark/mod.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use bitcoin_payment_instructions::amount::Amount;
2121
use breez_sdk_spark::{
2222
BreezSdk, EventListener, GetInfoRequest, ListPaymentsRequest, PaymentDetails, PaymentStatus,
2323
PaymentType, PrepareSendPaymentRequest, ReceivePaymentMethod, ReceivePaymentRequest,
24-
SdkBuilder, SdkError, SdkEvent, SendPaymentMethod, SendPaymentRequest,
24+
RegisterLightningAddressRequest, SdkBuilder, SdkError, SdkEvent, SendPaymentMethod,
25+
SendPaymentRequest,
2526
};
2627

2728
use graduated_rebalancer::ReceivedLightningPayment;
@@ -37,7 +38,7 @@ use std::time::Duration;
3738
use uuid::Uuid;
3839

3940
/// Configuration options for the Spark wallet.
40-
#[derive(Debug, Copy, Clone)]
41+
#[derive(Debug, Clone)]
4142
pub struct SparkWalletConfig {
4243
/// How often to sync the wallet with the blockchain, in seconds.
4344
/// Default is 60 seconds.
@@ -46,11 +47,17 @@ pub struct SparkWalletConfig {
4647
/// lightning when sending and receiving. This has the benefit of lower fees
4748
/// but is at the cost of privacy.
4849
pub prefer_spark_over_lightning: bool,
50+
/// The domain used for receiving through lnurl-pay and lightning address.
51+
pub lnurl_domain: Option<String>,
4952
}
5053

5154
impl Default for SparkWalletConfig {
5255
fn default() -> Self {
53-
SparkWalletConfig { sync_interval_secs: 60, prefer_spark_over_lightning: false }
56+
SparkWalletConfig {
57+
sync_interval_secs: 60,
58+
prefer_spark_over_lightning: false,
59+
lnurl_domain: Some("breez.tips".to_string()),
60+
}
5461
}
5562
}
5663

@@ -59,7 +66,7 @@ impl Default for SparkWalletConfig {
5966
const BREEZ_API_KEY: &str = "MIIBajCCARygAwIBAgIHPnfOjAhBgzAFBgMrZXAwEDEOMAwGA1UEAxMFQnJlZXowHhcNMjUwOTE5MjEzNTU1WhcNMzUwOTE3MjEzNTU1WjAqMRMwEQYDVQQKEwpvcmFuZ2Utc2RrMRMwEQYDVQQDEwpvcmFuZ2Utc2RrMCowBQYDK2VwAyEA0IP1y98gPByiIMoph1P0G6cctLb864rNXw1LRLOpXXejezB5MA4GA1UdDwEB/wQEAwIFoDAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBTaOaPuXmtLDTJVv++VYBiQr9gHCTAfBgNVHSMEGDAWgBTeqtaSVvON53SSFvxMtiCyayiYazAZBgNVHREEEjAQgQ5iZW5Ac3BpcmFsLnh5ejAFBgMrZXADQQCry+1LkA3nrYa1sovS5iFI1Tkpmr/R0nM/4gJtsO93vFOkm3vBEGwjKAV7lrGzFcFbbuyM1wEJPi4Po1XCEG0D";
6067

6168
impl SparkWalletConfig {
62-
fn to_breez_config(self, network: Network) -> Result<breez_sdk_spark::Config, TrustedError> {
69+
fn into_breez_config(self, network: Network) -> Result<breez_sdk_spark::Config, TrustedError> {
6370
let network = match network {
6471
Network::Bitcoin => breez_sdk_spark::Network::Mainnet,
6572
Network::Regtest => breez_sdk_spark::Network::Regtest,
@@ -75,7 +82,7 @@ impl SparkWalletConfig {
7582
real_time_sync_server_url: None,
7683
api_key: Some(BREEZ_API_KEY.to_string()),
7784
max_deposit_claim_fee: None,
78-
lnurl_domain: None,
85+
lnurl_domain: self.lnurl_domain,
7986
private_enabled_default: true,
8087
})
8188
}
@@ -245,6 +252,34 @@ impl TrustedWalletInterface for Spark {
245252
})
246253
}
247254

255+
fn get_lightning_address(
256+
&self,
257+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>> {
258+
Box::pin(async move {
259+
match self.spark_wallet.get_lightning_address().await? {
260+
None => Ok(None),
261+
Some(addr) => Ok(Some(addr.lightning_address)),
262+
}
263+
})
264+
}
265+
266+
fn register_lightning_address(
267+
&self, name: String,
268+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>> {
269+
Box::pin(async move {
270+
let res = self.get_lightning_address().await?;
271+
if res.is_some() {
272+
return Err(TrustedError::Other(
273+
"Wallet already has a lightning address".to_string(),
274+
));
275+
}
276+
277+
let params = RegisterLightningAddressRequest { username: name, description: None };
278+
self.spark_wallet.register_lightning_address(params).await?;
279+
Ok(())
280+
})
281+
}
282+
248283
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
249284
Box::pin(async move {
250285
log_info!(self.logger, "Stopping Spark wallet");
@@ -260,7 +295,8 @@ impl Spark {
260295
event_queue: Arc<EventQueue>, tx_metadata: TxMetadataStore, logger: Arc<Logger>,
261296
runtime: Arc<Runtime>,
262297
) -> Result<Self, InitFailure> {
263-
let spark_config: breez_sdk_spark::Config = spark_config.to_breez_config(config.network)?;
298+
let spark_config: breez_sdk_spark::Config =
299+
spark_config.into_breez_config(config.network)?;
264300

265301
let seed = match &config.seed {
266302
Seed::Seed64(bytes) => breez_sdk_spark::Seed::Entropy(bytes.to_vec()),

0 commit comments

Comments
 (0)