TransferDAO.kt (2613B)
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.nexus.db 21 22 import tech.libeufin.common.* 23 import tech.libeufin.common.db.* 24 import java.time.Instant 25 26 /** Data access logic for transfer specific logic */ 27 class TransferDAO(private val db: Database) { 28 /** Result of prepared transfer registration */ 29 sealed interface RegistrationResult { 30 data object Success: RegistrationResult 31 data object ReservePubReuse: RegistrationResult 32 data object SubjectReuse: RegistrationResult 33 } 34 35 /** Register a prepared transfer */ 36 suspend fun register( 37 type: TransferType, 38 accountPub: EddsaPublicKey, 39 authPub: EddsaPublicKey, 40 authSig: EddsaSignature, 41 recurrent: Boolean, 42 referenceNumber: String?, 43 timestamp: Instant 44 ): RegistrationResult = db.serializable( 45 """ 46 SELECT 47 out_subject_reuse 48 ,out_reserve_pub_reuse 49 FROM register_prepared_transfers ( 50 ?::taler_incoming_type, ?, ?, ?, ?, ?, ? 51 ) 52 """ 53 ) { 54 bind(type) 55 bind(accountPub) 56 bind(authPub) 57 bind(authSig) 58 bind(recurrent) 59 bind(referenceNumber) 60 bind(timestamp) 61 one { 62 when { 63 it.getBoolean("out_subject_reuse") -> RegistrationResult.SubjectReuse 64 it.getBoolean("out_reserve_pub_reuse") -> RegistrationResult.ReservePubReuse 65 else -> RegistrationResult.Success 66 } 67 } 68 } 69 70 /** Unregister a prepared transfer */ 71 suspend fun unregister( 72 authorizationPub: EddsaPublicKey, 73 timestamp: Instant 74 ) = db.serializable( 75 "SELECT out_found FROM delete_prepared_transfers(?,?)", 76 { 77 bind(authorizationPub) 78 bind(timestamp) 79 one { 80 it.getBoolean(1) 81 } 82 } 83 ) 84 }