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
use std::env;

use anyhow::Context;
use fedimint_core::util::SafeUrl;
use fedimint_derive::{Decodable, Encodable};
use jsonrpsee_core::Serialize;
use serde::Deserialize;

/// Env var for bitcoin RPC kind
pub const FM_BITCOIN_RPC_KIND: &str = "FM_BITCOIN_RPC_KIND";
/// Env var for bitcoin URL
pub const FM_BITCOIN_RPC_URL: &str = "FM_BITCOIN_RPC_URL";
/// Env var that can be set to point at the bitcoind's cookie file to use for
/// auth
pub const FM_BITCOIND_COOKIE_FILE_VAR_NAME: &str = "FM_BITCOIND_COOKIE_FILE";

/// Configuration for the bitcoin RPC
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
pub struct BitcoinRpcConfig {
    pub kind: String,
    pub url: SafeUrl,
}

impl BitcoinRpcConfig {
    pub fn from_env_vars() -> anyhow::Result<Self> {
        Ok(Self {
            kind: env::var(FM_BITCOIN_RPC_KIND).with_context(|| {
                anyhow::anyhow!("failure looking up env var {FM_BITCOIN_RPC_KIND}")
            })?,
            url: env::var(FM_BITCOIN_RPC_URL)
                .with_context(|| {
                    anyhow::anyhow!("failure looking up env var {FM_BITCOIN_RPC_URL}")
                })?
                .parse()
                .with_context(|| anyhow::anyhow!("failure parsing env var {FM_BITCOIN_RPC_URL}"))?,
        })
    }
}