summaryrefslogtreecommitdiff
path: root/btc-wire/src/btc_config.rs
blob: 85c96f494a84d982d8dceeb572432100a885b9b6 (plain)
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
/*
  This file is part of TALER
  Copyright (C) 2022 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
use std::{
    net::SocketAddr,
    path::{Path, PathBuf},
    str::FromStr,
};

use bitcoin::Network;
use common::{
    currency::CurrencyBtc,
    log::{fail, OrFail},
};

use crate::{
    check_network_currency,
    rpc_utils::{chain_dir, rpc_port},
};

pub const WIRE_WALLET_NAME: &str = "wire";

#[derive(Debug, Clone)]
pub enum BtcAuth {
    Cookie(PathBuf),
    Auth(String),
}

/// Bitcoin config relevant for btc-wire
#[derive(Debug, Clone)]
pub struct BitcoinConfig {
    pub network: Network,
    pub addr: SocketAddr,
    pub auth: BtcAuth,
}

impl BitcoinConfig {
    /// Load from bitcoin path
    pub fn load(path: impl AsRef<Path>, currency: CurrencyBtc) -> Result<Self, ini::Error> {
        let path = path.as_ref();
        let conf = if path.is_dir() {
            ini::Ini::load_from_file(path.join("bitcoin.conf"))
        } else {
            ini::Ini::load_from_file(path)
        }?;

        let main = conf.general_section();

        if !main.contains_key("txindex") {
            fail("require a bitcoind node running with 'txindex' option");
        }

        if !main.contains_key("maxtxfee") {
            fail("require a bitcoind node running with 'maxtxfee' option");
        }

        let network = if let Some("1") = main.get("testnet") {
            Network::Testnet
        } else if let Some("1") = main.get("signet") {
            Network::Signet
        } else if let Some("1") = main.get("regtest") {
            Network::Regtest
        } else {
            Network::Bitcoin
        };

        check_network_currency(network, currency);

        let section = match network {
            Network::Bitcoin => Some(main),
            Network::Testnet => conf.section(Some("test")),
            Network::Signet => conf.section(Some("signet")),
            Network::Regtest => conf.section(Some("regtest")),
            _ => unimplemented!(),
        };

        let port = if let Some(addr) = section.and_then(|s| s.get("rpcport")) {
            addr.parse()
                .or_fail(|_| "bitcoin config 'rpcport' is not a valid port number".into())
        } else {
            rpc_port(network)
        };

        let addr = if let Some(addr) = section.and_then(|s| s.get("rpcbind")) {
            SocketAddr::from_str(addr)
                .or_fail(|_| "bitcoin config 'rpcbind' is not a valid socket address".into())
        } else {
            ([127, 0, 0, 1], port).into()
        };

        let auth = if let (Some(login), Some(passwd)) = (
            section.and_then(|s| s.get("rpcuser")),
            section.and_then(|s| s.get("rpcpassword")),
        ) {
            BtcAuth::Auth(format!("{}:{}", login, passwd))
        } else if let (Some(login), Some(passwd)) = (main.get("rpcuser"), main.get("rpcpassword")) {
            BtcAuth::Auth(format!("{}:{}", login, passwd))
        } else {
            let cookie_file = if let Some(path) = section.and_then(|s| s.get("rpccookiefile")) {
                path
            } else if let Some(path) = main.get("rpccookiefile") {
                path
            } else {
                ".cookie"
            }
            .to_string();
            let data_dir = if let Some(path) = section.and_then(|s| s.get("datadir")) {
                PathBuf::from(path)
            } else if let Some(path) = main.get("datadir") {
                PathBuf::from(path)
            } else if path.is_file() {
                path.parent().unwrap().to_path_buf()
            } else {
                path.to_path_buf()
            };
            BtcAuth::Cookie(data_dir.join(chain_dir(network)).join(cookie_file))
        };

        Ok(Self {
            network,
            addr,
            auth,
        })
    }
}