bank: run Nostro's callback reader beside ours, as a shadow
All checks were successful
Deploy / build-deploy (push) Successful in 7m33s
All checks were successful
Deploy / build-deploy (push) Successful in 7m33s
ParseBunqCallback is to be replaced by ReadCallback of Nostro.Money (Nostro 1.1.0), so that this repo stops carrying bunq's wire format. That reader is stricter than ours: it wants a time, a counterparty and the account's own IBAN where ours reads four members, and nobody has run it on a callback bunq really sent. Ours has settled real payments since 2026-08. So before anything is swapped, both run. - The handler is untouched and still decides alone. Once it has answered and written its credit, Nostro's reader reads the same body and one line goes to the log: `bank callback shadow: AGREE ...` when the id, the amount and the reference are the same in both readings, and `DIFFER ...` with what parted them otherwise. - The line carries no payment data: no id, amount, name or reference, only whether values match and a few fixed words. A body holds the payer's name and IBAN, and may repeat the secret address. - It is on wherever BUNQ_WEBHOOK_PATH is set, with no setting of its own. - project.cpp takes Nostro with --product=money --banks=bunq, at the commit of its 1.1.0 release: the module alone, which brings no credential and no Crafter.Network, Crafter.DB or OpenSSL of its own. ShouldCompareCallbackReaders pins every verdict, and that none repeats anything a body carried. ShouldReceiveBankCallbacks posts callbacks to the real endpoint: an order settles from one, a payment Nostro's reader refuses is credited all the same, and the server's log holds nothing of what was posted. Built 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 the same way without this change where the post media is not mirrored. The binary needs the same shared libraries as before. Temporary: once real payments read AGREE the reader takes over, and this goes together with ParseBunqCallback. deploy/README.md says how to read the result. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
e0d3181840
commit
36a635fd48
8 changed files with 906 additions and 5 deletions
179
server/implementations/Catcrafts.Server-CallbackShadow.cpp
Normal file
179
server/implementations/Catcrafts.Server-CallbackShadow.cpp
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
/*
|
||||
catcrafts.net
|
||||
Copyright (C) 2026 Catcrafts
|
||||
|
||||
The source code of this website is made available for viewing purposes only.
|
||||
No permission is granted to copy, modify, distribute, or create derivative works.
|
||||
*/
|
||||
|
||||
// The callback shadow: what Nostro's reader makes of a bank callback, set
|
||||
// beside what this shop's own parser made of it. TEMPORARY, and it decides
|
||||
// nothing. The interface (ShadowCallbackVerdict) says why it exists and when
|
||||
// it goes.
|
||||
//
|
||||
// This is the one unit that imports Nostro.Money. Everything it takes from
|
||||
// there is read here and turned into this shop's own words before it leaves,
|
||||
// so nothing else in the server knows the module is there.
|
||||
//
|
||||
// The rule every line below is written to: NOTHING OF THE BODY COMES OUT. The
|
||||
// verdict is built from fixed words, from whether two values are equal, and
|
||||
// from the few words a bank has for a category or a type of payment, each
|
||||
// checked to be such a word before it is printed. An id, an amount, a name, an
|
||||
// IBAN or a description is compared and never quoted.
|
||||
|
||||
module;
|
||||
module Catcrafts.Server;
|
||||
|
||||
import std;
|
||||
import Catcrafts.Shared;
|
||||
import Nostro.Money;
|
||||
|
||||
namespace Catcrafts::Server {
|
||||
|
||||
namespace {
|
||||
|
||||
// A word that is safe to print whoever wrote the body: letters, digits and
|
||||
// the three marks a bank's own word or ours is made with, and not many of
|
||||
// them. Anything else is not printed at all. Nostro already holds a bank's
|
||||
// category to capitals, digits and underscores; this does not lean on that.
|
||||
std::string Word(std::string_view s) {
|
||||
constexpr std::size_t most = 48;
|
||||
if (s.empty() || s.size() > most) return "?";
|
||||
for (const char c : s) {
|
||||
const bool plain = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
||||
|| (c >= '0' && c <= '9') || c == '_' || c == ':'
|
||||
|| c == '-';
|
||||
if (!plain) return "?";
|
||||
}
|
||||
return std::string(s);
|
||||
}
|
||||
|
||||
// The sentence Nostro refused a body with. By its contract that sentence is
|
||||
// Nostro's own and holds nothing of the body, which is what makes it the one
|
||||
// piece of text here that is passed on. Still held to printable ASCII and a
|
||||
// length, so a contract that was broken cannot put a control character or a
|
||||
// page of text into the journal.
|
||||
std::string Sentence(std::string_view s) {
|
||||
constexpr std::size_t most = 200;
|
||||
std::string out;
|
||||
out.reserve(std::min(s.size(), most));
|
||||
for (const char c : s.substr(0, most)) {
|
||||
out += (c >= 0x20 && c < 0x7F) ? c : '?';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// IBANs compared as the rest of the server compares them: spacing and case
|
||||
// are how a person wrote one down, not part of it.
|
||||
std::string FoldIban(std::string_view s) {
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (const char c : s) {
|
||||
if (c == ' ' || c == '\t') continue;
|
||||
out += static_cast<char>(c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Nostro's answer to how a payment arrived, in the ledger's `via` words, so
|
||||
// it can be set beside what BunqMethodFor said. Empty for Unknown: the caller
|
||||
// then falls back to the bank's own word, as BunqMethodFor does.
|
||||
std::string_view ViaOf(Nostro::PaymentMethod method) {
|
||||
switch (method) {
|
||||
case Nostro::PaymentMethod::CreditTransfer: return "sepa";
|
||||
case Nostro::PaymentMethod::DirectDebit: return "directdebit";
|
||||
case Nostro::PaymentMethod::Card: return "card";
|
||||
case Nostro::PaymentMethod::Ideal: return "ideal";
|
||||
case Nostro::PaymentMethod::Internal: return "bunq";
|
||||
case Nostro::PaymentMethod::Swift: return "swift";
|
||||
case Nostro::PaymentMethod::Unknown: break;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// The `via` word Nostro's reading leads to for a payment of this kind.
|
||||
std::string NostroVia(std::string_view bank, std::string_view kind) {
|
||||
Nostro::PaymentMethod method = Nostro::PaymentMethod::Unknown;
|
||||
try {
|
||||
method = Nostro::PaymentMethodOf(bank, kind);
|
||||
} catch (...) {
|
||||
return "?";
|
||||
}
|
||||
if (const std::string_view via = ViaOf(method); !via.empty()) {
|
||||
return std::string(via);
|
||||
}
|
||||
// A kind Nostro's table has not. A kind is "<type>/<sub type>", and the
|
||||
// type alone is what BunqMethodFor passes through for one IT does not
|
||||
// know, with "bank" where the bank sent none.
|
||||
const std::string_view type = kind.substr(0, kind.find('/'));
|
||||
return type.empty() ? std::string("bank") : std::string(type);
|
||||
}
|
||||
|
||||
std::string_view Same(bool same) { return same ? "same" : "DIFFERENT"; }
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string ShadowCallbackVerdict(std::string_view bank, std::string_view body,
|
||||
const std::optional<BankCredit>& ours,
|
||||
std::string_view ownIban) {
|
||||
try {
|
||||
Nostro::CallbackEvent event;
|
||||
try {
|
||||
event = Nostro::ReadCallback(bank, body);
|
||||
} catch (const std::runtime_error& e) {
|
||||
// What Nostro promises to throw, and the only thing whose words
|
||||
// are passed on.
|
||||
const std::string why = Sentence(e.what());
|
||||
return ours
|
||||
? std::format("DIFFER nostro refused what ours reads as a payment: {}", why)
|
||||
: std::format("AGREE neither reads a payment (nostro: {})", why);
|
||||
} catch (...) {
|
||||
// Anything else is not in its contract, and its words could be
|
||||
// the body's: a JSON library quotes what it choked on.
|
||||
return "DIFFER nostro threw something other than it promises";
|
||||
}
|
||||
|
||||
const std::string eventWord = Word(event.event);
|
||||
if (!event.transaction) {
|
||||
return ours
|
||||
? std::format("DIFFER nostro reads no payment in it (event={}), ours does",
|
||||
eventWord)
|
||||
: std::format("AGREE no payment in it (event={})", eventWord);
|
||||
}
|
||||
|
||||
const Nostro::Transaction& theirs = *event.transaction;
|
||||
const std::string_view direction =
|
||||
theirs.amount > 0 ? "in" : theirs.amount < 0 ? "out" : "zero";
|
||||
const std::string_view account =
|
||||
ownIban.empty() ? "unknown"
|
||||
: FoldIban(event.account) == FoldIban(ownIban) ? "ours" : "other";
|
||||
|
||||
if (!ours) {
|
||||
return std::format(
|
||||
"DIFFER nostro reads a payment, ours does not ({} account={} event={})",
|
||||
direction, account, eventWord);
|
||||
}
|
||||
|
||||
const std::string theirVia = NostroVia(bank, theirs.kind);
|
||||
const std::string method = theirVia == ours->method
|
||||
? std::format("method={}", Word(theirVia))
|
||||
: std::format("method ours={} nostro={}", Word(ours->method), Word(theirVia));
|
||||
const std::string rest = std::format("{} {} account={} event={}", direction,
|
||||
method, account, eventWord);
|
||||
|
||||
const bool sameId = theirs.id == ours->id;
|
||||
const bool sameAmount = theirs.amount == ours->amountMinor;
|
||||
const bool sameReference = theirs.description == ours->reference;
|
||||
if (sameId && sameAmount && sameReference) {
|
||||
return std::format("AGREE {}", rest);
|
||||
}
|
||||
return std::format("DIFFER id={} amount={} reference={} {}", Same(sameId),
|
||||
Same(sameAmount), Same(sameReference), rest);
|
||||
} catch (...) {
|
||||
// Out of memory while building a line, in practice. The comparison
|
||||
// must never be the thing that fails a callback.
|
||||
return "FAILED the comparison itself threw";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Catcrafts::Server
|
||||
Loading…
Reference in a new issue