Skip to content

Commit 1be8447

Browse files
committed
Bunch of small fixes
1 parent 33af0cd commit 1be8447

6 files changed

Lines changed: 65 additions & 78 deletions

File tree

examples/cli/src/main.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustyline::error::ReadlineError;
66

77
use orange_sdk::bitcoin_payment_instructions::amount::Amount;
88
use orange_sdk::{
9-
ChainSource, Event, ExtraConfig, Mnemonic, PaymentInfo, Seed, SparkNetwork, SparkWalletConfig,
10-
StorageConfig, Tunables, Wallet, WalletConfig, bitcoin::Network,
9+
ChainSource, Event, ExtraConfig, Mnemonic, PaymentInfo, Seed, SparkWalletConfig, StorageConfig,
10+
Tunables, Wallet, WalletConfig, bitcoin::Network,
1111
};
1212
use rand::RngCore;
1313
use std::fs;
@@ -89,14 +89,7 @@ fn get_config(network: Network) -> Result<WalletConfig> {
8989
network,
9090
seed,
9191
tunables: Tunables::default(),
92-
extra_config: ExtraConfig::Spark(SparkWalletConfig {
93-
api_key: None,
94-
network: SparkNetwork::Regtest,
95-
sync_interval_secs: 60,
96-
max_deposit_claim_fee: None,
97-
lnurl_domain: None,
98-
prefer_spark_over_lightning: false,
99-
}),
92+
extra_config: ExtraConfig::Spark(SparkWalletConfig::default()),
10093
})
10194
},
10295
Network::Bitcoin => {
@@ -123,14 +116,7 @@ fn get_config(network: Network) -> Result<WalletConfig> {
123116
network,
124117
seed,
125118
tunables: Tunables::default(),
126-
extra_config: ExtraConfig::Spark(SparkWalletConfig {
127-
api_key: None,
128-
network: SparkNetwork::Mainnet,
129-
sync_interval_secs: 60,
130-
max_deposit_claim_fee: None,
131-
lnurl_domain: None,
132-
prefer_spark_over_lightning: false,
133-
}),
119+
extra_config: ExtraConfig::Spark(SparkWalletConfig::default()),
134120
})
135121
},
136122
_ => Err(anyhow::anyhow!("Unsupported network: {network:?}")),

orange-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bitcoin-payment-instructions = { workspace = true }
2323
chrono = { version = "0.4", default-features = false }
2424
rand = { version = "0.8.5", optional = true }
2525
reqwest = { version = "0.12.23", default-features = false, features = ["rustls-tls"] }
26-
breez-sdk-spark = { git = "https://github.com/breez/spark-sdk.git", rev = "6994785e98c13191e6dc26146be6ce87c5c8d987", default-features = false, features = ["rustls-tls"], optional = true }
26+
breez-sdk-spark = { git = "https://github.com/breez/spark-sdk.git", rev = "e0ecc7ff44ecc9d3ae82e40f5cb576e80711a45d", default-features = false, features = ["rustls-tls"], optional = true }
2727
tokio = { version = "1.0", default-features = false, features = ["rt-multi-thread", "sync"] }
2828
uuid = { version = "1.0", default-features = false, optional = true }
2929
cdk = { git = "https://github.com/benthecarman/cdk.git", rev = "7d25e9ae5ed7f47f9ae7e87d8a9ee16797fee8cd", default-features = false, features = ["wallet"], optional = true }

orange-sdk/src/ffi/spark.rs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
1-
use spark_wallet::Network as SparkNetwork;
2-
use spark_wallet::SparkWalletConfig as SparkSparkWalletConfig;
3-
4-
use crate::ffi::Network;
1+
use crate::SparkWalletConfig as OrangeSparkWalletConfig;
52
use crate::{impl_from_core_type, impl_into_core_type};
63

7-
impl From<SparkNetwork> for Network {
8-
fn from(network: SparkNetwork) -> Self {
9-
match network {
10-
SparkNetwork::Mainnet => Network::Mainnet,
11-
SparkNetwork::Regtest => Network::Regtest,
12-
SparkNetwork::Testnet => Network::Testnet,
13-
SparkNetwork::Signet => Network::Signet,
14-
}
15-
}
16-
}
17-
18-
impl From<Network> for SparkNetwork {
19-
fn from(network: Network) -> Self {
20-
match network {
21-
Network::Mainnet => SparkNetwork::Mainnet,
22-
Network::Regtest => SparkNetwork::Regtest,
23-
Network::Testnet => SparkNetwork::Testnet,
24-
Network::Signet => SparkNetwork::Signet,
25-
}
26-
}
27-
}
28-
294
#[derive(Clone, Debug, uniffi::Object)]
30-
pub struct SparkWalletConfig(pub SparkSparkWalletConfig);
5+
pub struct SparkWalletConfig(pub OrangeSparkWalletConfig);
316

327
// TODO: For now just support the default configuration.
338
// In the future we will want to expose all of the Spark configuration objects
349
#[uniffi::export]
3510
impl SparkWalletConfig {
3611
#[uniffi::constructor]
37-
pub fn default_config(network: Network) -> Self {
38-
SparkWalletConfig(SparkSparkWalletConfig::default_config(network.into()))
12+
pub fn default_config() -> Self {
13+
SparkWalletConfig(OrangeSparkWalletConfig::default())
3914
}
4015
}
4116

42-
impl_from_core_type!(SparkSparkWalletConfig, SparkWalletConfig);
43-
impl_into_core_type!(SparkWalletConfig, SparkSparkWalletConfig);
17+
impl_from_core_type!(OrangeSparkWalletConfig, SparkWalletConfig);
18+
impl_into_core_type!(SparkWalletConfig, OrangeSparkWalletConfig);

orange-sdk/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ use trusted_wallet::TrustedError;
6565

6666
#[cfg(feature = "cashu")]
6767
pub use crate::trusted_wallet::cashu::CashuConfig;
68-
pub use bitcoin_payment_instructions;
6968
#[cfg(feature = "spark")]
70-
pub use breez_sdk_spark::{Config as SparkWalletConfig, Fee, Network as SparkNetwork};
69+
pub use crate::trusted_wallet::spark::SparkWalletConfig;
70+
pub use bitcoin_payment_instructions;
7171
#[cfg(feature = "cashu")]
7272
pub use cdk::nuts::nut00::CurrencyUnit;
7373
pub use event::{Event, EventQueue};
@@ -522,7 +522,7 @@ impl Wallet {
522522
ExtraConfig::Spark(sp) => Arc::new(Box::new(
523523
Spark::init(
524524
&config,
525-
sp.clone(),
525+
*sp,
526526
Arc::clone(&store),
527527
Arc::clone(&event_queue),
528528
tx_metadata.clone(),

orange-sdk/src/trusted_wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<T: ?Sized + TrustedWalletInterface> graduated_rebalancer::TrustedWallet for
125125
pub enum ExtraConfig {
126126
/// Configuration for Spark wallet.
127127
#[cfg(feature = "spark")]
128-
Spark(crate::SparkWalletConfig), // todo make my own reduced version
128+
Spark(crate::SparkWalletConfig),
129129
/// Configuration for Cashu wallet.
130130
#[cfg(feature = "cashu")]
131131
Cashu(cashu::CashuConfig),

orange-sdk/src/trusted_wallet/spark.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! An implementation of `TrustedWalletInterface` using the Spark SDK.
22
use crate::bitcoin::hex::FromHex;
3-
use crate::bitcoin::io;
3+
use crate::bitcoin::{Network, io};
44
use crate::logging::Logger;
55
use crate::store::{PaymentId, TxMetadataStore, TxStatus};
66
use crate::trusted_wallet::{Payment, TrustedError, TrustedWalletInterface};
@@ -32,17 +32,53 @@ use std::time::Duration;
3232
use tokio::runtime::Runtime;
3333
use uuid::Uuid;
3434

35+
/// Configuration options for the Spark wallet.
36+
#[derive(Debug, Copy, Clone)]
37+
pub struct SparkWalletConfig {
38+
/// How often to sync the wallet with the blockchain, in seconds.
39+
/// Default is 60 seconds.
40+
pub sync_interval_secs: u32,
41+
/// When this is set to `true` we will prefer to use spark payments over
42+
/// lightning when sending and receiving. This has the benefit of lower fees
43+
/// but is at the cost of privacy.
44+
pub prefer_spark_over_lightning: bool,
45+
}
46+
47+
impl Default for SparkWalletConfig {
48+
fn default() -> Self {
49+
SparkWalletConfig { sync_interval_secs: 60, prefer_spark_over_lightning: false }
50+
}
51+
}
52+
53+
/// Breez API key for using the Spark SDK. We aren't using any of their services
54+
/// but the SDK requires a valid API key to function.
55+
const BREEZ_API_KEY: &str = "MIIBajCCARygAwIBAgIHPnfOjAhBgzAFBgMrZXAwEDEOMAwGA1UEAxMFQnJlZXowHhcNMjUwOTE5MjEzNTU1WhcNMzUwOTE3MjEzNTU1WjAqMRMwEQYDVQQKEwpvcmFuZ2Utc2RrMRMwEQYDVQQDEwpvcmFuZ2Utc2RrMCowBQYDK2VwAyEA0IP1y98gPByiIMoph1P0G6cctLb864rNXw1LRLOpXXejezB5MA4GA1UdDwEB/wQEAwIFoDAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBTaOaPuXmtLDTJVv++VYBiQr9gHCTAfBgNVHSMEGDAWgBTeqtaSVvON53SSFvxMtiCyayiYazAZBgNVHREEEjAQgQ5iZW5Ac3BpcmFsLnh5ejAFBgMrZXADQQCry+1LkA3nrYa1sovS5iFI1Tkpmr/R0nM/4gJtsO93vFOkm3vBEGwjKAV7lrGzFcFbbuyM1wEJPi4Po1XCEG0D";
56+
57+
impl SparkWalletConfig {
58+
fn to_breez_config(self, network: Network) -> Result<breez_sdk_spark::Config, TrustedError> {
59+
let network = match network {
60+
Network::Bitcoin => breez_sdk_spark::Network::Mainnet,
61+
Network::Regtest => breez_sdk_spark::Network::Regtest,
62+
_ => return Err(TrustedError::InvalidNetwork),
63+
};
64+
65+
Ok(breez_sdk_spark::Config {
66+
network,
67+
sync_interval_secs: self.sync_interval_secs,
68+
prefer_spark_over_lightning: self.prefer_spark_over_lightning,
69+
api_key: Some(BREEZ_API_KEY.to_string()),
70+
max_deposit_claim_fee: None,
71+
lnurl_domain: None,
72+
})
73+
}
74+
}
75+
3576
/// A wallet implementation using the Breez Spark SDK.
3677
#[derive(Clone)]
3778
pub(crate) struct Spark {
3879
spark_wallet: Arc<BreezSdk>,
39-
store: Arc<dyn KVStore + Send + Sync>,
40-
event_queue: Arc<EventQueue>,
41-
tx_metadata: TxMetadataStore,
4280
shutdown_sender: watch::Sender<()>,
43-
shutdown_receiver: watch::Receiver<()>,
4481
logger: Arc<Logger>,
45-
runtime: Arc<Runtime>,
4682
}
4783

4884
impl TrustedWalletInterface for Spark {
@@ -199,15 +235,11 @@ impl TrustedWalletInterface for Spark {
199235
impl Spark {
200236
/// Initialize a new Spark wallet instance with the given configuration.
201237
pub(crate) async fn init(
202-
config: &WalletConfig, spark_config: breez_sdk_spark::Config,
238+
config: &WalletConfig, spark_config: SparkWalletConfig,
203239
store: Arc<dyn KVStore + Sync + Send>, event_queue: Arc<EventQueue>,
204240
tx_metadata: TxMetadataStore, logger: Arc<Logger>, runtime: Arc<Runtime>,
205241
) -> Result<Self, InitFailure> {
206-
match (config.network, spark_config.network) {
207-
(crate::bitcoin::Network::Bitcoin, breez_sdk_spark::Network::Mainnet) => {},
208-
(crate::bitcoin::Network::Regtest, breez_sdk_spark::Network::Regtest) => {},
209-
_ => Err(TrustedError::InvalidNetwork)?,
210-
}
242+
let spark_config: breez_sdk_spark::Config = spark_config.to_breez_config(config.network)?;
211243

212244
let (mnemonic, passphrase) = match &config.seed {
213245
Seed::Seed64(bytes) => {
@@ -217,7 +249,7 @@ impl Spark {
217249
Seed::Mnemonic { mnemonic, passphrase } => (mnemonic.to_string(), passphrase.clone()),
218250
};
219251

220-
let spark_store = SparkStore(Arc::clone(&store));
252+
let spark_store = SparkStore(store);
221253
let builder = SdkBuilder::new(spark_config, mnemonic, passphrase, Arc::new(spark_store));
222254

223255
let spark_wallet = Arc::new(builder.build().await.map_err(|e| {
@@ -230,6 +262,7 @@ impl Spark {
230262
let (shutdown_sender, shutdown_receiver) = watch::channel::<()>(());
231263
let listener = SparkEventHandler {
232264
event_queue: Arc::clone(&event_queue),
265+
tx_metadata,
233266
logger: Arc::clone(&logger),
234267
};
235268

@@ -244,21 +277,14 @@ impl Spark {
244277

245278
log_info!(logger, "Spark wallet initialized");
246279

247-
Ok(Spark {
248-
spark_wallet,
249-
store,
250-
event_queue,
251-
tx_metadata,
252-
shutdown_sender,
253-
shutdown_receiver,
254-
logger,
255-
runtime,
256-
})
280+
Ok(Spark { spark_wallet, shutdown_sender, logger })
257281
}
258282
}
259283

260284
struct SparkEventHandler {
261285
event_queue: Arc<EventQueue>,
286+
#[allow(unused)] // will be used in future events
287+
tx_metadata: TxMetadataStore,
262288
logger: Arc<Logger>,
263289
}
264290

@@ -370,7 +396,7 @@ fn parse_payment_id(id: &str) -> Result<[u8; 32], TrustedError> {
370396
.map_err(|_| TrustedError::Other(format!("Failed to parse payment id: {id}")))?
371397
} else {
372398
// if it's not in the expected format, try to parse the whole thing as a uuid
373-
Uuid::from_str(&id)
399+
Uuid::from_str(id)
374400
.map_err(|_| TrustedError::Other(format!("Failed to parse payment id: {id}")))?
375401
};
376402
Ok(convert_from_uuid_id(uuid.into_bytes()))

0 commit comments

Comments
 (0)