Skip to content

Commit 49c6617

Browse files
committed
f Add Config and Network bindings
1 parent f2f26db commit 49c6617

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

bindings/ldk_node.udl

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
namespace ldk_node {
22
};
33

4+
dictionary Config {
5+
string storage_dir_path;
6+
string esplora_server_url;
7+
Network network;
8+
string? listening_address;
9+
u32 default_cltv_expiry_delta;
10+
};
11+
412
interface Builder {
513
constructor();
14+
[Name=from_config]
15+
constructor(Config config);
616
Node build();
717
};
818

@@ -41,6 +51,7 @@ enum NodeError {
4151
"InvoiceInvalid",
4252
"InvoiceCreationFailed",
4353
"ChannelIdInvalid",
54+
"NetworkInvalid",
4455
"RoutingFailed",
4556
"PeerInfoParseFailed",
4657
"ChannelCreationFailed",
@@ -77,3 +88,6 @@ typedef string ChannelId;
7788

7889
[Custom]
7990
typedef string UserChannelId;
91+
92+
[Custom]
93+
typedef string Network;

src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub enum Error {
2525
InvoiceCreationFailed,
2626
/// The given channel ID is invalid.
2727
ChannelIdInvalid,
28+
/// The given network is invalid.
29+
NetworkInvalid,
2830
/// No route for the given target could be found.
2931
RoutingFailed,
3032
/// A given peer info could not be parsed.
@@ -57,6 +59,7 @@ impl fmt::Display for Error {
5759
Self::InvoiceInvalid => write!(f, "The given invoice is invalid."),
5860
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
5961
Self::ChannelIdInvalid => write!(f, "The given channel ID is invalid."),
62+
Self::NetworkInvalid => write!(f, "The given network is invalid."),
6063
Self::RoutingFailed => write!(f, "Failed to find route."),
6164
Self::PeerInfoParseFailed => write!(f, "Failed to parse the given peer information."),
6265
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),

src/io_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn read_network_graph(
4242
}
4343

4444
let genesis_hash =
45-
bitcoin::blockdata::constants::genesis_block(config.network).header.block_hash();
45+
bitcoin::blockdata::constants::genesis_block(config.network.0).header.block_hash();
4646
Ok(NetworkGraph::new(genesis_hash, logger))
4747
}
4848

src/lib.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use event::Event;
4040
use event::{EventHandler, EventQueue};
4141
use peer_store::{PeerInfo, PeerInfoStorage};
4242
use types::{
43-
ChainMonitor, ChannelManager, GossipSync, InvoicePayer, KeysManager, NetworkGraph,
43+
ChainMonitor, ChannelManager, GossipSync, InvoicePayer, KeysManager, Network, NetworkGraph,
4444
OnionMessenger, PaymentInfoStorage, PeerManager, Router, Scorer,
4545
};
4646
pub use types::{ChannelId, PaymentInfo, PaymentStatus, UserChannelId};
@@ -85,6 +85,7 @@ use std::convert::{TryFrom, TryInto};
8585
use std::default::Default;
8686
use std::fs;
8787
use std::net::SocketAddr;
88+
use std::str::FromStr;
8889
use std::sync::atomic::{AtomicBool, Ordering};
8990
use std::sync::{Arc, Mutex, RwLock};
9091
use std::time::{Duration, Instant, SystemTime};
@@ -109,7 +110,7 @@ pub struct Config {
109110
/// The URL of the utilized Esplora server.
110111
pub esplora_server_url: String,
111112
/// The used Bitcoin network.
112-
pub network: bitcoin::Network,
113+
pub network: Network,
113114
/// The IP address and TCP port the node will listen on.
114115
pub listening_address: Option<String>,
115116
/// The default CLTV expiry delta to be used for payments.
@@ -121,7 +122,7 @@ impl Default for Config {
121122
// Set the config defaults
122123
let storage_dir_path = "/tmp/ldk_node/".to_string();
123124
let esplora_server_url = "https://blockstream.info/api".to_string();
124-
let network = bitcoin::Network::Regtest;
125+
let network = Network::default();
125126
let listening_address = Some("0.0.0.0:9735".to_string());
126127
let default_cltv_expiry_delta = 144;
127128

@@ -175,16 +176,9 @@ impl Builder {
175176
///
176177
/// Options: `mainnet`/`bitcoin`, `testnet`, `regtest`, `signet`
177178
///
178-
/// Default: `testnet`
179+
/// Default: `regtest`
179180
pub fn set_network(&mut self, network: &str) -> &mut Self {
180-
self.config.network = match network {
181-
"mainnet" => bitcoin::Network::Bitcoin,
182-
"bitcoin" => bitcoin::Network::Bitcoin,
183-
"testnet" => bitcoin::Network::Testnet,
184-
"regtest" => bitcoin::Network::Regtest,
185-
"signet" => bitcoin::Network::Signet,
186-
_ => bitcoin::Network::Regtest,
187-
};
181+
self.config.network = Network::from_str(network).unwrap_or(Network::default());
188182
self
189183
}
190184

@@ -213,13 +207,13 @@ impl Builder {
213207

214208
// Step 1: Initialize the on-chain wallet and chain access
215209
let seed = io_utils::read_or_generate_seed_file(Arc::clone(&config));
216-
let xprv = bitcoin::util::bip32::ExtendedPrivKey::new_master(config.network, &seed)
210+
let xprv = bitcoin::util::bip32::ExtendedPrivKey::new_master(config.network.0, &seed)
217211
.expect("Failed to read wallet master key");
218212

219213
let wallet_name = bdk::wallet::wallet_name_from_descriptor(
220214
Bip84(xprv, bdk::KeychainKind::External),
221215
Some(Bip84(xprv, bdk::KeychainKind::Internal)),
222-
config.network,
216+
config.network.0,
223217
&Secp256k1::new(),
224218
)
225219
.expect("Failed to derive on-chain wallet name");
@@ -229,7 +223,7 @@ impl Builder {
229223
let bdk_wallet = bdk::Wallet::new(
230224
Bip84(xprv, bdk::KeychainKind::External),
231225
Some(Bip84(xprv, bdk::KeychainKind::Internal)),
232-
config.network,
226+
config.network.0,
233227
database,
234228
)
235229
.expect("Failed to setup on-chain wallet");
@@ -318,12 +312,13 @@ impl Builder {
318312
channel_manager
319313
} else {
320314
// We're starting a fresh node.
321-
let dummy_block_hash = bitcoin::blockdata::constants::genesis_block(config.network)
322-
.header
323-
.block_hash();
315+
let dummy_block_hash =
316+
bitcoin::blockdata::constants::genesis_block(config.network.0)
317+
.header
318+
.block_hash();
324319

325320
let chain_params = ChainParameters {
326-
network: config.network,
321+
network: config.network.0,
327322
best_block: BestBlock::new(dummy_block_hash, 0),
328323
};
329324
channelmanager::ChannelManager::new(
@@ -937,7 +932,7 @@ impl Node {
937932
) -> Result<Invoice, Error> {
938933
let mut inbound_payments_lock = self.inbound_payments.lock().unwrap();
939934

940-
let currency = match self.config.network {
935+
let currency = match self.config.network.0 {
941936
bitcoin::Network::Bitcoin => Currency::Bitcoin,
942937
bitcoin::Network::Testnet => Currency::BitcoinTestnet,
943938
bitcoin::Network::Regtest => Currency::Regtest,

src/types.rs

+44
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use bitcoin::secp256k1::PublicKey;
2323
use bitcoin::Address;
2424

2525
use std::collections::HashMap;
26+
use std::default::Default;
27+
use std::fmt;
2628
use std::str::FromStr;
2729
use std::sync::{Arc, Mutex};
2830

@@ -207,3 +209,45 @@ impl UniffiCustomTypeConverter for UserChannelId {
207209
obj.0.to_string()
208210
}
209211
}
212+
213+
#[derive(PartialEq, Eq, Debug, Clone)]
214+
pub struct Network(pub bitcoin::Network);
215+
216+
impl Default for Network {
217+
fn default() -> Self {
218+
Self(bitcoin::Network::Regtest)
219+
}
220+
}
221+
222+
impl FromStr for Network {
223+
type Err = ();
224+
225+
fn from_str(s: &str) -> Result<Self, Self::Err> {
226+
match s {
227+
"mainnet" => Ok(Self(bitcoin::Network::Bitcoin)),
228+
"bitcoin" => Ok(Self(bitcoin::Network::Bitcoin)),
229+
"testnet" => Ok(Self(bitcoin::Network::Testnet)),
230+
"regtest" => Ok(Self(bitcoin::Network::Regtest)),
231+
"signet" => Ok(Self(bitcoin::Network::Signet)),
232+
_ => Err(()),
233+
}
234+
}
235+
}
236+
237+
impl fmt::Display for Network {
238+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
239+
write!(f, "{}", self.0)
240+
}
241+
}
242+
243+
impl UniffiCustomTypeConverter for Network {
244+
type Builtin = String;
245+
246+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
247+
Ok(Network::from_str(&val).map_err(|_| Error::NetworkInvalid)?)
248+
}
249+
250+
fn from_custom(obj: Self) -> Self::Builtin {
251+
obj.0.to_string()
252+
}
253+
}

0 commit comments

Comments
 (0)