initial commit

This commit is contained in:
Jorijn van der Graaf 2026-10-06 15:39:17 +02:00
commit be13205ffd
19 changed files with 33596 additions and 0 deletions

View file

@ -0,0 +1,28 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro:Account;
import std;
import :Currency;
import :Transaction;
namespace Nostro {
export struct Account {
std::string name;
std::string bic;
std::string iban;
Currency currency;
std::int64_t balance;
std::vector<Transaction> transactions;
};
}
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);
}
};

View file

@ -0,0 +1,25 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro:Banks;
#ifdef NOSTRO_BUNQ_ENABLED
import :Bunq;
#endif
import std;
namespace Nostro {
export template <typename T, typename Func>
constexpr void tuple_for(Func&& func) {
[&]<typename... Ts>(std::type_identity<std::tuple<Ts...>>) {
(func.template operator()<Ts>(), ...);
}(std::type_identity<T>{});
}
export using EnabledBanks = std::tuple<
#ifdef NOSTRO_BUNQ_ENABLED
BunqConnector
#endif
>;
}

View file

@ -0,0 +1,67 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#ifdef NOSTRO_BUNQ_ENABLED
#include "../lib/json.hpp"
#include <openssl/types.h>
#endif
export module Nostro:Bunq;
#ifdef NOSTRO_BUNQ_ENABLED
import std;
import Crafter.Network;
import :Account;
import :Currency;
import :Transaction;
using namespace Crafter;
namespace Nostro {
export struct BunqConnector;
export struct BunqAccount : public Account {
std::uint32_t id;
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) const;
void Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const;
private:
void SendPayment(std::string iban) const;
};
export struct BunqConnector {
std::string sessionToken;
std::string installationToken;
EVP_PKEY* privateKey;
std::string apiKey;
std::string name;
std::vector<BunqAccount> accounts;
std::uint32_t id;
void RefreshSession();
void RefreshAccounts();
bool TestConnector();
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);
using Account = BunqAccount;
};
void to_json(nlohmann::json& j, const BunqConnector& connector);
void from_json(const nlohmann::json& j, BunqConnector& connector);
}
template<>
struct std::formatter<Nostro::BunqConnector> : std::formatter<std::string> {
auto format(const Nostro::BunqConnector& obj, std::format_context& ctx) const {
return std::formatter<std::string>::format(obj.name, ctx);
}
};
#endif

View file

@ -0,0 +1,17 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro:Connector;
import std;
namespace Nostro {
export struct Connector {
static void SaveConnectors();
static void LoadConnectors();
static std::filesystem::path GetConnectorPath();
static std::string ListConnectors();
static std::string ListBalance();
};
}

View file

@ -0,0 +1,33 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#ifdef NOSTRO_OPENSSL_ENABLED
#include <openssl/types.h>
#include <openssl/err.h>
#endif
export module Nostro:Crypto;
#ifdef NOSTRO_OPENSSL_ENABLED
import std;
export namespace Nostro::Crypto {
long ThrowOnOpenssl(long status);
template <typename T>
T* ThrowOnOpenssl(T* pointer) {
if(pointer == nullptr) {
ERR_print_errors_fp(stderr);
throw std::runtime_error("openssl error");
}
return pointer;
}
std::string SignBody(EVP_PKEY* privateKey, std::string_view body);
std::tuple<EVP_PKEY*, std::string> CreateKey();
EVP_PKEY* LoadKeyFromPem(std::string_view pem);
};
#endif

View file

@ -0,0 +1,101 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro:Currency;
import std;
export namespace Nostro {
enum class Currency {
Euro
};
constexpr Currency GetCurrency(std::string_view name) {
if(name == "EUR" || name == "eur") {
return Currency::Euro;
} else {
throw std::runtime_error(std::format("unsupported currency '{}'", name));
}
}
constexpr std::string_view GetCurrencyCode(Currency code) {
switch(code) {
case Currency::Euro: return "EUR";
}
std::unreachable();
}
constexpr std::string_view GetCurrencySymbol(Currency code) {
switch(code) {
case Currency::Euro: return "€";
}
std::unreachable();
}
constexpr std::uint8_t GetCurrencyDecimals(Currency code) {
switch(code) {
case Currency::Euro: return 2;
}
std::unreachable();
}
constexpr std::int64_t ParseAmount(std::string_view value, Currency currency) {
const std::uint8_t decimals = GetCurrencyDecimals(currency);
std::string_view rest = value;
const bool negative = rest.starts_with('-');
if(negative) {
rest.remove_prefix(1);
}
const std::size_t dot = rest.find('.');
const std::string_view whole = rest.substr(0, dot);
const std::string_view fraction = dot == std::string_view::npos ? std::string_view() : rest.substr(dot + 1);
if(whole.empty() || fraction.size() > decimals) {
throw std::runtime_error(std::format("malformed amount '{}'", value));
}
std::string digits(whole);
digits += fraction;
digits.append(decimals - fraction.size(), '0');
std::int64_t minor = 0;
for(const char c : digits) {
if(c < '0' || c > '9') {
throw std::runtime_error(std::format("malformed amount '{}'", value));
}
minor = minor * 10 + (c - '0');
}
return negative ? -minor : minor;
}
constexpr std::string FormatAmount(std::int64_t minor, Currency currency) {
const std::uint8_t decimals = GetCurrencyDecimals(currency);
const bool negative = minor < 0;
std::uint64_t magnitude = negative ? std::uint64_t{0} - static_cast<std::uint64_t>(minor) : static_cast<std::uint64_t>(minor);
std::string digits;
do {
digits.push_back(static_cast<char>('0' + magnitude % 10));
magnitude /= 10;
} while(magnitude != 0);
std::ranges::reverse(digits);
if(digits.size() <= decimals) {
digits.insert(0, decimals + 1 - digits.size(), '0');
}
if(decimals > 0) {
digits.insert(digits.size() - decimals, 1, '.');
}
if(negative) {
digits.insert(0, 1, '-');
}
return digits;
}
}
template<>
struct std::formatter<Nostro::Currency> : std::formatter<std::string> {
auto format(const Nostro::Currency& obj, std::format_context& ctx) const {
return std::formatter<std::string>::format(std::format("{}", GetCurrencySymbol(obj)), ctx);
}
};

View file

@ -0,0 +1,17 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro:Transaction;
import std;
import :Currency;
namespace Nostro {
export struct Transaction {
std::string_view bic;
std::string_view iban;
Currency currency;
std::int64_t amount;
};
}

17
interfaces/Nostro.cppm Normal file
View file

@ -0,0 +1,17 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro;
export import :Account;
export import :Transaction;
export import :Connector;
export import :Banks;
export import :Currency;
#ifdef NOSTRO_BUNQ_ENABLED
export import :Bunq;
#endif
#ifdef NOSTRO_OPENSSL_ENABLED
export import :Crypto;
#endif