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-clear-continue (3905B)


      1 #!/bin/bash
      2 #
      3 #  This file is part of TALER
      4 #  Copyright (C) 2024, 2025 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             exit 0
     37             ;;
     38         c)
     39             # shellcheck disable=SC2034
     40             CONF="$OPTARG"
     41             ;;
     42         h)
     43             echo "This is a KYC measure program that clears a measure from the rule set and continues with another AML program, all controlled via the context."
     44             echo 'Supported options:'
     45             echo '  -a           -- show required attributes'
     46             # shellcheck disable=SC2016
     47             echo '  -c $CONF     -- set configuration'
     48             echo '  -h           -- print this help'
     49             echo '  -i           -- show required inputs'
     50             echo '  -r           -- show required context'
     51             echo '  -v           -- show version'
     52             echo '  -V           -- be verbose'
     53             exit 0
     54             ;;
     55         i)
     56             # Need context and current_rules.
     57             echo "context"
     58             echo "current_rules"
     59             echo "default_rules"
     60             exit 0
     61             ;;
     62         r)
     63             # Context for AML program to run next
     64             echo "next_context"
     65             # Binary name of AML program to run next
     66             echo "exec_name"
     67             # Which measure to clear?
     68             echo "clear_measure"
     69             exit 0
     70             ;;
     71         v)
     72             echo "$0 v0.0.0"
     73             exit 0
     74             ;;
     75         V)
     76             VERBOSE=1
     77             ;;
     78         ?)
     79         exit_fail "Unrecognized command line option"
     80         ;;
     81     esac
     82 done
     83 
     84 if [ 1 = "$VERBOSE" ]
     85 then
     86     echo "Running $0" 1>&2
     87 fi
     88 
     89 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput
     90 # for the full JSON with possible inputs.
     91 
     92 # First, extract inputs we need
     93 INPUTS=$(jq '{"current_rules":(.current_rules // .default_rules // error("neither current_rules nor default_rules provided")),"attributes":.attributes,"context":.context}')
     94 
     95 # Get current rules.
     96 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null')
     97 # Get context values.
     98 J_NEXT_CONTEXT=$(echo "$INPUTS" | jq '.context.next_context // {}')
     99 EXEC_NAME=$(echo "$INPUTS" | jq -r '.context.exec_name')
    100 CLEAR_MEASURE=$(echo "$INPUTS" | jq '.context.clear_measure // null')
    101 
    102 # Remove matching measure from current rules.
    103 J_NEW_RULES=$(echo "$CURRENT_RULES" | jq --argjson cm "$CLEAR_MEASURE" '(.rules[] |= if (.measures[0]==$cm) then del(.) else . end)')
    104 
    105 echo "Passing new rules ${J_NEW_RULES} to ${EXEC_NAME}." 1>&2
    106 
    107 # FIXME: we might want to restrict EXEC_NAME to binaries
    108 # with a certain prefix and/or even validate that it is
    109 # an AML program in some 'approved' list. Right now, an
    110 # AML officer (reasonably trusted...) could basically
    111 # run any binary on the server here...
    112 
    113 # Finally, pass the new rules as input to the AML program '$EXEC_NAME'.
    114 jq -n \
    115     --argjson nc "$J_NEXT_CONTEXT" \
    116     --argjson nr "$J_NEW_RULES" \
    117     '{"current_rules":$nr,"context":$nc}' \
    118     | exec "${EXEC_NAME}"