diff --git a/src/i_logos_blockchain_module.h b/src/i_logos_blockchain_module.h index d75332a..76f078f 100644 --- a/src/i_logos_blockchain_module.h +++ b/src/i_logos_blockchain_module.h @@ -2,7 +2,6 @@ #define I_LOGOS_BLOCKCHAIN_MODULE_API_H #include -#include #include class ILogosBlockchainModule { @@ -13,15 +12,18 @@ public: virtual void initLogos(LogosAPI* logosAPIInstance) = 0; // Node + virtual int generate_user_config(const QVariantMap& args) = 0; + virtual int generate_user_config_from_str(const QString& args) = 0; virtual int start(const QString& config_path, const QString& deployment) = 0; virtual int stop() = 0; virtual QString wallet_get_balance(const QString& addressHex) = 0; virtual QString wallet_transfer_funds( - const QString& changePublicKey, - const QStringList& senderAddresses, - const QString& recipientAddress, - const QString& amount, - const QString& optionalTipHex) = 0; + const QString& changePublicKey, + const QStringList& senderAddresses, + const QString& recipientAddress, + const QString& amount, + const QString& optionalTipHex + ) = 0; virtual QStringList wallet_get_known_addresses() = 0; }; diff --git a/src/logos_blockchain_module.cpp b/src/logos_blockchain_module.cpp index af3fc7b..c450d8a 100644 --- a/src/logos_blockchain_module.cpp +++ b/src/logos_blockchain_module.cpp @@ -23,7 +23,7 @@ namespace { return {}; return bytes; } -} +} // namespace void LogosBlockchainModule::onNewBlockCallback(const char* block) { if (s_instance) { @@ -150,12 +150,11 @@ QString LogosBlockchainModule::wallet_get_balance(const QString& addressHex) { return QStringLiteral("Error: Address must be 64 hex characters (32 bytes)."); } - auto [value, error] = get_balance(node, - reinterpret_cast(bytes.constData()), nullptr); + auto [value, error] = get_balance(node, reinterpret_cast(bytes.constData()), nullptr); if (!is_ok(&error)) { return QStringLiteral("Error: Failed to get balance: ") + QString::number(error); } - + return QString::number(value); } @@ -231,8 +230,8 @@ QString LogosBlockchainModule::wallet_transfer_funds( const QString& senderAddress, const QString& recipientAddress, const QString& amount, - const QString& optionalTipHex) -{ + const QString& optionalTipHex +) { return wallet_transfer_funds(changePublicKey, QStringList{senderAddress}, recipientAddress, amount, optionalTipHex); } @@ -261,6 +260,149 @@ QStringList LogosBlockchainModule::wallet_get_known_addresses() { return out; } +namespace { + // Wrapper that owns data and provides GenerateConfigArgs + struct OwnedGenerateConfigArgs { + std::vector initial_peers_data; + std::vector initial_peers_ptrs; + uint32_t initial_peers_count_val; + QByteArray output_data; + uint16_t net_port_val; + uint16_t blend_port_val; + QByteArray http_addr_data; + QByteArray external_address_data; + bool no_public_ip_check_val; + QByteArray custom_deployment_config_path_data; + Deployment deployment_val; + QByteArray state_path_data; + + // The FFI struct with pointers into owned data + GenerateConfigArgs ffi_args; + + // Constructor that populates both owned data and FFI struct + explicit OwnedGenerateConfigArgs(const QVariantMap& args) { + ffi_args = {}; + + // initial_peers (QStringList -> const char**) + if (args.contains("initial_peers")) { + QStringList peers = args["initial_peers"].toStringList(); + initial_peers_count_val = static_cast(peers.size()); + + for (const QString& peer : peers) { + initial_peers_data.push_back(peer.toUtf8()); + } + for (const QByteArray& data : initial_peers_data) { + initial_peers_ptrs.push_back(data.constData()); + } + + ffi_args.initial_peers = initial_peers_ptrs.data(); + ffi_args.initial_peers_count = &initial_peers_count_val; + } else { + ffi_args.initial_peers = nullptr; + ffi_args.initial_peers_count = nullptr; + } + + // output (QString -> const char*) + if (args.contains("output")) { + output_data = args["output"].toString().toUtf8(); + ffi_args.output = output_data.constData(); + } else { + ffi_args.output = nullptr; + } + + // net_port (int -> const uint16_t*) + if (args.contains("net_port")) { + net_port_val = static_cast(args["net_port"].toInt()); + ffi_args.net_port = &net_port_val; + } else { + ffi_args.net_port = nullptr; + } + + // blend_port (int -> const uint16_t*) + if (args.contains("blend_port")) { + blend_port_val = static_cast(args["blend_port"].toInt()); + ffi_args.blend_port = &blend_port_val; + } else { + ffi_args.blend_port = nullptr; + } + + // http_addr (QString -> const char*) + if (args.contains("http_addr")) { + http_addr_data = args["http_addr"].toString().toUtf8(); + ffi_args.http_addr = http_addr_data.constData(); + } else { + ffi_args.http_addr = nullptr; + } + + // external_address (QString -> const char*) + if (args.contains("external_address")) { + external_address_data = args["external_address"].toString().toUtf8(); + ffi_args.external_address = external_address_data.constData(); + } else { + ffi_args.external_address = nullptr; + } + + // no_public_ip_check (bool -> const bool*) + if (args.contains("no_public_ip_check")) { + no_public_ip_check_val = args["no_public_ip_check"].toBool(); + ffi_args.no_public_ip_check = &no_public_ip_check_val; + } else { + ffi_args.no_public_ip_check = nullptr; + } + + // deployment (const struct Deployment*) + // Expected format: { "deployment": { "well_known_deployment": "devnet" } } + // OR: { "deployment": { "config_path": "/path/to/config" } } + if (args.contains("deployment")) { + QVariantMap deployment = args["deployment"].toMap(); + + if (deployment.contains("well_known_deployment")) { + deployment_val.deployment_type = DeploymentType::WellKnown; + QString wellknown = deployment["well_known_deployment"].toString(); + if (wellknown == "devnet") { + deployment_val.well_known_deployment = WellKnownDeployment::Devnet; + } + deployment_val.custom_deployment_config_path = nullptr; + } else if (deployment.contains("config_path")) { + deployment_val.deployment_type = DeploymentType::Custom; + deployment_val.well_known_deployment = static_cast(0); + custom_deployment_config_path_data = deployment["config_path"].toString().toUtf8(); + deployment_val.custom_deployment_config_path = custom_deployment_config_path_data.constData(); + } + + ffi_args.deployment = &deployment_val; + } else { + ffi_args.deployment = nullptr; + } + + // state_path (QString -> const char*) + if (args.contains("state_path")) { + state_path_data = args["state_path"].toString().toUtf8(); + ffi_args.state_path = state_path_data.constData(); + } else { + ffi_args.state_path = nullptr; + } + } + }; +} // namespace + +int LogosBlockchainModule::generate_user_config(const QVariantMap& args) { + const OwnedGenerateConfigArgs owned_args(args); + + const OperationStatus status = ::generate_user_config(owned_args.ffi_args); + if (!is_ok(&status)) { + qCritical() << "Failed to generate user config. Error:" << status; + return 1; + } + + return 0; +} + +int LogosBlockchainModule::generate_user_config_from_str(const QString& args) { + const QVariantMap parsed_args = QJsonDocument::fromJson(args.toUtf8()).object().toVariantMap(); + return generate_user_config(parsed_args); +} + void LogosBlockchainModule::emitEvent(const QString& eventName, const QVariantList& data) { if (!logosAPI) { qWarning() << "LogosBlockchainModule: LogosAPI not available, cannot emit" << eventName; diff --git a/src/logos_blockchain_module.h b/src/logos_blockchain_module.h index 7b95396..cea694e 100644 --- a/src/logos_blockchain_module.h +++ b/src/logos_blockchain_module.h @@ -1,6 +1,7 @@ #pragma once + #include "i_logos_blockchain_module.h" -#include + #include #ifdef __cplusplus extern "C" { @@ -9,7 +10,6 @@ extern "C" { #ifdef __cplusplus } #endif -#include "i_logos_blockchain_module.h" class LogosBlockchainModule final : public QObject, public PluginInterface, public ILogosBlockchainModule { Q_OBJECT @@ -26,21 +26,25 @@ public: Q_INVOKABLE void initLogos(LogosAPI*) override; // Logos Blockchain + Q_INVOKABLE int generate_user_config(const QVariantMap& args) override; + Q_INVOKABLE int generate_user_config_from_str(const QString& args) override; Q_INVOKABLE int start(const QString& config_path, const QString& deployment) override; Q_INVOKABLE int stop() override; Q_INVOKABLE QString wallet_get_balance(const QString& addressHex) override; Q_INVOKABLE QString wallet_transfer_funds( - const QString& changePublicKey, - const QStringList& senderAddresses, - const QString& recipientAddress, - const QString& amount, - const QString& optionalTipHex) override; + const QString& changePublicKey, + const QStringList& senderAddresses, + const QString& recipientAddress, + const QString& amount, + const QString& optionalTipHex + ) override; Q_INVOKABLE QString wallet_transfer_funds( const QString& changePublicKey, const QString& senderAddress, const QString& recipientAddress, const QString& amount, - const QString& optionalTipHex); + const QString& optionalTipHex + ); Q_INVOKABLE QStringList wallet_get_known_addresses() override; signals: