All checks were successful
Deploy / build-deploy (push) Successful in 7m20s
The shadow run of 36a635f had Nostro's ReadCallback read a real
callback beside our own parser on 2026-11-25, for order CC-BCD155: the
same id, amount and reference, on the shop's own account. So the reader
takes over, and what this repo carried of bunq goes: the callback
parser, the payment-type table, the API client with its RSA signing,
`--pull-credits`, the mode that read the bank from the server with an
API key, and tools/register-bunq-webhook.sh.
The server now knows no bank. Catcrafts.Server-BankCallback.cpp is the
one unit that imports Nostro.Money: ReadBankCallback has a body read
and says what it comes to, CreditMethodFor says how a payment arrived.
Which bank it is is configuration: --banks= in project.cpp for what the
build can read, BANK_CALLBACK_BANK on the server for what arrives.
What changes for a running shop:
- The endpoint is configured with BANK_CALLBACK_PATH and
BANK_CALLBACK_BANK; BUNQ_WEBHOOK_PATH is no longer read. Without the
bank's name, or without TRANSFER_IBAN, the endpoint stays off and the
startup log says so: one that answered 200 and credited nothing would
look like working. Production's payments.env got both names before
this was pushed.
- Only a payment on TRANSFER_IBAN is credited. A bank posts to whatever
address any of its customers registered, so a callback about someone
else's account can be the bank's own. Until now a payment in a
callback was credited whichever account it was on.
- `via` says `internal` for a payment from another account at the bank
itself, where it said `bunq`, and `card` where it would have said
`MASTERCARD`. A kind without a word is still passed on as the bank
wrote it, now only where that is a plain word.
- Every callback leaves one line in the log, `bank callback: credited
...` or `bank callback: nothing credited: <why>`, with nothing of the
body in it beyond the credit.
- A line of the credits file may carry the bank's kind and the bank
(`"bank":"bunq","kind":"EBA_SCT/SCT"`) in place of `method`, put into
the ledger's words when the file is read. The pull script ships those
and takes the bank's name from nostrod, so it holds no table of a
bank's words either.
Also gone, with their last user: ParseAmountToMinor, PullCreditsInto
and the explicit -lcrypto (Crafter.Network names what it needs).
ShouldReadBankCallbacks replaces ShouldParseBunqPayments and
ShouldCompareCallbackReaders: what a callback comes to for each shape,
each method, the account and the sign, and that no note repeats
anything a body carried. ShouldReceiveBankCallbacks posts to the real
endpoint: an order settles from a callback, one from another account
quoting the same order does not pay for it, and a server without the
bank's name has no endpoint.
Built from scratch and run in a copy of this tree with crafter-build
6.4.1: the server and the wasm bundle build, and every suite passes but
ShouldServePostPages, which fails there for want of mirrored post media
and passed in CI on the commit before this. A dry run of the pull
script against nostrod gave 19 credits with their kinds.
Not tried: a real callback through the new handler, and `nostro
callbacks` against the registration that is in place, which was made
with the script this removes. deploy/README.md has the new setup.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
225 lines
10 KiB
Shell
Executable file
225 lines
10 KiB
Shell
Executable file
#!/bin/sh
|
|
# Ask nostrod what arrived on the shop's bank account, and hand the incoming
|
|
# credits to the server, which cannot read the bank itself.
|
|
#
|
|
# WHY THIS EXISTS, so nobody "simplifies" it back:
|
|
#
|
|
# A key that can read a bank account can as a rule also pay from it, so the
|
|
# public web host must never hold one. It never does: the server only ever
|
|
# reads a file of incoming credits, and this script is what fills that file
|
|
# from a trusted machine. The bank's callback adds to the same file within
|
|
# seconds of a payment; this is the backstop for a callback that got lost, and
|
|
# the one path that asks the bank with the bank's own authentication.
|
|
#
|
|
# This script holds no key either. The bank connection lives in nostrod
|
|
# (https://forgejo.catcrafts.net/Catcrafts/Nostro), which keeps it under a
|
|
# system user of its own and answers over the system bus: a program may LOOK at
|
|
# the accounts without a password, and nothing pays or changes anything without
|
|
# one. This script only looks. It also names no bank and knows no bank's words:
|
|
# it ships each payment's kind as the bank wrote it, with the name nostrod has
|
|
# for the bank, and the server puts that into the ledger's words.
|
|
#
|
|
# tools/pull-and-ship-credits.sh pull and ship
|
|
# tools/pull-and-ship-credits.sh --dry-run pull, print the credit lines
|
|
# that would be shipped, ship nothing
|
|
#
|
|
# Needs: nostrod with a connection to the account (`nostro connectors`),
|
|
# busctl and python3. Only TRANSFER_IBAN is read from the repo-root .env (or
|
|
# from the environment), to say which account; nothing else in .env reaches
|
|
# this script or the programs it starts.
|
|
#
|
|
# Environment:
|
|
# CREDITS_HOST ssh destination (default: hetzner)
|
|
# CREDITS_REMOTE the file the rail reads (default: the production path)
|
|
# CREDITS_DAYS how far back to ask (default: 90)
|
|
#
|
|
# WHY 90 DAYS: it is the longest TRANSFER_WINDOW_HOURS the server accepts, so no
|
|
# order can still be waiting for a credit older than that. Nothing accumulates
|
|
# here: nostrod keeps the account's whole history and answers for any window,
|
|
# so every run ships the complete window, and a machine that was off for a
|
|
# month leaves no hole.
|
|
#
|
|
# THE REMOTE FILE IS REPLACED, not added to. What the bank says is on the
|
|
# account in that window becomes the whole file, so a line the callback wrote
|
|
# for a payment that is really there is kept (the same payment, by the bank's
|
|
# id), and a line that never was a payment is gone at the next run.
|
|
#
|
|
# WHEN IT IS REFUSED WITH "needs your password": nostrod lets a program look
|
|
# without one only while you are logged in at this machine, in an active local
|
|
# session. From a timer that runs with nobody logged in, or over SSH, it would
|
|
# have to ask, and here it is told not to. deploy/README.md has the polkit
|
|
# rule that lets this user look without a session.
|
|
set -eu
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
DRY_RUN=0
|
|
case "${1:-}" in
|
|
--dry-run) DRY_RUN=1 ;;
|
|
"") ;;
|
|
*) echo "$0: unknown argument '$1' (the only one is --dry-run)" >&2; exit 2 ;;
|
|
esac
|
|
|
|
HOST="${CREDITS_HOST:-hetzner}"
|
|
REMOTE="${CREDITS_REMOTE:-/var/lib/catcrafts/orders.jsonl.transfer-credits.jsonl}"
|
|
DAYS="${CREDITS_DAYS:-90}"
|
|
|
|
for tool in busctl python3; do
|
|
command -v "$tool" >/dev/null 2>&1 || { echo "$0: $tool is not installed" >&2; exit 1; }
|
|
done
|
|
|
|
case "$DAYS" in
|
|
''|*[!0-9]*) echo "$0: CREDITS_DAYS='$DAYS' is not a whole number of days" >&2; exit 2 ;;
|
|
esac
|
|
[ "$DAYS" -ge 1 ] || { echo "$0: CREDITS_DAYS must be at least 1" >&2; exit 2; }
|
|
|
|
# Which account. Read in a subshell, without exporting: .env also holds the
|
|
# shipping, mail and hosting secrets, and none of them is this script's or its
|
|
# children's business.
|
|
IBAN="${TRANSFER_IBAN:-}"
|
|
if [ -z "$IBAN" ] && [ -f .env ]; then
|
|
# shellcheck disable=SC1091
|
|
IBAN=$(set +eu; . ./.env >/dev/null 2>&1; printf '%s' "${TRANSFER_IBAN:-}")
|
|
fi
|
|
[ -n "$IBAN" ] || { echo "$0: need TRANSFER_IBAN, in the environment or in .env" >&2; exit 1; }
|
|
# nostrod compares an IBAN exactly as the bank writes it: no spaces, upper case.
|
|
IBAN=$(printf '%s' "$IBAN" | tr -d ' \t' | tr '[:lower:]' '[:upper:]')
|
|
|
|
# mktemp makes these 0600. The raw answer is the account's transactions with
|
|
# names and IBANs in it, so it lives exactly as long as this run.
|
|
RAW=$(mktemp); ERR=$(mktemp); TMP=$(mktemp)
|
|
trap 'rm -f "$RAW" "$ERR" "$TMP"' EXIT
|
|
|
|
# One call to nostrod's published interface (packaging/dbus/
|
|
# net.catcrafts.Nostro1.xml in Nostro). Interactive authorization is OFF on
|
|
# purpose: nobody is there to answer a password dialog from a timer, and a
|
|
# dialog popping up hourly would train its reader to type a password without
|
|
# looking.
|
|
ask() {
|
|
busctl --system --json=short --timeout=300 --allow-interactive-authorization=no \
|
|
call net.catcrafts.Nostro1 /net/catcrafts/Nostro1 net.catcrafts.Nostro1 "$@"
|
|
}
|
|
|
|
refused() {
|
|
cat "$ERR" >&2
|
|
if grep -qi 'password' "$ERR"; then
|
|
echo "$0: nostrod only lets a program look without a password in your active" \
|
|
"local session. Run this while logged in at this machine, or see" \
|
|
"deploy/README.md for the polkit rule that lets the timer look." >&2
|
|
fi
|
|
echo "$0: could not read the account through nostrod; nothing was shipped and the" \
|
|
"remote file still holds what it did" >&2
|
|
exit 1
|
|
}
|
|
|
|
# Which bank the account is at, as nostrod names it. Accounts answers
|
|
# a(ssa(ssssx)) connectors: name, bank, accounts: name, BIC, IBAN, currency, balance
|
|
# and all that is taken from it is the bank of the connector that has this
|
|
# IBAN. The answer goes through a pipe and is never written down: it carries
|
|
# every account's balance.
|
|
BANK=$( { ask Accounts 2>"$ERR" || true; } | python3 -I -c '
|
|
import json, sys
|
|
want = sys.argv[1]
|
|
try:
|
|
reply = json.load(sys.stdin)
|
|
except ValueError:
|
|
sys.exit(0) # nostrod refused: the caller reads why from its stderr
|
|
if reply.get("type") != "a(ssa(ssssx))as":
|
|
sys.exit("pull-and-ship-credits: nostrod answered Accounts with type %r, not the "
|
|
"a(ssa(ssssx))as this script reads; nothing was shipped" % reply.get("type"))
|
|
for name, bank, accounts in reply["data"][0]:
|
|
for account in accounts:
|
|
if account[2].replace(" ", "").upper() == want:
|
|
print(bank)
|
|
sys.exit(0)
|
|
' "$IBAN" ) || exit 1
|
|
if [ -z "$BANK" ]; then
|
|
[ -s "$ERR" ] && refused
|
|
echo "$0: no account with the IBAN in TRANSFER_IBAN is connected in nostrod" \
|
|
"(\`nostro accounts\` lists them); nothing was shipped" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Times on nostrod's interface are microseconds since 1970 UTC.
|
|
FROM_US=$(( ($(date +%s) - DAYS * 86400) * 1000000 ))
|
|
# Far above anything 90 days of this account holds. The window is what bounds
|
|
# the answer; an answer that fills the limit is treated as cut short below.
|
|
LIMIT=100000
|
|
|
|
# Transactions asks the bank for what is new before it answers, so this is as
|
|
# fresh as a direct read:
|
|
# Transactions(account s, query a{sv}) -> (a(sssssxxss) transactions, as notes)
|
|
ask Transactions 'sa{sv}' "$IBAN" 2 from x "$FROM_US" limit t "$LIMIT" >"$RAW" 2>"$ERR" || refused
|
|
|
|
# nostrod's answer -> the credit lines the rail reads. INCOMING ONLY: outgoing
|
|
# lines are supplier payments and card spending, the matcher ignores negative
|
|
# amounts anyway, and sending them would put the business's outgoing payment
|
|
# history on a public-facing host for no settlement benefit at all.
|
|
#
|
|
# -I keeps python from importing anything out of this directory or the
|
|
# environment: this is the step that decides what counts as money received.
|
|
python3 -I - "$RAW" "$LIMIT" "$BANK" >"$TMP" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
raw_path, limit, bank = sys.argv[1], int(sys.argv[2]), sys.argv[3]
|
|
with open(raw_path, encoding="utf-8") as f:
|
|
reply = json.load(f)
|
|
|
|
# The signature is checked rather than assumed: the fields below are read by
|
|
# position, so an interface that grew or reordered one must stop this script,
|
|
# not shift a description into an amount.
|
|
if reply.get("type") != "a(sssssxxss)as":
|
|
sys.exit("pull-and-ship-credits: nostrod answered with type %r, not the "
|
|
"a(sssssxxss)as this script reads; nothing was shipped" % reply.get("type"))
|
|
transactions, notes = reply["data"]
|
|
for note in notes:
|
|
print("nostrod: " + note, file=sys.stderr)
|
|
if len(transactions) >= limit:
|
|
sys.exit("pull-and-ship-credits: nostrod returned %d transactions, which is the "
|
|
"limit asked for, so the answer may be cut short; nothing was shipped"
|
|
% len(transactions))
|
|
|
|
without_id = 0
|
|
for name, bic, iban, description, currency, amount, time, bank_id, kind in transactions:
|
|
# Only euro can pay a euro order, and only money coming in pays anything.
|
|
if currency != "EUR" or amount <= 0:
|
|
continue
|
|
# The id is what the server tells the same payment by when the callback
|
|
# delivered it too. A line without one could be counted twice, so it is
|
|
# left out and said aloud instead.
|
|
if not bank_id:
|
|
without_id += 1
|
|
continue
|
|
# How it arrived is not decided here. The line carries the bank's own
|
|
# kind for the payment ("<type>/<sub type>") and the bank it is of, and the
|
|
# server puts that into the ledger's words with the same table it uses for
|
|
# a payment the callback delivered (CreditMethodFor). So this script needs
|
|
# no table of any bank's words, and a payment gets one word either way.
|
|
print(json.dumps({"id": bank_id, "reference": description,
|
|
"amount_minor": amount, "bank": bank, "kind": kind},
|
|
ensure_ascii=False, separators=(",", ":")))
|
|
|
|
if without_id:
|
|
print("pull-and-ship-credits: left out %d incoming payment(s) nostrod stored "
|
|
"without the bank's id" % without_id, file=sys.stderr)
|
|
PY
|
|
|
|
COUNT=$(wc -l < "$TMP" | tr -d ' ')
|
|
|
|
# The read worked and nothing came in. That is a quiet quarter, not a failure,
|
|
# and there is nothing to add: leave the remote alone rather than blank the
|
|
# evidence the server settles from.
|
|
if [ "$COUNT" -eq 0 ]; then
|
|
echo "$0: no incoming credits in the last $DAYS days; the remote file is left as it is" >&2
|
|
exit 0
|
|
fi
|
|
|
|
if [ "$DRY_RUN" -eq 1 ]; then
|
|
cat "$TMP"
|
|
echo "$0: dry run, would ship $COUNT incoming credit(s) to $HOST:$REMOTE" >&2
|
|
exit 0
|
|
fi
|
|
|
|
ssh "$HOST" "cat > $REMOTE && chown catcrafts:catcrafts $REMOTE && chmod 600 $REMOTE" < "$TMP"
|
|
echo "$0: shipped $COUNT incoming credit(s) to $HOST:$REMOTE"
|