taler-exchange-helper-measure-update-from-context (3809B)
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 23 # Exit, with error message (hard failure) 24 function exit_fail() { 25 echo " FAIL: " "$@" >&2 26 EXIT_STATUS=1 27 exit "$EXIT_STATUS" 28 } 29 30 CONF="$HOME/.config/taler-exchange.conf" 31 VERBOSE=0 32 33 while getopts 'ac:hirvV' OPTION; 34 do 35 case "$OPTION" in 36 a) 37 # No attributes are required. 38 exit 0 39 ;; 40 c) 41 # shellcheck disable=SC2034 42 CONF="$OPTARG" 43 ;; 44 h) 45 echo "This is a KYC measure program that updates the current rules of an account based on values given in the context." 46 echo 'Supported options:' 47 echo ' -a -- show required attributes' 48 # shellcheck disable=SC2016 49 echo ' -c $CONF -- set configuration' 50 echo ' -h -- print this help' 51 echo ' -i -- show required inputs' 52 echo ' -r -- show required context' 53 echo ' -v -- show version' 54 echo ' -V -- be verbose' 55 exit 0 56 ;; 57 i) 58 # Need context and current_rules. 59 echo "context" 60 echo "current_rules" 61 echo "default_rules" 62 exit 0 63 ;; 64 r) 65 # Need new_rules, custom_measures, expiration time and successor measure. 66 echo "new_rules" 67 echo "custom_measures" 68 echo "expiration_time" 69 echo "successor_measure" 70 exit 0 71 ;; 72 v) 73 echo "$0 v0.0.0" 74 exit 0 75 ;; 76 V) 77 VERBOSE=1 78 ;; 79 ?) 80 exit_fail "Unrecognized command line option" 81 ;; 82 esac 83 done 84 85 if [ 1 = "$VERBOSE" ] 86 then 87 echo "Running $0" 1>&2 88 fi 89 90 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput 91 # for the full JSON with possible inputs. 92 93 # First, extract inputs we need 94 INPUTS=$(jq '{"context":.context,"current_rules":(.current_rules // .default_rules // error("neither current_rules nor default_rules provided"))}') 95 96 # Get context values. 97 EXPIRATION_TIME=$(echo "$INPUTS" | jq '.context.expiration_time // .current_rules.expiration_time // null') 98 SUCCESSOR_MEASURE=$(echo "$INPUTS" | jq '.context.successor_measure // .current_rules.successor_measure // null') 99 CUSTOM_MEASURES=$(echo "$INPUTS" | jq '.context.custom_measures // null') 100 NEW_RULES=$(echo "$INPUTS" | jq '.context.new_rules // null') 101 102 # Finally, output the new rules. 103 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlOutcome 104 # for the required output format. 105 106 echo "$INPUTS" \ 107 | jq \ 108 --argjson et "$EXPIRATION_TIME" \ 109 --argjson sm "$SUCCESSOR_MEASURE" \ 110 --argjson cm "$CUSTOM_MEASURES" \ 111 --argjson nr "$NEW_RULES" \ 112 '.current_rules+{"new_rules":(.current_rules.new_rules+{"expiration_time":$et,"successor_measure":$sm,"rules":(.current_rules.new_rules.rules+$nr),"custom_measures":({}+.current_rules.custom_measures+$cm)})}|del(..|nulls)' 113 114 exit 0