exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

persist_aml_program_result.c (4838B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023, 2024 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  * @file persist_aml_program_result.c
     18  * @brief helper function store results of AML programs
     19  * @author Christian Grothoff
     20  */
     21 #include "exchangedb_lib.h"
     22 #include "taler/taler_kyclogic_lib.h"
     23 #include "exchange-database/insert_aml_decision.h"
     24 #include "exchange-database/insert_aml_program_failure.h"
     25 #include "exchange-database/insert_successor_measure.h"
     26 #include "exchange-database/persist_aml_program_result.h"
     27 #include "helper.h"
     28 #include <gnunet/gnunet_common.h>
     29 
     30 
     31 enum GNUNET_DB_QueryStatus
     32 TALER_EXCHANGEDB_persist_aml_program_result (
     33   struct TALER_EXCHANGEDB_PostgresContext *pg,
     34   uint64_t process_row,
     35   const struct TALER_NormalizedPaytoHashP *account_id,
     36   const struct TALER_KYCLOGIC_AmlProgramResult *apr,
     37   enum TALER_EXCHANGEDB_PersistProgramResultStatus *ret_pprs)
     38 {
     39   enum GNUNET_DB_QueryStatus qs;
     40   json_t *jmeasures = NULL;
     41   struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs = NULL;
     42 
     43   GNUNET_assert (NULL != ret_pprs);
     44 
     45   *ret_pprs = TALER_EXCHANGEDB_PPRS_OK;
     46 
     47   if ( (TALER_KYCLOGIC_AMLR_SUCCESS == apr->status) &&
     48        (NULL != apr->details.success.new_measures) )
     49   {
     50     lrs = TALER_KYCLOGIC_rules_parse (apr->details.success.new_rules);
     51     if (NULL == lrs)
     52     {
     53       qs = TALER_EXCHANGEDB_insert_aml_program_failure (
     54         pg,
     55         process_row,
     56         account_id,
     57         "Failed to parse AML program output",
     58         TALER_EC_EXCHANGE_KYC_AML_PROGRAM_MALFORMED_RESULT);
     59       GNUNET_break (qs > 0);
     60       return qs;
     61     }
     62     jmeasures = TALER_KYCLOGIC_get_jmeasures (
     63       lrs,
     64       apr->details.success.new_measures);
     65     if (NULL == jmeasures)
     66     {
     67       char *err;
     68 
     69       GNUNET_break (0);
     70       GNUNET_asprintf (&err,
     71                        "Failed to find measures `%s' specified in AML program output",
     72                        apr->details.success.new_measures);
     73       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     74                   "AML program specified invalid measures `%s'\n",
     75                   apr->details.success.new_measures);
     76       qs = TALER_EXCHANGEDB_insert_aml_program_failure (
     77         pg,
     78         process_row,
     79         account_id,
     80         err,
     81         TALER_EC_EXCHANGE_KYC_AML_PROGRAM_MALFORMED_RESULT);
     82       *ret_pprs = TALER_EXCHANGEDB_PPRS_BAD_OUTCOME;
     83       TALER_KYCLOGIC_rules_free (lrs);
     84       GNUNET_free (err);
     85       GNUNET_break (qs > 0);
     86       return qs;
     87     }
     88   }
     89 
     90   qs = TALER_EXCHANGEDB_clear_aml_lock (
     91     pg,
     92     account_id);
     93   if (qs < 0)
     94   {
     95     GNUNET_break (0);
     96     goto cleanup;
     97   }
     98   switch (apr->status)
     99   {
    100   case TALER_KYCLOGIC_AMLR_FAILURE:
    101     qs = TALER_EXCHANGEDB_insert_aml_program_failure (
    102       pg,
    103       process_row,
    104       account_id,
    105       apr->details.failure.error_message,
    106       apr->details.failure.ec);
    107     GNUNET_break (qs > 0);
    108     goto cleanup;
    109   case TALER_KYCLOGIC_AMLR_SUCCESS:
    110     {
    111       struct TALER_FullPayto null_payto_uri = { 0 };
    112       bool invalid_officer;
    113       bool unknown_account;
    114       struct GNUNET_TIME_Timestamp last_date;
    115       uint64_t legitimization_measure_serial_id;
    116       bool is_wallet;
    117 
    118       qs = TALER_EXCHANGEDB_insert_aml_decision (
    119         pg,
    120         null_payto_uri,
    121         account_id,
    122         GNUNET_TIME_timestamp_get (),
    123         apr->details.success.expiration_time,
    124         apr->details.success.account_properties,
    125         apr->details.success.new_rules,
    126         apr->details.success.to_investigate,
    127         apr->details.success.new_measures,
    128         jmeasures,
    129         NULL, /* justification */
    130         NULL, /* decider_pub */
    131         NULL, /* decider_sig */
    132         apr->details.success.num_events,
    133         apr->details.success.events,
    134         NULL, /* form ID */
    135         0, /* enc_attributes_size*/
    136         NULL, /* enc_attributes*/
    137         NULL, /* attributes_hash */
    138         GNUNET_TIME_UNIT_ZERO_TS, /* attributes_expiration_time */
    139         &invalid_officer,
    140         &unknown_account,
    141         &last_date,
    142         &legitimization_measure_serial_id,
    143         &is_wallet);
    144       GNUNET_break (qs > 0);
    145       goto cleanup;
    146     }
    147   }
    148   GNUNET_break (0);
    149   qs = GNUNET_DB_STATUS_HARD_ERROR;
    150 cleanup:
    151   TALER_KYCLOGIC_rules_free (lrs);
    152   json_decref (jmeasures);
    153   return qs;
    154 }