taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

exchange2.sh (4934B)


      1 #!/bin/bash
      2 # This file is part of GNU Taler
      3 # (C) 2026 Taler Systems S.A.
      4 #
      5 # GNU 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 # GNU 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 # GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # A second exchange beside the stack, so the wallet has something to choose
     17 # between.
     18 #
     19 # Usage: exchange2.sh start|stop|status
     20 #
     21 # The main stack has to be up already: this one has no bank of its own, it
     22 # registers an account at the stack's libeufin bank and uses it.  Everything
     23 # else (port, database, offline key, secmod sockets) is its own; see
     24 # sim-exchange2.conf.
     25 
     26 set -eu
     27 
     28 SIM_ROOT=${SIM_ROOT:-/opt/taler-sim}
     29 RUN=${SIM_RUN:-$SIM_ROOT/run/stack}
     30 RUN2=${SIM_RUN2:-$SIM_ROOT/run/exchange2}
     31 HERE="$(cd "$(dirname "$0")" && pwd)"
     32 CONF_NAME=sim-exchange2.conf
     33 DB=${SIM_DB2:-talersim2}
     34 ACCOUNT=exchange-account-sim2
     35 CURRENCY=${SIM_CURRENCY:-EUR}
     36 PORT=8091
     37 BANK_PORT=8082
     38 BANK_ADMIN=${SIM_BANK_ADMIN:-admin}
     39 BANK_ADMIN_PASS=${SIM_BANK_ADMIN_PASS:-secret-password}
     40 READY_TIMEOUT=${SIM_READY_TIMEOUT:-300}
     41 PIDFILE="$RUN2/exchange2.pid"
     42 
     43 die() { echo "exchange2.sh: $*" >&2; exit 1; }
     44 
     45 running_pid() {
     46     [ -s "$PIDFILE" ] || return 1
     47     local pid
     48     pid=$(cat "$PIDFILE")
     49     kill -0 "$pid" 2>/dev/null || return 1
     50     echo "$pid"
     51 }
     52 
     53 register_bank_account() {
     54     # is_taler_exchange makes libeufin expose the wire gateway API on it, which
     55     # is what wirewatch and the aggregator talk to.  A second call comes back
     56     # 409, which is fine.
     57     local code
     58     code=$(curl -s -o register-account.log -w '%{http_code}' \
     59         -u "$BANK_ADMIN:$BANK_ADMIN_PASS" \
     60         -H 'Content-Type: application/json' \
     61         -X POST "http://localhost:$BANK_PORT/accounts" \
     62         -d '{"username":"exchange2","password":"password2",
     63              "name":"Second Exchange","is_taler_exchange":true,
     64              "payto_uri":"payto://x-taler-bank/localhost/exchange2?receiver-name=Second+Exchange"}')
     65     case "$code" in
     66         200|201|204|409) echo "bank account exchange2: HTTP $code" >&2 ;;
     67         *) die "could not register the exchange2 bank account (HTTP $code): $(cat register-account.log)" ;;
     68     esac
     69 }
     70 
     71 start() {
     72     if running_pid > /dev/null; then
     73         echo "already running (pid $(running_pid))" >&2
     74         return 0
     75     fi
     76     curl -s -m 5 -o /dev/null "http://localhost:$BANK_PORT/config" ||
     77         die "the stack's bank is not up; start the stack first"
     78     rm -rf "$RUN2"
     79     mkdir -p "$RUN2/home"
     80     dropdb "$DB" 2>/dev/null || true
     81     createdb "$DB" || die "could not create the database $DB"
     82     cp "$HERE/$CONF_NAME" "$RUN2/$CONF_NAME"
     83     cd "$RUN2"
     84     register_bank_account
     85     # Exchange, wirewatch, aggregator, transfer: enough to withdraw from it and
     86     # to deposit into it, which is all the wallet needs from a second one.
     87     setsid taler-unified-setup.sh -W -c "$CONF_NAME" -u "$ACCOUNT" -e -w -g -t \
     88            > setup.stdout 2> setup.stderr < /dev/null &
     89     echo $! > "$PIDFILE"
     90     local pid waited
     91     pid=$(cat "$PIDFILE")
     92     waited=0
     93     while [ "$waited" -lt "$READY_TIMEOUT" ]; do
     94         if grep -q '^READY:' setup.stdout 2>/dev/null; then
     95             echo "exchange2 up after ${waited}s on port $PORT" >&2
     96             echo "READY"
     97             return 0
     98         fi
     99         if ! kill -0 "$pid" 2>/dev/null; then
    100             echo "--- setup.stderr (tail) ---" >&2
    101             tail -40 setup.stderr >&2 || true
    102             die "taler-unified-setup.sh died before exchange2 became ready"
    103         fi
    104         sleep 1
    105         waited=$((waited + 1))
    106     done
    107     echo "--- setup.stderr (tail) ---" >&2
    108     tail -40 setup.stderr >&2 || true
    109     die "timed out after ${READY_TIMEOUT}s waiting for exchange2"
    110 }
    111 
    112 stop() {
    113     if pid=$(running_pid); then
    114         kill -TERM "$pid"
    115         local waited=0
    116         while kill -0 "$pid" 2>/dev/null && [ "$waited" -lt 60 ]; do
    117             sleep 1
    118             waited=$((waited + 1))
    119         done
    120         kill -KILL "$pid" 2>/dev/null || true
    121         echo "stopped" >&2
    122     else
    123         echo "not running" >&2
    124     fi
    125     rm -f "$PIDFILE"
    126 }
    127 
    128 status() {
    129     if pid=$(running_pid); then
    130         echo "running (pid $pid)"
    131     else
    132         echo "not running"
    133     fi
    134     printf '  port %s: ' "$PORT"
    135     if curl -s -m 2 -o /dev/null "http://localhost:$PORT/config"; then
    136         echo "ok"
    137     else
    138         echo "DOWN"
    139     fi
    140 }
    141 
    142 case "${1:-}" in
    143     start) start ;;
    144     stop) stop ;;
    145     status) status ;;
    146     *) die "usage: $0 start|stop|status" ;;
    147 esac