depolymerization

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

sql.rs (2231B)


      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 
     17 use bitcoin::{Address, Amount as BtcAmount, Txid, address::NetworkUnchecked, hashes::Hash};
     18 use sqlx::{Row, postgres::PgRow};
     19 use taler_api::db::TypeHelper;
     20 use taler_common::types::{
     21     amount::Currency,
     22     payto::{PaytoImpl as _, PaytoURI},
     23 };
     24 
     25 use crate::{payto::BtcWallet, taler_utils::taler_to_btc};
     26 
     27 /// Bitcoin amount from sql
     28 pub fn sql_btc_amount(row: &PgRow, idx: usize, currency: &Currency) -> sqlx::Result<BtcAmount> {
     29     let amount = row.try_get_amount(idx, currency)?;
     30     Ok(taler_to_btc(&amount))
     31 }
     32 
     33 /// Bitcoin address from sql
     34 pub fn sql_addr(row: &PgRow, idx: usize) -> sqlx::Result<Address> {
     35     Ok(row
     36         .try_get_parse::<_, _, Address<NetworkUnchecked>>(idx)?
     37         .assume_checked())
     38 }
     39 
     40 /// Bitcoin transaction id from sql
     41 pub fn sql_txid(row: &PgRow, idx: usize) -> sqlx::Result<Txid> {
     42     row.try_get_map(idx, Txid::from_slice)
     43 }
     44 
     45 pub fn sql_payto<I: sqlx::ColumnIndex<PgRow>>(
     46     r: &PgRow,
     47     addr: I,
     48     name: I,
     49 ) -> sqlx::Result<PaytoURI> {
     50     let addr = r
     51         .try_get_parse::<_, _, bitcoin::Address<NetworkUnchecked>>(addr)?
     52         .assume_checked();
     53     let name: Option<&str> = r.try_get(name)?;
     54 
     55     Ok(BtcWallet(addr).as_full_uri(name.unwrap_or("Bitcoin User")))
     56 }
     57 
     58 pub fn sql_generic_payto<I: sqlx::ColumnIndex<PgRow>>(
     59     row: &PgRow,
     60     idx: I,
     61 ) -> sqlx::Result<PaytoURI> {
     62     let addr = row
     63         .try_get_parse::<_, _, bitcoin::Address<NetworkUnchecked>>(idx)?
     64         .assume_checked();
     65 
     66     Ok(BtcWallet(addr).as_full_uri("Bitcoin User"))
     67 }