Skip to content

Commit

Permalink
fix: remove mandatory API key checks (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Jan 21, 2025
1 parent 4abcebc commit 9599801
Show file tree
Hide file tree
Showing 28 changed files with 119 additions and 111 deletions.
7 changes: 2 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ async fn main() -> Result<()> {
let mut config = LiquidSdk::default_config(network, breez_api_key)?;
config.working_dir = data_dir_str;
config.cache_dir = args.cache_dir;
if let Some(sync_service_url) = std::env::var_os("SYNC_SERVICE_URL") {
config.sync_service_url = sync_service_url
.into_string()
.expect("Expected valid sync service url");
}
config.sync_service_url = std::env::var_os("SYNC_SERVICE_URL")
.map(|var| var.into_string().expect("Expected valid sync service url"));
let sdk = LiquidSdk::connect(ConnectRequest {
mnemonic: mnemonic.to_string(),
config,
Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ dictionary Config {
LiquidNetwork network;
u64 payment_timeout_sec;
u32 zero_conf_min_fee_rate_msat;
string sync_service_url;
string? sync_service_url;
string? breez_api_key;
string? cache_dir;
u64? zero_conf_max_amount_sat;
Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/tests/bindings/csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try
{
var mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
var config = BreezSdkLiquidMethods.DefaultConfig(LiquidNetwork.Testnet, null);
var config = BreezSdkLiquidMethods.DefaultConfig(LiquidNetwork.Testnet, null) with { syncServiceUrl = null };

var connectReq = new ConnectRequest(config, mnemonic);
BindingLiquidSdk sdk = BreezSdkLiquidMethods.Connect(connectReq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func main() {
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
config, err := breez_sdk_liquid.DefaultConfig(breez_sdk_liquid.LiquidNetworkTestnet, nil)
config.SyncServiceUrl = nil

if err != nil {
log.Fatalf("Config creation failed: %#v", err)
Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/tests/bindings/test_breez_liquid_sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try
{
var mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
var config = BreezSdkLiquidMethods.DefaultConfig(LiquidNetwork.Testnet, null);
var config = BreezSdkLiquidMethods.DefaultConfig(LiquidNetwork.Testnet, null) with { syncServiceUrl = null };

var connectReq = new ConnectRequest(config, mnemonic);
BindingLiquidSdk sdk = BreezSdkLiquidMethods.Connect(connectReq);
Expand Down
1 change: 1 addition & 0 deletions lib/bindings/tests/bindings/test_breez_sdk_liquid.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SDKListener: breez_sdk_liquid.EventListener {
try {
var mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
var config = breez_sdk_liquid.defaultConfig(breez_sdk_liquid.LiquidNetwork.TESTNET, null)
config.syncServiceUrl = null
var connectRequest = breez_sdk_liquid.ConnectRequest(config, mnemonic)
var sdk = breez_sdk_liquid.connect(connectRequest)

Expand Down
1 change: 1 addition & 0 deletions lib/bindings/tests/bindings/test_breez_sdk_liquid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def on_event(self, event):
def test():
mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
config = breez_sdk_liquid.default_config(breez_sdk_liquid.LiquidNetwork.TESTNET, None)
config.sync_service_url = None
connect_request = breez_sdk_liquid.ConnectRequest(config=config, mnemonic=mnemonic)
sdk = breez_sdk_liquid.connect(connect_request)

Expand Down
3 changes: 2 additions & 1 deletion lib/bindings/tests/bindings/test_breez_sdk_liquid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class SDKListener: EventListener {
}

let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
let config = try breez_sdk_liquid.defaultConfig(network: .testnet, breezApiKey: nil);
var config = try breez_sdk_liquid.defaultConfig(network: .testnet, breezApiKey: nil);
config.syncServiceUrl = nil
let connectRequest = breez_sdk_liquid.ConnectRequest(config: config, mnemonic: mnemonic);
let sdk = try breez_sdk_liquid.connect(req: connectRequest);

Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,7 @@ impl SseDecode for crate::model::Config {
let mut var_network = <crate::model::LiquidNetwork>::sse_decode(deserializer);
let mut var_paymentTimeoutSec = <u64>::sse_decode(deserializer);
let mut var_zeroConfMinFeeRateMsat = <u32>::sse_decode(deserializer);
let mut var_syncServiceUrl = <String>::sse_decode(deserializer);
let mut var_syncServiceUrl = <Option<String>>::sse_decode(deserializer);
let mut var_zeroConfMaxAmountSat = <Option<u64>>::sse_decode(deserializer);
let mut var_breezApiKey = <Option<String>>::sse_decode(deserializer);
let mut var_externalInputParsers =
Expand Down Expand Up @@ -7082,7 +7082,7 @@ impl SseEncode for crate::model::Config {
<crate::model::LiquidNetwork>::sse_encode(self.network, serializer);
<u64>::sse_encode(self.payment_timeout_sec, serializer);
<u32>::sse_encode(self.zero_conf_min_fee_rate_msat, serializer);
<String>::sse_encode(self.sync_service_url, serializer);
<Option<String>>::sse_encode(self.sync_service_url, serializer);
<Option<u64>>::sse_encode(self.zero_conf_max_amount_sat, serializer);
<Option<String>>::sse_encode(self.breez_api_key, serializer);
<Option<Vec<crate::bindings::ExternalInputParser>>>::sse_encode(
Expand Down
15 changes: 8 additions & 7 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::utils;
// Uses f64 for the maximum precision when converting between units
pub const LIQUID_FEE_RATE_SAT_PER_VBYTE: f64 = 0.1;
pub const LIQUID_FEE_RATE_MSAT_PER_VBYTE: f32 = (LIQUID_FEE_RATE_SAT_PER_VBYTE * 1000.0) as f32;
const BREEZ_SYNC_SERVICE_URL: &str = "https://datasync.breez.technology";
pub const BREEZ_SYNC_SERVICE_URL: &str = "https://datasync.breez.technology";

/// Configuration for the Liquid SDK
#[derive(Clone, Debug, Serialize)]
Expand All @@ -49,8 +49,9 @@ pub struct Config {
pub payment_timeout_sec: u64,
/// Zero-conf minimum accepted fee-rate in millisatoshis per vbyte
pub zero_conf_min_fee_rate_msat: u32,
/// The url of the real-time sync service
pub sync_service_url: String,
/// The url of the real-time sync service. Defaults to [BREEZ_SYNC_SERVICE_URL]
/// Setting this field to `None` will disable the service
pub sync_service_url: Option<String>,
/// Maximum amount in satoshi to accept zero-conf payments with
/// Defaults to [DEFAULT_ZERO_CONF_MAX_SAT]
pub zero_conf_max_amount_sat: Option<u64>,
Expand All @@ -74,7 +75,7 @@ pub struct Config {
}

impl Config {
pub fn mainnet(breez_api_key: String) -> Self {
pub fn mainnet(breez_api_key: Option<String>) -> Self {
Config {
liquid_electrum_url: "elements-mainnet.breez.technology:50002".to_string(),
bitcoin_electrum_url: "bitcoin-mainnet.blockstream.info:50002".to_string(),
Expand All @@ -84,9 +85,9 @@ impl Config {
network: LiquidNetwork::Mainnet,
payment_timeout_sec: 15,
zero_conf_min_fee_rate_msat: DEFAULT_ZERO_CONF_MIN_FEE_RATE,
sync_service_url: BREEZ_SYNC_SERVICE_URL.to_string(),
sync_service_url: Some(BREEZ_SYNC_SERVICE_URL.to_string()),
zero_conf_max_amount_sat: None,
breez_api_key: Some(breez_api_key),
breez_api_key,
external_input_parsers: None,
use_default_external_input_parsers: true,
onchain_fee_rate_leeway_sat_per_vbyte: None,
Expand All @@ -103,7 +104,7 @@ impl Config {
network: LiquidNetwork::Testnet,
payment_timeout_sec: 15,
zero_conf_min_fee_rate_msat: DEFAULT_ZERO_CONF_MIN_FEE_RATE,
sync_service_url: BREEZ_SYNC_SERVICE_URL.to_string(),
sync_service_url: Some(BREEZ_SYNC_SERVICE_URL.to_string()),
zero_conf_max_amount_sat: None,
breez_api_key,
external_input_parsers: None,
Expand Down
5 changes: 2 additions & 3 deletions lib/core/src/persist/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Persister {
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;
self.set_last_derivation_index_inner(&tx, index)?;
tx.commit()?;
self.sync_trigger.try_send(())?;
self.trigger_sync()?;
Ok(())
}

Expand All @@ -183,8 +183,7 @@ impl Persister {
None => None,
};
tx.commit()?;
self.sync_trigger.try_send(())?;

self.trigger_sync()?;
Ok(res)
}
}
Expand Down
18 changes: 7 additions & 11 deletions lib/core/src/persist/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Persister {
true => {
self.commit_outgoing(&tx, &chain_swap.id, RecordType::Chain, updated_fields)?;
tx.commit()?;
self.sync_trigger.try_send(())?;
self.trigger_sync()?;
}
false => {
tx.commit()?;
Expand Down Expand Up @@ -305,11 +305,9 @@ impl Persister {
Some(vec!["accept_zero_conf".to_string()]),
)?;
tx.commit()?;
self.sync_trigger
.try_send(())
.map_err(|err| PaymentError::Generic {
err: format!("Could not trigger manual sync: {err:?}"),
})?;
self.trigger_sync().map_err(|err| PaymentError::Generic {
err: format!("Could not trigger manual sync: {err:?}"),
})?;

Ok(())
}
Expand Down Expand Up @@ -367,11 +365,9 @@ impl Persister {
Some(vec!["accepted_receiver_amount_sat".to_string()]),
)?;
tx.commit()?;
self.sync_trigger
.try_send(())
.map_err(|err| PaymentError::Generic {
err: format!("Could not trigger manual sync: {err:?}"),
})?;
self.trigger_sync().map_err(|err| PaymentError::Generic {
err: format!("Could not trigger manual sync: {err:?}"),
})?;

Ok(())
}
Expand Down
13 changes: 6 additions & 7 deletions lib/core/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) mod sync;

use std::collections::{HashMap, HashSet};
use std::ops::Not;
use std::sync::RwLock;
use std::{fs::create_dir_all, path::PathBuf, str::FromStr};

use crate::lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription};
Expand All @@ -33,7 +34,7 @@ const DEFAULT_DB_FILENAME: &str = "storage.sql";
pub(crate) struct Persister {
main_db_dir: PathBuf,
network: LiquidNetwork,
sync_trigger: Sender<()>,
pub(crate) sync_trigger: RwLock<Option<Sender<()>>>,
}

/// Builds a WHERE clause that checks if `state` is any of the given arguments
Expand All @@ -52,7 +53,7 @@ impl Persister {
pub fn new(
working_dir: &str,
network: LiquidNetwork,
sync_trigger: Sender<()>,
sync_trigger: Option<Sender<()>>,
) -> Result<Self> {
let main_db_dir = PathBuf::from_str(working_dir)?;
if !main_db_dir.exists() {
Expand All @@ -61,7 +62,7 @@ impl Persister {
Ok(Persister {
main_db_dir,
network,
sync_trigger,
sync_trigger: RwLock::new(sync_trigger),
})
}

Expand Down Expand Up @@ -238,9 +239,8 @@ impl Persister {
}

tx.commit()?;

if trigger_sync {
self.sync_trigger.try_send(())?;
self.trigger_sync()?;
}

Ok(())
Expand Down Expand Up @@ -299,8 +299,7 @@ impl Persister {
None,
)?;
tx.commit()?;

self.sync_trigger.try_send(())?;
self.trigger_sync()?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/persist/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Persister {
true => {
self.commit_outgoing(&tx, &receive_swap.id, RecordType::Receive, updated_fields)?;
tx.commit()?;
self.sync_trigger.try_send(())?;
self.trigger_sync()?;
}
false => {
tx.commit()?;
Expand Down
11 changes: 5 additions & 6 deletions lib/core/src/persist/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Persister {
true => {
self.commit_outgoing(&tx, &send_swap.id, RecordType::Send, updated_fields)?;
tx.commit()?;
self.sync_trigger.try_send(())?;
self.trigger_sync()?;
}
false => {
tx.commit()?;
Expand Down Expand Up @@ -298,11 +298,10 @@ impl Persister {
let updated_fields = get_updated_fields!(preimage);
self.commit_outgoing(&tx, swap_id, RecordType::Send, updated_fields)?;
tx.commit()?;
self.sync_trigger
.try_send(())
.map_err(|err| PaymentError::Generic {
err: format!("Could not trigger manual sync: {err:?}"),
})?;

self.trigger_sync().map_err(|err| PaymentError::Generic {
err: format!("Could not trigger manual sync: {err:?}"),
})?;

Ok(())
}
Expand Down
13 changes: 13 additions & 0 deletions lib/core/src/persist/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ impl Persister {
record_type: RecordType,
updated_fields: Option<Vec<String>>,
) -> Result<()> {
if self.sync_trigger.try_read().is_ok_and(|t| t.is_none()) {
return Ok(());
}

let record_id = Record::get_id_from_record_type(record_type, data_id);
let updated_fields = updated_fields
.map(|fields| {
Expand Down Expand Up @@ -493,4 +497,13 @@ impl Persister {

Ok(())
}

pub(crate) fn trigger_sync(&self) -> Result<()> {
if let Ok(lock) = self.sync_trigger.try_read() {
if let Some(trigger) = lock.clone() {
trigger.try_send(())?;
}
}
Ok(())
}
}
Loading

0 comments on commit 9599801

Please sign in to comment.