libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

TransferDAO.kt (2964B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2026 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 package tech.libeufin.bank.db
     21 
     22 import tech.libeufin.common.*
     23 import tech.libeufin.common.db.*
     24 import java.time.Instant
     25 import java.util.UUID
     26 
     27 /** Data access logic for transfer specific logic */
     28 class TransferDAO(private val db: Database) {
     29     /** Result of prepared transfer registration */
     30     sealed interface RegistrationResult {
     31         data class Success(val uuid: UUID?): RegistrationResult
     32         data object NotExchange: RegistrationResult
     33         data object ReservePubReuse: RegistrationResult
     34         data object UnknownCreditor: RegistrationResult
     35     }
     36 
     37     /** Register a prepared transfer */
     38     suspend fun register(
     39         account: Payto,
     40         type: TransferType,
     41         accountPub: EddsaPublicKey,
     42         authPub: EddsaPublicKey,
     43         authSig: EddsaSignature,
     44         recurrent: Boolean,
     45         amount: TalerAmount,
     46         timestamp: Instant
     47     ): RegistrationResult = db.serializable(
     48         """
     49         SELECT out_unknown_creditor, out_not_exchange, out_reserve_pub_reuse, out_withdrawal_uuid
     50         FROM register_prepared_transfers (
     51             ?, ?::taler_incoming_type, ?, ?, ?, ?, (?, ?)::taler_amount, ?, ?
     52         )
     53         """
     54     ) {
     55         bind(account.canonical)
     56         bind(type)
     57         bind(accountPub)
     58         bind(authPub)
     59         bind(authSig)
     60         bind(recurrent)
     61         bind(amount)
     62         bind(timestamp)
     63         bind("Taler prepared MAP:$authPub")
     64         one {
     65             when {
     66                 it.getBoolean("out_unknown_creditor") -> RegistrationResult.UnknownCreditor
     67                 it.getBoolean("out_not_exchange") -> RegistrationResult.NotExchange
     68                 it.getBoolean("out_reserve_pub_reuse") -> RegistrationResult.ReservePubReuse
     69                 else -> RegistrationResult.Success(it.getOptObject("out_withdrawal_uuid"))
     70             }
     71         }
     72     }
     73 
     74     /** Unregister a prepared transfer */
     75     suspend fun unregister(
     76         authorizationPub: EddsaPublicKey,
     77         timestamp: Instant
     78     ) = db.serializable(
     79         "SELECT out_found FROM delete_prepared_transfers(?,?)",
     80         {
     81             bind(authorizationPub)
     82             bind(timestamp)
     83             one {
     84                 it.getBoolean(1)
     85             }
     86         }
     87     )
     88 }