depolymerization

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

rpc_utils.rs (2989B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022-2025 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 use std::{path::PathBuf, str::FromStr};
     17 
     18 use bitcoin::{Address, Amount, Network};
     19 
     20 use crate::rpc::{self, Rpc, Transaction};
     21 
     22 /// Default chain dir <https://github.com/bitcoin/bitcoin/blob/master/doc/files.md#data-directory-location>
     23 pub fn chain_dir(network: Network) -> &'static str {
     24     match network {
     25         Network::Bitcoin => "main",
     26         Network::Testnet => "testnet3",
     27         Network::Regtest => "regtest",
     28         Network::Signet => "signet",
     29         _ => unimplemented!(),
     30     }
     31 }
     32 
     33 /// Default rpc port <https://github.com/bitcoin/bitcoin/blob/master/share/examples/bitcoin.conf>
     34 pub fn rpc_port(network: Network) -> u16 {
     35     match network {
     36         Network::Bitcoin => 8332,
     37         Network::Testnet => 18332,
     38         Network::Regtest => 18443,
     39         Network::Signet => 38333,
     40         _ => unimplemented!(),
     41     }
     42 }
     43 
     44 /// Default bitcoin data_dir <https://github.com/bitcoin/bitcoin/blob/master/doc/bitcoin-conf.md>
     45 pub fn default_data_dir() -> PathBuf {
     46     if cfg!(target_os = "windows") {
     47         PathBuf::from_str(&std::env::var("APPDATA").unwrap())
     48             .unwrap()
     49             .join("Bitcoin")
     50     } else if cfg!(target_os = "linux") {
     51         PathBuf::from_str(&std::env::var("HOME").unwrap())
     52             .unwrap()
     53             .join(".bitcoin")
     54     } else if cfg!(target_os = "macos") {
     55         PathBuf::from_str(&std::env::var("HOME").unwrap())
     56             .unwrap()
     57             .join("Library/Application Support/Bitcoin")
     58     } else {
     59         unimplemented!("Only windows, linux or macos")
     60     }
     61 }
     62 
     63 /// Minimum dust amount to perform a transaction to a segwit address
     64 pub fn segwit_min_amount() -> Amount {
     65     // https://github.com/bitcoin/bitcoin/blob/master/src/policy/policy.cpp
     66     Amount::from_sat(294)
     67 }
     68 
     69 /// Get the first sender address from a raw transaction
     70 pub async fn sender_address(rpc: &mut Rpc, full: &Transaction) -> rpc::Result<Address> {
     71     let first = &full.decoded.vin[0];
     72     let tx = rpc.get_input_output(&first.txid.unwrap()).await?;
     73     Ok(tx
     74         .vout
     75         .into_iter()
     76         .find(|it| it.n == first.vout.unwrap())
     77         .unwrap()
     78         .script_pub_key
     79         .address
     80         .unwrap()
     81         .assume_checked())
     82 }