cli MVP
This commit is contained in:
parent
367c19386c
commit
1ea50c50bc
62 changed files with 9982 additions and 608 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Account;
|
||||
|
||||
|
|
@ -8,33 +8,39 @@ import :Currency;
|
|||
import :Transaction;
|
||||
|
||||
namespace Nostro {
|
||||
// What a transaction list shows: the newest `limit` transactions that match every field. `from` and `to` are inclusive.
|
||||
export struct TransactionQuery {
|
||||
std::string search; // case-insensitive (ASCII), in the name, description or IBAN; empty matches all
|
||||
std::optional<std::int64_t> amount; // exact, in the currency's minor unit, negative when outgoing
|
||||
std::chrono::system_clock::time_point from = std::chrono::system_clock::time_point::min();
|
||||
std::chrono::system_clock::time_point to = std::chrono::system_clock::time_point::max();
|
||||
std::size_t limit = 100;
|
||||
};
|
||||
|
||||
export struct Account {
|
||||
std::string name;
|
||||
std::string bic;
|
||||
std::string iban;
|
||||
Currency currency;
|
||||
std::int64_t balance;
|
||||
std::vector<Transaction> transactions;
|
||||
|
||||
// Transactions are stored per connector and account name, in a table ordered by time.
|
||||
std::chrono::system_clock::time_point GetLastTransactionTime(std::string_view connector) const;
|
||||
void SaveTransactions(std::string_view connector);
|
||||
void LoadTransactions(std::string_view connector);
|
||||
void LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from);
|
||||
void LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from, std::chrono::system_clock::time_point to);
|
||||
void LoadTransactionsByDescription(std::string_view connector, std::string_view description);
|
||||
void LoadTransactionsByIban(std::string_view connector, std::string_view iban);
|
||||
void LoadTransactions(std::string_view connector, std::int64_t amount);
|
||||
// Appends the ones newer than the last stored transaction, in any order.
|
||||
void SaveTransactions(std::string_view connector, std::vector<Transaction> transactions);
|
||||
// Newest first.
|
||||
std::vector<Transaction> FindTransactions(std::string_view connector, const TransactionQuery& query) const;
|
||||
|
||||
private:
|
||||
std::ifstream LoadUntilFrom(std::string_view connector, std::chrono::system_clock::time_point from);
|
||||
Transaction& LoadTransaction(std::ifstream& file);
|
||||
// Deletes every table stored under `connector` (a connector's storage key covers all its accounts), closing the
|
||||
// ones this process has open first: a table left open on deleted files would take the next sync's rows with it.
|
||||
static void ForgetTransactions(std::string_view connector);
|
||||
};
|
||||
}
|
||||
|
||||
template<std::derived_from<Nostro::Account> T>
|
||||
struct std::formatter<T> : std::formatter<std::string> {
|
||||
auto format(const Nostro::Account& obj, std::format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{}\t{} {}\t{}{}", obj.name, obj.bic, obj.iban, obj.currency, Nostro::FormatAmount(obj.balance, obj.currency)), ctx);
|
||||
return std::formatter<std::string>::format(std::format("{}\t{} {}\t{}", obj.name, obj.bic, obj.iban, Nostro::FormatMoney(obj.balance, obj.currency)), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Banks;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
|
|
@ -12,47 +12,266 @@ export module Nostro:Bunq;
|
|||
#ifdef NOSTRO_BUNQ_ENABLED
|
||||
|
||||
import std;
|
||||
import Crafter.Network;
|
||||
import :Account;
|
||||
import :Crypto;
|
||||
import :Currency;
|
||||
import :Transaction;
|
||||
|
||||
using namespace Crafter;
|
||||
import :Utils;
|
||||
|
||||
namespace Nostro {
|
||||
export struct BunqConnector;
|
||||
|
||||
// Which bunq a connector talks to: api.bunq.com, or the sandbox at public-api.sandbox.bunq.com. Sandbox API
|
||||
// keys start with "sandbox_"; OAuth access tokens do not say.
|
||||
export enum class BunqEnvironment : std::uint8_t {
|
||||
Production,
|
||||
Sandbox
|
||||
};
|
||||
|
||||
// A server to send bunq's API and OAuth token calls to instead of bunq's own: a local fake, for tests.
|
||||
export struct BunqEndpoint {
|
||||
std::string host;
|
||||
std::uint16_t port = 443;
|
||||
std::string caPem; // a trust anchor beside the system store, for a self-signed certificate
|
||||
};
|
||||
|
||||
// An error response from bunq, with its HTTP status (bunq also uses 466, 490 and 491) and error_description.
|
||||
export struct BunqError : std::runtime_error {
|
||||
std::uint16_t status;
|
||||
BunqError(std::uint16_t status, const std::string& message);
|
||||
};
|
||||
|
||||
// The other side of a payment or a request, as bunq addresses it.
|
||||
export struct BunqCounterparty {
|
||||
std::string type; // "IBAN", "EMAIL" or "PHONE_NUMBER"
|
||||
std::string value; // an IBAN without spaces, an e-mail address, or a phone number in international format
|
||||
std::string name; // required by bunq for an IBAN outside bunq
|
||||
|
||||
// `value` with an '@' is an e-mail address, one starting with '+' a phone number, anything else an IBAN,
|
||||
// whose check digits are verified so a typo fails here rather than at the bank.
|
||||
static BunqCounterparty Parse(std::string_view value, std::string_view name);
|
||||
};
|
||||
|
||||
// How bunq shows a party in a listing.
|
||||
export struct BunqLabel {
|
||||
std::string name;
|
||||
std::string iban; // empty when bunq gives none (a card merchant, or a party known by e-mail or phone)
|
||||
};
|
||||
|
||||
// A request for money this account sent (a bunq RequestInquiry).
|
||||
export struct BunqPaymentRequest {
|
||||
std::uint64_t id;
|
||||
std::string status; // PENDING, ACCEPTED, REJECTED, REVOKED or EXPIRED
|
||||
BunqLabel counterparty;
|
||||
Currency currency;
|
||||
std::int64_t amount; // what was asked, in the currency's minor unit
|
||||
std::optional<std::int64_t> amountPaid; // what was paid, once accepted
|
||||
std::string description;
|
||||
std::chrono::system_clock::time_point created;
|
||||
std::string shareUrl; // a bunq.me link anyone can pay through, when bunq made one
|
||||
};
|
||||
|
||||
// A request for money this account received (a bunq RequestResponse). Incoming SEPA direct debits are these too.
|
||||
export struct BunqIncomingRequest {
|
||||
std::uint64_t id;
|
||||
std::string status; // PENDING, ACCEPTED, REJECTED, REFUND_REQUESTED, REFUNDED or REVOKED
|
||||
std::string type; // INTERNAL, DIRECT_DEBIT, DIRECT_DEBIT_B2B, IDEAL or SOFORT
|
||||
BunqLabel counterparty;
|
||||
Currency currency;
|
||||
std::int64_t amount; // what is asked, in the currency's minor unit
|
||||
std::string description;
|
||||
std::chrono::system_clock::time_point created;
|
||||
};
|
||||
|
||||
// When a scheduled payment runs. bunq keeps these times in UTC.
|
||||
export struct BunqSchedule {
|
||||
std::chrono::system_clock::time_point start;
|
||||
std::optional<std::chrono::system_clock::time_point> end;
|
||||
std::string unit = "ONCE"; // ONCE, HOURLY, DAILY, WEEKLY, MONTHLY or YEARLY
|
||||
std::uint32_t every = 1; // with WEEKLY, 2 is every other week
|
||||
std::string status; // ACTIVE, FINISHED or CANCELLED; ignored when scheduling
|
||||
std::optional<std::chrono::system_clock::time_point> next; // the next run, when bunq says; ignored when scheduling
|
||||
|
||||
// Throws std::invalid_argument for a unit bunq does not know or `every` of 0, as Schedule would.
|
||||
void Check() const;
|
||||
};
|
||||
|
||||
// A payment bunq makes on a schedule (a bunq SchedulePayment).
|
||||
export struct BunqScheduledPayment {
|
||||
std::uint64_t id;
|
||||
BunqLabel counterparty;
|
||||
Currency currency;
|
||||
std::int64_t amount; // each payment, in the currency's minor unit
|
||||
std::string description;
|
||||
BunqSchedule schedule;
|
||||
};
|
||||
|
||||
// A payment waiting for approval (a bunq DraftPayment): made through OAuth, which may not pay others directly, or
|
||||
// for an account that needs a second approval.
|
||||
export struct BunqDraftPayment {
|
||||
struct Entry {
|
||||
BunqLabel counterparty;
|
||||
Currency currency;
|
||||
std::int64_t amount;
|
||||
std::string description;
|
||||
};
|
||||
std::uint64_t id;
|
||||
std::string status; // PENDING, ACCEPTED, REJECTED or CANCELLED
|
||||
std::string createdBy;
|
||||
std::vector<Entry> entries; // each becomes a payment when the draft is accepted
|
||||
std::string updated; // bunq's time of its last change, as bunq wrote it; empty when bunq gave none
|
||||
};
|
||||
|
||||
export struct BunqCard {
|
||||
std::uint64_t id;
|
||||
std::string kind; // bunq's object: CardDebit, CardCredit, ...
|
||||
std::string productType; // MASTERCARD_DEBIT, MAESTRO_DEBIT, ...
|
||||
std::string status; // ACTIVE, DEACTIVATED, LOST, STOLEN, CANCELLED, EXPIRED or PIN_TRIES_EXCEEDED
|
||||
std::string orderStatus;
|
||||
std::string nameOnCard;
|
||||
std::string secondLine;
|
||||
std::string lastFour; // last four digits of the card number
|
||||
std::string expiry; // YYYY-MM-DD
|
||||
std::string iban; // the account it pays from
|
||||
};
|
||||
|
||||
export struct BunqAccount : public Account {
|
||||
std::uint32_t id;
|
||||
std::uint64_t id;
|
||||
std::string type; // bunq's object: MonetaryAccountBank, MonetaryAccountSavings, MonetaryAccountJoint, ...
|
||||
BunqConnector* connector;
|
||||
|
||||
BunqAccount(std::string&& name, std::string&& bic, std::string&& iban, Currency currency, std::int64_t balance, std::uint32_t id, BunqConnector* connector);
|
||||
void Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const;
|
||||
BunqAccount(std::string&& name, std::string&& bic, std::string&& iban, Currency currency, std::int64_t balance, std::uint64_t id, std::string&& type, BunqConnector* connector);
|
||||
|
||||
// "/user/{user}/monetary-account/{id}", the API path everything below hangs from.
|
||||
std::string Path() const;
|
||||
// Where this account's transactions are stored (the `connector` argument of Account's transaction calls):
|
||||
// the connector's StorageKey and the account id, so two accounts with one name never share a table.
|
||||
std::string StorageKey() const;
|
||||
|
||||
// Amounts are in the currency's minor unit and positive; `to` and `from` are read by BunqCounterparty::Parse.
|
||||
// Each returns the id bunq gave the new object.
|
||||
std::uint64_t Send(std::string_view to, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const;
|
||||
std::uint64_t Draft(std::string_view to, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const;
|
||||
std::uint64_t Schedule(std::string_view to, std::string_view name, std::int64_t amount, Currency currency, std::string_view description, const BunqSchedule& schedule) const;
|
||||
// Asks `from` for money. bunq.me is allowed, so someone without bunq can pay through the returned share URL.
|
||||
BunqPaymentRequest Request(std::string_view from, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const;
|
||||
|
||||
// Newest first.
|
||||
std::vector<BunqPaymentRequest> Requests() const;
|
||||
std::vector<BunqIncomingRequest> IncomingRequests() const;
|
||||
std::vector<BunqScheduledPayment> ScheduledPayments() const;
|
||||
std::vector<BunqDraftPayment> DraftPayments() const;
|
||||
// One of each, as bunq has it now: what to show before answering it.
|
||||
BunqIncomingRequest IncomingRequest(std::uint64_t id) const;
|
||||
BunqDraftPayment DraftPayment(std::uint64_t id) const;
|
||||
|
||||
void RevokeRequest(std::uint64_t id) const;
|
||||
// Answers the request as it was shown: accepting pays shown.amount from this account, whatever the request
|
||||
// asks by now.
|
||||
void RespondToRequest(const BunqIncomingRequest& shown, bool accept) const;
|
||||
void CancelScheduledPayment(std::uint64_t id) const;
|
||||
// Answers the draft as it was shown: bunq refuses the answer when the draft changed since (by shown.updated).
|
||||
void RespondToDraftPayment(const BunqDraftPayment& shown, bool accept) const;
|
||||
};
|
||||
|
||||
export struct BunqConnector {
|
||||
std::string sessionToken;
|
||||
BunqEnvironment environment = BunqEnvironment::Production;
|
||||
std::string apiKey; // the API key, or the OAuth access token it was connected with
|
||||
std::string installationToken;
|
||||
EVP_PKEY* privateKey;
|
||||
std::string apiKey;
|
||||
std::string sessionToken;
|
||||
std::uint64_t sessionId = 0; // 0 for connectors saved before it was kept: bunq logs out the current session for 0
|
||||
Crypto::Key privateKey;
|
||||
Crypto::Key serverPublicKey; // verifies every response; fetched on first use for connectors saved before it was kept
|
||||
std::string userType; // UserPerson, UserCompany, UserApiKey (connected through OAuth) or UserPaymentServiceProvider
|
||||
std::string name;
|
||||
std::uint64_t id = 0; // the user id in API paths; for OAuth the UserApiKey's own id
|
||||
std::uint64_t ownerId = 0; // the bunq user whose accounts these are: `id`, or for OAuth the user who granted access
|
||||
std::vector<BunqAccount> accounts;
|
||||
std::uint32_t id;
|
||||
|
||||
// One call to the bunq API: `path` is below /v1 ("/user/1/monetary-account"), or a pagination URL. Bodies are
|
||||
// signed, responses verified against the server's key, rate limits waited out, and an expired session
|
||||
// renewed once. Throws BunqError for an error response. A POST, PUT or DELETE goes over a new connection, so
|
||||
// nothing below replays it; when its outcome is unknown (a lost connection, a 5xx) the error says so.
|
||||
nlohmann::json Get(std::string_view path);
|
||||
nlohmann::json Post(std::string_view path, const nlohmann::json& body);
|
||||
nlohmann::json Put(std::string_view path, const nlohmann::json& body);
|
||||
nlohmann::json Delete(std::string_view path);
|
||||
// Every item of a listing, following its pages, newest first.
|
||||
std::vector<nlohmann::json> List(std::string_view path);
|
||||
|
||||
void RefreshSession();
|
||||
// All open accounts, of every kind bunq has. One in a currency Nostro does not know is skipped, with a Note.
|
||||
void RefreshAccounts();
|
||||
// Fetches each account's payments newer than the last stored one and stores them.
|
||||
void GetNewTransactions();
|
||||
void GetNewTransactions(BunqAccount& account);
|
||||
bool TestConnector();
|
||||
// Ends the session at bunq. The API key or OAuth grant stays valid; revoke it in the bunq app.
|
||||
void Logout();
|
||||
|
||||
std::vector<BunqCard> Cards();
|
||||
// DEACTIVATED and ACTIVE freeze and unfreeze a card; LOST and STOLEN block it for good. Returns the card as
|
||||
// bunq has it afterwards, which can differ from what was asked: a card never activated stays DEACTIVATED.
|
||||
BunqCard SetCardStatus(std::uint64_t card, std::string_view status);
|
||||
|
||||
// The directory of this connector's transaction tables below <cache>/transactions: bunq, the environment
|
||||
// and the user id, so two connectors never share a table.
|
||||
std::string StorageKey() const;
|
||||
// Connected through OAuth: bunq then allows payments between the user's own accounts and draft payments only.
|
||||
bool IsOAuth() const;
|
||||
|
||||
static constexpr std::string connectorId = "bunq";
|
||||
inline static ClientHTTP1 client = ClientHTTP1("public-api.sandbox.bunq.com", 443, Crafter::TLSClientCredentials{});
|
||||
static std::vector<BunqConnector> connectors;
|
||||
static BunqConnector& Create(std::string&& apiKey, std::string_view allowedIp);
|
||||
// A deque, so an account's `connector` stays valid when another connector is added.
|
||||
static std::deque<BunqConnector> connectors;
|
||||
|
||||
// Registers a new key pair and device for `apiKey` (an API key or OAuth access token) and opens a session.
|
||||
// `allowedIps` is a comma-separated list for the device, "*" for any address (a wildcard API key), or empty
|
||||
// for the address this call comes from. A connector of the same kind for the same bunq user is replaced (that
|
||||
// is how a key is renewed); one of the other kind (API key against OAuth) is refused, as every account would
|
||||
// then be listed twice.
|
||||
static BunqConnector& Create(std::string&& apiKey, std::string_view allowedIps, BunqEnvironment environment);
|
||||
// Forgets a connector: deletes its stored transactions and takes it out of `connectors`, keeping the other
|
||||
// connectors' accounts pointing at their connector. Does not log out; call Logout first.
|
||||
static void Remove(BunqConnector& connector);
|
||||
static BunqEnvironment EnvironmentOfKey(std::string_view apiKey);
|
||||
|
||||
// OAuth: send the user to this URL, where they approve with the bunq app; bunq then redirects to
|
||||
// `redirectUri` with `code` and `state` in the query. `redirectUri` must be registered with the OAuth client.
|
||||
static std::string OAuthAuthorizationUrl(BunqEnvironment environment, std::string_view clientId, std::string_view redirectUri, std::string_view state);
|
||||
// Exchanges the code from that redirect for an access token, which Create takes in place of an API key.
|
||||
static std::string OAuthExchangeCode(BunqEnvironment environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code);
|
||||
|
||||
// Where API calls for `environment` go: bunq's own host, or wherever RedirectTo sent them.
|
||||
static BunqEndpoint Endpoint(BunqEnvironment environment);
|
||||
// Sends every API and OAuth token call, whatever the connector's environment, to `endpoint` from now on.
|
||||
// For tests against a local server; nostro itself never calls it.
|
||||
static void RedirectTo(BunqEndpoint endpoint);
|
||||
|
||||
using Account = BunqAccount;
|
||||
|
||||
private:
|
||||
enum class Auth : std::uint8_t { None, Installation, Session };
|
||||
nlohmann::json Call(std::string_view method, std::string_view path, const nlohmann::json* body, Auth auth, bool renew = true);
|
||||
void ReadUser(const nlohmann::json& response);
|
||||
void EnsureServerPublicKey();
|
||||
bool fetchingServerKey = false;
|
||||
};
|
||||
|
||||
void to_json(nlohmann::json& j, const BunqConnector& connector);
|
||||
void from_json(const nlohmann::json& j, BunqConnector& connector);
|
||||
|
||||
// Shared by the implementation units; not exported.
|
||||
namespace BunqJson {
|
||||
// The object under `key` in a response's "Response" array.
|
||||
nlohmann::json Item(const nlohmann::json& response, std::string_view key);
|
||||
// The single object inside an item, such as the MonetaryAccountBank in {"MonetaryAccountBank": {...}}, with its key.
|
||||
std::pair<std::string, const nlohmann::json*> Only(const nlohmann::json& item);
|
||||
nlohmann::json Amount(std::int64_t amount, Currency currency);
|
||||
nlohmann::json Pointer(const BunqCounterparty& counterparty);
|
||||
std::pair<Currency, std::int64_t> ReadAmount(const nlohmann::json& amount);
|
||||
BunqLabel ReadLabel(const nlohmann::json& label);
|
||||
std::string String(const nlohmann::json& object, std::string_view key);
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
|
|
@ -62,4 +281,74 @@ struct std::formatter<Nostro::BunqConnector> : std::formatter<std::string> {
|
|||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
template<>
|
||||
struct std::formatter<Nostro::BunqLabel> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqLabel& obj, std::format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(obj.iban.empty() ? obj.name : std::format("{} {}", obj.name, obj.iban), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::formatter<Nostro::BunqPaymentRequest> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqPaymentRequest& obj, std::format_context& ctx) const {
|
||||
std::string out = std::format("{}\t{}\t{}\t{}\t{}", obj.id, Nostro::FormatLocalTime(obj.created), obj.status, obj.counterparty, Nostro::FormatMoney(obj.amount, obj.currency));
|
||||
if(obj.amountPaid && *obj.amountPaid != obj.amount) {
|
||||
out += std::format(" (paid {})", Nostro::FormatMoney(*obj.amountPaid, obj.currency));
|
||||
}
|
||||
out += std::format("\t{}", obj.description);
|
||||
if(!obj.shareUrl.empty()) {
|
||||
out += std::format("\t{}", obj.shareUrl);
|
||||
}
|
||||
return std::formatter<std::string>::format(out, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::formatter<Nostro::BunqIncomingRequest> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqIncomingRequest& obj, std::format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{}\t{}\t{}\t{}\t{}\t{}\t{}", obj.id, Nostro::FormatLocalTime(obj.created), obj.status, obj.type, obj.counterparty, Nostro::FormatMoney(obj.amount, obj.currency), obj.description), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::formatter<Nostro::BunqScheduledPayment> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqScheduledPayment& obj, std::format_context& ctx) const {
|
||||
const Nostro::BunqSchedule& schedule = obj.schedule;
|
||||
const std::map<std::string_view, std::pair<std::string_view, std::string_view>> units = {
|
||||
{"HOURLY", {"hourly", "hours"}}, {"DAILY", {"daily", "days"}}, {"WEEKLY", {"weekly", "weeks"}}, {"MONTHLY", {"monthly", "months"}}, {"YEARLY", {"yearly", "years"}},
|
||||
};
|
||||
std::string when;
|
||||
if(const auto unit = units.find(schedule.unit); unit != units.end()) {
|
||||
when = schedule.every == 1 ? std::format("{} from {}", unit->second.first, Nostro::FormatLocalTime(schedule.start)) : std::format("every {} {} from {}", schedule.every, unit->second.second, Nostro::FormatLocalTime(schedule.start));
|
||||
} else {
|
||||
when = std::format("once on {}", Nostro::FormatLocalTime(schedule.start));
|
||||
}
|
||||
if(schedule.end) {
|
||||
when += std::format(" until {}", Nostro::FormatLocalTime(*schedule.end));
|
||||
}
|
||||
if(schedule.next && schedule.status == "ACTIVE") {
|
||||
when += std::format(", next {}", Nostro::FormatLocalTime(*schedule.next));
|
||||
}
|
||||
return std::formatter<std::string>::format(std::format("{}\t{}\t{}\t{}\t{}\t{}", obj.id, schedule.status, when, obj.counterparty, Nostro::FormatMoney(obj.amount, obj.currency), obj.description), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::formatter<Nostro::BunqDraftPayment> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqDraftPayment& obj, std::format_context& ctx) const {
|
||||
std::string out = std::format("{}\t{}\t{}", obj.id, obj.status, obj.createdBy);
|
||||
for(const Nostro::BunqDraftPayment::Entry& entry : obj.entries) {
|
||||
out += std::format("\n\t{}\t{}\t{}", entry.counterparty, Nostro::FormatMoney(entry.amount, entry.currency), entry.description);
|
||||
}
|
||||
return std::formatter<std::string>::format(out, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::formatter<Nostro::BunqCard> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqCard& obj, std::format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{}\t{}\t{}\t**** {}\t{}\t{}\t{}\texpires {}", obj.id, obj.status, obj.productType, obj.lastFour, obj.nameOnCard, obj.secondLine, obj.iban, obj.expiry), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
295
interfaces/Nostro-Bus.cppm
Normal file
295
interfaces/Nostro-Bus.cppm
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
// 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 Nostro.Checkout;
|
||||
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";
|
||||
|
||||
// What nostrod answers with besides the standard AccessDenied and InteractiveAuthorizationRequired. The ones an app
|
||||
// at checkout tells apart too are Nostro.Checkout's.
|
||||
inline constexpr std::string_view errorInvalidArgument = Checkout::errorInvalidArgument; // refused before anything was sent
|
||||
inline constexpr std::string_view errorBank = Checkout::errorBank; // 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 = Checkout::errorCancelled; // the user did not authenticate at the prompt
|
||||
inline constexpr std::string_view errorNoCheckoutAccount = Checkout::errorNoCheckoutAccount; // a checkout, and no account chosen to pay it
|
||||
inline constexpr std::string_view errorFailed = Checkout::errorFailed; // anything else
|
||||
|
||||
// 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";
|
||||
inline constexpr std::string_view actionCheckout = "net.catcrafts.Nostro1.Checkout";
|
||||
}
|
||||
|
||||
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},
|
||||
};
|
||||
|
||||
// net.catcrafts.Nostro1.Checkout, which nostrod serves under a bus name of its own (Checkout::busName). Its reply
|
||||
// carries no notes: they are the user's, and the app that asks is someone else.
|
||||
inline constexpr std::array checkoutMethods = {
|
||||
Method{"Pay", Checkout::payArguments, Checkout::payReply, Checkout::payNames},
|
||||
};
|
||||
|
||||
// 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 Interface(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();
|
||||
};
|
||||
|
||||
// A vtable of every method of `table` (methods or checkoutMethods), all of them unprivileged (polkit decides, not
|
||||
// sd-bus), each handled by `handler(member)`. It must outlive the object it is registered for.
|
||||
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
|
||||
74
interfaces/Nostro-Client.cppm
Normal file
74
interfaces/Nostro-Client.cppm
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Client;
|
||||
#ifdef NOSTRO_DBUS_ENABLED
|
||||
|
||||
import std;
|
||||
import :Account;
|
||||
import :Bunq;
|
||||
import :Bus;
|
||||
import :Transaction;
|
||||
|
||||
namespace Nostro {
|
||||
// An error nostrod answered with, by its D-Bus name, for the errors a client tells apart: Bus::errorBank,
|
||||
// Bus::errorOAuthCannotPay, Bus::errorCancelled, org.freedesktop.DBus.Error.AccessDenied and the like. Input
|
||||
// nostrod refused before doing anything comes as std::invalid_argument instead.
|
||||
export struct DaemonError : std::runtime_error {
|
||||
std::string name;
|
||||
DaemonError(std::string name, const std::string& message);
|
||||
};
|
||||
|
||||
// nostrod over the system bus: each method of its interface (Bus::methods), with nostro's own types. `account` is
|
||||
// an account's name or IBAN. A call may wait for a password dialog, which nostrod has polkit show, and for the
|
||||
// bank. The library's warnings during the calls come out of TakeNotes().
|
||||
export class Client {
|
||||
public:
|
||||
// Connects to the system bus; D-Bus starts nostrod with the first call. A call waits up to `timeout` for its
|
||||
// answer: long enough for the user to type a password, and for the bank's rate limits. Throws when there is
|
||||
// no system bus.
|
||||
static Client System(std::chrono::seconds timeout = std::chrono::minutes(10));
|
||||
|
||||
std::vector<ConnectorSummary> Connectors();
|
||||
// `environment` is "production", "sandbox", or empty for the one the key's prefix says.
|
||||
AddedConnector AddBunq(std::string_view apiKey, std::string_view allowedIps, std::string_view environment);
|
||||
AddedConnector AddBunqOAuth(std::string_view environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code);
|
||||
// By name, or by its number in Connectors(); returns the name.
|
||||
std::string RemoveConnector(std::string_view connector);
|
||||
std::vector<ConnectorAccounts> Accounts();
|
||||
// Fetches what is new first, then answers from the stored history.
|
||||
std::vector<Transaction> Transactions(std::string_view account, const TransactionQuery& query);
|
||||
|
||||
Created Send(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description);
|
||||
Created Schedule(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, const BunqSchedule& schedule);
|
||||
Created Draft(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description);
|
||||
BunqPaymentRequest Request(std::string_view account, std::string_view from, std::string_view name, std::int64_t amount, std::string_view description);
|
||||
|
||||
std::vector<BunqPaymentRequest> Requests(std::string_view account);
|
||||
void RevokeRequest(std::string_view account, std::uint64_t id);
|
||||
std::vector<BunqIncomingRequest> IncomingRequests(std::string_view account);
|
||||
void RespondToRequest(std::string_view account, std::uint64_t id, bool accept);
|
||||
std::vector<BunqScheduledPayment> ScheduledPayments(std::string_view account);
|
||||
void CancelScheduledPayment(std::string_view account, std::uint64_t id);
|
||||
std::vector<BunqDraftPayment> DraftPayments(std::string_view account);
|
||||
void RespondToDraftPayment(std::string_view account, std::uint64_t id, bool accept);
|
||||
std::vector<ConnectorCards> Cards();
|
||||
// "ACTIVE", "DEACTIVATED", "LOST" or "STOLEN"; returns the card as bunq has it afterwards.
|
||||
BunqCard SetCardStatus(std::uint64_t card, std::string_view status);
|
||||
|
||||
// The account apps' checkouts (Nostro.Checkout) pay from: none until one is chosen, or once it is gone.
|
||||
std::optional<Account> DefaultCheckoutAccount();
|
||||
// Chooses it, and returns it. An account reached through OAuth cannot pay a shop, and is refused.
|
||||
Account SetDefaultCheckoutAccount(std::string_view account);
|
||||
|
||||
// The library's warnings from the calls so far, taken out.
|
||||
std::vector<std::string> TakeNotes();
|
||||
|
||||
private:
|
||||
explicit Client(Bus::BusPtr bus);
|
||||
Bus::BusPtr bus;
|
||||
std::vector<std::string> notes;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,14 +1,19 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Connector;
|
||||
|
||||
import std;
|
||||
|
||||
namespace Nostro {
|
||||
// Both take an exclusive lock on connectors.lock in the config directory and hold it until the process ends, so
|
||||
// a second nostro process waits (saying so through Note) instead of overwriting the first one's changes.
|
||||
export struct Connector {
|
||||
// Writes every bank's connectors to connectors.json in the config directory: synced to the disk, then
|
||||
// renamed over the old file.
|
||||
static void SaveConnectors();
|
||||
// Reads connectors.json (a missing file is no connectors yet) and refreshes each connector's accounts. A
|
||||
// connector whose refresh fails keeps no accounts and is reported through Note, so the others stay usable.
|
||||
static void LoadConnectors();
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
|
|
@ -14,7 +14,7 @@ export module Nostro:Crypto;
|
|||
import std;
|
||||
|
||||
export namespace Nostro::Crypto {
|
||||
long ThrowOnOpenssl(long status);
|
||||
std::int64_t ThrowOnOpenssl(std::int64_t status);
|
||||
|
||||
template <typename T>
|
||||
T* ThrowOnOpenssl(T* pointer) {
|
||||
|
|
@ -25,9 +25,22 @@ export namespace Nostro::Crypto {
|
|||
return pointer;
|
||||
}
|
||||
|
||||
// A key that frees itself; copies share it.
|
||||
using Key = std::shared_ptr<EVP_PKEY>;
|
||||
Key OwnKey(EVP_PKEY* key);
|
||||
|
||||
// RSA PKCS#1 v1.5 over SHA-256, base64: what bunq expects in X-Bunq-Client-Signature.
|
||||
std::string SignBody(EVP_PKEY* privateKey, std::string_view body);
|
||||
// Checks a base64 signature made as SignBody makes one, with the signer's public key.
|
||||
bool VerifyBody(EVP_PKEY* publicKey, std::string_view body, std::string_view signature);
|
||||
// A new 2048-bit RSA key pair and its public half in PEM.
|
||||
std::tuple<EVP_PKEY*, std::string> CreateKey();
|
||||
EVP_PKEY* LoadKeyFromPem(std::string_view pem);
|
||||
EVP_PKEY* LoadPublicKeyFromPem(std::string_view pem);
|
||||
std::string PrivateKeyToPem(EVP_PKEY* key);
|
||||
std::string PublicKeyToPem(EVP_PKEY* key);
|
||||
// `bytes` random bytes from the system's CSPRNG, as lower-case hex.
|
||||
std::string RandomHex(std::size_t bytes);
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Currency;
|
||||
|
||||
|
|
@ -9,7 +9,27 @@ export namespace Nostro {
|
|||
enum class Currency : std::uint16_t {
|
||||
Euro
|
||||
};
|
||||
}
|
||||
|
||||
namespace Nostro {
|
||||
struct CurrencyInfo {
|
||||
std::string_view code;
|
||||
std::string_view symbol;
|
||||
std::uint8_t decimals;
|
||||
};
|
||||
|
||||
// What nostro knows of each currency, in the order of Currency's values: a new currency is a value there and a
|
||||
// row here.
|
||||
constexpr std::array<CurrencyInfo, 1> currencies = {{
|
||||
{"EUR", "€", 2},
|
||||
}};
|
||||
|
||||
constexpr const CurrencyInfo& Info(Currency currency) {
|
||||
return currencies[std::to_underlying(currency)];
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Nostro {
|
||||
constexpr Currency GetCurrency(std::string_view name) {
|
||||
if(name == "EUR" || name == "eur") {
|
||||
return Currency::Euro;
|
||||
|
|
@ -19,24 +39,15 @@ export namespace Nostro {
|
|||
}
|
||||
|
||||
constexpr std::string_view GetCurrencyCode(Currency code) {
|
||||
switch(code) {
|
||||
case Currency::Euro: return "EUR";
|
||||
}
|
||||
std::unreachable();
|
||||
return Info(code).code;
|
||||
}
|
||||
|
||||
constexpr std::string_view GetCurrencySymbol(Currency code) {
|
||||
switch(code) {
|
||||
case Currency::Euro: return "€";
|
||||
}
|
||||
std::unreachable();
|
||||
return Info(code).symbol;
|
||||
}
|
||||
|
||||
constexpr std::uint8_t GetCurrencyDecimals(Currency code) {
|
||||
switch(code) {
|
||||
case Currency::Euro: return 2;
|
||||
}
|
||||
std::unreachable();
|
||||
return Info(code).decimals;
|
||||
}
|
||||
|
||||
constexpr std::int64_t ParseAmount(std::string_view value, Currency currency) {
|
||||
|
|
@ -91,6 +102,12 @@ export namespace Nostro {
|
|||
}
|
||||
return digits;
|
||||
}
|
||||
|
||||
// The amount with its currency's symbol, the sign in front: "-€1.02".
|
||||
constexpr std::string FormatMoney(std::int64_t minor, Currency currency) {
|
||||
const std::string amount = FormatAmount(minor, currency);
|
||||
return amount.starts_with('-') ? std::format("-{}{}", GetCurrencySymbol(currency), amount.substr(1)) : std::format("{}{}", GetCurrencySymbol(currency), amount);
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
|
|
|
|||
31
interfaces/Nostro-Daemon.cppm
Normal file
31
interfaces/Nostro-Daemon.cppm
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Daemon;
|
||||
#ifdef NOSTRO_DBUS_ENABLED
|
||||
|
||||
import std;
|
||||
|
||||
namespace Nostro {
|
||||
// How nostrod runs: where it keeps each user's connectors and transactions, and when it lets go.
|
||||
export struct DaemonOptions {
|
||||
// Each user's directory is users/<uid> below it.
|
||||
std::filesystem::path state;
|
||||
// A worker exits after this long without a call, and the front once no worker is left; the next call starts
|
||||
// them again, D-Bus activation the front.
|
||||
std::chrono::seconds idle{300};
|
||||
// How long a call waits for polkit, the user's password prompt included, before it is refused.
|
||||
std::chrono::seconds promptTimeout{300};
|
||||
};
|
||||
|
||||
// The front: owns net.catcrafts.Nostro1 on the system bus, works out each caller's uid, and hands every call to
|
||||
// that uid's worker, started on demand as a process of its own. It holds no connector itself. Returns 0 once it
|
||||
// was idle with no worker left or the bus went away, after it ended its workers. Throws when it cannot start.
|
||||
export std::int32_t RunDaemon(const DaemonOptions& options);
|
||||
|
||||
// A worker: the library for one user, `uid`, whom logind calls `name`, serving the front over the connected socket
|
||||
// `fd`. Every call is authorised with polkit before it runs. Returns 0 when it was idle or the front went away.
|
||||
export std::int32_t RunWorker(std::int32_t fd, std::uint32_t uid, std::string_view name, const DaemonOptions& options);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:File;
|
||||
|
||||
|
|
@ -13,7 +13,7 @@ export namespace Nostro {
|
|||
fs::path configPath = GetConfigPath();
|
||||
fs::path cachePath = GetCachePath();
|
||||
void CreatePrivateFile(const fs::path& path);
|
||||
void WriteString(std::ofstream& file, std::string_view str);
|
||||
void ReadString(std::ifstream& file, std::string& str);
|
||||
std::uint16_t SeekString(std::ifstream& file);
|
||||
// A line from stdin, trimmed. On a terminal the prompt goes to stderr and what is typed is not echoed, so a key
|
||||
// never sits in the shell's history or in the process's command line. Throws when stdin ends first.
|
||||
std::string ReadSecret(std::string_view prompt);
|
||||
}
|
||||
|
|
@ -1,13 +1,15 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Transaction;
|
||||
|
||||
import std;
|
||||
import :Currency;
|
||||
import :Utils;
|
||||
|
||||
namespace Nostro {
|
||||
export struct Transaction {
|
||||
// Version 1 of Transaction, kept so tables written by it migrate when opened. Do not change it.
|
||||
struct TransactionV1 {
|
||||
std::string name;
|
||||
std::string bic;
|
||||
std::string iban;
|
||||
|
|
@ -16,8 +18,37 @@ namespace Nostro {
|
|||
std::int64_t amount;
|
||||
std::chrono::system_clock::time_point timePoint;
|
||||
|
||||
Transaction();
|
||||
Transaction(std::string&& name, std::string&& bic, std::string&& iban, std::string&& description, Currency currency, std::int64_t amount, std::chrono::system_clock::time_point timePoint);
|
||||
static constexpr std::uint32_t version = 1;
|
||||
static constexpr auto orderedBy = &TransactionV1::timePoint;
|
||||
};
|
||||
|
||||
// Also the row type of the Crafter.DB transaction tables, so it has to stay an aggregate. The members are the
|
||||
// on-disk schema: changing them needs a `version` bump (with the old struct as `using Previous`), or existing
|
||||
// tables are refused when opened.
|
||||
export struct Transaction {
|
||||
std::string name;
|
||||
std::string bic;
|
||||
std::string iban;
|
||||
std::string description;
|
||||
Currency currency;
|
||||
std::int64_t amount;
|
||||
std::chrono::system_clock::time_point timePoint;
|
||||
// The bank's own id for it, so a sync that overlaps the last stored timestamp stores nothing twice.
|
||||
// Empty for rows stored by version 1.
|
||||
std::string id;
|
||||
// The bank's own classification, e.g. bunq's "MASTERCARD/PAYMENT" or "EBA_SDD/SDD". Empty for rows stored by version 1.
|
||||
std::string kind;
|
||||
|
||||
static constexpr std::uint32_t version = 2;
|
||||
static constexpr auto orderedBy = &Transaction::timePoint;
|
||||
using Previous = TransactionV1;
|
||||
};
|
||||
}
|
||||
|
||||
// One line of a transaction list: local time, amount, counterparty, IBAN and description, tab separated.
|
||||
template<>
|
||||
struct std::formatter<Nostro::Transaction> : std::formatter<std::string> {
|
||||
auto format(const Nostro::Transaction& obj, std::format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{}\t{}\t{}\t{}\t{}", Nostro::FormatLocalTime(obj.timePoint), Nostro::FormatMoney(obj.amount, obj.currency), obj.name, obj.iban, obj.description), ctx);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Utils;
|
||||
|
||||
|
|
@ -19,8 +19,8 @@ export namespace Nostro {
|
|||
case '\r': out += "\\r"; break;
|
||||
case '\t': out += "\\t"; break;
|
||||
default:
|
||||
if (static_cast<unsigned char>(c) < 0x20) {
|
||||
out += std::format("\\u{:04x}", static_cast<unsigned char>(c));
|
||||
if (static_cast<std::uint8_t>(c) < 0x20) {
|
||||
out += std::format("\\u{:04x}", static_cast<std::uint8_t>(c));
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
|
|
@ -31,13 +31,11 @@ export namespace Nostro {
|
|||
|
||||
constexpr std::chrono::system_clock::time_point ParseTimestamp(std::string_view text) {
|
||||
const auto field = [text](std::size_t offset, std::size_t length) {
|
||||
// substr cuts a field that would run past the end short, and the size check refuses it.
|
||||
const std::string_view digits = text.substr(offset, length);
|
||||
std::uint32_t value = 0;
|
||||
const char* const first = text.data() + offset;
|
||||
if(offset + length > text.size()) {
|
||||
throw std::runtime_error(std::format("malformed timestamp '{}'", text));
|
||||
}
|
||||
const std::from_chars_result result = std::from_chars(first, first + length, value);
|
||||
if(result.ec != std::errc() || result.ptr != first + length) {
|
||||
const std::from_chars_result result = std::from_chars(digits.data(), digits.data() + digits.size(), value);
|
||||
if(digits.size() != length || result.ec != std::errc() || result.ptr != digits.data() + digits.size()) {
|
||||
throw std::runtime_error(std::format("malformed timestamp '{}'", text));
|
||||
}
|
||||
return value;
|
||||
|
|
@ -66,4 +64,117 @@ export namespace Nostro {
|
|||
|
||||
return std::chrono::sys_days(date) + std::chrono::hours(field(11, 2)) + std::chrono::minutes(field(14, 2)) + std::chrono::seconds(field(17, 2)) + std::chrono::microseconds(micros);
|
||||
}
|
||||
|
||||
// The inverse of ParseTimestamp: "YYYY-MM-DD hh:mm:ss.ffffff" in UTC, the format bunq reads and writes.
|
||||
inline std::string FormatTimestamp(std::chrono::system_clock::time_point time) {
|
||||
return std::format("{:%Y-%m-%d %H:%M:%S}", std::chrono::floor<std::chrono::microseconds>(time));
|
||||
}
|
||||
|
||||
// "YYYY-MM-DD" or "YYYY-MM-DD hh:mm" in the local time zone, for dates typed by a person, moved `addDays` days on
|
||||
// by the calendar: ParseLocalDate(day, 1) is the midnight that ends `day`, 23 or 25 hours on when the clocks change.
|
||||
inline std::chrono::system_clock::time_point ParseLocalDate(std::string_view text, std::int32_t addDays = 0) {
|
||||
const auto number = [text](std::size_t offset, std::size_t length) {
|
||||
int value = 0;
|
||||
const char* const first = text.data() + offset;
|
||||
const std::from_chars_result result = std::from_chars(first, first + length, value);
|
||||
if(result.ec != std::errc() || result.ptr != first + length) {
|
||||
throw std::runtime_error(std::format("'{}' is not a date (YYYY-MM-DD or YYYY-MM-DD hh:mm)", text));
|
||||
}
|
||||
return value;
|
||||
};
|
||||
const bool hasTime = text.size() == 16;
|
||||
if((text.size() != 10 && !hasTime) || text[4] != '-' || text[7] != '-' || (hasTime && (text[10] != ' ' || text[13] != ':'))) {
|
||||
throw std::runtime_error(std::format("'{}' is not a date (YYYY-MM-DD or YYYY-MM-DD hh:mm)", text));
|
||||
}
|
||||
std::tm local{};
|
||||
local.tm_year = number(0, 4) - 1900;
|
||||
local.tm_mon = number(5, 2) - 1;
|
||||
local.tm_mday = number(8, 2);
|
||||
local.tm_hour = hasTime ? number(11, 2) : 0;
|
||||
local.tm_min = hasTime ? number(14, 2) : 0;
|
||||
local.tm_isdst = -1;
|
||||
const std::chrono::year_month_day date(std::chrono::year(local.tm_year + 1900), std::chrono::month(local.tm_mon + 1), std::chrono::day(local.tm_mday));
|
||||
if(!date.ok() || local.tm_hour > 23 || local.tm_min > 59) {
|
||||
throw std::runtime_error(std::format("'{}' is not a date (YYYY-MM-DD or YYYY-MM-DD hh:mm)", text));
|
||||
}
|
||||
// mktime normalises a day past the month's end, and works out the offset of the day it lands on.
|
||||
local.tm_mday += addDays;
|
||||
return std::chrono::system_clock::from_time_t(std::mktime(&local));
|
||||
}
|
||||
|
||||
// "YYYY-MM-DD hh:mm" in the local time zone.
|
||||
inline std::string FormatLocalTime(std::chrono::system_clock::time_point time) {
|
||||
const std::time_t seconds = std::chrono::system_clock::to_time_t(time);
|
||||
const std::tm local = *std::localtime(&seconds);
|
||||
return std::format("{:04}-{:02}-{:02} {:02}:{:02}", local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, local.tm_hour, local.tm_min);
|
||||
}
|
||||
|
||||
// Percent-encodes everything but RFC 3986's unreserved characters, for a query parameter.
|
||||
constexpr std::string UrlEncode(std::string_view text) {
|
||||
constexpr std::string_view hex = "0123456789ABCDEF";
|
||||
std::string out;
|
||||
for(const char c : text) {
|
||||
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' || c == '~') {
|
||||
out += c;
|
||||
} else {
|
||||
const std::uint8_t byte = static_cast<std::uint8_t>(c);
|
||||
out += '%';
|
||||
out += hex[byte >> 4];
|
||||
out += hex[byte & 0xF];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Decodes %XX and, as in a query string, '+' for a space. A '%' not followed by two hex digits stays as it is.
|
||||
constexpr std::string UrlDecode(std::string_view text) {
|
||||
const auto digit = [](char c) -> std::int32_t {
|
||||
if(c >= '0' && c <= '9') return c - '0';
|
||||
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if(c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
};
|
||||
std::string out;
|
||||
for(std::size_t i = 0; i < text.size(); i++) {
|
||||
if(text[i] == '+') {
|
||||
out += ' ';
|
||||
} else if(text[i] == '%' && i + 2 < text.size() && digit(text[i + 1]) >= 0 && digit(text[i + 2]) >= 0) {
|
||||
out += static_cast<char>(digit(text[i + 1]) * 16 + digit(text[i + 2]));
|
||||
i += 2;
|
||||
} else {
|
||||
out += text[i];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The decoded value of `name` in the query string of `url` (everything between '?' and '#'), if it is there.
|
||||
constexpr std::optional<std::string> QueryParameter(std::string_view url, std::string_view name) {
|
||||
const std::size_t question = url.find('?');
|
||||
if(question == std::string_view::npos) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::string_view query = url.substr(question + 1);
|
||||
query = query.substr(0, query.find('#'));
|
||||
for(const auto part : std::views::split(query, '&')) {
|
||||
const std::string_view pair(part.begin(), part.end());
|
||||
const std::size_t equals = pair.find('=');
|
||||
if(UrlDecode(pair.substr(0, equals)) == name) {
|
||||
return equals == std::string_view::npos ? std::string() : UrlDecode(pair.substr(equals + 1));
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Where the library's warnings go: a connector that could not be refreshed, an account it skipped, a request made
|
||||
// but not read back. A line on stderr unless the program replaces it; a daemon sends them to the client that made
|
||||
// the call instead, as on stderr they would only reach its journal.
|
||||
std::function<void(std::string_view)> noteSink = [](std::string_view note) {
|
||||
std::println(std::cerr, "{}", note);
|
||||
};
|
||||
|
||||
// Hands `note` to noteSink.
|
||||
inline void Note(std::string_view note) {
|
||||
noteSink(note);
|
||||
}
|
||||
}
|
||||
73
interfaces/Nostro.Checkout.cppm
Normal file
73
interfaces/Nostro.Checkout.cppm
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Checkout;
|
||||
import std;
|
||||
|
||||
// Checkout: how a shop app has the user's nostro pay an order, and nothing else of nostro. nostrod pays the order from
|
||||
// the account the user chose for checkouts, once the user has confirmed it at a password prompt that shows the order and
|
||||
// that account; the app never names nor sees an account, and learns only the payment's id. A module of its own, so
|
||||
// `--product=checkout` builds it without nostro's bank code, OpenSSL, Crafter.Network or Crafter.DB: it needs libsystemd
|
||||
// alone. Apps in other languages use packaging/dbus/net.catcrafts.Nostro1.Checkout.xml instead.
|
||||
|
||||
using namespace std::string_view_literals;
|
||||
|
||||
export namespace Nostro::Checkout {
|
||||
// String views of literals, so `.data()` is NUL-terminated as sd-bus wants it.
|
||||
inline constexpr std::string_view busName = "net.catcrafts.Nostro1.Checkout";
|
||||
inline constexpr std::string_view objectPath = "/net/catcrafts/Nostro1";
|
||||
inline constexpr std::string_view interfaceName = "net.catcrafts.Nostro1.Checkout";
|
||||
|
||||
// Pay(payeeIban, payeeName, amount, currency, reference, options) -> (paymentId, results): the arguments' signature,
|
||||
// the reply's, and their names, each ended by a NUL.
|
||||
inline constexpr std::string_view payArguments = "ssxssa{sv}";
|
||||
inline constexpr std::string_view payReply = "ta{sv}";
|
||||
inline constexpr std::string_view payNames = "payeeIban\0payeeName\0amount\0currency\0reference\0options\0paymentId\0results\0"sv;
|
||||
|
||||
// The errors an app tells apart, besides org.freedesktop.DBus.Error.AccessDenied (the system's policy refused),
|
||||
// ServiceUnknown (no nostro) and InteractiveAuthorizationRequired. nostrod's other interface uses the same names.
|
||||
inline constexpr std::string_view errorCancelled = "net.catcrafts.Nostro1.Error.Cancelled"; // the user did not confirm: nothing was paid
|
||||
inline constexpr std::string_view errorNoCheckoutAccount = "net.catcrafts.Nostro1.Error.NoCheckoutAccount"; // the user has no account to pay checkouts from
|
||||
inline constexpr std::string_view errorInvalidArgument = "net.catcrafts.Nostro1.Error.InvalidArgument"; // refused before anything was paid
|
||||
inline constexpr std::string_view errorBank = "net.catcrafts.Nostro1.Error.Bank"; // the bank refused
|
||||
inline constexpr std::string_view errorFailed = "net.catcrafts.Nostro1.Error.Failed"; // anything else
|
||||
|
||||
// An order to pay.
|
||||
struct Order {
|
||||
std::string payeeIban; // who is paid: an IBAN, the only kind of payee checkout pays
|
||||
std::string payeeName; // their name, as the bank and the prompt show it
|
||||
std::int64_t amount = 0; // in the currency's smallest unit: 1250 is 12.50
|
||||
std::string currency = "EUR"; // ISO 4217
|
||||
std::string reference; // the order's own number: the payment's description, and what tells orders apart
|
||||
};
|
||||
|
||||
// What became of an order.
|
||||
struct Paid {
|
||||
std::uint64_t paymentId = 0; // the bank's id of the payment
|
||||
bool repeated = false; // paid before, by an earlier call with the same order: nothing was paid now
|
||||
};
|
||||
|
||||
// An error nostrod answered with, by its D-Bus name: errorBank, org.freedesktop.DBus.Error.AccessDenied, ...
|
||||
struct Error : std::runtime_error {
|
||||
std::string name;
|
||||
Error(std::string name, const std::string& message);
|
||||
};
|
||||
|
||||
// The user did not confirm the order: nothing was paid.
|
||||
struct Cancelled : Error {
|
||||
using Error::Error;
|
||||
};
|
||||
|
||||
// The user has no account to pay checkouts from (`nostro checkout account <account>` chooses one): nothing was paid.
|
||||
struct NoCheckoutAccount : Error {
|
||||
using Error::Error;
|
||||
};
|
||||
|
||||
// Has the user's nostro pay `order`, and waits for it: for nostrod to start, for the user to read the order and
|
||||
// confirm it with their password, and for the bank, up to `timeout`; so call it off the UI thread. An order is paid
|
||||
// once: the same payee and reference again, after a timeout or a crash, answers with the first payment
|
||||
// (Paid::repeated), or waits for it while the user is still at the prompt, and the same reference with another
|
||||
// amount is refused. Throws Cancelled, NoCheckoutAccount, std::invalid_argument for text that is not UTF-8, and
|
||||
// Error for the rest; an Error named org.freedesktop.DBus.Error.ServiceUnknown means nostro is not installed.
|
||||
Paid Pay(const Order& order, std::chrono::seconds timeout = std::chrono::minutes(10));
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro;
|
||||
|
||||
|
|
@ -16,4 +16,9 @@ export import :Bunq;
|
|||
#endif
|
||||
#ifdef NOSTRO_OPENSSL_ENABLED
|
||||
export import :Crypto;
|
||||
#endif
|
||||
#ifdef NOSTRO_DBUS_ENABLED
|
||||
export import :Bus;
|
||||
export import :Client;
|
||||
export import :Daemon;
|
||||
#endif
|
||||
Loading…
Reference in a new issue