depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

config.rs (5340B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025, 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use std::net::SocketAddr;
     18 
     19 use bitcoin::Amount;
     20 use sqlx::postgres::PgConnectOptions;
     21 use taler_api::{
     22     Serve, config::{ApiCfg, DbCfg, WireGatewayCfg},
     23 };
     24 use taler_common::{
     25     config::{CfgErr, Config},
     26     map_config,
     27     types::amount::Currency,
     28 };
     29 
     30 use crate::{
     31     payto::{BtcWallet, FullBtcPayto},
     32     taler_utils::taler_to_btc,
     33 };
     34 
     35 pub fn parse_db_cfg(cfg: &Config) -> Result<DbCfg, CfgErr> {
     36     DbCfg::parse(cfg.section("depolymerizer-bitcoindb-postgres"))
     37 }
     38 
     39 pub fn parse_account_payto(cfg: &Config) -> Result<FullBtcPayto, CfgErr> {
     40     let sect = cfg.section("depolymerizer-bitcoin");
     41     let wallet: BtcWallet = sect.parse("bitcoin wallet address", "WALLET").require()?;
     42     let name = sect.cstr("NAME").require()?;
     43 
     44     Ok(FullBtcPayto::new(wallet, &name))
     45 }
     46 
     47 #[derive(Debug, Clone)]
     48 pub enum RpcAuth {
     49     Cookie(String),
     50     Basic(String),
     51 }
     52 
     53 pub struct ServeCfg {
     54     pub payto: FullBtcPayto,
     55     pub serve: Serve,
     56     pub wire_gateway: Option<WireGatewayCfg>,
     57     pub revenue: Option<ApiCfg>,
     58     pub observability: Option<ApiCfg>,
     59     pub currency: Currency,
     60     pub lifetime: Option<u32>,
     61 }
     62 
     63 impl ServeCfg {
     64     pub fn parse(cfg: &Config) -> Result<Self, CfgErr> {
     65         let payto = parse_account_payto(cfg)?;
     66 
     67         let s = cfg.section("depolymerizer-bitcoin-httpd");
     68 
     69         let lifetime = s.number("LIFETIME").opt()?.filter(|it| *it != 0);
     70 
     71         let serve = Serve::parse(&s)?;
     72 
     73         let sect = cfg.section("depolymerizer-bitcoin");
     74         Ok(Self {
     75             currency: sect.parse("currency", "CURRENCY").require()?,
     76             lifetime,
     77             payto,
     78             serve,
     79             wire_gateway: WireGatewayCfg::parse(
     80                 cfg.section("depolymerizer-bitcoin-httpd-wire-gateway-api"),
     81             )?,
     82             revenue: ApiCfg::parse(cfg.section("depolymerizer-bitcoin-httpd-revenue-api"))?,
     83             observability: ApiCfg::parse(
     84                 cfg.section("depolymerizer-bitcoin-httpd-observability-api"),
     85             )?,
     86         })
     87     }
     88 }
     89 
     90 #[derive(Debug, Clone)]
     91 
     92 pub struct WorkerCfg {
     93     pub conf: u32,
     94     pub max_conf: u32,
     95     pub bounce_fee: Amount,
     96     pub lifetime: Option<u32>,
     97     pub bump_delay: Option<u32>,
     98     pub db_config: PgConnectOptions,
     99     pub currency: Currency,
    100     pub rpc_cfg: RpcCfg,
    101     pub wallet_cfg: WalletCfg,
    102 }
    103 
    104 impl WorkerCfg {
    105     pub fn parse(cfg: &Config) -> Result<Self, CfgErr> {
    106         let currency: Currency = cfg
    107             .section("depolymerizer-bitcoin")
    108             .parse("currency", "CURRENCY")
    109             .require()?;
    110 
    111         let sect = cfg.section("depolymerizer-bitcoin-worker");
    112         let confirmation = sect.number("CONFIRMATION").require()?;
    113 
    114         Ok(Self {
    115             conf: confirmation,
    116             max_conf: confirmation * 2,
    117             bounce_fee: sect
    118                 .amount("BOUNCE_FEE", &currency)
    119                 .opt()?
    120                 .map(|it| taler_to_btc(&it))
    121                 .unwrap_or_default(),
    122             lifetime: sect.number("LIFETIME").opt()?.filter(|it| *it != 0),
    123             bump_delay: sect.number("BUMP_DELAY").opt()?.filter(|it| *it != 0),
    124             db_config: cfg
    125                 .section("depolymerizer-bitcoindb-postgres")
    126                 .parse("Postgres", "CONFIG")
    127                 .require()
    128                 .unwrap(),
    129             currency,
    130             rpc_cfg: RpcCfg::parse(cfg)?,
    131             wallet_cfg: WalletCfg::parse(cfg)?,
    132         })
    133     }
    134 }
    135 
    136 #[derive(Debug, Clone)]
    137 pub struct WalletCfg {
    138     pub name: String,
    139     pub password: Option<String>,
    140 }
    141 
    142 impl WalletCfg {
    143     pub fn parse(cfg: &Config) -> Result<Self, CfgErr> {
    144         let sect = cfg.section("depolymerizer-bitcoin-worker");
    145         let password = sect.str("PASSWORD").opt()?;
    146         let name = sect.str("WALLET_NAME").require()?;
    147 
    148         Ok(Self { name, password })
    149     }
    150 }
    151 
    152 #[derive(Debug, Clone)]
    153 pub struct RpcCfg {
    154     pub addr: SocketAddr,
    155     pub auth: RpcAuth,
    156 }
    157 
    158 impl RpcCfg {
    159     pub fn parse(cfg: &Config) -> Result<Self, CfgErr> {
    160         let sect = cfg.section("depolymerizer-bitcoin-worker");
    161 
    162         let addr = sect.parse("RPC server address", "RPC_BIND").require()?;
    163 
    164         let auth = map_config!(sect, "auth_method", "RPC_AUTH_METHOD",
    165             "basic" => {
    166                 let username = sect.str("RPC_USERNAME").require()?;
    167                 let password = sect.str("RPC_PASSWORD").require()?;
    168                 RpcAuth::Basic(format!("{username}:{password}"))
    169             },
    170             "cookie" => {
    171                 RpcAuth::Cookie(sect.path("RPC_COOKIE_FILE").require()?)
    172             }
    173         )
    174         .require()?;
    175 
    176         Ok(Self { addr, auth })
    177     }
    178 }