Skip to content

Commit cc31c7e

Browse files
Merge pull request #1167 from getlipa/feature/use-config-for-all-parameters
Use Config for all parameters
2 parents baf4b5b + dbd4c2a commit cc31c7e

File tree

14 files changed

+475
-202
lines changed

14 files changed

+475
-202
lines changed

.cargo/config.toml.breez.sample

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[env]
22
GITHUB_REF = "local_build"
33

4-
BACKEND_URL_LOCAL = ""
5-
BACKEND_URL_DEV = ""
6-
BACKEND_URL_STAGE = ""
7-
BACKEND_URL_PROD = ""
4+
BACKEND_COMPLETE_URL_LOCAL = ""
5+
BACKEND_COMPLETE_URL_DEV = ""
6+
BACKEND_COMPLETE_URL_STAGE = ""
7+
BACKEND_COMPLETE_URL_PROD = ""
88

99
NOTIFICATION_WEBHOOK_URL_LOCAL = ""
1010
NOTIFICATION_WEBHOOK_URL_DEV = ""

src/environment.rs renamed to examples/node/environment.rs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
use crate::errors::Result;
2-
use breez_sdk_core::{EnvironmentType, Network};
3-
use hex::FromHex;
4-
use perro::MapToError;
5-
6-
/// A code of the environment for the node to run.
71
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
82
pub enum EnvironmentCode {
93
Local,
@@ -12,81 +6,63 @@ pub enum EnvironmentCode {
126
Prod,
137
}
148

15-
// TODO remove dead code after breez sdk implementation and fix implementation (e.g. currently it selects mainnet when Local or Dev codes are provided)
16-
#[allow(dead_code)]
179
pub(crate) struct Environment {
18-
pub network: Network,
19-
pub environment_type: EnvironmentType,
2010
pub backend_url: String,
21-
pub backend_health_url: String,
2211
pub pocket_url: String,
2312
pub notification_webhook_base_url: String,
24-
pub notification_webhook_secret: [u8; 32],
13+
pub notification_webhook_secret_hex: String,
2514
pub lipa_lightning_domain: String,
2615
}
2716

2817
impl Environment {
29-
pub fn load(environment: EnvironmentCode) -> Result<Self> {
30-
let base_url = get_backend_base_url(environment);
31-
let backend_url = format!("{base_url}/v1/graphql");
32-
let backend_health_url = format!("{base_url}/healthz");
18+
pub fn load(environment: EnvironmentCode) -> Self {
19+
let backend_url = get_backend_url(environment).to_string();
3320

3421
let notification_webhook_base_url =
3522
get_notification_webhook_base_url(environment).to_string();
36-
let notification_webhook_secret = get_notification_webhook_secret(environment)?;
23+
let notification_webhook_secret_hex =
24+
get_notification_webhook_secret_hex(environment).to_string();
3725
let lipa_lightning_domain = get_lipa_lightning_domain(environment).to_string();
3826

39-
Ok(match environment {
27+
match environment {
4028
EnvironmentCode::Local => Self {
41-
network: Network::Bitcoin,
42-
environment_type: EnvironmentType::Production,
4329
backend_url,
44-
backend_health_url,
4530
pocket_url: env!("POCKET_URL_LOCAL").to_string(),
4631
notification_webhook_base_url,
47-
notification_webhook_secret,
32+
notification_webhook_secret_hex,
4833
lipa_lightning_domain,
4934
},
5035
EnvironmentCode::Dev => Self {
51-
network: Network::Bitcoin,
52-
environment_type: EnvironmentType::Production,
5336
backend_url,
54-
backend_health_url,
5537
pocket_url: env!("POCKET_URL_DEV").to_string(),
5638
notification_webhook_base_url,
57-
notification_webhook_secret,
39+
notification_webhook_secret_hex,
5840
lipa_lightning_domain,
5941
},
6042
EnvironmentCode::Stage => Self {
61-
network: Network::Bitcoin,
62-
environment_type: EnvironmentType::Production,
6343
backend_url,
64-
backend_health_url,
6544
pocket_url: env!("POCKET_URL_STAGE").to_string(),
6645
notification_webhook_base_url,
67-
notification_webhook_secret,
46+
notification_webhook_secret_hex,
6847
lipa_lightning_domain,
6948
},
7049
EnvironmentCode::Prod => Self {
71-
network: Network::Bitcoin,
72-
environment_type: EnvironmentType::Production,
7350
backend_url,
74-
backend_health_url,
7551
pocket_url: env!("POCKET_URL_PROD").to_string(),
7652
notification_webhook_base_url,
77-
notification_webhook_secret,
53+
notification_webhook_secret_hex,
7854
lipa_lightning_domain,
7955
},
80-
})
56+
}
8157
}
8258
}
8359

84-
fn get_backend_base_url(environment: EnvironmentCode) -> &'static str {
60+
fn get_backend_url(environment: EnvironmentCode) -> &'static str {
8561
match environment {
86-
EnvironmentCode::Local => env!("BACKEND_URL_LOCAL"),
87-
EnvironmentCode::Dev => env!("BACKEND_URL_DEV"),
88-
EnvironmentCode::Stage => env!("BACKEND_URL_STAGE"),
89-
EnvironmentCode::Prod => env!("BACKEND_URL_PROD"),
62+
EnvironmentCode::Local => env!("BACKEND_COMPLETE_URL_LOCAL"),
63+
EnvironmentCode::Dev => env!("BACKEND_COMPLETE_URL_DEV"),
64+
EnvironmentCode::Stage => env!("BACKEND_COMPLETE_URL_STAGE"),
65+
EnvironmentCode::Prod => env!("BACKEND_COMPLETE_URL_PROD"),
9066
}
9167
}
9268

@@ -99,16 +75,13 @@ fn get_notification_webhook_base_url(environment_code: EnvironmentCode) -> &'sta
9975
}
10076
}
10177

102-
fn get_notification_webhook_secret(environment_code: EnvironmentCode) -> Result<[u8; 32]> {
103-
let secret_hex = match environment_code {
78+
fn get_notification_webhook_secret_hex(environment_code: EnvironmentCode) -> &'static str {
79+
match environment_code {
10480
EnvironmentCode::Local => env!("NOTIFICATION_WEBHOOK_SECRET_LOCAL"),
10581
EnvironmentCode::Dev => env!("NOTIFICATION_WEBHOOK_SECRET_DEV"),
10682
EnvironmentCode::Stage => env!("NOTIFICATION_WEBHOOK_SECRET_STAGE"),
10783
EnvironmentCode::Prod => env!("NOTIFICATION_WEBHOOK_SECRET_PROD"),
108-
};
109-
110-
<[u8; 32]>::from_hex(secret_hex)
111-
.map_to_permanent_failure("Failed to parse embedded notification webhook secret")
84+
}
11285
}
11386

11487
fn get_lipa_lightning_domain(environment_code: EnvironmentCode) -> &'static str {

examples/node/main.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
mod cli;
2+
mod environment;
23
mod hinter;
34
mod overview;
45
#[path = "../../tests/print_events_handler/mod.rs"]
56
mod print_events_handler;
67

78
use crate::print_events_handler::PrintEventsHandler;
89

9-
use uniffi_lipalightninglib::{mnemonic_to_secret, recover_lightning_node, LightningNode};
10-
use uniffi_lipalightninglib::{Config, EnvironmentCode, TzConfig};
10+
use uniffi_lipalightninglib::{
11+
mnemonic_to_secret, recover_lightning_node, BreezSdkConfig, LightningNode, MaxRoutingFeeConfig,
12+
ReceiveLimitsConfig, RemoteServicesConfig,
13+
};
14+
use uniffi_lipalightninglib::{Config, TzConfig};
1115

16+
use crate::environment::{Environment, EnvironmentCode};
1217
use log::Level;
1318
use std::path::Path;
1419
use std::thread::sleep;
@@ -22,7 +27,8 @@ fn main() {
2227
let environment = env::args().nth(1).unwrap_or("local".to_string());
2328
let base_dir = format!("{BASE_DIR}_{environment}");
2429

25-
let environment = map_environment_code(&environment);
30+
let environment_code = map_environment_code(&environment);
31+
let environment = Environment::load(environment_code);
2632

2733
// Create dir for node data persistence.
2834
fs::create_dir_all(&base_dir).unwrap();
@@ -36,7 +42,7 @@ fn main() {
3642
.is_ok_and(|mut d| d.next().is_none())
3743
{
3844
recover_lightning_node(
39-
environment,
45+
environment.backend_url.clone(),
4046
seed.clone(),
4147
base_dir.clone(),
4248
Some(Level::Debug),
@@ -45,7 +51,6 @@ fn main() {
4551
}
4652

4753
let config = Config {
48-
environment,
4954
seed,
5055
fiat_currency: "EUR".to_string(),
5156
local_persistence_path: base_dir.clone(),
@@ -57,6 +62,26 @@ fn main() {
5762
phone_number_allowed_countries_iso_3166_1_alpha_2: ["AT", "CH", "DE"]
5863
.map(String::from)
5964
.to_vec(),
65+
remote_services_config: RemoteServicesConfig {
66+
backend_url: environment.backend_url.clone(),
67+
pocket_url: environment.pocket_url.clone(),
68+
notification_webhook_base_url: environment.notification_webhook_base_url.clone(),
69+
notification_webhook_secret_hex: environment.notification_webhook_secret_hex.clone(),
70+
lipa_lightning_domain: environment.lipa_lightning_domain,
71+
},
72+
breez_sdk_config: BreezSdkConfig {
73+
breez_sdk_api_key: env!("BREEZ_SDK_API_KEY").to_string(),
74+
breez_sdk_partner_certificate: env!("BREEZ_SDK_PARTNER_CERTIFICATE").to_string(),
75+
breez_sdk_partner_key: env!("BREEZ_SDK_PARTNER_KEY").to_string(),
76+
},
77+
max_routing_fee_config: MaxRoutingFeeConfig {
78+
max_routing_fee_permyriad: 150,
79+
max_routing_fee_exempt_fee_sats: 21,
80+
},
81+
receive_limits_config: ReceiveLimitsConfig {
82+
max_receive_amount_sat: 1_000_000,
83+
min_receive_channel_open_fee_multiplier: 2.0,
84+
},
6085
};
6186

6287
let node = LightningNode::new(config, events).unwrap();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2+
pub enum EnvironmentCode {
3+
Local,
4+
Dev,
5+
Stage,
6+
Prod,
7+
}
8+
9+
pub(crate) struct Environment {
10+
pub backend_url: String,
11+
pub pocket_url: String,
12+
pub notification_webhook_base_url: String,
13+
pub notification_webhook_secret_hex: String,
14+
pub lipa_lightning_domain: String,
15+
}
16+
17+
impl Environment {
18+
pub fn load(environment: EnvironmentCode) -> Self {
19+
let backend_url = get_backend_url(environment).to_string();
20+
21+
let notification_webhook_base_url =
22+
get_notification_webhook_base_url(environment).to_string();
23+
let notification_webhook_secret_hex =
24+
get_notification_webhook_secret_hex(environment).to_string();
25+
let lipa_lightning_domain = get_lipa_lightning_domain(environment).to_string();
26+
27+
match environment {
28+
EnvironmentCode::Local => Self {
29+
backend_url,
30+
pocket_url: env!("POCKET_URL_LOCAL").to_string(),
31+
notification_webhook_base_url,
32+
notification_webhook_secret_hex,
33+
lipa_lightning_domain,
34+
},
35+
EnvironmentCode::Dev => Self {
36+
backend_url,
37+
pocket_url: env!("POCKET_URL_DEV").to_string(),
38+
notification_webhook_base_url,
39+
notification_webhook_secret_hex,
40+
lipa_lightning_domain,
41+
},
42+
EnvironmentCode::Stage => Self {
43+
backend_url,
44+
pocket_url: env!("POCKET_URL_STAGE").to_string(),
45+
notification_webhook_base_url,
46+
notification_webhook_secret_hex,
47+
lipa_lightning_domain,
48+
},
49+
EnvironmentCode::Prod => Self {
50+
backend_url,
51+
pocket_url: env!("POCKET_URL_PROD").to_string(),
52+
notification_webhook_base_url,
53+
notification_webhook_secret_hex,
54+
lipa_lightning_domain,
55+
},
56+
}
57+
}
58+
}
59+
60+
fn get_backend_url(environment: EnvironmentCode) -> &'static str {
61+
match environment {
62+
EnvironmentCode::Local => env!("BACKEND_COMPLETE_URL_LOCAL"),
63+
EnvironmentCode::Dev => env!("BACKEND_COMPLETE_URL_DEV"),
64+
EnvironmentCode::Stage => env!("BACKEND_COMPLETE_URL_STAGE"),
65+
EnvironmentCode::Prod => env!("BACKEND_COMPLETE_URL_PROD"),
66+
}
67+
}
68+
69+
fn get_notification_webhook_base_url(environment_code: EnvironmentCode) -> &'static str {
70+
match environment_code {
71+
EnvironmentCode::Local => env!("NOTIFICATION_WEBHOOK_URL_LOCAL"),
72+
EnvironmentCode::Dev => env!("NOTIFICATION_WEBHOOK_URL_DEV"),
73+
EnvironmentCode::Stage => env!("NOTIFICATION_WEBHOOK_URL_STAGE"),
74+
EnvironmentCode::Prod => env!("NOTIFICATION_WEBHOOK_URL_PROD"),
75+
}
76+
}
77+
78+
fn get_notification_webhook_secret_hex(environment_code: EnvironmentCode) -> &'static str {
79+
match environment_code {
80+
EnvironmentCode::Local => env!("NOTIFICATION_WEBHOOK_SECRET_LOCAL"),
81+
EnvironmentCode::Dev => env!("NOTIFICATION_WEBHOOK_SECRET_DEV"),
82+
EnvironmentCode::Stage => env!("NOTIFICATION_WEBHOOK_SECRET_STAGE"),
83+
EnvironmentCode::Prod => env!("NOTIFICATION_WEBHOOK_SECRET_PROD"),
84+
}
85+
}
86+
87+
fn get_lipa_lightning_domain(environment_code: EnvironmentCode) -> &'static str {
88+
match environment_code {
89+
EnvironmentCode::Local => env!("LIPA_LIGHTNING_DOMAIN_LOCAL"),
90+
EnvironmentCode::Dev => env!("LIPA_LIGHTNING_DOMAIN_DEV"),
91+
EnvironmentCode::Stage => env!("LIPA_LIGHTNING_DOMAIN_STAGE"),
92+
EnvironmentCode::Prod => env!("LIPA_LIGHTNING_DOMAIN_PROD"),
93+
}
94+
}

examples/notification_handler/main.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
mod environment;
12
mod hinter;
23

4+
use crate::environment::{Environment, EnvironmentCode};
35
use crate::hinter::{CommandHint, CommandHinter};
46
use anyhow::{anyhow, Result};
57
use colored::Colorize;
@@ -13,7 +15,8 @@ use std::collections::HashSet;
1315
use std::env;
1416
use std::time::Duration;
1517
use uniffi_lipalightninglib::{
16-
handle_notification, mnemonic_to_secret, Config, EnvironmentCode, NotificationToggles, TzConfig,
18+
handle_notification, mnemonic_to_secret, BreezSdkConfig, Config, MaxRoutingFeeConfig,
19+
NotificationToggles, ReceiveLimitsConfig, RemoteServicesConfig, TzConfig,
1720
};
1821

1922
static BASE_DIR: &str = ".3l_node";
@@ -112,12 +115,12 @@ fn map_environment_code(code: &str) -> EnvironmentCode {
112115
fn get_config() -> Config {
113116
let base_dir = format!("{BASE_DIR}_{}", ENVIRONMENT.as_str());
114117

115-
let environment = map_environment_code(ENVIRONMENT.as_str());
118+
let environment_code = map_environment_code(ENVIRONMENT.as_str());
119+
let environment = Environment::load(environment_code);
116120

117121
let seed = read_seed_from_env();
118122

119123
Config {
120-
environment,
121124
seed,
122125
fiat_currency: "EUR".to_string(),
123126
local_persistence_path: base_dir.clone(),
@@ -131,6 +134,26 @@ fn get_config() -> Config {
131134
"CH".to_string(),
132135
"DE".to_string(),
133136
],
137+
remote_services_config: RemoteServicesConfig {
138+
backend_url: environment.backend_url.clone(),
139+
pocket_url: environment.pocket_url.clone(),
140+
notification_webhook_base_url: environment.notification_webhook_base_url.clone(),
141+
notification_webhook_secret_hex: environment.notification_webhook_secret_hex.clone(),
142+
lipa_lightning_domain: environment.lipa_lightning_domain,
143+
},
144+
breez_sdk_config: BreezSdkConfig {
145+
breez_sdk_api_key: env!("BREEZ_SDK_API_KEY").to_string(),
146+
breez_sdk_partner_certificate: env!("BREEZ_SDK_PARTNER_CERTIFICATE").to_string(),
147+
breez_sdk_partner_key: env!("BREEZ_SDK_PARTNER_KEY").to_string(),
148+
},
149+
max_routing_fee_config: MaxRoutingFeeConfig {
150+
max_routing_fee_permyriad: 150,
151+
max_routing_fee_exempt_fee_sats: 21,
152+
},
153+
receive_limits_config: ReceiveLimitsConfig {
154+
max_receive_amount_sat: 1_000_000,
155+
min_receive_channel_open_fee_multiplier: 2.0,
156+
},
134157
}
135158
}
136159

0 commit comments

Comments
 (0)