sql.rs (2058B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022-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, address::NetworkUnchecked}; 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 pub fn sql_payto<I: sqlx::ColumnIndex<PgRow>>( 41 r: &PgRow, 42 addr: I, 43 name: I, 44 ) -> sqlx::Result<PaytoURI> { 45 let addr = r 46 .try_get_parse::<_, _, bitcoin::Address<NetworkUnchecked>>(addr)? 47 .assume_checked(); 48 let name: Option<&str> = r.try_get(name)?; 49 50 Ok(BtcWallet(addr).as_full_uri(name.unwrap_or("Bitcoin User"))) 51 } 52 53 pub fn sql_generic_payto<I: sqlx::ColumnIndex<PgRow>>( 54 row: &PgRow, 55 idx: I, 56 ) -> sqlx::Result<PaytoURI> { 57 let addr = row 58 .try_get_parse::<_, _, bitcoin::Address<NetworkUnchecked>>(idx)? 59 .assume_checked(); 60 61 Ok(BtcWallet(addr).as_full_uri("Bitcoin User")) 62 }