urma-cli 0.2.1

Command-line program for URMA identities, wallets, Wire, archives and Git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::gateway_host::{LinkScheme, PublicPort};
use crate::gateway_http::HttpLimits;
use crate::gateway_route::Freshness;
use serde::Deserialize;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::path::{Path, PathBuf};
use std::time::Duration;
use urma_chain::observation::Chain;
use urma_runtime::error::{Context, Error, ensure};
use urma_runtime::node::NodeConfig;
use urma_web::config::Limits;

#[derive(Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub(crate) struct Settings {
    pub(crate) log_level: LogLevel,
    pub(crate) rpc_url: Option<String>,
    pub(crate) node_auth_file: Option<PathBuf>,
    pub(crate) vault: Option<PathBuf>,
    pub(crate) unlock_file: Option<PathBuf>,
}

#[derive(Clone, Copy, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum LogLevel {
    #[default]
    Info,
    Warn,
    Debug,
}

impl LogLevel {
    pub(crate) fn filter(self) -> tracing_subscriber::filter::LevelFilter {
        use tracing_subscriber::filter::LevelFilter;
        match self {
            Self::Info => LevelFilter::INFO,
            Self::Warn => LevelFilter::WARN,
            Self::Debug => LevelFilter::DEBUG,
        }
    }
}

pub(crate) fn log_directory() -> Result<PathBuf, Error> {
    let state = match std::env::var_os("XDG_STATE_HOME") {
        Some(value) if PathBuf::from(&value).is_absolute() => PathBuf::from(value),
        Some(value) => {
            tracing::warn!(path = ?value, "ignoring relative XDG_STATE_HOME");
            home()?.join(".local/state")
        }
        None => home()?.join(".local/state"),
    };
    Ok(state.join("urma/logs"))
}

fn data_directory() -> Result<PathBuf, Error> {
    match std::env::var_os("XDG_DATA_HOME") {
        Some(value) if PathBuf::from(&value).is_absolute() => Ok(PathBuf::from(value)),
        Some(value) => {
            tracing::warn!(path = ?value, "ignoring relative XDG_DATA_HOME");
            Ok(home()?.join(".local/share"))
        }
        None => Ok(home()?.join(".local/share")),
    }
}

pub(crate) struct StoreChoice(pub(crate) Option<PathBuf>);

pub(crate) fn web_store(requested: StoreChoice) -> Result<PathBuf, Error> {
    match requested.0 {
        Some(path) => Ok(path),
        None => match std::env::var_os("URMA_WEB_STORE") {
            Some(path) => Ok(PathBuf::from(path)),
            None => Ok(data_directory()?.join("urma/web-store")),
        },
    }
}

pub(crate) struct IndexChoice {
    pub(crate) path: Option<PathBuf>,
    pub(crate) registry: Option<bitcoin::Txid>,
}

pub(crate) fn names_index(choice: IndexChoice, network: &str) -> Result<PathBuf, Error> {
    match choice.path {
        Some(path) => Ok(path),
        None => match choice.registry {
            Some(registry) => Ok(registry_index(
                &names_directory(NamesDirChoice(None))?,
                network,
                registry,
            )),
            None => Err(Error::Missing(
                "name the registry with --registry GENESIS_TXID or the index with --index".into(),
            )),
        },
    }
}

pub(crate) struct NamesDirChoice(pub(crate) Option<PathBuf>);

pub(crate) fn names_directory(requested: NamesDirChoice) -> Result<PathBuf, Error> {
    match requested.0 {
        Some(path) => Ok(path),
        None => match std::env::var_os("URMA_NAMES_DIR") {
            Some(path) => Ok(PathBuf::from(path)),
            None => Ok(data_directory()?.join("urma/names")),
        },
    }
}

pub(crate) fn registry_index(directory: &Path, network: &str, registry: bitcoin::Txid) -> PathBuf {
    directory.join(network).join(format!("{registry}.json"))
}

pub(crate) struct GatewayChoice {
    pub(crate) bind: Option<SocketAddr>,
    pub(crate) scheme: Option<LinkScheme>,
    pub(crate) public_port: Option<u16>,
    pub(crate) store: Option<PathBuf>,
    pub(crate) names_dir: Option<PathBuf>,
    pub(crate) rescan_seconds: Option<u64>,
    pub(crate) max_bytes: Option<usize>,
    pub(crate) workers: Option<usize>,
}

pub(crate) struct GatewaySettings {
    pub(crate) bind: SocketAddr,
    pub(crate) scheme: LinkScheme,
    pub(crate) public_port: PublicPort,
    pub(crate) store: PathBuf,
    pub(crate) names_dir: PathBuf,
    pub(crate) rescan: Duration,
    pub(crate) scan_blocks: u64,
    pub(crate) max_bytes: usize,
    pub(crate) workers: usize,
    pub(crate) http: HttpLimits,
    pub(crate) max_age: u64,
    pub(crate) fetch_wait: Duration,
    pub(crate) fetch_retry: Duration,
    pub(crate) fetch_queue: usize,
    pub(crate) cache_bytes: usize,
    pub(crate) freshness: Freshness,
}

impl GatewaySettings {
    const WORKERS: usize = 8;
    const RESCAN_SECONDS: u64 = 30;
    const MAX_INDEX_LAG_BLOCKS: u64 = 2;
    const MAX_SCAN_AGE: Duration = Duration::from_secs(600);
}

pub(crate) fn gateway(choice: GatewayChoice) -> Result<GatewaySettings, Error> {
    let workers = match choice.workers {
        Some(count) => count,
        None => GatewaySettings::WORKERS,
    };
    ensure!((1..=256).contains(&workers), "--workers must be 1..256");
    let rescan = match choice.rescan_seconds {
        Some(seconds) => seconds,
        None => GatewaySettings::RESCAN_SECONDS,
    };
    ensure!(rescan >= 1, "--rescan-seconds must be at least 1");
    ensure!(
        Duration::from_secs(rescan) < GatewaySettings::MAX_SCAN_AGE,
        "--rescan-seconds must stay under {}, the age at which a names index stops being current",
        GatewaySettings::MAX_SCAN_AGE.as_secs()
    );
    Ok(GatewaySettings {
        bind: match choice.bind {
            Some(address) => address,
            None => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 8080)),
        },
        scheme: match choice.scheme {
            Some(scheme) => scheme,
            None => LinkScheme::Https,
        },
        public_port: match choice.public_port {
            Some(port) => PublicPort::Explicit(port),
            None => PublicPort::Default,
        },
        store: web_store(StoreChoice(choice.store))?,
        names_dir: names_directory(NamesDirChoice(choice.names_dir))?,
        rescan: Duration::from_secs(rescan),
        scan_blocks: 1000,
        max_bytes: match choice.max_bytes {
            Some(bytes) => bytes,
            None => Limits::DEFAULT.max_package_bytes,
        },
        workers,
        http: HttpLimits {
            head_bytes: 16 * 1024,
            max_headers: 64,
            target_bytes: 8 * 1024,
            io_timeout: Duration::from_secs(15),
            head_timeout: Duration::from_secs(20),
            accept_backoff: Duration::from_millis(250),
            linger: Duration::from_secs(2),
            linger_bytes: 64 * 1024,
        },
        max_age: 60,
        fetch_wait: Duration::from_secs(8),
        fetch_retry: Duration::from_secs(60),
        fetch_queue: 32,
        cache_bytes: 256 * 1024 * 1024,
        freshness: Freshness {
            max_lag: GatewaySettings::MAX_INDEX_LAG_BLOCKS,
            max_age: GatewaySettings::MAX_SCAN_AGE,
            retry: Duration::from_secs(rescan),
        },
    })
}

pub(crate) fn names_watch_interval() -> std::time::Duration {
    std::time::Duration::from_secs(15)
}

pub(crate) fn home() -> Result<PathBuf, Error> {
    Ok(std::env::var_os("HOME")
        .context("HOME is unset; set HOME to your user directory")?
        .into())
}

pub(crate) fn load() -> Result<Settings, Error> {
    let path = match std::env::var_os("URMA_CONFIG") {
        Some(path) => PathBuf::from(path),
        None => home()?.join(".config/urma/config.json"),
    };
    if !path.try_exists()? {
        return Ok(Settings::default());
    }
    Ok(serde_json::from_slice(
        &urma_runtime::storage::read_bounded(&path, 16_384)?,
    )?)
}

pub(crate) fn chain(testnet: bool) -> Result<Chain, Error> {
    if testnet {
        return Ok(Chain::LitecoinTestnet);
    }
    let name = match std::env::var_os("URMA_NETWORK") {
        Some(name) => name,
        None => return Ok(Chain::LitecoinMainnet),
    };
    match name.to_str().context("URMA_NETWORK must be UTF-8")? {
        "litecoin-mainnet" => Ok(Chain::LitecoinMainnet),
        "litecoin-testnet" => Ok(Chain::LitecoinTestnet),
        "bitcoin-regtest" => Ok(Chain::BitcoinRegtest),
        "bitcoin-testnet4" => Ok(Chain::BitcoinTestnet4),
        other => Err(Error::Invalid(format!("invalid URMA_NETWORK {other}"))),
    }
}

pub(crate) fn connection(chain: Chain) -> Result<Connection, Error> {
    let settings = load()?;
    let rpc = select(
        std::env::var_os("URMA_RPC_URL")
            .map(|value| {
                value
                    .into_string()
                    .map_err(|invalid| Error::Invalid(format!("non-UTF-8 RPC URL {invalid:?}")))
            })
            .transpose()?,
        settings.rpc_url,
    );
    let auth = select(
        std::env::var_os("URMA_NODE_AUTH_FILE").map(PathBuf::from),
        settings.node_auth_file,
    );
    match (rpc, auth) {
        (Some(rpc_url), Some(cookie_file)) => Ok(Connection::Local(NodeConfig {
            chain,
            rpc_url,
            cookie_file,
        })),
        (None, None) => Ok(Connection::Public),
        (Some(rpc), None) => Err(Error::Invalid(format!(
            "local node {rpc} needs node_auth_file in config"
        ))),
        (None, Some(auth)) => Err(Error::Invalid(format!(
            "local auth {} needs rpc_url in config",
            auth.display()
        ))),
    }
}

pub(crate) fn credentials() -> Result<(PathBuf, PathBuf), Error> {
    let settings = load()?;
    let vault = path_setting("URMA_VAULT", settings.vault, ".local/share/urma/vault.urma")?;
    let unlock = path_setting(
        "URMA_UNLOCK_FILE",
        settings.unlock_file,
        ".config/urma/unlock",
    )?;
    ensure!(
        vault.try_exists()?,
        "no identity vault; run urma key create --help"
    );
    ensure!(
        unlock.try_exists()?,
        "identity is locked; configure unlock_file in ~/.config/urma/config.json or set URMA_UNLOCK_FILE"
    );
    Ok((vault, unlock))
}

pub(crate) enum Connection {
    Local(NodeConfig),
    Public,
}

fn select<T>(first: Option<T>, second: Option<T>) -> Option<T> {
    match first {
        Some(value) => Some(value),
        None => second,
    }
}

fn path_setting(name: &str, setting: Option<PathBuf>, default: &str) -> Result<PathBuf, Error> {
    match select(std::env::var_os(name).map(PathBuf::from), setting) {
        Some(path) => Ok(path),
        None => Ok(home()?.join(default)),
    }
}

pub(crate) fn git_limits() -> Result<urma_git::inventory::Limits, Error> {
    match std::env::var_os("URMA_GIT_LIMITS") {
        Some(path) => Ok(serde_json::from_slice(
            &urma_runtime::storage::read_bounded(&PathBuf::from(path), 4096)?,
        )?),
        None => Ok(urma_git::inventory::Limits::default()),
    }
}

pub(crate) fn expert_node() -> Result<NodeConfig, Error> {
    match connection(chain(false)?)? {
        Connection::Local(node) => Ok(node),
        Connection::Public => Err(Error::Missing(
            "this expert command needs a local node configured with rpc_url and node_auth_file"
                .into(),
        )),
    }
}

pub(crate) enum Output {
    Human,
    Json,
}

pub(crate) fn output() -> Result<Output, Error> {
    match std::env::var_os("URMA_OUTPUT") {
        None => Ok(Output::Human),
        Some(value) => match value.to_str().context("URMA_OUTPUT must be UTF-8")? {
            "json" => Ok(Output::Json),
            "human" => Ok(Output::Human),
            other => Err(Error::Invalid(format!(
                "unsupported URMA_OUTPUT {other}; use human or json"
            ))),
        },
    }
}

pub(crate) fn archive_key() -> Result<PathBuf, Error> {
    path_setting("URMA_ARCHIVE_KEY", None, ".local/share/urma/archive.key")
}

pub(crate) struct KeyLocation(pub(crate) Option<PathBuf>);

pub(crate) fn key_location(location: KeyLocation) -> Result<PathBuf, Error> {
    match location.0 {
        Some(path) => Ok(path),
        None => archive_key(),
    }
}

pub(crate) fn require_archive_key() -> Result<PathBuf, Error> {
    let path = archive_key()?;
    ensure!(
        path.try_exists()?,
        "no private recovery key at {}; run urma key recovery-generate, or configure URMA_ARCHIVE_KEY",
        path.display()
    );
    Ok(path)
}

static VERBOSITY: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);

pub(crate) fn set_verbosity(level: u8) {
    VERBOSITY.store(level, std::sync::atomic::Ordering::Relaxed);
}

pub(crate) fn verbosity() -> u8 {
    VERBOSITY.load(std::sync::atomic::Ordering::Relaxed)
}

pub(crate) struct PublicationOutput(pub Option<PathBuf>);

pub(crate) fn publication_directory(
    repo: &std::path::Path,
    requested: PublicationOutput,
) -> Result<PathBuf, Error> {
    match requested.0 {
        Some(path) => Ok(path),
        None => Ok(repo.join(".urma-plan")),
    }
}

pub(crate) fn publication_poll_interval() -> std::time::Duration {
    std::time::Duration::from_secs(30)
}

pub(crate) fn publication_tip_interval() -> std::time::Duration {
    std::time::Duration::from_secs(2)
}

pub(crate) fn web_watch_interval() -> std::time::Duration {
    std::time::Duration::from_secs(10)
}

pub(crate) struct FeeCeiling(pub Option<u64>);

pub(crate) fn publication_fee_ceiling(requested: FeeCeiling, estimate: u64) -> u64 {
    match requested.0 {
        Some(limit) => limit,
        None => estimate,
    }
}

pub(crate) enum LogOutput {
    File,
    Console,
}

pub(crate) fn log_output() -> Result<LogOutput, Error> {
    match std::env::var_os("URMA_LOG_OUTPUT") {
        None => Ok(LogOutput::File),
        Some(value) if value == "file" => Ok(LogOutput::File),
        Some(value) if value == "stderr" => Ok(LogOutput::Console),
        Some(value) => Err(Error::Invalid(format!(
            "URMA_LOG_OUTPUT must be file or stderr, got {value:?}"
        ))),
    }
}

pub(crate) fn provider_warning_window() -> Duration {
    Duration::from_secs(60)
}