Nostro/interfaces/Nostro-Bus.cppm

284 lines
14 KiB
Text
Raw Permalink Normal View History

2026-11-01 18:35:23 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#ifdef NOSTRO_DBUS_ENABLED
#include <systemd/sd-bus.h>
#endif
export module Nostro:Bus;
#ifdef NOSTRO_DBUS_ENABLED
import std;
import :Account;
import :Bunq;
import :Currency;
import :Transaction;
// The wire between nostrod and its clients: the names, the methods and their signatures, the errors, the polkit
// actions, and how each type travels. The daemon and the client both build on it, so the two cannot disagree.
namespace Nostro {
// What Connectors() reports of one connector.
export struct ConnectorSummary {
std::string name;
std::string bank; // its connectorId: "bunq"
std::string environment; // "production" or "sandbox"
std::string auth; // "api key" or "oauth"
bool working = false; // whether a call to the bank worked just now
};
// What AddBunq() and AddBunqOAuth() report of the connector they added.
export struct AddedConnector {
std::string name;
std::string environment;
std::uint32_t accounts = 0;
};
// One connector's accounts, as Accounts() lists them.
export struct ConnectorAccounts {
std::string name;
std::string bank;
std::vector<Account> accounts;
};
// One connector's cards, as Cards() lists them.
export struct ConnectorCards {
std::string name;
std::string bank;
std::vector<BunqCard> cards;
};
// What Send(), Schedule() and Draft() made: bunq's id for it, and the currency its amount was in.
export struct Created {
std::uint64_t id = 0;
Currency currency = Currency::Euro;
};
}
export namespace Nostro::Bus {
// String views of literals, so `.data()` is NUL-terminated as sd-bus wants it.
inline constexpr std::string_view busName = "net.catcrafts.Nostro1";
inline constexpr std::string_view objectPath = "/net/catcrafts/Nostro1";
inline constexpr std::string_view interfaceName = "net.catcrafts.Nostro1";
feat: open payto links on the pay form, and take the checkout over D-Bus out An app could have nostrod pay an order: it called Pay of net.catcrafts.Nostro1.Checkout on the system bus, and nostrod paid from an account the user had chosen, after a dialog of its own. On 2026-11-22 the owner decided that a link takes its place. The app registers for payto links (RFC 8905), so that an app or a website can hand one to the desktop, and shows a link as its pay form filled in. The link: - The app's desktop entry takes the scheme (Exec=nostro-app %u, MimeType=x-scheme-handler/payto), and nostro-app takes an argument that is no option as a link: it overwrites it in argv and hands it to App::Open once the app has started. - ReadPaymentLink (Nostro.App:Forms) reads payto://iban/<IBAN> with the options amount, receiver-name and message, and refuses a link rather than guess at it, in a sentence of the app's own with nothing of the link in it. Among what it refuses: an IBAN whose check digits do not hold, one of the three options given twice, instruction, a comma in the amount, and a name or a message with a code point that is Unshown, or Blank and no plain space. - A link that is read waits until the accounts are read and DefaultCheckoutAccount has answered or failed, with a line on the accounts' page. Then App::TakeLink fills in the pay form, which says in a banner that a link filled it in, and with more than one account opens it under the choice of accounts that the form's From row has. A link pays nothing: the form's button sends, and polkit's dialog confirms as for any payment (design rule 12, and 14, which is new). - Unshown and Blank are the two tables the worker's Shown() works from. They move into Nostro:Utils, where the app's reader asks them too. The move is within the library. The reader holds a link's name to those two and to no more of Shown(): a name with an & is read, and the dialog shows it with a + as it does any name. The checkout: - nostrod serves one interface under one bus name. Gone are net.catcrafts.Nostro1.Checkout with its XML and its activation file, the polkit action Checkout, the module Nostro.Checkout with --product=checkout, and the worker's record of orders. That changes the published contract after a release, 0.0.0, and design rule 10 records it as the owner's decision. - DefaultCheckoutAccount and SetDefaultCheckoutAccount keep their names and signatures. The account they keep is where a link's pay form starts: nostrod's dialog for choosing it says "have pay links start from", the app's setting reads "Pay links start from", and `nostro checkout account` prints "pay links start from". - The D-Bus policy still lets nostrod own the old name: the nostrod of 0.0.0 asks for it when it starts, and the system files can be newer than the Flatpak it runs from. install.sh removes the old name's activation file. A user's checkouts.cdb is read by nothing any more, and is left where it is. - The Arch package stops installing the checkout's two files, and its pkgrel goes up to 2. Where makepkg -si finds a package of the version already built it installs that one again, which would bring the activation file back. The guide now says that pkgrel goes up with a change to what the package installs. - Bus::Interface goes too: the worker called it in two places, to tell a call to the checkout from one to the main interface. The review's own files follow. gate0 loses the build of --product=checkout, and the three lens sections that named the checkout no longer do, and ask about a link. The README has "Pay links" in place of "Checkout for apps", and AGENTS.consumer.md says what the app reads of a link. Its line on lib/json.hpp no longer gives a release: the file is of none, and the guide names its commit. The pay form and the accounts' page change only by a banner each, shown while a link is in play, which docs/screenshots/take.sh never has: the README's pictures stand. Tests: ShouldValidateForms reads and refuses links and fills the form from one. ShouldDriveTheApp opens links against a real nostrod on the fake bus: before the accounts are known, with no account, with one and with several, with the accounts known and the system bus gone, for a second user whose first connection pays their own accounts only, one that cannot be read, one whose name has an &, and through polkit's dialog to the payment. It now counts nostrod's answers to hold that the account pay links start from is read again once a connection is removed: the check it had there passed without that. ShouldHandleMoneyAndText holds the two tables at the ends of their ranges. ShouldServeEveryUser holds that nostrod serves no checkout, that the bus's policy lets an older nostrod own the old name, and the account pay links start from. ShouldRunCommands holds the CLI's wording. The review's gate0 passes at this commit: the version check, 17 tests, the builds of the CLI, the library, nostrod, the fake system, the app and its --debug-api build, and five lint runs that report nothing. tests/coverage.sh reports lines, functions and regions at 100.00%. Looked at on 2026-11-22 on the made-up system (run fake), on a desktop (KWin at a scale of 1.85). At 420x760, at --scale 3 --size 360x760 and at 1100x720 in the light look: a link's pay form under the choice of accounts, the form after a tap beside that choice, and the accounts' page while a link waits, its line above the rows that stand in for the accounts (--bank-delay 10000). At 420x760: that line above what the page says when the accounts cannot be read (--polkit deny) and above the accounts themselves (--polkit slow, the link opened in the running app), the settings' row and page for the account pay links start from, the sheet for a link that cannot be read, and with --empty the sheet for no account.
2026-11-22 13:35:51 +01:00
// What nostrod answers with besides the standard AccessDenied and InteractiveAuthorizationRequired.
inline constexpr std::string_view errorInvalidArgument = "net.catcrafts.Nostro1.Error.InvalidArgument"; // refused before anything was sent
inline constexpr std::string_view errorBank = "net.catcrafts.Nostro1.Error.Bank"; // the bank answered with an error
inline constexpr std::string_view errorOAuthCannotPay = "net.catcrafts.Nostro1.Error.OAuthCannotPay"; // bunq refused to pay through an OAuth connector
inline constexpr std::string_view errorCancelled = "net.catcrafts.Nostro1.Error.Cancelled"; // the user did not authenticate at the prompt
inline constexpr std::string_view errorFailed = "net.catcrafts.Nostro1.Error.Failed"; // anything else
2026-11-01 18:35:23 +01:00
// The polkit actions, declared in packaging/system/net.catcrafts.Nostro1.policy.
inline constexpr std::string_view actionView = "net.catcrafts.Nostro1.View";
inline constexpr std::string_view actionChange = "net.catcrafts.Nostro1.Change";
inline constexpr std::string_view actionPay = "net.catcrafts.Nostro1.Pay";
inline constexpr std::string_view actionUnfreezeCard = "net.catcrafts.Nostro1.UnfreezeCard";
inline constexpr std::string_view actionBlockCard = "net.catcrafts.Nostro1.BlockCard";
inline constexpr std::string_view actionConnectors = "net.catcrafts.Nostro1.Connectors";
}
using namespace std::string_view_literals;
namespace Nostro::Bus {
// One method of the interface. Every reply ends with `as notes`: the library's warnings during the call.
struct Method {
std::string_view member;
std::string_view in; // the arguments' signature
std::string_view out; // the reply's
std::string_view names; // the arguments' names, then the reply's, each ended by a NUL
bool sensitive = false; // carries a secret: sd-bus wipes the message when it frees it
};
// The interface, in the order introspection lists it. The front and the workers build their vtables from it.
inline constexpr std::array methods = {
Method{"Connectors", "", "a(ssssb)as", "connectors\0notes\0"sv},
Method{"AddBunq", "sss", "ssuas", "apiKey\0allowedIps\0environment\0name\0addedEnvironment\0accounts\0notes\0"sv, true},
Method{"AddBunqOAuth", "sssss", "ssuas", "environment\0clientId\0clientSecret\0redirectUri\0code\0name\0addedEnvironment\0accounts\0notes\0"sv, true},
Method{"RemoveConnector", "s", "sas", "connector\0name\0notes\0"sv},
Method{"Accounts", "", "a(ssa(ssssx))as", "connectors\0notes\0"sv},
Method{"Transactions", "sa{sv}", "a(sssssxxss)as", "account\0query\0transactions\0notes\0"sv},
Method{"Send", "sssxs", "tsas", "account\0to\0name\0amount\0description\0id\0currency\0notes\0"sv},
Method{"Schedule", "sssxs(xaxsusax)", "tsas", "account\0to\0name\0amount\0description\0schedule\0id\0currency\0notes\0"sv},
Method{"Request", "sssxs", "(ts(ss)sxaxsxs)as", "account\0from\0name\0amount\0description\0request\0notes\0"sv},
Method{"Requests", "s", "a(ts(ss)sxaxsxs)as", "account\0requests\0notes\0"sv},
Method{"RevokeRequest", "st", "as", "account\0id\0notes\0"sv},
Method{"IncomingRequests", "s", "a(tss(ss)sxsx)as", "account\0requests\0notes\0"sv},
Method{"RespondToRequest", "stb", "as", "account\0id\0accept\0notes\0"sv},
Method{"ScheduledPayments", "s", "a(t(ss)sxs(xaxsusax))as", "account\0scheduled\0notes\0"sv},
Method{"CancelScheduledPayment", "st", "as", "account\0id\0notes\0"sv},
Method{"Draft", "sssxs", "tsas", "account\0to\0name\0amount\0description\0id\0currency\0notes\0"sv},
Method{"DraftPayments", "s", "a(tssa((ss)sxs)s)as", "account\0drafts\0notes\0"sv},
Method{"RespondToDraftPayment", "stb", "as", "account\0id\0accept\0notes\0"sv},
Method{"Cards", "", "a(ssa(tsssssssss))as", "connectors\0notes\0"sv},
Method{"SetCardStatus", "ts", "(tsssssssss)as", "card\0status\0updated\0notes\0"sv},
Method{"DefaultCheckoutAccount", "", "a(ssssx)as", "account\0notes\0"sv},
Method{"SetDefaultCheckoutAccount", "s", "(ssssx)as", "account\0chosen\0notes\0"sv},
};
// Throws a std::system_error naming `what` when an sd-bus call returned a negative errno; returns the result.
std::int32_t ThrowOnSdBus(std::int32_t result, std::string_view what);
// The header fields and error texts sd-bus hands out as C strings, as strings: empty when there are none.
std::string Sender(sd_bus_message* message);
std::string Member(sd_bus_message* message);
std::string ErrorName(const sd_bus_error& error);
std::string ErrorMessage(const sd_bus_error& error);
// Whether `member` carries a secret (Method::sensitive).
bool IsSensitive(std::string_view member);
// A string, or an object path ('o'), read as text.
std::string ReadText(sd_bus_message* message, char type);
struct BusUnref {
void operator()(sd_bus* bus) const;
};
using BusPtr = std::unique_ptr<sd_bus, BusUnref>;
struct MessageUnref {
void operator()(sd_bus_message* message) const;
};
using MessagePtr = std::unique_ptr<sd_bus_message, MessageUnref>;
// An sd_bus_error that frees itself.
struct Error {
sd_bus_error error = SD_BUS_ERROR_NULL;
Error() = default;
Error(const Error&) = delete;
Error& operator=(const Error&) = delete;
~Error();
};
feat: open payto links on the pay form, and take the checkout over D-Bus out An app could have nostrod pay an order: it called Pay of net.catcrafts.Nostro1.Checkout on the system bus, and nostrod paid from an account the user had chosen, after a dialog of its own. On 2026-11-22 the owner decided that a link takes its place. The app registers for payto links (RFC 8905), so that an app or a website can hand one to the desktop, and shows a link as its pay form filled in. The link: - The app's desktop entry takes the scheme (Exec=nostro-app %u, MimeType=x-scheme-handler/payto), and nostro-app takes an argument that is no option as a link: it overwrites it in argv and hands it to App::Open once the app has started. - ReadPaymentLink (Nostro.App:Forms) reads payto://iban/<IBAN> with the options amount, receiver-name and message, and refuses a link rather than guess at it, in a sentence of the app's own with nothing of the link in it. Among what it refuses: an IBAN whose check digits do not hold, one of the three options given twice, instruction, a comma in the amount, and a name or a message with a code point that is Unshown, or Blank and no plain space. - A link that is read waits until the accounts are read and DefaultCheckoutAccount has answered or failed, with a line on the accounts' page. Then App::TakeLink fills in the pay form, which says in a banner that a link filled it in, and with more than one account opens it under the choice of accounts that the form's From row has. A link pays nothing: the form's button sends, and polkit's dialog confirms as for any payment (design rule 12, and 14, which is new). - Unshown and Blank are the two tables the worker's Shown() works from. They move into Nostro:Utils, where the app's reader asks them too. The move is within the library. The reader holds a link's name to those two and to no more of Shown(): a name with an & is read, and the dialog shows it with a + as it does any name. The checkout: - nostrod serves one interface under one bus name. Gone are net.catcrafts.Nostro1.Checkout with its XML and its activation file, the polkit action Checkout, the module Nostro.Checkout with --product=checkout, and the worker's record of orders. That changes the published contract after a release, 0.0.0, and design rule 10 records it as the owner's decision. - DefaultCheckoutAccount and SetDefaultCheckoutAccount keep their names and signatures. The account they keep is where a link's pay form starts: nostrod's dialog for choosing it says "have pay links start from", the app's setting reads "Pay links start from", and `nostro checkout account` prints "pay links start from". - The D-Bus policy still lets nostrod own the old name: the nostrod of 0.0.0 asks for it when it starts, and the system files can be newer than the Flatpak it runs from. install.sh removes the old name's activation file. A user's checkouts.cdb is read by nothing any more, and is left where it is. - The Arch package stops installing the checkout's two files, and its pkgrel goes up to 2. Where makepkg -si finds a package of the version already built it installs that one again, which would bring the activation file back. The guide now says that pkgrel goes up with a change to what the package installs. - Bus::Interface goes too: the worker called it in two places, to tell a call to the checkout from one to the main interface. The review's own files follow. gate0 loses the build of --product=checkout, and the three lens sections that named the checkout no longer do, and ask about a link. The README has "Pay links" in place of "Checkout for apps", and AGENTS.consumer.md says what the app reads of a link. Its line on lib/json.hpp no longer gives a release: the file is of none, and the guide names its commit. The pay form and the accounts' page change only by a banner each, shown while a link is in play, which docs/screenshots/take.sh never has: the README's pictures stand. Tests: ShouldValidateForms reads and refuses links and fills the form from one. ShouldDriveTheApp opens links against a real nostrod on the fake bus: before the accounts are known, with no account, with one and with several, with the accounts known and the system bus gone, for a second user whose first connection pays their own accounts only, one that cannot be read, one whose name has an &, and through polkit's dialog to the payment. It now counts nostrod's answers to hold that the account pay links start from is read again once a connection is removed: the check it had there passed without that. ShouldHandleMoneyAndText holds the two tables at the ends of their ranges. ShouldServeEveryUser holds that nostrod serves no checkout, that the bus's policy lets an older nostrod own the old name, and the account pay links start from. ShouldRunCommands holds the CLI's wording. The review's gate0 passes at this commit: the version check, 17 tests, the builds of the CLI, the library, nostrod, the fake system, the app and its --debug-api build, and five lint runs that report nothing. tests/coverage.sh reports lines, functions and regions at 100.00%. Looked at on 2026-11-22 on the made-up system (run fake), on a desktop (KWin at a scale of 1.85). At 420x760, at --scale 3 --size 360x760 and at 1100x720 in the light look: a link's pay form under the choice of accounts, the form after a tap beside that choice, and the accounts' page while a link waits, its line above the rows that stand in for the accounts (--bank-delay 10000). At 420x760: that line above what the page says when the accounts cannot be read (--polkit deny) and above the accounts themselves (--polkit slow, the link opened in the running app), the settings' row and page for the account pay links start from, the sheet for a link that cannot be read, and with --empty the sheet for no account.
2026-11-22 13:35:51 +01:00
// A vtable of every method of `table`, all of them unprivileged (polkit decides, not sd-bus), each handled by
// `handler(member)`. It must outlive the object it is registered for.
2026-11-01 18:35:23 +01:00
std::vector<sd_bus_vtable> Vtable(std::span<const Method> table, const std::function<sd_bus_message_handler_t(std::string_view member)>& handler);
// Enters a container, or throws when the message holds something else there.
void Enter(sd_bus_message* message, char type, std::string_view contents);
void Leave(sd_bus_message* message);
// Opens and closes a container to append to.
void Open(sd_bus_message* message, char type, std::string_view contents);
void Close(sd_bus_message* message);
// How a type travels: its signature, and how it is appended to a message and read from one. A struct also has
// its `contents`, the signature without the parentheses.
template<typename T> struct Wire;
template<> struct Wire<std::string> {
static constexpr std::string_view signature = "s";
static void Append(sd_bus_message* message, const std::string& value);
static std::string Read(sd_bus_message* message);
};
template<> struct Wire<bool> {
static constexpr std::string_view signature = "b";
static void Append(sd_bus_message* message, bool value);
static bool Read(sd_bus_message* message);
};
template<> struct Wire<std::uint32_t> {
static constexpr std::string_view signature = "u";
static void Append(sd_bus_message* message, std::uint32_t value);
static std::uint32_t Read(sd_bus_message* message);
};
template<> struct Wire<std::int64_t> {
static constexpr std::string_view signature = "x";
static void Append(sd_bus_message* message, std::int64_t value);
static std::int64_t Read(sd_bus_message* message);
};
template<> struct Wire<std::uint64_t> {
static constexpr std::string_view signature = "t";
static void Append(sd_bus_message* message, std::uint64_t value);
static std::uint64_t Read(sd_bus_message* message);
};
// Microseconds since the epoch, UTC.
template<> struct Wire<std::chrono::system_clock::time_point> {
static constexpr std::string_view signature = "x";
static void Append(sd_bus_message* message, std::chrono::system_clock::time_point value);
static std::chrono::system_clock::time_point Read(sd_bus_message* message);
};
// Its ISO code.
template<> struct Wire<Currency> {
static constexpr std::string_view signature = "s";
static void Append(sd_bus_message* message, Currency value);
static Currency Read(sd_bus_message* message);
};
// An array.
template<typename T> struct Wire<std::vector<T>> {
static void Append(sd_bus_message* message, const std::vector<T>& values) {
Open(message, 'a', Wire<T>::signature);
for(const T& value : values) {
Wire<T>::Append(message, value);
}
Close(message);
}
static std::vector<T> Read(sd_bus_message* message) {
Enter(message, 'a', Wire<T>::signature);
std::vector<T> values;
while(ThrowOnSdBus(sd_bus_message_at_end(message, 0), "read an array") == 0) {
values.push_back(Wire<T>::Read(message));
}
Leave(message);
return values;
}
};
// An array of zero or one: D-Bus has no optional type.
template<typename T> struct Wire<std::optional<T>> {
static void Append(sd_bus_message* message, const std::optional<T>& value) {
Open(message, 'a', Wire<T>::signature);
if(value) {
Wire<T>::Append(message, *value);
}
Close(message);
}
static std::optional<T> Read(sd_bus_message* message) {
Enter(message, 'a', Wire<T>::signature);
std::optional<T> value;
if(ThrowOnSdBus(sd_bus_message_at_end(message, 0), "read an optional value") == 0) {
value = Wire<T>::Read(message);
}
if(ThrowOnSdBus(sd_bus_message_at_end(message, 0), "read an optional value") == 0) {
throw std::runtime_error("an optional value with more than one element");
}
Leave(message);
return value;
}
};
// The structs. Each is declared here and defined in the implementation unit.
#define NOSTRO_BUS_STRUCT(Type, Contents) \
template<> struct Wire<Type> { \
static constexpr std::string_view contents = Contents; \
static constexpr std::string_view signature = "(" Contents ")"; \
static void Append(sd_bus_message* message, const Type& value); \
static Type Read(sd_bus_message* message); \
}
NOSTRO_BUS_STRUCT(Account, "ssssx");
NOSTRO_BUS_STRUCT(Transaction, "sssssxxss");
NOSTRO_BUS_STRUCT(BunqLabel, "ss");
NOSTRO_BUS_STRUCT(BunqPaymentRequest, "ts(ss)sxaxsxs");
NOSTRO_BUS_STRUCT(BunqIncomingRequest, "tss(ss)sxsx");
NOSTRO_BUS_STRUCT(BunqSchedule, "xaxsusax");
NOSTRO_BUS_STRUCT(BunqScheduledPayment, "t(ss)sxs(xaxsusax)");
NOSTRO_BUS_STRUCT(BunqDraftPayment::Entry, "(ss)sxs");
NOSTRO_BUS_STRUCT(BunqDraftPayment, "tssa((ss)sxs)s");
NOSTRO_BUS_STRUCT(BunqCard, "tsssssssss");
NOSTRO_BUS_STRUCT(ConnectorSummary, "ssssb");
NOSTRO_BUS_STRUCT(ConnectorAccounts, "ssa(ssssx)");
NOSTRO_BUS_STRUCT(ConnectorCards, "ssa(tsssssssss)");
#undef NOSTRO_BUS_STRUCT
template<typename T> void Append(sd_bus_message* message, const T& value) {
Wire<T>::Append(message, value);
}
template<typename T> T Read(sd_bus_message* message) {
return Wire<T>::Read(message);
}
// A transaction query, as `a{sv}`: search, amount, from, to and limit, each only when it narrows anything.
// Unknown keys are skipped, so a newer client can ask an older daemon.
void AppendQuery(sd_bus_message* message, const TransactionQuery& query);
TransactionQuery ReadQuery(sd_bus_message* message);
}
#endif