Skip to content

Commit d1a8f35

Browse files
committed
Put cashu and spark behind feature flags
1 parent 1067b6d commit d1a8f35

7 files changed

Lines changed: 45 additions & 39 deletions

File tree

orange-sdk/Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ version = "0.1.0"
44
edition = "2024"
55

66
[features]
7-
_test-utils = ["corepc-node"]
7+
default = ["spark"]
8+
spark = ["spark-wallet", "uuid"]
9+
cashu = ["cdk", "serde_json"]
10+
_test-utils = ["corepc-node", "cashu"]
811

912
[dependencies]
1013
graduated-rebalancer = { path = "../graduated-rebalancer", version = "0.1.0" }
@@ -14,11 +17,11 @@ bitcoin-payment-instructions = { version = "0.4.0", features = ["http"] }
1417
chrono = { version = "0.4", default-features = false }
1518
rand = "0.8.5"
1619
reqwest = { version = "0.12.23", default-features = false, features = ["rustls-tls"] }
17-
spark-wallet = { path = "../../rust-spark/crates/spark-wallet" }
20+
spark-wallet = { path = "../../rust-spark/crates/spark-wallet", optional = true }
1821
tokio = { version = "1.0", default-features = false, features = ["rt-multi-thread", "sync"] }
19-
uuid = { version = "1.0", default-features = false }
20-
cdk = "0.12.0"
21-
serde_json = "1.0"
22+
uuid = { version = "1.0", default-features = false, optional = true }
23+
cdk = { version = "0.12.0", default-features = false, features = ["wallet"], optional = true }
24+
serde_json = { version = "1.0", optional = true }
2225
async-trait = "0.1"
2326

2427
corepc-node = { version = "0.8.0", features = ["29_0", "download"], optional = true }

orange-sdk/src/builder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Builder for constructing [`Wallet`] instances with a fluent API.
22
3+
#[cfg(feature = "spark")]
34
use crate::trusted_wallet::TrustedError;
45
use crate::{
56
ChainSource, ExtraConfig, InitFailure, Seed, StorageConfig, Tunables, Wallet, WalletConfig,
@@ -8,7 +9,6 @@ use crate::{
89
use ldk_node::bitcoin::secp256k1::PublicKey;
910
use ldk_node::bitcoin::{Network, io};
1011
use ldk_node::lightning::ln::msgs::SocketAddress;
11-
use spark_wallet::SparkWalletConfig;
1212
use std::fmt;
1313
use std::path::PathBuf;
1414
use std::sync::Arc;
@@ -285,14 +285,15 @@ impl WalletBuilder {
285285
/// - Network connection fails
286286
/// - Trusted wallet initialization fails
287287
/// - LDK node fails to start
288+
#[cfg(feature = "spark")]
288289
pub async fn build_spark(
289-
self, spark_config: Option<SparkWalletConfig>,
290+
self, spark_config: Option<spark_wallet::SparkWalletConfig>,
290291
) -> Result<Wallet, InitFailure> {
291292
let spark_config = match spark_config {
292293
Some(cfg) => cfg,
293294
None => {
294295
let network = self.network.ok_or(BuilderError::MissingNetwork)?;
295-
SparkWalletConfig::default_config(
296+
spark_wallet::SparkWalletConfig::default_config(
296297
network
297298
.try_into()
298299
.map_err(|_| InitFailure::TrustedFailure(TrustedError::InvalidNetwork))?,

orange-sdk/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ use bitcoin_payment_instructions::amount::Amount;
2121

2222
use crate::rebalancer::{OrangeRebalanceEventHandler, OrangeTrigger};
2323
use crate::store::{PaymentId, TxMetadata, TxMetadataStore, TxType};
24+
#[cfg(feature = "cashu")]
2425
use crate::trusted_wallet::cashu::Cashu;
2526
#[cfg(feature = "_test-utils")]
2627
use crate::trusted_wallet::dummy::DummyTrustedWallet;
28+
#[cfg(feature = "spark")]
2729
use crate::trusted_wallet::spark::Spark;
2830
use crate::trusted_wallet::{DynTrustedWalletInterface, WalletTrusted};
2931
use graduated_rebalancer::GraduatedRebalancer;
@@ -60,12 +62,17 @@ use lightning_wallet::LightningWallet;
6062
use logging::Logger;
6163
use trusted_wallet::TrustedError;
6264

65+
#[cfg(feature = "cashu")]
66+
pub use crate::trusted_wallet::cashu::CashuConfig;
6367
pub use bitcoin_payment_instructions;
6468
pub use builder::{BuilderError, WalletBuilder};
69+
#[cfg(feature = "cashu")]
70+
pub use cdk::nuts::nut00::CurrencyUnit;
6571
pub use event::{Event, EventQueue};
6672
pub use ldk_node::bip39::Mnemonic;
6773
pub use ldk_node::bitcoin;
6874
pub use ldk_node::payment::ConfirmationStatus;
75+
#[cfg(feature = "spark")]
6976
pub use spark_wallet::{OperatorPoolConfig, ServiceProviderConfig, SparkWalletConfig};
7077
pub use store::{PaymentType, Transaction, TxStatus};
7178
pub use trusted_wallet::ExtraConfig;
@@ -496,18 +503,22 @@ impl Wallet {
496503
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
497504

498505
let trusted: Arc<Box<DynTrustedWalletInterface>> = match &config.extra_config {
499-
ExtraConfig::Spark(_) => Arc::new(Box::new(
506+
#[cfg(feature = "spark")]
507+
ExtraConfig::Spark(sp) => Arc::new(Box::new(
500508
Spark::init(
501509
&config,
510+
sp.clone(),
502511
Arc::clone(&store),
503512
Arc::clone(&event_queue),
504513
Arc::clone(&logger),
505514
)
506515
.await?,
507516
)),
508-
ExtraConfig::Cashu(_) => Arc::new(Box::new(
517+
#[cfg(feature = "cashu")]
518+
ExtraConfig::Cashu(cashu) => Arc::new(Box::new(
509519
Cashu::init(
510520
&config,
521+
cashu.clone(),
511522
Arc::clone(&store),
512523
Arc::clone(&event_queue),
513524
Arc::clone(&logger),

orange-sdk/src/store.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub struct Transaction {
9292
/// A [Transaction] that is stored in the database. We have to modify the `Transaction` type
9393
/// to have types that all implement `Writeable` and `Readable` so that we can store it in the database.
9494
#[derive(Debug, Clone)]
95+
#[allow(dead_code)] // Some trusted backends don't use this
9596
pub(crate) struct StoreTransaction {
9697
pub status: TxStatus,
9798
pub outbound: bool,

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::logging::Logger;
44
use crate::store::{PaymentId, TxStatus};
55
use crate::trusted_wallet::{Payment, TrustedError, TrustedWalletInterface};
6-
use crate::{Event, EventQueue, ExtraConfig, InitFailure, Seed, WalletConfig};
6+
use crate::{Event, EventQueue, InitFailure, Seed, WalletConfig};
77

88
use ldk_node::bitcoin::hashes::Hash;
99
use ldk_node::bitcoin::hashes::sha256::Hash as Sha256;
@@ -267,14 +267,9 @@ impl TrustedWalletInterface for Cashu {
267267

268268
impl Cashu {
269269
pub(crate) async fn init(
270-
config: &WalletConfig, store: Arc<dyn KVStore + Sync + Send>,
270+
config: &WalletConfig, cashu_config: CashuConfig, store: Arc<dyn KVStore + Sync + Send>,
271271
event_queue: Arc<EventQueue>, logger: Arc<Logger>,
272272
) -> Result<Self, InitFailure> {
273-
let cashu_config = match config.extra_config {
274-
ExtraConfig::Cashu(ref c) => c.clone(),
275-
_ => unreachable!(),
276-
};
277-
278273
// Create the seed from the configuration
279274
let seed = match &config.seed {
280275
Seed::Seed64(bytes) => {
@@ -372,13 +367,7 @@ impl Cashu {
372367
}
373368
}
374369

375-
Ok(Cashu {
376-
cashu_wallet,
377-
shutdown_sender,
378-
logger,
379-
supports_bolt12,
380-
mint_quote_sender,
381-
})
370+
Ok(Cashu { cashu_wallet, shutdown_sender, logger, supports_bolt12, mint_quote_sender })
382371
}
383372

384373
/// Convert an ID string to a 32-byte array

orange-sdk/src/trusted_wallet/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ use std::pin::Pin;
1212
use std::sync::Arc;
1313
use std::time::Duration;
1414

15+
#[cfg(feature = "cashu")]
16+
pub mod cashu;
1517
#[cfg(feature = "_test-utils")]
1618
pub mod dummy;
19+
#[cfg(feature = "spark")]
1720
pub mod spark;
18-
pub mod cashu;
1921

2022
/// Represents a payment with its associated details.
2123
///
@@ -115,8 +117,10 @@ impl<T: ?Sized + TrustedWalletInterface> graduated_rebalancer::TrustedWallet for
115117
/// Extra configuration needed for different types of wallets.
116118
pub enum ExtraConfig {
117119
/// Configuration for Spark wallet.
120+
#[cfg(feature = "spark")]
118121
Spark(crate::SparkWalletConfig),
119122
/// Configuration for Cashu wallet.
123+
#[cfg(feature = "cashu")]
120124
Cashu(cashu::CashuConfig),
121125
/// Configuration for dummy wallet (test-only).
122126
#[cfg(feature = "_test-utils")]
@@ -131,7 +135,9 @@ mod private {
131135
pub trait Sealed {}
132136

133137
// Only implement Sealed for types you want to allow
138+
#[cfg(feature = "spark")]
134139
impl Sealed for super::spark::Spark {}
140+
#[cfg(feature = "cashu")]
135141
impl Sealed for super::cashu::Cashu {}
136142
#[cfg(feature = "_test-utils")]
137143
impl Sealed for super::dummy::DummyTrustedWallet {}

orange-sdk/src/trusted_wallet/spark.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::bitcoin::{Txid, io};
55
use crate::logging::Logger;
66
use crate::store::{PaymentId, StoreTransaction, TxStatus};
77
use crate::trusted_wallet::{Payment, TrustedError, TrustedWalletInterface};
8-
use crate::{Event, EventQueue, ExtraConfig, InitFailure, PaymentType, Seed, WalletConfig};
8+
use crate::{Event, EventQueue, InitFailure, PaymentType, Seed, WalletConfig};
99

1010
use ldk_node::bitcoin::hashes::Hash;
1111
use ldk_node::bitcoin::hashes::sha256::Hash as Sha256;
@@ -21,8 +21,8 @@ use bitcoin_payment_instructions::PaymentMethod;
2121
use bitcoin_payment_instructions::amount::Amount;
2222

2323
use spark_wallet::{
24-
DefaultSigner, Order, PagingFilter, PayLightningInvoiceResult, SparkWallet, SparkWalletError,
25-
SspUserRequest, TransferStatus, WalletEvent, WalletTransfer,
24+
DefaultSigner, Order, PagingFilter, PayLightningInvoiceResult, SparkWallet, SparkWalletConfig,
25+
SparkWalletError, SspUserRequest, TransferStatus, WalletEvent, WalletTransfer,
2626
};
2727

2828
use tokio::sync::watch;
@@ -226,37 +226,32 @@ const SPARK_SYNC_OFFSET_KEY: &str = "sync_offset";
226226
impl Spark {
227227
/// Initialize a new Spark wallet instance with the given configuration.
228228
pub(crate) async fn init(
229-
config: &WalletConfig, store: Arc<dyn KVStore + Sync + Send>, event_queue: Arc<EventQueue>,
230-
logger: Arc<Logger>,
229+
config: &WalletConfig, spark_config: SparkWalletConfig,
230+
store: Arc<dyn KVStore + Sync + Send>, event_queue: Arc<EventQueue>, logger: Arc<Logger>,
231231
) -> Result<Self, InitFailure> {
232-
let extra_config = match &config.extra_config {
233-
ExtraConfig::Spark(sp) => sp.clone(),
234-
_ => unreachable!("Not reachable, we are in Spark"),
235-
};
236-
237-
if config.network != extra_config.network.into() {
232+
if config.network != spark_config.network.into() {
238233
Err(TrustedError::InvalidNetwork)?
239234
}
240235

241236
let signer = match &config.seed {
242237
Seed::Seed64(bytes) => {
243238
// hash the seed to make sure it does not conflict with the lightning keys
244239
let seed = Sha256::hash(bytes);
245-
DefaultSigner::new(&seed[..], extra_config.network)
240+
DefaultSigner::new(&seed[..], spark_config.network)
246241
.map_err(|e| TrustedError::Other(format!("Failed to create signer: {e}")))?
247242
},
248243
Seed::Mnemonic { mnemonic, passphrase } => {
249244
// We don't hash the seed here, as mnemonics are meant to be easily recoverable
250245
// and if we hashed them, then you could not recover your spark coins from the mnemonic
251246
// in separate wallets.
252247
let seed = mnemonic.to_seed(passphrase.as_deref().unwrap_or(""));
253-
DefaultSigner::new(&seed[..], extra_config.network)
248+
DefaultSigner::new(&seed[..], spark_config.network)
254249
.map_err(|e| TrustedError::Other(format!("Failed to create signer: {e}")))?
255250
},
256251
};
257252

258253
let spark_wallet = Arc::new(
259-
SparkWallet::connect(extra_config, signer)
254+
SparkWallet::connect(spark_config, signer)
260255
.await
261256
.map_err(|e| InitFailure::TrustedFailure(e.into()))?,
262257
);

0 commit comments

Comments
 (0)