bark/config.rs
1
2use std::fmt;
3use std::path::{Path, PathBuf};
4
5use anyhow::Context;
6use bitcoin::{FeeRate, Network};
7
8use bitcoin_ext::{BlockDelta, BlockHeight};
9
10use crate::chain::ChainSourceSpec;
11
12
13/// Networks bark can be used on
14#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub enum BarkNetwork {
16 /// Bitcoin's mainnet
17 Mainnet,
18 /// The official Bitcoin Core signet
19 Signet,
20 /// Mutinynet
21 Mutinynet,
22 /// Any regtest network
23 Regtest,
24}
25
26impl BarkNetwork {
27 /// Map to the [Network] types
28 pub fn as_bitcoin(&self) -> Network {
29 match self {
30 Self::Mainnet => Network::Bitcoin,
31 Self::Signet => Network::Signet,
32 Self::Mutinynet => Network::Signet,
33 Self::Regtest => Network::Regtest,
34 }
35 }
36}
37
38impl fmt::Display for BarkNetwork {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match self {
41 Self::Mainnet => f.write_str("mainnet"),
42 Self::Signet => f.write_str("signet"),
43 Self::Mutinynet => f.write_str("mutinynet"),
44 Self::Regtest => f.write_str("regtest"),
45 }
46 }
47}
48
49impl fmt::Debug for BarkNetwork {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 fmt::Display::fmt(self, f)
52 }
53}
54
55/// Configuration of the Bark wallet.
56///
57/// - [Config::esplora_address] or [Config::bitcoind_address] must be provided.
58/// - If you use [Config::bitcoind_address], you must also provide:
59/// - [Config::bitcoind_cookiefile] or
60/// - [Config::bitcoind_user] and [Config::bitcoind_pass]
61/// - Other optional fields can be omitted.
62///
63/// # Example
64/// Configure the wallet using defaults, then override endpoints for public signet:
65///
66/// ```rust
67/// use bark::Config;
68///
69/// let cfg = Config {
70/// server_address: "https://ark.signet.2nd.dev".into(),
71/// esplora_address: Some("https://esplora.signet.2nd.dev".into()),
72/// ..Config::network_default(bitcoin::Network::Bitcoin)
73/// };
74/// // cfg now has all other fields from the default configuration
75/// ```
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct Config {
78 /// The address of your ark server.
79 pub server_address: String,
80
81 /// An access token used to access a private server
82 pub server_access_token: Option<String>,
83
84 /// The address of the Esplora HTTP REST server to use.
85 ///
86 /// Either this or the `bitcoind_address` field has to be provided.
87 pub esplora_address: Option<String>,
88
89 /// The address of the bitcoind RPC server to use.
90 ///
91 /// Either this or the `esplora_address` field has to be provided.
92 /// Either `bitcoind_cookiefile` or `bitcoind_user` and `bitcoind_pass` has to be provided.
93 pub bitcoind_address: Option<String>,
94
95 /// The path to the bitcoind rpc cookie file.
96 ///
97 /// Only used with `bitcoind_address`.
98 pub bitcoind_cookiefile: Option<PathBuf>,
99
100 /// The bitcoind RPC username.
101 ///
102 /// Only used with `bitcoind_address`.
103 pub bitcoind_user: Option<String>,
104
105 /// The bitcoind RPC password.
106 ///
107 /// Only used with `bitcoind_address`.
108 pub bitcoind_pass: Option<String>,
109
110 /// The number of blocks before expiration to refresh vtxos.
111 ///
112 /// Default value: 144 (24h) for mainnet, 12 for testnets
113 pub vtxo_refresh_expiry_threshold: BlockHeight,
114
115 /// An upper limit of the number of blocks we expect to need to
116 /// safely exit the vtxos.
117 ///
118 /// Default value: 12
119 pub vtxo_exit_margin: BlockDelta,
120
121 /// The number of blocks to claim a HTLC-recv VTXO.
122 ///
123 /// Default value: 18
124 pub htlc_recv_claim_delta: BlockDelta,
125
126 /// Maximum number of retry attempts when claiming a Lightning receive
127 /// against the server fails. After this budget is exhausted, the HTLC-recv
128 /// VTXOs will be exited on-chain.
129 ///
130 /// Default value: 5
131 pub lightning_receive_claim_retries: u8,
132
133 /// Optional SOCKS5 proxy URL for network traffic.
134 ///
135 /// The proxy is automatically bypassed for localhost addresses
136 /// (127.0.0.1, localhost, ::1), so a local bitcoind works without
137 /// extra configuration.
138 ///
139 /// Use `socks5h://` to resolve DNS through the proxy which is required for .onion addresses
140 /// and to prevent DNS leaks. We don't allow `socks5://` to be used to preserve privacy.
141 ///
142 /// Example: `socks5h://127.0.0.1:9050` for a local Tor daemon.
143 #[cfg(feature = "socks5-proxy")]
144 pub socks5_proxy: Option<String>,
145
146 /// A fallback fee rate to use in sat/kWu when we fail to retrieve a fee rate from the
147 /// configured bitcoind/esplora connection.
148 ///
149 /// Example for 1 sat/vB: --fallback-fee-rate 250
150 pub fallback_fee_rate: Option<FeeRate>,
151
152 /// The number of confirmations required before considering a round tx
153 /// fully confirmed
154 ///
155 /// Default value: 6 for mainnet, 2 for testnets
156 pub round_tx_required_confirmations: BlockHeight,
157
158 /// The number of confirmations required before considering an offboard tx
159 /// confirmed. If set to 0, offboard movements are marked as successful
160 /// immediately without waiting for confirmation.
161 ///
162 /// Default value: 2 for mainnet
163 pub offboard_required_confirmations: BlockHeight,
164
165 /// Daemon sync interval in seconds for periodic tasks (onchain, exits,
166 /// boards, offboards, maintenance, rounds, mailbox).
167 ///
168 /// Default value: 60
169 pub daemon_sync_interval_secs: u64,
170
171 /// When set, the daemon skips all automatic wallet syncing — startup
172 /// sync, the fast/slow sync intervals, round event subscription, and
173 /// the mailbox subscription. Only the server connection heartbeat
174 /// keeps running. The operator is responsible for triggering syncs
175 /// via the REST API (e.g. `POST /sync`).
176 ///
177 /// Default value: false
178 pub daemon_manual_sync: bool,
179}
180
181impl Config {
182 /// A network-dependent default config that sets some useful defaults
183 ///
184 /// The [Default::default] provides a sane default for mainnet
185 pub fn network_default(network: Network) -> Self {
186 let mut ret = Self {
187 server_address: "http://127.0.0.1:3535".to_owned(),
188 server_access_token: None,
189 esplora_address: None,
190 bitcoind_address: None,
191 bitcoind_cookiefile: None,
192 bitcoind_user: None,
193 bitcoind_pass: None,
194 #[cfg(feature = "socks5-proxy")]
195 socks5_proxy: None,
196 vtxo_refresh_expiry_threshold: 144,
197 vtxo_exit_margin: 12,
198 htlc_recv_claim_delta: 18,
199 lightning_receive_claim_retries: 5,
200 fallback_fee_rate: Some(FeeRate::from_sat_per_vb_u32(2)),
201 round_tx_required_confirmations: 1,
202 offboard_required_confirmations: 2,
203 daemon_sync_interval_secs: 60,
204 daemon_manual_sync: false,
205 };
206
207 if network != Network::Bitcoin {
208 ret.vtxo_refresh_expiry_threshold = 12;
209 ret.fallback_fee_rate = Some(FeeRate::from_sat_per_vb_u32(1));
210 ret.round_tx_required_confirmations = 1;
211 ret.offboard_required_confirmations = 0;
212 }
213
214 ret
215 }
216
217 /// Load config from the config file path, filling missing fields
218 /// from the network default.
219 ///
220 /// Config values are loaded in the following priority order (highest to lowest):
221 /// 1. Environment variables with `BARK_` prefix (e.g., `BARK_ESPLORA_ADDRESS`)
222 /// 2. Config file values
223 /// 3. Network defaults
224 pub fn load(network: Network, path: impl AsRef<Path>) -> anyhow::Result<Config> {
225 let default = config::Config::try_from(&Self::network_default(network))
226 .expect("default config failed to deconstruct");
227
228 Ok(config::Config::builder()
229 .add_source(default)
230 .add_source(config::File::from(path.as_ref()).required(false))
231 .add_source(config::Environment::with_prefix("BARK"))
232 .build().context("error building config")?
233 .try_deserialize::<Config>().context("error parsing config")?)
234 }
235
236 /// Creates a [chain::ChainSource] instance to communicate with a chain
237 /// backend from this [Config].
238 pub fn chain_source(&self) -> anyhow::Result<ChainSourceSpec> {
239 if let Some(ref url) = self.esplora_address {
240 Ok(ChainSourceSpec::Esplora {
241 url: url.clone(),
242 })
243 } else if let Some(ref url) = self.bitcoind_address {
244 let auth = if let Some(ref c) = self.bitcoind_cookiefile {
245 bitcoin_ext::rpc::Auth::CookieFile(c.clone())
246 } else {
247 bitcoin_ext::rpc::Auth::UserPass(
248 self.bitcoind_user.clone().context("need bitcoind auth config")?,
249 self.bitcoind_pass.clone().context("need bitcoind auth config")?,
250 )
251 };
252 Ok(ChainSourceSpec::Bitcoind {
253 url: url.clone(),
254 auth,
255 })
256 } else {
257 bail!("Need to either provide esplora or bitcoind info");
258 }
259 }
260}
261