exchange

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

exchange_do_lookup_kyc_requirement_by_row.sql (4517B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 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 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 General Public License for more details.
     12 --
     13 -- You should have received a copy of the GNU General Public License along with
     14 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 --
     16 -- @author: Christian Grothoff
     17 
     18 DROP FUNCTION IF EXISTS exchange_do_lookup_kyc_requirement_by_row;
     19 
     20 CREATE FUNCTION exchange_do_lookup_kyc_requirement_by_row(
     21   IN in_h_normalized_payto BYTEA,
     22   IN in_account_pub BYTEA, -- NULL allowed
     23   OUT out_account_pub BYTEA,  -- NULL allowed
     24   OUT out_reserve_pub BYTEA, -- NULL allowed
     25   OUT out_access_token BYTEA, -- NULL if 'out_not_found'
     26   OUT out_jrules JSONB, -- NULL allowed
     27   OUT out_is_wallet BOOLEAN, -- NULL allowed
     28   OUT out_not_found BOOLEAN,
     29   OUT out_rule_gen INT8, -- NULL allowed
     30   OUT out_aml_review BOOLEAN, -- NULL allowed
     31   OUT out_kyc_required BOOLEAN)
     32 LANGUAGE plpgsql
     33 AS $$
     34 DECLARE
     35   my_wtrec RECORD;
     36   my_lorec RECORD;
     37   my_ok BOOL;
     38 BEGIN
     39 
     40 -- Find the access token and the current account public key.
     41 SELECT access_token
     42       ,target_pub
     43       ,is_wallet
     44   INTO my_wtrec
     45   FROM kyc_targets
     46  WHERE h_normalized_payto=in_h_normalized_payto;
     47 
     48 IF NOT FOUND
     49 THEN
     50   -- RAISE WARNING 'kyc_target % not found', in_h_normalized_payto;
     51   -- Given that we don't recognize the normalized payto, there is no
     52   -- chance that we can match the incoming public key against anything,
     53   -- so this is a 404-case.
     54   out_not_found = TRUE;
     55   out_kyc_required = FALSE;
     56   RETURN;
     57 END IF;
     58 
     59 my_ok = (in_account_pub IS NOT NULL) AND
     60         (my_wtrec.target_pub = in_account_pub);
     61 
     62 IF ( (NOT my_ok) AND
     63      (in_account_pub IS NOT NULL) )
     64 THEN
     65   -- RAISE WARNING 'target_pub % does not match', in_account_pub;
     66   -- We were given an in_account_pub, but it did not match the
     67   -- target pub.
     68   -- Try to see if the in_account_pub appears in ANY reserve_in
     69   -- for this account instead.
     70   PERFORM
     71      FROM reserves_in
     72     WHERE reserve_pub=in_account_pub
     73       AND wire_source_h_payto IN
     74      (SELECT wire_target_h_payto
     75         FROM wire_targets
     76        WHERE h_normalized_payto=in_h_normalized_payto);
     77   IF FOUND
     78   THEN
     79     my_wtrec.target_pub = in_account_pub;
     80     my_ok = TRUE;
     81   END IF;
     82 END IF;
     83 
     84 IF (NOT my_ok AND
     85     ( (in_account_pub IS NOT NULL) OR
     86       (my_wtrec.target_pub IS NULL) ) )
     87 THEN
     88   -- We failed to find a matching public key for in_account_pub, and
     89   -- either the client provided a specific one to match OR
     90   -- we could not return any one that could even work, which means
     91   -- we are lacking the KYC auth or any even a triggered requirement.
     92   out_not_found = TRUE;
     93   out_kyc_required = FALSE;
     94   RETURN;
     95 END IF;
     96 
     97 -- We have found "something", which may or may not match the input
     98 -- public key (if there was one), but at least some KYC requirement
     99 -- exists.
    100 out_not_found = FALSE;
    101 
    102 out_is_wallet = my_wtrec.is_wallet;
    103 out_account_pub = my_wtrec.target_pub;
    104 out_access_token = my_wtrec.access_token;
    105 
    106 -- RAISE WARNING 'account_pub established, checking measures for %', out_access_token;
    107 
    108 -- Check if there are active measures for the account.
    109 PERFORM
    110   FROM legitimization_measures
    111  WHERE access_token=out_access_token
    112    AND NOT is_finished
    113  LIMIT 1;
    114 
    115 out_kyc_required = FOUND;
    116 
    117 -- Get currently applicable rules.
    118 -- Only one should ever be active per account.
    119 SELECT jnew_rules
    120       ,to_investigate
    121       ,outcome_serial_id
    122   INTO my_lorec
    123   FROM legitimization_outcomes
    124  WHERE h_payto=in_h_normalized_payto
    125    AND is_active;
    126 
    127 IF FOUND
    128 THEN
    129   out_jrules=my_lorec.jnew_rules;
    130   out_aml_review=my_lorec.to_investigate;
    131   out_rule_gen=my_lorec.outcome_serial_id;
    132 END IF;
    133 
    134 -- Check most recent reserve_in wire transfer, we also
    135 -- allow that reserve public key for authentication!
    136 -- Only needed for old wallets that don't pass
    137 -- in the account pub explicitly.
    138 SELECT reserve_pub
    139   INTO out_reserve_pub
    140   FROM reserves_in
    141  WHERE wire_source_h_payto
    142    IN (SELECT wt.wire_target_h_payto
    143          FROM wire_targets wt
    144         WHERE h_normalized_payto=in_h_normalized_payto)
    145  ORDER BY execution_date DESC, reserve_in_serial_id DESC
    146  LIMIT 1;
    147 
    148 END $$;