exchange

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

taler-exchange-helper-measure-tops-postal-check (5354B)


      1 #!/bin/bash
      2 #
      3 #  This file is part of TALER
      4 #  Copyright (C) 2024 Taler Systems SA
      5 #
      6 #  TALER is free software; you can redistribute it and/or modify it under the
      7 #  terms of the GNU General Public License as published by the Free Software
      8 #  Foundation; either version 3, or (at your option) any later version.
      9 #
     10 #  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     11 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     12 #  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     13 #
     14 #  You should have received a copy of the GNU General Public License along with
     15 #  TALER; see the file COPYING.  If not, If not, see <http://www.gnu.org/license>
     16 #
     17 
     18 # Hard error reporting on.
     19 set -eu
     20 
     21 
     22 # Exit, with error message (hard failure)
     23 function exit_fail() {
     24     echo " FAIL: " "$@" >&2
     25     EXIT_STATUS=1
     26     exit "$EXIT_STATUS"
     27 }
     28 
     29 CONF="$HOME/.config/taler-exchange.conf"
     30 VERBOSE=0
     31 
     32 while getopts 'ac:hirvV' OPTION;
     33 do
     34     case "$OPTION" in
     35         a)
     36             echo "CONTACT_NAME"
     37             echo "ADDRESS_LINES"
     38             echo "ADDRESS_COUNTRY"
     39             exit 0
     40             ;;
     41         c)
     42             # shellcheck disable=SC2034
     43             CONF="$OPTARG"
     44             ;;
     45         h)
     46             echo "This is a KYC measure program that lifts restrictions on withdraw and P2P transfers after a (domestic) postal address was confirmed via PIN/TAN letter. Expiration rules are set based on the context. It should not be confused with the taler-exchange-helper-measure-tops-address-check, which also validates a postal address but then triggers an investigation by AML staff to fully open an account."
     47             echo 'Supported options:'
     48             echo '  -a           -- show required attributes'
     49             # shellcheck disable=SC2016
     50             echo '  -c $CONF     -- set configuration'
     51             echo '  -h           -- print this help'
     52             echo '  -i           -- show required inputs'
     53             echo '  -r           -- show required context'
     54             echo '  -v           -- show version'
     55             echo '  -V           -- be verbose'
     56             exit 0
     57             ;;
     58         i)
     59             # Need attributes, context and current_rules.
     60             echo "attributes"
     61             echo "context"
     62             echo "current_rules"
     63             echo "default_rules"
     64             exit 0
     65             ;;
     66         r)
     67             exit 0
     68             ;;
     69         v)
     70             echo "$0 v0.0.3"
     71             exit 0
     72             ;;
     73         V)
     74             VERBOSE=1
     75             ;;
     76         ?)
     77         exit_fail "Unrecognized command line option"
     78         ;;
     79     esac
     80 done
     81 
     82 if [ 1 = "$VERBOSE" ]
     83 then
     84     echo "Running $0" 1>&2
     85 fi
     86 
     87 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput
     88 # for the full JSON with possible inputs.
     89 
     90 # First, extract inputs we need
     91 INPUTS=$(jq '{"current_rules":(.current_rules // .default_rules // error("neither current_rules nor default_rules provided")),"attributes":.attributes,"context":.context}')
     92 
     93 # Get country number.
     94 COUNTRY=$(echo "$INPUTS" | jq -r '.attributes.ADDRESS_COUNTRY // null')
     95 # Get current rules.
     96 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null')
     97 # Get context values.
     98 # How long the rules we emit are valid.  This must NOT be derived from
     99 # .attributes.expires: that is the challenger's address validation lifetime,
    100 # and a validation that has already aged out yields a rule set which expired
    101 # before it was written.  The exchange applies such a set, immediately falls
    102 # back to the default rules, and asks for this very same check again.
    103 VALIDITY_YEARS=$(echo "$INPUTS" | jq -r '.context.validity_years // 5')
    104 EXPIRATION_STAMP=$((VALIDITY_YEARS * 365 * 24 * 60 * 60 + $(date +%s)))
    105 EXPIRATION_TIME=$(echo "$INPUTS" | jq --argjson es "$EXPIRATION_STAMP" '.context.expiration_time // {"t_s":$es}')
    106 SUCCESSOR_MEASURE=$(echo "$INPUTS" | jq '.context.successor_measure // .current_rules.successor_measure // null')
    107 CUSTOM_MEASURES=$(echo "$INPUTS" | jq '.context.custom_measures // null')
    108 
    109 # Validate country
    110 if eval echo "$COUNTRY" | grep -E -e "${EXCHANGE_AML_PROGRAM_TOPS_POSTAL_CHECK_COUNTRY_REGEX}" > /dev/null
    111 then
    112     # Valid country
    113     # Remove limitation from current rules.
    114     NEW_RULES=$(echo "$CURRENT_RULES" | jq 'del(.rules[] | select ((.rule_name=="p2p-domestic-identification-requirement") or (.rule_name=="withdraw-limit-low") ))')
    115     TO_INVESTIGATE="false"
    116 else
    117     # Invalid country.  Repeating the check cannot change the outcome, so hand
    118     # the account to an AML officer.  Echoing the rules back unchanged would
    119     # leave the rule that asked for the address in place and the exchange would
    120     # request the very same check again, forever.
    121     echo "Country ${COUNTRY} invalid." 1>&2
    122     echo "$INPUTS" | taler-exchange-helper-measure-inform-investigate
    123     exit $?
    124 fi
    125 
    126 # Finally, output the new rules.
    127 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlOutcome
    128 # for the required output format.
    129 exec jq -n \
    130     --argjson et "$EXPIRATION_TIME" \
    131     --argjson sm "$SUCCESSOR_MEASURE" \
    132     --argjson cm "$CUSTOM_MEASURES" \
    133     --argjson nr "$NEW_RULES" \
    134     --argjson inv "$TO_INVESTIGATE" \
    135     '{"new_rules":($nr+{"expiration_time":$et,"successor_measure":$sm,"custom_measures":({}+$nr.custom_measures+$cm)}),"to_investigate":$inv}|del(..|nulls)'