exchange

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

taler_kyclogic_lib.h (31098B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022, 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 taler_kyclogic_lib.h
     18  * @brief server-side KYC API
     19  * @author Christian Grothoff
     20  */
     21 #ifndef TALER_KYCLOGIC_LIB_H
     22 #define TALER_KYCLOGIC_LIB_H
     23 
     24 #include <taler/taler_kyclogic_plugin.h>
     25 
     26 
     27 /**
     28  * KYC measure that can be taken.
     29  */
     30 struct TALER_KYCLOGIC_Measure
     31 {
     32   /**
     33    * Name of the KYC measure.
     34    */
     35   char *measure_name;
     36 
     37   /**
     38    * Name of the KYC check.
     39    */
     40   char *check_name;
     41 
     42   /**
     43    * Name of the AML program.
     44    */
     45   char *prog_name;
     46 
     47   /**
     48    * Context for the check. Can be NULL.
     49    */
     50   json_t *context;
     51 
     52   /**
     53    * Can this measure be triggered voluntarily?
     54    */
     55   bool voluntary;
     56 };
     57 
     58 
     59 /**
     60  * Information about a KYC provider.
     61  */
     62 struct TALER_KYCLOGIC_KycProvider;
     63 
     64 
     65 /**
     66  * Abstract representation of a KYC check.
     67  */
     68 struct TALER_KYCLOGIC_KycCheck
     69 {
     70   /**
     71    * Human-readable name given to the KYC check.
     72    */
     73   char *check_name;
     74 
     75   /**
     76    * Human-readable description of the check in English.
     77    */
     78   char *description;
     79 
     80   /**
     81    * Optional translations of @e description, can be
     82    * NULL.
     83    */
     84   json_t *description_i18n;
     85 
     86   /**
     87    * Array of fields that the context must provide as
     88    * inputs for this check.
     89    */
     90   char **requires;
     91 
     92   /**
     93    * Name of an original measure to take as a fallback
     94    * in case the check fails.
     95    */
     96   char *fallback;
     97 
     98   /**
     99    * Array of outputs provided by the check. Names of the attributes provided
    100    * by the check for the AML program.  Either from the configuration or
    101    * obtained via the converter.
    102    */
    103   char **outputs;
    104 
    105   /**
    106    * Length of the @e requires array.
    107    */
    108   unsigned int num_requires;
    109 
    110   /**
    111    * Length of the @e outputs array.
    112    */
    113   unsigned int num_outputs;
    114 
    115   /**
    116    * Type of the KYC check.
    117    */
    118   enum TALER_KYCLOGIC_CheckType type;
    119 
    120   /**
    121    * Details depending on @e type.
    122    */
    123   union
    124   {
    125 
    126     /**
    127      * Fields present only if @e type is #TALER_KYCLOGIC_CT_FORM.
    128      */
    129     struct
    130     {
    131 
    132       /**
    133        * Name of the form to render.
    134        */
    135       char *name;
    136 
    137     } form;
    138 
    139     /**
    140      * Fields present only if @e type is TALER_KYCLOGIC_CT_LINK.
    141      */
    142     struct
    143     {
    144 
    145       /**
    146        * Provider used.
    147        */
    148       const struct TALER_KYCLOGIC_KycProvider *provider;
    149 
    150     } link;
    151 
    152   } details;
    153 
    154 };
    155 
    156 
    157 /**
    158  * Rule that triggers some measure(s).
    159  */
    160 struct TALER_KYCLOGIC_KycRule;
    161 
    162 /**
    163  * Set of rules that applies to an account.
    164  */
    165 struct TALER_KYCLOGIC_LegitimizationRuleSet;
    166 
    167 
    168 /**
    169  * Parse KYC trigger string value from a string
    170  * into enumeration value.
    171  *
    172  * @param trigger_s string to parse
    173  * @param[out] trigger set to the value found
    174  * @return #GNUNET_OK on success, #GNUNET_NO if option
    175  *         does not exist, #GNUNET_SYSERR if option is
    176  *         malformed
    177  */
    178 enum GNUNET_GenericReturnValue
    179 TALER_KYCLOGIC_kyc_trigger_from_string (
    180   const char *trigger_s,
    181   enum TALER_KYCLOGIC_KycTriggerEvent *trigger);
    182 
    183 
    184 /**
    185  * Initialize KYC subsystem. Loads the KYC configuration.
    186  *
    187  * @param cfg configuration to parse
    188  * @param cfg_fn configuration filename for AML helpers
    189  * @return #GNUNET_OK on success
    190  */
    191 enum GNUNET_GenericReturnValue
    192 TALER_KYCLOGIC_kyc_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
    193                          const char *cfg_fn);
    194 
    195 
    196 /**
    197  * Shut down the KYC subsystem.
    198  */
    199 void
    200 TALER_KYCLOGIC_kyc_done (void);
    201 
    202 
    203 /**
    204  * Return JSON array with amounts with thresholds that
    205  * may change KYC requirements for the wallet.
    206  *
    207  * @return JSON array, NULL if no limits apply
    208  */
    209 json_t *
    210 TALER_KYCLOGIC_get_wallet_thresholds (void);
    211 
    212 
    213 /**
    214  * Function called on each @a amount that was found to
    215  * be relevant for a KYC check.
    216  *
    217  * @param cls closure to allow the KYC module to
    218  *        total up amounts and evaluate rules
    219  * @param amount encountered transaction amount
    220  * @param date when was the amount encountered
    221  * @return #GNUNET_OK to continue to iterate,
    222  *         #GNUNET_NO to abort iteration
    223  *         #GNUNET_SYSERR on internal error (also abort itaration)
    224  */
    225 typedef enum GNUNET_GenericReturnValue
    226 (*TALER_KYCLOGIC_KycAmountCallback)(
    227   void *cls,
    228   const struct TALER_Amount *amount,
    229   struct GNUNET_TIME_Absolute date);
    230 
    231 
    232 /**
    233  * Function called to iterate over KYC-relevant
    234  * transaction amounts for a particular time range.
    235  * Called within a database transaction, so must
    236  * not start a new one.
    237  *
    238  * @param cls closure, identifies the event type and
    239  *        account to iterate over events for
    240  * @param limit maximum time-range for which events
    241  *        should be fetched (timestamp in the past)
    242  * @param cb function to call on each event found,
    243  *        events must be returned in reverse chronological
    244  *        order
    245  * @param cb_cls closure for @a cb
    246  * @return transaction status
    247  */
    248 typedef enum GNUNET_DB_QueryStatus
    249 (*TALER_KYCLOGIC_KycAmountIterator)(
    250   void *cls,
    251   struct GNUNET_TIME_Absolute limit,
    252   TALER_KYCLOGIC_KycAmountCallback cb,
    253   void *cb_cls);
    254 
    255 
    256 /**
    257  * Function called to iterate over KYC-relevant
    258  * transaction thresholds amounts.
    259  *
    260  * @param cls closure, identifies the event type and
    261  *        account to iterate over events for
    262  * @param threshold a relevant threshold amount
    263  */
    264 typedef void
    265 (*TALER_KYCLOGIC_KycThresholdIterator)(
    266   void *cls,
    267   const struct TALER_Amount *threshold);
    268 
    269 
    270 /**
    271  * Parse set of legitimization rules that applies to an account.
    272  *
    273  * @param jlrs JSON representation to parse
    274  * @return rule set, NULL if JSON is invalid
    275  */
    276 struct TALER_KYCLOGIC_LegitimizationRuleSet *
    277 TALER_KYCLOGIC_rules_parse (const json_t *jlrs);
    278 
    279 
    280 /**
    281  * Free set of legitimization rules.
    282  *
    283  * @param[in] lrs set of rules to free
    284  */
    285 void
    286 TALER_KYCLOGIC_rules_free (struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs);
    287 
    288 
    289 /**
    290  * Check if KYC is provided for a particular operation. Returns the set of
    291  * checks that still need to be satisfied.
    292  *
    293  * Called within a database transaction, so must
    294  * not start a new one.
    295  *
    296  * @param event what type of operation is triggering the
    297  *         test if KYC is required
    298  * @param lrs legitimization rules to apply;
    299  *         NULL to use default rules
    300  * @param ai callback offered to inquire about historic
    301  *         amounts involved in this type of operation
    302  *         at the given account
    303  * @param ai_cls closure for @a ai
    304  * @param[out] triggered_rule set to NULL if no rule
    305  *   is triggered, otherwise the rule with measures
    306  *   that must be satisfied (will be the highest
    307  *   applicable rule by threshold magnitude)
    308  * @param[out] next_threshold set to the next amount
    309  *   that may trigger a KYC check (note: only really
    310  *   useful for the wallet balance right now, as we
    311  *   cannot easily state the applicable timeframe)
    312  * @return transaction status
    313  */
    314 enum GNUNET_DB_QueryStatus
    315 TALER_KYCLOGIC_kyc_test_required (
    316   enum TALER_KYCLOGIC_KycTriggerEvent event,
    317   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs,
    318   TALER_KYCLOGIC_KycAmountIterator ai,
    319   void *ai_cls,
    320   const struct TALER_KYCLOGIC_KycRule **triggered_rule,
    321   struct TALER_Amount *next_threshold);
    322 
    323 
    324 /**
    325  * Return JSON array of AccountLimit objects with hard limits of this exchange
    326  * suitable for the "hard_limits" field of the "/keys" response.
    327  *
    328  * @return the JSON array of AccountLimit objects,
    329  *   empty array if there are no hard limits
    330  */
    331 json_t *
    332 TALER_KYCLOGIC_get_hard_limits (void);
    333 
    334 
    335 /**
    336  * Return JSON array of ZeroLimitedOperation objects with
    337  * operations for which this exchange has a limit
    338  * of zero, that means KYC is always required (or
    339  * the operation is categorically forbidden).
    340  *
    341  * @return the JSON array of ZeroLimitedOperation objects,
    342  *   empty array if there are no hard limits
    343  */
    344 json_t *
    345 TALER_KYCLOGIC_get_zero_limits (void);
    346 
    347 
    348 /**
    349  * Obtain the fallback measure to be run if @a prog_name fails
    350  *
    351  * @param prog_name name of an AML program
    352  * @return NULL if @a prog_name is unknown
    353  */
    354 const char *
    355 TALER_KYCLOGIC_get_aml_program_fallback (const char *prog_name);
    356 
    357 
    358 /**
    359  * Obtain set of all measures that
    360  * could be triggered at an amount of zero and that
    361  * thus might be requested before a client even
    362  * has performed any operation.
    363  *
    364  * @param lrs rule set to investigate, NULL for default
    365  * @param is_wallet #GNUNET_YES if this is for a wallet,
    366  *         #GNUNET_NO for account,
    367  *         #GNUNET_SYSERR for unknown (returns all rules)
    368  * @return LegitimizationMeasures, NULL if there are no
    369  *   zero measures
    370  */
    371 json_t *
    372 TALER_KYCLOGIC_zero_measures (
    373   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs,
    374   enum GNUNET_GenericReturnValue is_wallet);
    375 
    376 
    377 /**
    378  * Obtain set of all voluntary measures that
    379  * could be triggered by clients at will.
    380  *
    381  * @param lrs rule set to investigate, NULL for default
    382  * @return array of MeasureInformation, never NULL
    383  */
    384 json_t *
    385 TALER_KYCLOGIC_voluntary_measures (
    386   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs);
    387 
    388 
    389 /**
    390  * Get human-readable name of KYC rule.
    391  *
    392  * @param r rule to convert
    393  * @return name of the rule
    394  */
    395 const char *
    396 TALER_KYCLOGIC_rule2s (const struct TALER_KYCLOGIC_KycRule *r);
    397 
    398 
    399 /**
    400  * Convert KYC status to human-readable string.
    401  *
    402  * @param status status to convert
    403  * @return human-readable string
    404  */
    405 const char *
    406 TALER_KYCLOGIC_status2s (enum TALER_KYCLOGIC_KycStatus status);
    407 
    408 
    409 /**
    410  * Get priority of KYC rule.
    411  *
    412  * @param r rule to convert
    413  * @return priority of the rule
    414  */
    415 uint32_t
    416 TALER_KYCLOGIC_rule2priority (const struct TALER_KYCLOGIC_KycRule *r);
    417 
    418 
    419 /**
    420  * Iterate over all thresholds that are applicable to a particular type of @a
    421  * event under exposed global rules.
    422  *
    423  * @param event thresholds to look up
    424  * @param it function to call on each
    425  * @param it_cls closure for @a it
    426  */
    427 void
    428 TALER_KYCLOGIC_kyc_iterate_thresholds (
    429   enum TALER_KYCLOGIC_KycTriggerEvent event,
    430   TALER_KYCLOGIC_KycThresholdIterator it,
    431   void *it_cls);
    432 
    433 
    434 /**
    435  * Check if a given @a rule can be satisfied in principle.
    436  *
    437  * @param rule the rule to check if it is verboten
    438  * @return true if the check can be satisfied,
    439  *         false if the check can never be satisfied,
    440  */
    441 bool
    442 TALER_KYCLOGIC_is_satisfiable (
    443   const struct TALER_KYCLOGIC_KycRule *rule);
    444 
    445 
    446 /**
    447  * A KYC rule @a r has been triggered. Convert the resulting requirements into
    448  * JSON of type ``LegitimizationMeasures`` for the legitimization measures table.
    449  *
    450  * @param r a rule that was triggered
    451  * @return JSON serialization of the corresponding
    452  *   ``LegitimizationMeasures``, NULL on error
    453  */
    454 json_t *
    455 TALER_KYCLOGIC_rule_to_measures (
    456   const struct TALER_KYCLOGIC_KycRule *r);
    457 
    458 
    459 /**
    460  * Tuple with information about a KYC check to perform.  Note that it will
    461  * have references into the legitimization rule set provided to
    462  * #TALER_KYCLOGIC_requirements_to_check() and thus has a lifetime that
    463  * matches the legitimization rule set.
    464  *
    465  * FIXME(fdold, 2024-11-07): Consider not making this public,
    466  * instead use struct TALER_KYCLOGIC_Measure.
    467  */
    468 struct TALER_KYCLOGIC_KycCheckContext
    469 {
    470   /**
    471    * KYC check to perform.
    472    */
    473   const struct TALER_KYCLOGIC_KycCheck *check;
    474 
    475   /**
    476    * Context for the check. Can be NULL.
    477    */
    478   const json_t *context;
    479 
    480   /**
    481    * Name of the AML program.
    482    */
    483   char *prog_name;
    484 };
    485 
    486 
    487 /**
    488  * A KYC check @a kcc has been triggered. Convert the resulting singular
    489  * requirement (only a single check is possible, not multiple alternatives)
    490  * into JSON of type ``LegitimizationMeasures`` for the legitimization
    491  * measures table.
    492  *
    493  * @param kcc check that was triggered
    494  * @return JSON serialization of the corresponding
    495  *   ``LegitimizationMeasures``
    496  */
    497 json_t *
    498 TALER_KYCLOGIC_check_to_jmeasures (
    499   const struct TALER_KYCLOGIC_KycCheckContext *kcc);
    500 
    501 
    502 /**
    503  * Convert (internal) @a jrules to (public) @a jlimits.
    504  *
    505  * @param jrules a ``LegitimizationRuleSet`` with KYC rules;
    506  *     NULL to use default rules
    507  * @param is_wallet #GNUNET_YES if this is for a wallet,
    508  *         #GNUNET_NO for account,
    509  *         #GNUNET_SYSERR for unknown (returns all rules)
    510  * @return set to JSON array with public limits
    511  *   of type ``AccountLimit``
    512  */
    513 json_t *
    514 TALER_KYCLOGIC_rules_to_limits (const json_t *jrules,
    515                                 enum GNUNET_GenericReturnValue is_wallet);
    516 
    517 
    518 /**
    519  * Name of the KYC form used to affirm acceptance of the exchange's
    520  * terms of service.  This is the ``form`` of the requirement returned
    521  * by ``GET /kyc-info`` and the ``FORM_ID`` submitted to
    522  * ``POST /kyc-upload/$ID`` for a terms-of-service acceptance.
    523  */
    524 #define TALER_KYCLOGIC_TOS_ACCEPTANCE_FORM "accept-tos"
    525 
    526 
    527 /**
    528  * Check if @a jrules contains a (still active) rule that requires the
    529  * client to accept the exchange's terms of service, that is a rule one
    530  * of whose measures resolves to a KYC check of type
    531  * #TALER_KYCLOGIC_CT_FORM rendering the
    532  * #TALER_KYCLOGIC_TOS_ACCEPTANCE_FORM form.
    533  *
    534  * @param jrules a ``LegitimizationRuleSet`` with KYC rules;
    535  *     NULL to use default rules
    536  * @return true if terms-of-service acceptance is among the
    537  *     measures required by @a jrules
    538  */
    539 bool
    540 TALER_KYCLOGIC_rules_require_tos_acceptance (const json_t *jrules);
    541 
    542 
    543 /**
    544  * Parse the given @a jmeasures and return the measure
    545  * at the @a measure_index.
    546  *
    547  * @param jmeasures a LegitimizationMeasures object
    548  * @param measure_index an index into the measures
    549  * @param[out] check_name set to the name of the check
    550  * @param[out] prog_name set to the name of the program,
    551  *             possibly NULL for "SKIP" checks
    552  * @param[out] context set to the measure context
    553  *   (or NULL if there is no context)
    554  * @return #TALER_EC_NONE on success
    555  */
    556 enum TALER_ErrorCode
    557 TALER_KYCLOGIC_select_measure (
    558   const json_t *jmeasures,
    559   size_t measure_index,
    560   const char **check_name,
    561   const char **prog_name,
    562   const json_t **context);
    563 
    564 
    565 /**
    566  * Check if the form data matches the requirements
    567  * of the selected measure.
    568  *
    569  * @param jmeasures a LegitimizationMeasures object
    570  * @param measure_index an index into the measures
    571  * @param form_data form data submitted for the measure
    572  * @param[out] form_name set to the form name (on success)
    573  * @param[out] error_message set to error details
    574  * @return #TALER_EC_NONE if the form data matches the measure
    575  */
    576 enum TALER_ErrorCode
    577 TALER_KYCLOGIC_check_form (
    578   const json_t *jmeasures,
    579   size_t measure_index,
    580   const json_t *form_data,
    581   char **form_name,
    582   const char **error_message);
    583 
    584 
    585 /**
    586  * Convert MeasureInformation into the
    587  * KycRequirementInformation used by the client.
    588  *
    589  * @param check_name the prescribed check
    590  * @param context context to return, can be NULL
    591  * @param access_token access token for the measure
    592  * @param offset offset of the measure
    593  * @param legitimization_measure_row_id row in the legitimization_measures table
    594  * @return JSON object with matching KycRequirementInformation
    595  */
    596 json_t *
    597 TALER_KYCLOGIC_measure_to_requirement (
    598   const char *check_name,
    599   const json_t *context,
    600   const struct TALER_AccountAccessTokenP *access_token,
    601   size_t offset,
    602   uint64_t legitimization_measure_row_id);
    603 
    604 
    605 /**
    606  * Lookup measures from @a measures_spec in @a lrs and create JSON object with
    607  * the corresponding LegitimizationMeasures.
    608  *
    609  * @param lrs set of legitimization rules
    610  * @param measures_spec space-separated set of a measures to trigger from @a lrs; "+"-prefixed if AND-cominbation applies
    611  * @return JSON object of type LegitimizationMeasures
    612  */
    613 json_t *
    614 TALER_KYCLOGIC_get_jmeasures (
    615   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs,
    616   const char *measures_spec);
    617 
    618 /**
    619  * Lookup the provider for the given @a check_name.
    620  *
    621  * @param check_name check to lookup provider for
    622  * @return NULL on error (@a check_name unknown or
    623  *    not a check that has a provider)
    624  */
    625 const struct TALER_KYCLOGIC_KycProvider *
    626 TALER_KYCLOGIC_check_to_provider (const char *check_name);
    627 
    628 
    629 /**
    630  * Extract logic data from a KYC @a provider.
    631  *
    632  * @param provider provider to get logic data from
    633  * @param[out] plugin set to the KYC logic API
    634  * @param[out] pd set to the specific operation context
    635  * @param[out] provider_name set to the name
    636  *    of the KYC provider
    637  */
    638 void
    639 TALER_KYCLOGIC_provider_to_logic (
    640   const struct TALER_KYCLOGIC_KycProvider *provider,
    641   struct TALER_KYCLOGIC_Plugin **plugin,
    642   struct TALER_KYCLOGIC_ProviderDetails **pd,
    643   const char **provider_name);
    644 
    645 
    646 /**
    647  * Find default measure @a measure_name.
    648  *
    649  * @param measure_name name of measure to find
    650  * @param[out] kcc initialized with KYC check data
    651  *    for the default measure
    652  * @return #GNUNET_OK on success
    653  */
    654 enum GNUNET_GenericReturnValue
    655 TALER_KYCLOGIC_get_original_measure (
    656   const char *measure_name,
    657   struct TALER_KYCLOGIC_KycCheckContext *kcc);
    658 
    659 
    660 /**
    661  * Obtain the provider logic for a given set of @a lrs
    662  * and a specific @a kyc_rule from @a lrs that was
    663  * triggered and the chosen @a measure_name from the
    664  * list of measures of that @a kyc_rule.  Can also be
    665  * used to obtain the "current" check of a @a lrs if
    666  * no trigger has been hit.
    667  *
    668  * @param lrs rule set
    669  * @param kyc_rule rule that was triggered, NULL
    670  *   to merely lookup the measure without any trigger
    671  * @param measure_name selected measure,
    672  *   NULL to return the "new_check" set by the @a lrs
    673  * @param[out] kcc set to check to run;
    674  *   kcc->check will be NULL if the "skip" check is used
    675  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
    676  */
    677 enum GNUNET_GenericReturnValue
    678 TALER_KYCLOGIC_requirements_to_check (
    679   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs,
    680   const struct TALER_KYCLOGIC_KycRule *kyc_rule,
    681   const char *measure_name,
    682   struct TALER_KYCLOGIC_KycCheckContext *kcc);
    683 
    684 
    685 /**
    686  * Obtain the provider logic for a given @a name.
    687  *
    688  * @param name name of the logic or provider
    689  * @param[out] plugin set to the KYC logic API
    690  * @param[out] pd set to the specific operation context
    691  * @param[out] configuration_section set to the name of the KYC logic configuration section
    692  * @return #GNUNET_OK on success
    693  */
    694 enum GNUNET_GenericReturnValue
    695 TALER_KYCLOGIC_lookup_logic (
    696   const char *name,
    697   struct TALER_KYCLOGIC_Plugin **plugin,
    698   struct TALER_KYCLOGIC_ProviderDetails **pd,
    699   const char **configuration_section);
    700 
    701 
    702 /**
    703  * Return expiration time for the given @a lrs
    704  *
    705  * @param lrs legitimization rules to inspect
    706  * @return expiration time
    707  */
    708 struct GNUNET_TIME_Timestamp
    709 TALER_KYCLOGIC_rules_get_expiration (
    710   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs);
    711 
    712 
    713 /**
    714  * Return successor measure for the given @a lrs
    715  *
    716  * @param lrs legitimization rules to inspect
    717  * @return successor measure;
    718  *    NULL to fall back to default rules;
    719  *    pointer will be valid as long as @a lrs is valid
    720  */
    721 const struct TALER_KYCLOGIC_Measure *
    722 TALER_KYCLOGIC_rules_get_successor (
    723   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs);
    724 
    725 
    726 /**
    727  * Function called with the provider details and
    728  * associated plugin closures for matching logics.
    729  *
    730  * @param cls closure
    731  * @param pd provider details of a matching logic
    732  * @param plugin_cls closure of the plugin
    733  * @return #GNUNET_OK to continue to iterate
    734  */
    735 typedef enum GNUNET_GenericReturnValue
    736 (*TALER_KYCLOGIC_DetailsCallback)(
    737   void *cls,
    738   const struct TALER_KYCLOGIC_ProviderDetails *pd,
    739   void *plugin_cls);
    740 
    741 
    742 /**
    743  * Call @a cb for all logics with name @a logic_name,
    744  * providing the plugin closure and the @a pd configurations.
    745  * Obtain the provider logic for a given set of @a lrs
    746  * and a specific @a kyc_rule from @a lrs that was
    747  * triggered and the chosen @a measure_name from the
    748  * list of measures of that @a kyc_rule.
    749   *
    750  * @param logic_name name of the logic to match
    751  * @param cb function to call on matching results
    752  * @param cb_cls closure for @a cb
    753  */
    754 void
    755 TALER_KYCLOGIC_kyc_get_details (
    756   const char *logic_name,
    757   TALER_KYCLOGIC_DetailsCallback cb,
    758   void *cb_cls);
    759 
    760 
    761 /**
    762  * Return configuration data useful for the
    763  * /aml/$PUB/measures endpoint.
    764  *
    765  * @param[out] proots set to the root measures
    766  * @param[out] pprograms set to available AML programs
    767  * @param[out] pchecks set to available KYC checks
    768  * @param[out] pdefault_rules set to array of default KycRules
    769  */
    770 void
    771 TALER_KYCLOGIC_get_measure_configuration (
    772   json_t **proots,
    773   json_t **pprograms,
    774   json_t **pchecks,
    775   json_t **pdefault_rules);
    776 
    777 
    778 /**
    779  * Check if there is a measure triggered by the
    780  * KYC rule @a r that has a check name of "SKIP" and
    781  * thus should be immediately executed. If such a
    782  * measure exists, return it.
    783  *
    784  * @param r rule to check for instant measures
    785  * @return NULL if there is no instant measure
    786  */
    787 const struct TALER_KYCLOGIC_Measure *
    788 TALER_KYCLOGIC_rule_get_instant_measure (
    789   const struct TALER_KYCLOGIC_KycRule *r);
    790 
    791 
    792 /**
    793  * Check if there is a measure in @a lrs
    794  * that is included in @a measure_spec
    795  * and a SKIP measure, and thus should be immediately
    796  * executed.
    797  *
    798  * @param lrs legitimization rule set
    799  * @param measures_spec measures spec
    800  * @returns NULL if there is no instant measure
    801  */
    802 const struct TALER_KYCLOGIC_Measure *
    803 TALER_KYCLOGIC_get_instant_measure (
    804   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs,
    805   const char *measures_spec);
    806 
    807 
    808 /**
    809  * Return default legitimization rule set in JSON.
    810  *
    811  * @param for_wallet true to return only rules that apply to
    812  *         wallets, false to return only rules that apply to accounts
    813  * @return default legitimization rules
    814  */
    815 json_t *
    816 TALER_KYCLOGIC_get_default_legi_rules (bool for_wallet);
    817 
    818 /**
    819  * Check if there is a measure in @a lrs that is named @a measure.
    820  *
    821  * @param lrs legitimization rule set
    822  * @param measure_name measures spec
    823  * @returns NULL if not found
    824  */
    825 const struct TALER_KYCLOGIC_Measure *
    826 TALER_KYCLOGIC_get_measure (
    827   const struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs,
    828   const char *measure_name);
    829 
    830 
    831 /**
    832  * Convert a measure to JSON.
    833  *
    834  * @param m measure to convert to JSON
    835  * @returns JSON representation of the measure
    836  */
    837 json_t *
    838 TALER_KYCLOGIC_measure_to_jmeasures (
    839   const struct TALER_KYCLOGIC_Measure *m);
    840 
    841 
    842 /**
    843  * Handle to manage a running AML program.
    844  */
    845 struct TALER_KYCLOGIC_AmlProgramRunnerHandle;
    846 
    847 
    848 /**
    849  * Type of function called after AML program was run.
    850  *
    851  * @param cls closure
    852  * @param apr result of the AML program.
    853  */
    854 typedef void
    855 (*TALER_KYCLOGIC_AmlProgramResultCallback) (
    856   void *cls,
    857   const struct TALER_KYCLOGIC_AmlProgramResult *apr);
    858 
    859 
    860 /**
    861  * Type of function called to obtain an AML or KYC
    862  * history in JSON on-demand if needed.
    863  *
    864  * @param cls closure
    865  * @return AML or KYC history in JSON format, NULL on error
    866  */
    867 typedef json_t *
    868 (*TALER_KYCLOGIC_HistoryBuilderCallback) (void *cls);
    869 
    870 
    871 /**
    872  * Run AML program based on @a jmeasures using
    873  * the the given inputs.
    874  *
    875  * @param jmeasures current KYC/AML rules to apply;
    876  *           they determine also the AML program and
    877  *           provide the context
    878  * @param is_wallet true if this is for a wallet
    879  * @param measure_index which KYC measure yielded the
    880  *       @a attributes
    881  * @param current_attributes_cb function to get current KYC attributes
    882  * @param current_attributes_cb_cls closure for @a current_attributes_cb
    883  * @param current_rules_cb callback to get current KYC rules that apply to the account
    884  * @param current_rules_cb_cls closure for @a current_rules_cb
    885  * @param aml_history_cb callback to get the AML history of the account
    886  * @param aml_history_cb_cls closure for @a aml_history_cb
    887  * @param kyc_history_cb callback to get the KYC history of the account
    888  * @param kyc_history_cb_cls closure for @a aml_history_cb
    889  * @param timeout timeout for running the AML program;
    890  *  terminates the AML program and runs the fallback measure immediately
    891  *  once the timeout is reached; in this case, the result from the
    892  *  fallback measure is returned; the fallback measure is also granted
    893  *  the same amount of time (so after 2x @a timeout we will call @a aprc)
    894  * @param aprc function to call with the result
    895  * @param aprc_cls closure for @a aprc
    896  * @return NULL if @a jmeasures is invalid for the
    897  *   selected @a measure_index or @a attributes
    898  */
    899 struct TALER_KYCLOGIC_AmlProgramRunnerHandle *
    900 TALER_KYCLOGIC_run_aml_program (
    901   const json_t *jmeasures,
    902   bool is_wallet,
    903   unsigned int measure_index,
    904   TALER_KYCLOGIC_HistoryBuilderCallback current_attributes_cb,
    905   void *current_attributes_cb_cls,
    906   TALER_KYCLOGIC_HistoryBuilderCallback current_rules_cb,
    907   void *current_rules_cb_cls,
    908   TALER_KYCLOGIC_HistoryBuilderCallback aml_history_cb,
    909   void *aml_history_cb_cls,
    910   TALER_KYCLOGIC_HistoryBuilderCallback kyc_history_cb,
    911   void *kyc_history_cb_cls,
    912   struct GNUNET_TIME_Relative timeout,
    913   TALER_KYCLOGIC_AmlProgramResultCallback aprc,
    914   void *aprc_cls);
    915 
    916 
    917 /**
    918  * Run AML program @a prog_name with the given @a context.
    919  *
    920  * @param prog_name name of AML program to run
    921  * @param context context to run with
    922  * @param is_wallet true if this is for a wallet
    923  * @param current_attributes_cb function to get current KYC attributes
    924  * @param current_attributes_cb_cls closure for @a current_attributes_cb
    925  * @param current_rules_cb callback to get current KYC rules that apply to the account
    926  * @param current_rules_cb_cls closure for @a current_rules_cb
    927  * @param aml_history_cb callback to get the AML history of the account
    928  * @param aml_history_cb_cls closure for @a aml_history_cb
    929  * @param kyc_history_cb callback to get the KYC history of the account
    930  * @param kyc_history_cb_cls closure for @a aml_history_cb
    931  * @param timeout timeout for running the AML program;
    932  *  terminates the AML program and runs the fallback measure immediately
    933  *  once the timeout is reached; in this case, the result from the
    934  *  fallback measure is returned; the fallback measure is also granted
    935  *  the same amount of time (so after 2x @a timeout we will call @a aprc)
    936  * @param aprc function to call with the result
    937  * @param aprc_cls closure for @a aprc
    938  * @return NULL if @a jmeasures is invalid for the
    939  *   selected @a measure_index or @a attributes
    940  */
    941 struct TALER_KYCLOGIC_AmlProgramRunnerHandle *
    942 TALER_KYCLOGIC_run_aml_program2 (
    943   const char *prog_name,
    944   const json_t *context,
    945   bool is_wallet,
    946   TALER_KYCLOGIC_HistoryBuilderCallback current_attributes_cb,
    947   void *current_attributes_cb_cls,
    948   TALER_KYCLOGIC_HistoryBuilderCallback current_rules_cb,
    949   void *current_rules_cb_cls,
    950   TALER_KYCLOGIC_HistoryBuilderCallback aml_history_cb,
    951   void *aml_history_cb_cls,
    952   TALER_KYCLOGIC_HistoryBuilderCallback kyc_history_cb,
    953   void *kyc_history_cb_cls,
    954   struct GNUNET_TIME_Relative timeout,
    955   TALER_KYCLOGIC_AmlProgramResultCallback aprc,
    956   void *aprc_cls);
    957 
    958 
    959 /**
    960  * Run AML program specified by the given
    961  * measure.
    962  *
    963  * @param is_wallet true if this is for a wallet
    964  * @param measure measure with program name and context
    965  *         to run
    966  * @param current_attributes_cb function to get current KYC attributes
    967  * @param current_attributes_cb_cls closure for @a current_attributes_cb
    968  * @param current_rules_cb callback to get current KYC rules that apply to the account
    969  * @param current_rules_cb_cls closure for @a current_rules_cb
    970  * @param aml_history_cb callback to get the AML history of the account
    971  * @param aml_history_cb_cls closure for @a aml_history_cb
    972  * @param kyc_history_cb callback to get the KYC history of the account
    973  * @param kyc_history_cb_cls closure for @a aml_history_cb
    974  * @param timeout timeout for running the AML program;
    975  *  terminates the AML program and runs the fallback measure immediately
    976  *  once the timeout is reached; in this case, the result from the
    977  *  fallback measure is returned; the fallback measure is also granted
    978  *  the same amount of time (so after 2x @a timeout we will call @a aprc)
    979  * @param aprc function to call with the result
    980  * @param aprc_cls closure for @a aprc
    981  * @return NULL if @a jmeasures is invalid for the
    982  *   selected @a measure_index or @a attributes
    983  */
    984 struct TALER_KYCLOGIC_AmlProgramRunnerHandle *
    985 TALER_KYCLOGIC_run_aml_program3 (
    986   bool is_wallet,
    987   const struct TALER_KYCLOGIC_Measure *measure,
    988   TALER_KYCLOGIC_HistoryBuilderCallback current_attributes_cb,
    989   void *current_attributes_cb_cls,
    990   TALER_KYCLOGIC_HistoryBuilderCallback current_rules_cb,
    991   void *current_rules_cb_cls,
    992   TALER_KYCLOGIC_HistoryBuilderCallback aml_history_cb,
    993   void *aml_history_cb_cls,
    994   TALER_KYCLOGIC_HistoryBuilderCallback kyc_history_cb,
    995   void *kyc_history_cb_cls,
    996   struct GNUNET_TIME_Relative timeout,
    997   TALER_KYCLOGIC_AmlProgramResultCallback aprc,
    998   void *aprc_cls);
    999 
   1000 
   1001 /**
   1002  * Get the name of the AML program run by @a aprh.
   1003  * Can of course only be called while @a aprh is running.
   1004  *
   1005  * @param aprh handle to the AML program
   1006  * @return name of the AML program
   1007  */
   1008 const char *
   1009 TALER_KYCLOGIC_run_aml_program_get_name (
   1010   const struct TALER_KYCLOGIC_AmlProgramRunnerHandle *aprh);
   1011 
   1012 
   1013 /**
   1014  * Cancel running AML program.
   1015  *
   1016  * @param[in] aprh handle of program to cancel
   1017  */
   1018 void
   1019 TALER_KYCLOGIC_run_aml_program_cancel (
   1020   struct TALER_KYCLOGIC_AmlProgramRunnerHandle *aprh);
   1021 
   1022 
   1023 /**
   1024  * Handle to a sanction list evaluation helper process.
   1025  */
   1026 struct TALER_KYCLOGIC_SanctionRater;
   1027 
   1028 /**
   1029  * Function called with the result of a sanction evaluation.
   1030  *
   1031  * @param cls closure
   1032  * @param ec error code, #TALER_EC_NONE on success
   1033  * @param best_match identifies the sanction list entry with the best match
   1034  * @param rating likelihood of the match, from 0 (none) to 1 (perfect)
   1035  * @param confidence confidence in the evaluation, from 0 (none) to 1 (perfect)
   1036  */
   1037 typedef void
   1038 (*TALER_KYCLOGIC_SanctionResultCallback)(
   1039   void *cls,
   1040   enum TALER_ErrorCode ec,
   1041   const char *best_match,
   1042   double rating,
   1043   double confidence);
   1044 
   1045 
   1046 /**
   1047  * Launch sanction rating helper process
   1048  *
   1049  * @param binary program name
   1050  * @param argv argument to give to the process
   1051  * @return handle for sanction list rating
   1052  */
   1053 struct TALER_KYCLOGIC_SanctionRater *
   1054 TALER_KYCLOGIC_sanction_rater_start (
   1055   const char *binary,
   1056   char *const*argv);
   1057 
   1058 
   1059 /**
   1060  * KYC evaluation.
   1061  */
   1062 struct TALER_KYCLOGIC_EvaluationEntry;
   1063 
   1064 /**
   1065  * Evaluate KYC attributes against sacntions list using @a sr
   1066  *
   1067  * @param[in,out] sr santion list evaluator
   1068  * @param attributes KYC attributes to evaluate
   1069  * @param cb function to call with the results
   1070  * @param cb_cls closure
   1071  * @return NULL on error
   1072  */
   1073 struct TALER_KYCLOGIC_EvaluationEntry *
   1074 TALER_KYCLOGIC_sanction_rater_eval (
   1075   struct TALER_KYCLOGIC_SanctionRater *sr,
   1076   const json_t *attributes,
   1077   TALER_KYCLOGIC_SanctionResultCallback cb,
   1078   void *cb_cls);
   1079 
   1080 
   1081 /**
   1082  * Stop sanction rating helper process.
   1083  *
   1084  * @param[in] sr process to stop
   1085  */
   1086 void
   1087 TALER_KYCLOGIC_sanction_rater_stop (
   1088   struct TALER_KYCLOGIC_SanctionRater *sr);
   1089 
   1090 #endif