bank: Nostro reads the bank's callback, and the shop's bunq code is gone
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>
This commit is contained in:
Jorijn van der Graaf 2026-11-25 15:08:42 +01:00
commit 33ce8f45d8
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
24 changed files with 1218 additions and 2385 deletions

View file

@ -4,22 +4,20 @@
#
# WHY THIS EXISTS, so nobody "simplifies" it back:
#
# A bunq API key can INITIATE PAYMENTS (bunq has no read-only scope), so the
# 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.
# 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.
#
# Since 2026-11-21 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. That is the read-only scope bunq does not offer. This
# script only looks. Until then it ran this repo's own bunq client
# (`catcrafts-server --pull-credits`) with the key from .env.
#
# The bunq key is also IP-restricted at the KEY level, and bunq enforces that
# on every call, so the reading has to happen on a machine the key permits.
# That is now nostrod's concern: run this where nostrod has the connection.
# 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
@ -37,11 +35,14 @@
#
# 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 any more: 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 copy this script used to keep under
# ~/.cache/catcrafts did, because the old reader only ever saw the newest 50
# payments.)
# 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
@ -89,23 +90,17 @@ IBAN=$(printf '%s' "$IBAN" | tr -d ' \t' | tr '[:lower:]' '[:upper:]')
RAW=$(mktemp); ERR=$(mktemp); TMP=$(mktemp)
trap 'rm -f "$RAW" "$ERR" "$TMP"' EXIT
# 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
# 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 "$@"
}
# Transactions asks the bank for what is new before it answers, so this is as
# fresh as a direct read. The interface is nostrod's published one
# (packaging/dbus/net.catcrafts.Nostro1.xml in Nostro):
# Transactions(account s, query a{sv}) -> (a(sssssxxss) transactions, as notes)
# 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.
if ! busctl --system --json=short --timeout=300 --allow-interactive-authorization=no \
call net.catcrafts.Nostro1 /net/catcrafts/Nostro1 net.catcrafts.Nostro1 \
Transactions 'sa{sv}' "$IBAN" 2 from x "$FROM_US" limit t "$LIMIT" \
>"$RAW" 2>"$ERR"; then
refused() {
cat "$ERR" >&2
if grep -qi 'password' "$ERR"; then
echo "$0: nostrod only lets a program look without a password in your active" \
@ -115,8 +110,47 @@ if ! busctl --system --json=short --timeout=300 --allow-interactive-authorizatio
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
@ -124,11 +158,11 @@ fi
#
# -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" >"$TMP" <<'PY'
python3 -I - "$RAW" "$LIMIT" "$BANK" >"$TMP" <<'PY'
import json
import sys
raw_path, limit = sys.argv[1], int(sys.argv[2])
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)
@ -146,38 +180,24 @@ if len(transactions) >= limit:
"limit asked for, so the answer may be cut short; nothing was shipped"
% len(transactions))
# bunq's Payment.type -> the ledger's `via` vocabulary. THE SAME TABLE AS
# BunqMethodFor in server/implementations/Catcrafts.Server-Bunq.cpp, which
# labels the credits the webhook writes. Keep the two in step: a payment must
# get one label whichever path delivers it first. An unknown type goes through
# verbatim, never guessed, because "sepa" is final and a card payment is not.
METHODS = {
"EBA_SCT": "sepa",
"IDEAL": "ideal",
"FIS": "card",
"BUNQ": "bunq",
"SWIFT": "swift",
"EBA_SDD": "directdebit",
}
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 deduplicates on when the webhook delivers the
# same payment. A line without one could be counted twice, so it is left out
# and said aloud instead.
# 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
# nostrod's kind is "<type>/<sub_type>", e.g. "EBA_SCT/SCT".
payment_type = kind.split("/", 1)[0]
method = METHODS.get(payment_type, payment_type) or "bank"
# Same keys, same order and same escaping as the server's own writer
# (AppendCreditTo): text as UTF-8, control characters escaped.
# 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, "method": method},
"amount_minor": amount, "bank": bank, "kind": kind},
ensure_ascii=False, separators=(",", ":")))
if without_id: