prepared.rs (4871B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 //! Type for the Taler Wire Transfer Gateway HTTP API <https://docs.taler.net/core/api-bank-transfer.html#taler-prepared-transfer-http-api> 18 19 use aws_lc_rs::signature::Ed25519KeyPair; 20 use serde::{Deserialize, Serialize}; 21 use taler_macros::api_config; 22 use url::Url; 23 24 use super::EddsaPublicKey; 25 use crate::{ 26 api::EddsaSignature, 27 db::IncomingType, 28 signature::{Buf, Signature}, 29 types::{ 30 amount::{Amount, Currency}, 31 payto::PaytoURI, 32 timestamp::TalerTimestamp, 33 }, 34 }; 35 36 /// <https://docs.taler.net/core/api-bank-transfer.html#tsref-type-SubjectFormat> 37 #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] 38 #[allow(non_camel_case_types)] 39 pub enum SubjectFormat { 40 SIMPLE, 41 URI, 42 CH_QR_BILL, 43 } 44 45 /// <https://docs.taler.net/core/api-bank-transfer.html#tsref-type-PreparedTransferConfig> 46 #[api_config("taler-prepared-transfer")] 47 #[derive(Debug, Clone, Serialize, Deserialize)] 48 pub struct PreparedTransferConfig<'a> { 49 pub currency: Currency, 50 pub supported_formats: Vec<SubjectFormat>, 51 } 52 53 #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] 54 #[allow(non_camel_case_types)] 55 pub enum TransferType { 56 reserve, 57 kyc, 58 } 59 60 impl From<TransferType> for IncomingType { 61 fn from(value: TransferType) -> Self { 62 match value { 63 TransferType::reserve => IncomingType::reserve, 64 TransferType::kyc => IncomingType::kyc, 65 } 66 } 67 } 68 69 #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] 70 pub enum PublicKeyAlg { 71 EdDSA, 72 } 73 74 /// <https://docs.taler.net/core/api-bank-transfer.html#tsref-type-RegistrationRequest> 75 #[derive(Debug, Clone, Serialize, Deserialize)] 76 pub struct RegistrationRequest { 77 pub credit_account: PaytoURI, 78 pub r#type: TransferType, 79 pub recurrent: bool, 80 pub credit_amount: Amount, 81 pub alg: PublicKeyAlg, 82 pub account_pub: EddsaPublicKey, 83 pub authorization_pub: EddsaPublicKey, 84 pub authorization_sig: EddsaSignature, 85 } 86 87 impl RegistrationRequest { 88 pub fn signed(mut self, pair: &Ed25519KeyPair) -> Self { 89 self.authorization_sig = self.sign(pair); 90 self 91 } 92 } 93 94 impl Signature<104> for RegistrationRequest { 95 const PURPOSE: u32 = 1224; 96 97 fn signature_bytes(&self, buf: Buf<104>) -> Buf<104> { 98 buf.put_payto(&self.credit_account) 99 .put_amount(self.credit_amount) 100 .put_u32(match self.r#type { 101 TransferType::reserve => 1, 102 TransferType::kyc => 2, 103 }) 104 .put_u16(match self.recurrent { 105 true => 2, 106 false => 1, 107 }) 108 .put_u16(match self.alg { 109 PublicKeyAlg::EdDSA => 1, 110 }) 111 .put(self.account_pub.as_ref()) 112 } 113 } 114 115 /// <https://docs.taler.net/core/api-bank-transfer.html#tsref-type-TransferSubject> 116 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] 117 #[serde(tag = "type")] 118 pub enum TransferSubject { 119 #[serde(rename = "SIMPLE")] 120 Simple { 121 credit_amount: Amount, 122 subject: String, 123 }, 124 #[serde(rename = "URI")] 125 Uri { credit_amount: Amount, uri: Url }, 126 #[serde(rename = "CH_QR_BILL")] 127 QrBill { 128 credit_amount: Amount, 129 qr_reference_number: String, 130 }, 131 } 132 133 /// <https://docs.taler.net/core/api-bank-transfer.html#tsref-type-RegistrationResponse> 134 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] 135 pub struct RegistrationResponse { 136 pub subjects: Vec<TransferSubject>, 137 pub expiration: TalerTimestamp, 138 } 139 140 /// <https://docs.taler.net/core/api-bank-transfer.html#tsref-type-Unregistration> 141 #[derive(Debug, Clone, Serialize, Deserialize)] 142 pub struct Unregistration { 143 pub timestamp: TalerTimestamp, 144 pub authorization_pub: EddsaPublicKey, 145 pub authorization_sig: EddsaSignature, 146 } 147 148 impl Unregistration { 149 pub fn signed(mut self, pair: &Ed25519KeyPair) -> Self { 150 self.authorization_sig = self.sign(pair); 151 self 152 } 153 } 154 155 impl Signature<16> for Unregistration { 156 const PURPOSE: u32 = 1225; 157 158 fn signature_bytes(&self, buf: Buf<16>) -> Buf<16> { 159 buf.put_timestamp(self.timestamp) 160 } 161 }