ansible-taler-exchange

Ansible playbook to deploy a production Taler Exchange
Log | Files | Refs | Submodules | README | LICENSE

taler-devtesting (3042B)


      1 #!/usr/bin/env python3
      2 
      3 import click
      4 import shlex
      5 import os
      6 import sys
      7 import subprocess
      8 import random
      9 
     10 
     11 def calculate_iban_check_digits(country_code, bban):
     12     numeric_iban_str = ""
     13     for char in bban + country_code + "00":
     14         if char.isdigit():
     15             numeric_iban_str += char
     16         elif "A" <= char.upper() <= "Z":
     17             numeric_iban_str += str(ord(char.upper()) - ord("A") + 10)
     18         else:
     19             raise ValueError(
     20                 f"Invalid character '{char}' in IBAN for checksum calculation."
     21             )
     22     remainder = int(numeric_iban_str) % 97
     23     check_digits_int = 98 - remainder
     24     return str(check_digits_int).zfill(2)
     25 
     26 
     27 def generate_random_swiss_iban():
     28     country_code = "CH"
     29     bban = "".join([str(random.randint(0, 9)) for _ in range(12 + 5)])
     30     check_digits = calculate_iban_check_digits(country_code, bban)
     31     return f"{country_code}{check_digits}{bban}"
     32 
     33 
     34 def generate_random_german_iban():
     35     country_code = "DE"
     36     bban = "".join([str(random.randint(0, 9)) for _ in range(18)])
     37     check_digits = calculate_iban_check_digits(country_code, bban)
     38     return f"{country_code}{check_digits}{bban}"
     39 
     40 
     41 @click.group()
     42 def cli():
     43     pass
     44 
     45 
     46 def get_nexus_currency():
     47     currency_out = subprocess.check_output(
     48         "libeufin-nexus config get nexus-ebics currency",
     49         shell=True,
     50         encoding="utf-8",
     51         stderr=subprocess.DEVNULL,
     52     )
     53     return currency_out.strip().upper()
     54 
     55 
     56 @cli.command()
     57 @click.option("--amount", required=True)
     58 @click.option("--subject", required=True)
     59 @click.option("--debitor-payto")
     60 def fake_incoming(amount, subject, debitor_payto):
     61     currency = get_nexus_currency()
     62 
     63     if debitor_payto is None:
     64         if currency == "CHF":
     65             iban = generate_random_swiss_iban()
     66         elif currency == "EUR":
     67             iban = generate_random_german_iban()
     68         else:
     69             raise Exception(f"unsupported currency {repr(currency)}")
     70         debitor_payto = f"payto://iban/{iban}?receiver-name=Tammy%20Tester"
     71 
     72     print(f"Faking incoming {currency} transaction", file=sys.stderr)
     73     print(f"Debitor Payto: ", debitor_payto, file=sys.stderr)
     74     subprocess.run(
     75         [
     76             "sudo",
     77             "-u",
     78             "libeufin-nexus",
     79             "libeufin-nexus",
     80             "testing",
     81             "fake-incoming",
     82             "--amount",
     83             amount,
     84             "--subject",
     85             subject,
     86             debitor_payto,
     87         ],
     88         check=True,
     89     )
     90 
     91 
     92 @cli.command()
     93 def geniban():
     94     currency = get_nexus_currency()
     95     if currency == "CHF":
     96         iban = generate_random_swiss_iban()
     97     elif currency == "EUR":
     98         iban = generate_random_german_iban()
     99     else:
    100         raise Exception(f"unsupported currency {repr(currency)}")
    101     print(iban)
    102 
    103 
    104 if __name__ == "__main__":
    105     orig_cmd = os.environ.get("SSH_ORIGINAL_COMMAND")
    106     if orig_cmd:
    107         print("Running command", repr(orig_cmd), file=sys.stderr)
    108         cmd = shlex.split(orig_cmd)
    109     else:
    110         cmd = sys.argv[1:]
    111     cli(cmd)