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

@ -142,8 +142,8 @@ export namespace Catcrafts::Server {
//
// Bank-side donations and expenses come from an aggregates file written
// by the owner's own tooling, off this box — no bank credential and no
// bank callback exists here (the bunq mutation callback was retired
// 2026-08-17; its code is in git history). Aggregates by construction:
// bank callback feeds it (the callback that once kept these numbers live
// was retired 2026-08-17; its code is in git history). Aggregates by construction:
// category totals and an as-of date are all the file can carry, which is
// the page's privacy design.
@ -425,15 +425,6 @@ export namespace Catcrafts::Server {
}
};
// Exact decimal-string-to-minor-units parser for amounts that arrive as
// strings ("614.00" -> 61400), which is how every external source quotes
// them. Rejects anything that is not a plain non-negative decimal with at
// most two fraction digits — no floats touch money on the way in, and a
// sign is refused here so it cannot quietly halve a total (see
// ParseSignedAmountToMinor, which handles the one case where the sign is
// meaningful). Exported for the self-test.
std::optional<std::int64_t> ParseAmountToMinor(std::string_view s);
// One chain the EURC rail watches. Every field is configuration because
// every field is a fact about the world rather than about this shop:
// Circle deploys to a new chain, an RPC endpoint moves, a token is
@ -534,166 +525,115 @@ export namespace Catcrafts::Server {
TransferMatch MatchCredits(std::span<const BankCredit> credits,
std::string_view reference);
// Where a transfer rail gets its incoming credits. One implementation talks
// to the bank; the suites use a file-backed one so the whole rail — payment
// instructions, matching, settlement, the window — runs with no network.
// Where a transfer rail gets its incoming credits. The one implementation
// reads a file (MakeFileCreditSource) that is filled from outside the rail:
// by the bank's callback, by the periodic pull, and in the suites by the
// suite itself, so the whole rail — payment instructions, matching,
// settlement, the window — runs with no network and no credential.
class CreditSource {
public:
virtual ~CreditSource() = default;
// nullopt = the bank could not be reached. NEVER an empty vector for
// that case: "no credits yet" and "cannot ask" must not look alike, or
// an outage would read as a shop full of unpaid orders.
// nullopt = the credits could not be read. NEVER an empty vector for
// that case: "no credits yet" and "cannot tell" must not look alike,
// or a failure would read as a shop full of unpaid orders.
virtual std::optional<std::vector<BankCredit>> Recent() = 0;
virtual std::string_view Name() const = 0;
};
// The rail itself. Takes its credit source so the bank is swappable — a
// deliberate hedge, since the account that reconciles the shop is also the
// shop's bank account, and replacing one adapter must not mean rewriting
// the rail.
// The rail itself. Takes its credit source, so that where the credits
// come from can change without the rail being rewritten: it knows nothing
// of any bank.
std::unique_ptr<PaymentRail> MakeTransferRail(const RailConfig& config,
std::unique_ptr<CreditSource> credits);
// A CreditSource reading newline-delimited JSON from a file, one credit per
// line: {"id":…,"reference":…,"amount_minor":…,"method":…}. This is how the
// suites drive real settlement, and how an operator can settle a transfer
// by hand without touching the ledger. A missing file is an EMPTY list, not
// a failure: no transfers yet is a normal state.
// line. Two shapes are read:
//
// {"id":…,"reference":…,"amount_minor":…,"method":"sepa"}
// {"id":…,"reference":…,"amount_minor":…,"bank":"bunq","kind":"EBA_SCT/SCT"}
//
// The first names how the money arrived in the ledger's own words: what
// the callback endpoint writes, what a suite writes, and what an operator
// writes to settle a transfer by hand without touching the ledger. The
// second carries the bank's own classification as the bank wrote it, with
// the bank it came from, and the method is worked out here
// (CreditMethodFor): that is what the hourly pull ships, so the script
// that makes it needs no table of any bank's words. A line with neither is
// taken for a plain transfer. A missing file is an EMPTY list, not a
// failure: no transfers yet is a normal state.
std::unique_ptr<CreditSource> MakeFileCreditSource(std::filesystem::path path);
// ── the bunq credit source ────────────────────────────────────────
struct BunqConfig {
std::string apiKey; // BUNQ_API_KEY; can MOVE MONEY, see below
std::filesystem::path statePath; // keypair + tokens, 0600
// Which account, when the key can see more than one. Compared to the
// account's IBAN ignoring spacing and case; without it a key that sees
// several active accounts is a refusal rather than a guess, because
// guessing means reconciling the shop against its savings.
std::string iban;
// Registered with bunq ONCE, at device-server time. bunq has no
// read-only key scope, so this is the only thing standing between a
// leaked key and someone spending the balance. "*" works and is
// announced loudly; an explicit egress address is what should be used.
std::string permittedIps;
int count = 50; // payments per read
};
// nullptr when no key is configured. NOTE the deployment rule that goes
// with this: a bunq key can initiate payments and bunq offers no read-only
// scope, so the project's standing policy is that it does NOT live on the
// internet-facing host. Run `--pull-credits` on a trusted machine and ship
// the credits file to the server, which then holds no credential at all.
std::unique_ptr<CreditSource> MakeBunqCreditSource(const BunqConfig& config);
// bunq's payment list -> credits. Exported for the self-test: the money
// decisions downstream are only as good as this decoding, and the HTTP
// around it is thin. Same reasoning as ParseEthCallUint.
std::vector<BankCredit> ParseBunqPayments(std::string_view json);
// Amount strings as bunq quotes them, INCLUDING the leading minus an
// outgoing payment carries. Exported because the sign is the difference
// between income and a refund, and getting it wrong would let a refund pay
// for the order it reversed.
std::optional<std::int64_t> ParseSignedAmountToMinor(std::string_view s);
// bunq's Payment.type -> the ledger's `via` vocabulary. Exported so the
// suite can pin the mapping that decides whether an order is safe to ship:
// "sepa" is final, "card" can be reversed for months.
std::string_view BunqMethodFor(std::string_view paymentType);
// ── the bunq callback (webhook) ───────────────────────────────────
// ── the bank's callback (webhook) ─────────────────────────────────
//
// bunq pushes a notification when the account changes, which is the only
// way this shop learns about money when the API key's IP allowlist forbids
// the web host from asking. Shape:
//
// {"NotificationUrl":{"category":"MUTATION",
// "object":{"Payment":{…the same Payment object…}}}}
// The bank posts a notification to an address of ours when the account
// changes, which is how a transfer settles within seconds: this host
// holds no bank credential and cannot ask. Nothing in this server knows
// how any bank writes one. The body is read by Nostro's ReadCallback (the
// module Nostro.Money of https://forgejo.catcrafts.net/Catcrafts/Nostro),
// told which bank by configuration, and comes back in no bank's terms.
//
// SECURITY, stated plainly because the design depends on understanding it:
// **bunq does not sign these.** Verified against doc.bunq.com — no HMAC, no
// server signature; certificate pinning authenticates US to bunq, not bunq
// to us. So the body is an unauthenticated claim that money arrived, and
// the only things standing between it and the ledger are transport-level:
// a source-IP allowlist for bunq's published range (185.40.108.0/22, which
// bunq warns may change), a secret path segment, and the fact that nothing
// ships without a human. Treat a callback as evidence exactly as strong as
// those controls, and keep a periodic --pull-credits as the backstop:
// bunq retries roughly six times and then drops the notification forever,
// so a backend that was down during a deploy loses that payment silently.
std::optional<BankCredit> ParseBunqCallback(std::string_view json);
// a bank need not sign a callback, and bunq does not. The body is then an
// unauthenticated claim that money arrived, and what stands between it and
// the ledger is: a source-IP allowlist in the web server for the range the
// bank publishes, a secret path segment, the check that the payment is on
// the shop's OWN account, and the fact that nothing ships without a human.
// The account check is not a formality: a bank posts to whatever address
// any of its customers registered, so someone with an account there could
// register ours for an account of their own and pay themselves. Treat a
// callback as evidence exactly as strong as those controls, and keep the
// periodic pull (tools/pull-and-ship-credits.sh) as the backstop: a bank
// retries a failed callback a few times and then drops it for good, so a
// backend that was down during a deploy loses that callback silently.
// What one callback came to.
struct BankCallbackResult {
// The credit to record: set only for money coming IN, on the shop's
// own account, with the bank's id for it.
std::optional<BankCredit> credit;
// Without a credit: why there is none. In this server's words or the
// reader's own, with nothing of the body in them, so it can be logged:
// a body holds the payer's name and IBAN, and may repeat the address
// it was posted to, which is the endpoint's secret.
std::string note;
};
// Reads what the bank a Nostro connector calls `bank` ("bunq") posted.
// `ownIban` is the account the shop is paid into, compared ignoring
// spacing and case; with none, nothing is credited. Exported for the
// suite, for the reason ParseEthCallUint is: this is where a stranger's
// bytes become a claim on an order. Does not throw.
BankCallbackResult ReadBankCallback(std::string_view bank, std::string_view body,
std::string_view ownIban);
// How a payment arrived, in the ledger's `via` words, from the bank's own
// classification of it (`kind`, as the bank wrote it: bunq's
// "EBA_SCT/SCT"). The words are "sepa" (a credit transfer), "directdebit",
// "card", "ideal", "swift", and "internal" for a payment between two
// accounts at the bank itself. They are what tells an operator whether an
// order is safe to ship: a credit transfer is final, a card payment can be
// reversed for months. A kind the bank's table has no word for is NOT
// guessed at: it comes back as the bank's own word for it where that is a
// plain word, and as "bank" otherwise. The table is Nostro's
// (PaymentMethodOf), so a payment gets one word whether the callback
// delivered it or the pull did. Does not throw.
std::string CreditMethodFor(std::string_view bank, std::string_view kind);
// Append one credit to the file the transfer rail reads, unless an entry
// with the same id is already there. Returns false on a write failure or a
// duplicate — the caller answers the webhook 200 either way, because a
// duplicate is a SUCCESS from bunq's point of view and retrying it would
// achieve nothing.
// duplicate — the caller answers the callback 200 either way, because a
// duplicate is a SUCCESS from the bank's point of view and retrying it
// would achieve nothing.
bool AppendCreditTo(const std::filesystem::path& creditsPath,
const BankCredit& credit);
// Enable the bunq callback endpoint. Both arguments are required and the
// secret must be at least 24 characters, or the endpoint stays off — see
// ParseBunqCallback for why the path IS the authentication here, and why
// that is only acceptable alongside Caddy's source-IP allowlist.
void ConfigureBunqCallback(std::filesystem::path creditsPath,
std::string secretPath);
// ── the callback shadow (temporary, since 2026-11-25) ─────────────
//
// A second reading of every bank callback, by Nostro's ReadCallback (the
// module Nostro.Money of https://forgejo.catcrafts.net/Catcrafts/Nostro,
// at its 1.1.0 release), set beside what ParseBunqCallback made of the
// same body. It decides NOTHING: the handler has answered and written its
// credit before this runs, and all that comes out is one log line.
//
// Why it exists: ParseBunqCallback is to be replaced by that reader, so
// that this repo stops carrying bunq's wire format. The reader is
// stricter. It wants a time, a counterparty and the account's own IBAN
// where ours reads four members, and when this was written nobody had run
// it on a callback bunq really sent: its shape came from a sample in
// bunq's SDK dated 2017. Ours has settled real payments since 2026-08. So
// the two run side by side until the log shows the reader agreeing on
// real payments. Then it takes over, and this goes together with
// ParseBunqCallback.
//
// The line carries no payment data, on purpose: no id, no amount, no name
// and no reference, only whether the two readings match and a few fixed
// words. A callback's body holds the payer's name and IBAN, which the
// privacy notice says this shop does not copy out of the bank, and it may
// repeat the address it was sent to, which is a secret.
//
// AGREE in method=sepa account=ours event=transactions
// AGREE out method ours=MASTERCARD nostro=card account=other event=transactions
// DIFFER nostro refused what ours reads as a payment: <nostro's sentence>
// DIFFER id=same amount=DIFFERENT reference=same in method=sepa …
//
// AGREE means the id, the amount and the reference are the same, which is
// everything settlement rests on. The method is reported beside it and is
// allowed to differ: Nostro calls bunq's MASTERCARD a card payment, where
// ours passes a type it does not know through as bunq wrote it. `account`
// says whether the payment is on `ownIban`, which ours never looked at.
//
// `ours` is what ParseBunqCallback made of the same body, `bank` what a
// Nostro connector to the bank is called ("bunq"), and `ownIban` the
// account the shop is paid into; empty reports the account as unknown.
// Whatever the reading or the comparison throws is caught and becomes the
// verdict, in words that are not the body's.
std::string ShadowCallbackVerdict(std::string_view bank, std::string_view body,
const std::optional<BankCredit>& ours,
std::string_view ownIban);
// Turn the shadow on for the callback endpoint, naming the shop's own
// account. Without this call the endpoint behaves exactly as it did.
void ConfigureCallbackShadow(std::string ownIban);
// One pull: read the account and append every credit not already in the
// file to it, newest last. Returns the number appended, or nullopt if the
// bank could not be reached. This is what `--pull-credits` runs, and it is
// deliberately a separate entry point from the rail so the machine holding
// the key need not be the machine serving the shop.
std::optional<int> PullCreditsInto(CreditSource& source,
const std::filesystem::path& creditsPath);
// Enable the callback endpoint. All four are required and the secret must
// be at least 24 characters, or the endpoint stays off: the path IS the
// authentication (see above), which is only acceptable alongside the web
// server's source-IP allowlist; without the bank's name nothing can be
// read, and without the account nothing can be credited.
void ConfigureBankCallback(std::filesystem::path creditsPath, std::string secretPath,
std::string bank, std::string ownIban);
// ── shipping rates ────────────────────────────────────────────────
//