summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine A <>2024-02-28 16:41:41 +0100
committerAntoine A <>2024-02-28 16:41:41 +0100
commit1b9b41bd9e9690404f77c375eedd7130322f2feb (patch)
treefc782407d8f886d3a8ed506dd96ebce24de61e4f
parentcd70aaeaf604f5fda3a38686693430d4c0a7718d (diff)
downloaddeployment-1b9b41bd9e9690404f77c375eedd7130322f2feb.tar.gz
deployment-1b9b41bd9e9690404f77c375eedd7130322f2feb.tar.bz2
deployment-1b9b41bd9e9690404f77c375eedd7130322f2feb.zip
regio: automate exchange conversion setup
-rwxr-xr-xregional-currency/ask_questions.py57
-rwxr-xr-xregional-currency/setup-exchange.sh10
-rwxr-xr-xregional-currency/setup-libeufin.sh25
3 files changed, 83 insertions, 9 deletions
diff --git a/regional-currency/ask_questions.py b/regional-currency/ask_questions.py
index 200f779..ea266a2 100755
--- a/regional-currency/ask_questions.py
+++ b/regional-currency/ask_questions.py
@@ -4,12 +4,15 @@
import base64
import os
import subprocess
+import urllib.parse
import uuid
+import re
from typing import Callable, Dict, TypeVar
log = open("setup.log", "ab", buffering=0)
CONFIG_FILE = "config/user.conf"
-
+BIC_PATTERN = re.compile("[A-Z0-9]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?")
+IBAN_PATTERN = re.compile("[A-Z]{2}[0-9]{2}[A-Z0-9]{,28}")
def load_conf() -> Dict[str, str]:
"""Load user configuration file"""
@@ -139,13 +142,36 @@ def ask_str(name: str | None, msg: str, default: str | None = None) -> str:
"Prompt the user to configure a string"
return ask(name, msg, default)
+def ask_bic(name: str | None, msg: str, default: str | None = None) -> str:
+ "Prompt the user to configure a BIC"
+ def check_bic(raw: str) -> str | None:
+ raw = raw.translate({ord(i): None for i in ' -'})
+ if not BIC_PATTERN.fullmatch(raw):
+ print("Invalid BIC")
+ return None
+ else:
+ return raw
+
+ return ask(name, msg, default, check_bic)
+
+def ask_iban(name: str | None, msg: str, default: str | None = None) -> str:
+ "Prompt the user to configure a IBAN"
+ def check_iban(raw: str) -> str | None:
+ raw = raw.translate({ord(i): None for i in ' -'})
+ if not IBAN_PATTERN.fullmatch(raw):
+ print("Invalid IBAN") # Checksum check ?
+ return None
+ else:
+ return raw
+
+ return ask(name, msg, default, check_iban)
def ask_currency(name: str, msg: str, default: str | None = None) -> str:
"Prompt the user to configure a currency name"
def check_currency(currency: str) -> str | None:
currency = currency.upper()
- if not currency.isascii() or not currency.isalpha():
+ if not all([c.isascii() and c.isalpha() for c in currency]):
print("The currency name must be an ASCII alphabetic string")
elif len(currency) < 3 or 11 < len(currency):
print("The currency name had to be between 3 and 11 characters long")
@@ -192,9 +218,30 @@ ask_currency(
"1. Enter the name of the regional currency (e.g. 'NETZBON'): ",
"NETZBON",
)
-ask_currency(
- "FIAT_CURRENCY", "2. Enter the name of the fiat currency (e.g. 'CHF'): ", "CHF"
+do_conversion = ask_yes_no(
+ "DO_CONVERSION",
+ "2. Do you want setup regional currency conversion to fiat currency (Y/n): ",
+ True,
)
+if do_conversion:
+ ask_currency(
+ "FIAT_CURRENCY",
+ "2.1. Enter the name of the fiat currency (e.g. 'CHF'): ",
+ "CHF",
+ )
+ iban = ask_iban(
+ "FIAT_ACCOUNT_IBAN",
+ "2.2. Enter the IBAN of your fiat bank account (e.g. 'CH7789144474425692816'): ",
+ )
+ bic = ask_bic(
+ "FIAT_ACCOUNT_BIC",
+ "2.3. Enter the BIC of your fiat bank account (e.g. 'POFICHBEXXX'): ",
+ )
+ name = ask_str(
+ "FIAT_ACCOUNT_NAME", "2.4. Enter the legal name of your fiat bank account: "
+ )
+ params = urllib.parse.urlencode({"receiver-name": name})
+ add_conf("CONVERSION_PAYTO", f"payto://iban/{iban}/{bic}?{params}")
ask_str(
"BANK_NAME",
"3. Enter the human-readable name of the bank (e.g. 'Taler Bank'): ",
@@ -266,4 +313,4 @@ ask_str(
"BANK_ADMIN_PASSWORD",
"8. Enter the admin password for the bank (or press enter to autogenerate password): ",
str(uuid.uuid4()),
-)
+) \ No newline at end of file
diff --git a/regional-currency/setup-exchange.sh b/regional-currency/setup-exchange.sh
index 8618f07..e1de3c1 100755
--- a/regional-currency/setup-exchange.sh
+++ b/regional-currency/setup-exchange.sh
@@ -205,4 +205,14 @@ curl -sS --max-time 2 \
--retry 10 \
"${EXCHANGE_BASE_URL}"keys &>> setup.log
+if test ${DO_CONVERSION} == y; then
+ say "Conversion account setup..."
+
+ sudo -i -u taler-exchange-offline taler-exchange-offline \
+ enable-account \
+ "${CONVERSION_PAYTO}" \
+ conversion-url "${PROTO}://bank.$DOMAIN/conversion-info/" \
+ upload &>> setup.log
+fi
+
say "Exchange setup finished"
diff --git a/regional-currency/setup-libeufin.sh b/regional-currency/setup-libeufin.sh
index ba2bf5b..c857345 100755
--- a/regional-currency/setup-libeufin.sh
+++ b/regional-currency/setup-libeufin.sh
@@ -29,10 +29,19 @@ if test -z "${BANK_EXCHANGE_PASSWORD:-}"; then
exit 1
fi
-say "Configuring libeufin-nexus with ${FIAT_CURRENCY}..."
+if test ${DO_CONVERSION} == y; then
+ say "Configuring libeufin-nexus with ${FIAT_CURRENCY}..."
+
+ taler-config -s nexus-ebics -o CURRENCY \
+ -V $FIAT_CURRENCY -c /etc/libeufin/libeufin-nexus.conf
+ taler-config -s nexus-ebics -o IBAN \
+ -V $FIAT_ACCOUNT_IBAN -c /etc/libeufin/libeufin-nexus.conf
+ taler-config -s nexus-ebics -o BIC \
+ -V $FIAT_ACCOUNT_BIC -c /etc/libeufin/libeufin-nexus.conf
+ taler-config -s nexus-ebics -o NAME \
+ -V $FIAT_ACCOUNT_NAME -c /etc/libeufin/libeufin-nexus.conf
+fi
-taler-config -s nexus-ebics -o currency \
- -V $FIAT_CURRENCY -c /etc/libeufin/libeufin-nexus.conf
say "Configuring libeufin-bank with ${CURRENCY}..."
@@ -44,10 +53,15 @@ X_TALER_BANK_PAYTO_HOSTNAME=bank.${DOMAIN_NAME}
SUGGESTED_WITHDRAWAL_EXCHANGE=${PROTO}://exchange.${DOMAIN_NAME}
SERVE=tcp
PORT=${BANK_PORT}
+EOF
+
+if test ${DO_CONVERSION} == y; then
+ cat >>/etc/libeufin/libeufin-bank.conf <<EOF
ALLOW_CONVERSION=yes
FIAT_CURRENCY=${FIAT_CURRENCY}
ALLOW_EDIT_CASHOUT_PAYTO_URI=yes
EOF
+fi
if test -n "${TELESIGN_AUTH_TOKEN:-}"; then
cat >>/etc/libeufin/libeufin-bank.conf <<EOF
@@ -100,7 +114,9 @@ curl -sS --max-time 2 \
--retry 10 \
${PROTO}://bank.${DOMAIN_NAME}/config &>> setup.log
-say "Setting conversion figures..."
+if test ${DO_CONVERSION} == y; then
+say "Setting conversion rates to 1:1 ..."
+# TODO only set conversion rates if known have been set
curl -sS -u "admin:${BANK_ADMIN_PASSWORD}" \
-H 'Content-Type: application/json; charset=utf-8' \
${PROTO}://bank.${DOMAIN_NAME}/conversion-info/conversion-rate \
@@ -118,5 +134,6 @@ curl -sS -u "admin:${BANK_ADMIN_PASSWORD}" \
"cashout_min_amount": "${CURRENCY}:1"
}
EOF
+fi
say "LibEuFin setup finished" \ No newline at end of file