taler-exchange-kyc-kycaid-converter.sh (3091B)
1 #!/bin/bash 2 # This file is in the public domain. 3 # 4 # This code converts (some of) the JSON output from KYCAID into the GNU Taler 5 # specific KYC attribute data (again in JSON format). We may need to download 6 # and inline file data in the process, for authorization pass "-a" with the 7 # respective bearer token. 8 # 9 10 # Die if anything goes wrong. 11 set -eu 12 13 # Parse command-line options 14 while getopts ':a:' OPTION; do 15 case "$OPTION" in 16 a) 17 TOKEN="$OPTARG" 18 ;; 19 ?) 20 echo "Unrecognized command line option" 21 exit 1 22 ;; 23 esac 24 done 25 26 # First, extract everything from stdin. 27 J=$(jq '{"type":.type,"email":.email,"phone":.phone,"first_name":.first_name,"name-middle":.middle_name,"last_name":.last_name,"dob":.dob,"residence_country":.residence_country,"gender":.gender,"pep":.pep,"addresses":.addresses,"documents":.documents,"company_name":.company_name,"business_activity_id":.business_activity_id,"registration_country":.registration_country,"documents":.documents,"decline_reasons":.decline_reasons}') 28 29 # TODO: 30 # log_failure (json_object_get (j, "decline_reasons")); 31 32 TYPE=$(echo "$J" | jq -r '.type') 33 34 N=0 35 DOCS_RAW="" 36 DOCS_JSON="" 37 for ID in $(jq -r '.documents[]|select(.status=="valid")|.id') 38 do 39 TYPE=$(jq -r ".documents[]|select(.id==\"$ID\")|.type") 40 # EXPIRY=$(jq -r ".documents[]|select(.id==\"$ID\")|.expiry_date") 41 DOCUMENT_FILE=$(mktemp -t tmp.XXXXXXXXXX) 42 # Authorization: Token $TOKEN 43 DOCUMENT_URL="https://api.kycaid.com/documents/$ID" 44 if [ -z "${TOKEN:-}" ] 45 then 46 wget -q --output-document=- "$DOCUMENT_URL" \ 47 | gnunet-base32 > "${DOCUMENT_FILE}" 48 else 49 wget -q --output-document=- "$DOCUMENT_URL" \ 50 --header "Authorization: Token $TOKEN" \ 51 | gnunet-base32 > "${DOCUMENT_FILE}" 52 fi 53 DOCS_RAW="$DOCS_RAW --rawfile photo$N \"${DOCUMENT_FILE}\"" 54 if [ "$N" = 0 ] 55 then 56 DOCS_JSON="{\"type\":\"$TYPE\",\"image\":\$photo$N}" 57 else 58 DOCS_JSON="{\"type\":\"$TYPE\",\"image\":\$photo$N},$DOCS_JSON" 59 fi 60 N=$((N + 1)) 61 done 62 63 64 if [ "PERSON" = "${TYPE}" ] 65 then 66 67 # Next, combine some fields into larger values. 68 FULLNAME=$(echo "$J" | jq -r '[.first_name,.middle_name,.last_name]|join(" ")') 69 # STREET=$(echo $J | jq -r '[."street-1",."street-2"]|join(" ")') 70 # CITY=$(echo $J | jq -r '[.postcode,.city,."address-subdivision,.cc"]|join(" ")') 71 72 # Combine into final result for individual. 73 echo "$J" \ 74 | jq \ 75 --arg full_name "${FULLNAME}" \ 76 '{"FORM_ID":"kycaid-individual",$full_name,"birthdate":.dob,"pep":.pep,"phone":.phone,"email":.email,"residences":.residence_country}' \ 77 | jq \ 78 'del(..|select(.==null))' 79 80 else 81 # Combine into final result for business. 82 echo "$J" \ 83 | jq \ 84 "$DOCS_RAW" \ 85 '{"FORM_ID":"kycaid-business","company_name":.company_name,"phone":.phone,"email":.email,"registration_country":.registration_country,"documents":['${DOCS_JSON}']}' \ 86 | jq \ 87 'del(..|select(.==null))' 88 fi 89 90 exit 0