Skip to content

Commit 27fae1c

Browse files
committed
Integrate ln addr for spark
1 parent d74dfef commit 27fae1c

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
@@ -296,4 +296,16 @@ impl Wallet {
296296
pub fn event_handled(&self) -> bool {
297297
self.inner.event_handled().is_ok()
298298
}
299+
300+
/// Gets the lightning address for this wallet, if one is set.
301+
pub async fn get_lightning_address(&self) -> Result<Option<String>, WalletError> {
302+
let result = self.inner.get_lightning_address().await?;
303+
Ok(result)
304+
}
305+
306+
/// Attempts to register the lightning address for this wallet.
307+
pub async fn register_lightning_address(&self, name: String) -> Result<(), WalletError> {
308+
self.inner.register_lightning_address(name).await?;
309+
Ok(())
310+
}
299311
}

orange-sdk/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl Wallet {
536536
ExtraConfig::Spark(sp) => Arc::new(Box::new(
537537
Spark::init(
538538
&config,
539-
*sp,
539+
sp.clone(),
540540
Arc::clone(&store),
541541
Arc::clone(&event_queue),
542542
tx_metadata.clone(),
@@ -1371,6 +1371,16 @@ impl Wallet {
13711371
res
13721372
}
13731373

1374+
/// Gets the lightning address for this wallet, if one is set.
1375+
pub async fn get_lightning_address(&self) -> Result<Option<String>, WalletError> {
1376+
Ok(self.inner.trusted.get_lightning_address().await?)
1377+
}
1378+
1379+
/// Attempts to register the lightning address for this wallet.
1380+
pub async fn register_lightning_address(&self, name: String) -> Result<(), WalletError> {
1381+
Ok(self.inner.trusted.register_lightning_address(name).await?)
1382+
}
1383+
13741384
/// Stops the wallet, which will stop the underlying LDK node and any background tasks.
13751385
/// This will ensure that any critical tasks have completed before stopping.
13761386
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
@@ -472,6 +472,22 @@ impl TrustedWalletInterface for Cashu {
472472
})
473473
}
474474

475+
fn get_lightning_address(
476+
&self,
477+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>> {
478+
Box::pin(async { Ok(None) })
479+
}
480+
481+
fn register_lightning_address(
482+
&self, _name: String,
483+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>> {
484+
Box::pin(async {
485+
Err(TrustedError::UnsupportedOperation(
486+
"register_lightning_address is not supported in Cashu Wallet".to_string(),
487+
))
488+
})
489+
}
490+
475491
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
476492
Box::pin(async move {
477493
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
@@ -412,6 +412,22 @@ impl TrustedWalletInterface for DummyTrustedWallet {
412412
})
413413
}
414414

415+
fn get_lightning_address(
416+
&self,
417+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>> {
418+
Box::pin(async { Ok(None) })
419+
}
420+
421+
fn register_lightning_address(
422+
&self, _name: String,
423+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>> {
424+
Box::pin(async {
425+
Err(TrustedError::UnsupportedOperation(
426+
"register_lightning_address is not supported in DummyTrustedWallet".to_string(),
427+
))
428+
})
429+
}
430+
415431
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
416432
Box::pin(async move {
417433
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, OptimizationConfig,
2323
PaymentDetails, PaymentStatus, PaymentType, PrepareSendPaymentRequest, ReceivePaymentMethod,
24-
ReceivePaymentRequest, SdkBuilder, SdkError, SdkEvent, SendPaymentMethod, SendPaymentRequest,
24+
ReceivePaymentRequest, RegisterLightningAddressRequest, SdkBuilder, SdkError, SdkEvent,
25+
SendPaymentMethod, 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
optimization_config: OptimizationConfig { auto_enabled: true, multiplicity: 1 },
8188
})
@@ -253,6 +260,34 @@ impl TrustedWalletInterface for Spark {
253260
})
254261
}
255262

263+
fn get_lightning_address(
264+
&self,
265+
) -> Pin<Box<dyn Future<Output = Result<Option<String>, TrustedError>> + Send + '_>> {
266+
Box::pin(async move {
267+
match self.spark_wallet.get_lightning_address().await? {
268+
None => Ok(None),
269+
Some(addr) => Ok(Some(addr.lightning_address)),
270+
}
271+
})
272+
}
273+
274+
fn register_lightning_address(
275+
&self, name: String,
276+
) -> Pin<Box<dyn Future<Output = Result<(), TrustedError>> + Send + '_>> {
277+
Box::pin(async move {
278+
let res = self.get_lightning_address().await?;
279+
if res.is_some() {
280+
return Err(TrustedError::Other(
281+
"Wallet already has a lightning address".to_string(),
282+
));
283+
}
284+
285+
let params = RegisterLightningAddressRequest { username: name, description: None };
286+
self.spark_wallet.register_lightning_address(params).await?;
287+
Ok(())
288+
})
289+
}
290+
256291
fn stop(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
257292
Box::pin(async move {
258293
log_info!(self.logger, "Stopping Spark wallet");
@@ -268,7 +303,8 @@ impl Spark {
268303
event_queue: Arc<EventQueue>, tx_metadata: TxMetadataStore, logger: Arc<Logger>,
269304
runtime: Arc<Runtime>,
270305
) -> Result<Self, InitFailure> {
271-
let spark_config: breez_sdk_spark::Config = spark_config.to_breez_config(config.network)?;
306+
let spark_config: breez_sdk_spark::Config =
307+
spark_config.into_breez_config(config.network)?;
272308

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

0 commit comments

Comments
 (0)