depolymerization

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

lib.rs (5389B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022-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 use std::str::FromStr;
     17 
     18 use bitcoin::{Address, Amount, Network, Txid, hashes::hex::FromHex};
     19 use rpc::{Category, Rpc, Transaction};
     20 use rpc_utils::{segwit_min_amount, sender_address};
     21 use segwit::{decode_segwit_msg, encode_segwit_key};
     22 use taler_common::{api::EddsaPublicKey, config::parser::ConfigSource};
     23 
     24 use crate::segwit::DecodeSegWitErr;
     25 
     26 pub mod api;
     27 pub mod cli;
     28 pub mod config;
     29 pub mod db;
     30 mod fail_point;
     31 mod loops;
     32 pub mod payto;
     33 pub mod rpc;
     34 pub mod rpc_utils;
     35 pub mod segwit;
     36 pub mod setup;
     37 pub mod sql;
     38 pub mod taler_utils;
     39 
     40 pub const CONFIG_SOURCE: ConfigSource = ConfigSource::simple("depolymerizer-bitcoin");
     41 pub const DB_SCHEMA: &str = "depolymerizer_bitcoin";
     42 
     43 #[derive(Debug, thiserror::Error)]
     44 pub enum GetOpReturnErr {
     45     #[error("Missing opreturn")]
     46     MissingOpReturn,
     47     #[error(transparent)]
     48     RPC(#[from] rpc::Error),
     49 }
     50 
     51 /// An extended bitcoincore JSON-RPC api client who can send and retrieve metadata with their transaction
     52 impl Rpc {
     53     /// Send a transaction with a 32B key as metadata encoded using fake segwit addresses
     54     pub async fn send_segwit_key(
     55         &mut self,
     56         to: &Address,
     57         amount: &Amount,
     58         metadata: &[u8; 32],
     59     ) -> rpc::Result<Txid> {
     60         let network = guess_network(to);
     61         let hrp = match network {
     62             Network::Bitcoin => bech32::hrp::BC,
     63             Network::Testnet | Network::Signet => bech32::hrp::TB,
     64             Network::Regtest => bech32::hrp::BCRT,
     65             _ => unimplemented!(),
     66         };
     67         let addresses = encode_segwit_key(hrp, metadata);
     68         let addresses = [
     69             Address::from_str(&addresses[0]).unwrap().assume_checked(),
     70             Address::from_str(&addresses[1]).unwrap().assume_checked(),
     71         ];
     72         let mut recipients = vec![(to, amount)];
     73         let min = segwit_min_amount();
     74         recipients.extend(addresses.iter().map(|addr| (addr, &min)));
     75         self.send_many(recipients).await
     76     }
     77 
     78     /// Get detailed information about an in-wallet transaction and it's 32B metadata key encoded using fake segwit addresses
     79     pub async fn get_tx_segwit_key(
     80         &mut self,
     81         id: &Txid,
     82     ) -> Result<(Transaction, Result<EddsaPublicKey, DecodeSegWitErr>), rpc::Error> {
     83         let full = self.get_tx(id).await?;
     84 
     85         let addresses: Vec<String> = full
     86             .decoded
     87             .vout
     88             .iter()
     89             .filter_map(|it| {
     90                 it.script_pub_key
     91                     .address
     92                     .as_ref()
     93                     .map(|addr| addr.clone().assume_checked().to_string())
     94             })
     95             .collect();
     96 
     97         Ok((full, decode_segwit_msg(&addresses)))
     98     }
     99 
    100     /// Get detailed information about an in-wallet transaction and its op_return metadata
    101     pub async fn get_tx_op_return(
    102         &mut self,
    103         id: &Txid,
    104     ) -> Result<(Transaction, Vec<u8>), GetOpReturnErr> {
    105         let full = self.get_tx(id).await?;
    106 
    107         let op_return_out = full
    108             .decoded
    109             .vout
    110             .iter()
    111             .find(|it| it.script_pub_key.asm.starts_with("OP_RETURN"))
    112             .ok_or(GetOpReturnErr::MissingOpReturn)?;
    113 
    114         let hex = op_return_out.script_pub_key.asm.split_once(' ').unwrap().1;
    115         // Op return payload is always encoded in hexadecimal
    116         let metadata = Vec::from_hex(hex).unwrap();
    117 
    118         Ok((full, metadata))
    119     }
    120 
    121     /// Bounce a transaction bask to its sender
    122     ///
    123     /// There is no reliable way to bounce a transaction as you cannot know if the addresses
    124     /// used are shared or come from a third-party service. We only send back to the first input
    125     /// address as a best-effort gesture.
    126     pub async fn bounce(
    127         &mut self,
    128         id: &Txid,
    129         bounce_fee: &Amount,
    130         metadata: Option<&[u8]>,
    131     ) -> Result<Txid, rpc::Error> {
    132         let full = self.get_tx(id).await?;
    133         let detail = &full.details[0];
    134         assert!(detail.category == Category::Receive);
    135 
    136         let amount = detail.amount.to_unsigned().unwrap();
    137         let sender = sender_address(self, &full).await?;
    138         let bounce_amount = Amount::from_sat(amount.to_sat().saturating_sub(bounce_fee.to_sat()));
    139         // Send refund making recipient pay the transaction fees
    140         self.send(&sender, &bounce_amount, metadata, true).await
    141     }
    142 }
    143 
    144 pub fn guess_network(address: &Address) -> Network {
    145     let addr = address.as_unchecked();
    146     for network in [
    147         Network::Bitcoin,
    148         Network::Regtest,
    149         Network::Signet,
    150         Network::Regtest,
    151     ] {
    152         if addr.is_valid_for_network(network) {
    153             return network;
    154         }
    155     }
    156     unreachable!()
    157 }