- helpers.cpp:
- functions for dealing with:
- std::vector<std::string>
- std::vector<uint32_t>
- std::vector<std::set<uint32_t>>
- wallet2_api_c.h:
- PendingTransaction
- txid
- subaddrAccount
- subaddrIndices
- multisigSignData
- signMultisigTx
- signersKeys
This commit is contained in:
86
libbridge/src/main/cpp/helpers.cpp
Normal file
86
libbridge/src/main/cpp/helpers.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "helpers.hpp"
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
const char* vectorToString(const std::vector<std::string>& vec, const std::string separator) {
|
||||
// Concatenate all strings in the vector
|
||||
std::string result;
|
||||
for (const auto& str : vec) {
|
||||
result += str;
|
||||
}
|
||||
const char* cstr = result.c_str();
|
||||
return cstr;
|
||||
}
|
||||
|
||||
const char* vectorToString(const std::vector<uint32_t>& vec, const std::string separator) {
|
||||
// Calculate the size needed for the result string
|
||||
size_t size = 0;
|
||||
for (size_t i = 0; i < vec.size(); ++i) {
|
||||
// Calculate the number of digits in each element
|
||||
size += snprintf(nullptr, 0, "%u", vec[i]);
|
||||
// Add comma and space for all elements except the last one
|
||||
if (i < vec.size() - 1) {
|
||||
size += 2; // comma and space
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate memory for the result string
|
||||
char* result = static_cast<char*>(malloc(size + 1));
|
||||
if (result == nullptr) {
|
||||
// Handle memory allocation failure
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Fill in the result string
|
||||
char* current = result;
|
||||
for (size_t i = 0; i < vec.size(); ++i) {
|
||||
// Convert each element to string and copy to the result string
|
||||
int written = snprintf(current, size + 1, "%u", vec[i]);
|
||||
current += written;
|
||||
// Add comma and space for all elements except the last one
|
||||
if (i < vec.size() - 1) {
|
||||
strcpy(current, ", ");
|
||||
current += 2;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const char* vectorToString(const std::vector<std::set<uint32_t>>& vec, const std::string separator) {
|
||||
// Check if the vector is empty
|
||||
if (vec.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Use a stringstream to concatenate sets with commas and individual elements with spaces
|
||||
std::ostringstream oss;
|
||||
oss << "{";
|
||||
for (auto it = vec.begin(); it != vec.end(); ++it) {
|
||||
if (it != vec.begin()) {
|
||||
oss << separator;
|
||||
}
|
||||
|
||||
oss << "{";
|
||||
for (auto setIt = it->begin(); setIt != it->end(); ++setIt) {
|
||||
if (setIt != it->begin()) {
|
||||
oss << separator;
|
||||
}
|
||||
oss << *setIt;
|
||||
}
|
||||
oss << "}";
|
||||
}
|
||||
oss << "}";
|
||||
std::string str = oss.str();
|
||||
const std::string::size_type size = str.size();
|
||||
char *buffer = new char[size + 1]; //we need extra char for NUL
|
||||
memcpy(buffer, str.c_str(), size + 1);
|
||||
return buffer;
|
||||
}
|
||||
8
libbridge/src/main/cpp/helpers.hpp
Normal file
8
libbridge/src/main/cpp/helpers.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
const char* vectorToString(const std::vector<std::string>& vec, const std::string separator);
|
||||
const char* vectorToString(const std::vector<uint32_t>& vec, const std::string separator);
|
||||
const char* vectorToString(const std::vector<std::set<uint32_t>>& vec, const std::string separator);
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "wallet2_api_c.h"
|
||||
#include "wallet2_api.h"
|
||||
#include <unistd.h>
|
||||
#include "helpers.hpp"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -55,11 +56,42 @@ uint64_t MONERO_PendingTransaction_fee(void* pendingTx_ptr) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
return pendingTx->fee();
|
||||
}
|
||||
const char* MONERO_PendingTransaction_txid(void* pendingTx_ptr, const char* separator) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
std::vector<std::string> txid = pendingTx->txid();
|
||||
return vectorToString(txid, std::string(separator));
|
||||
}
|
||||
uint64_t MONERO_PendingTransaction_txCount(void* pendingTx_ptr) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
return pendingTx->txCount();
|
||||
}
|
||||
|
||||
const char* MONERO_PendingTransaction_subaddrAccount(void* pendingTx_ptr, const char* separator) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
std::vector<uint32_t> subaddrAccount = pendingTx->subaddrAccount();
|
||||
return vectorToString(subaddrAccount, std::string(separator));
|
||||
}
|
||||
const char* MONERO_PendingTransaction_subaddrIndices(void* pendingTx_ptr, const char* separator) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
std::vector<std::set<uint32_t>> subaddrIndices = pendingTx->subaddrIndices();
|
||||
return vectorToString(subaddrIndices, std::string(separator));
|
||||
}
|
||||
const char* MONERO_PendingTransaction_multisigSignData(void* pendingTx_ptr) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
std::string str = pendingTx->multisigSignData();
|
||||
const std::string::size_type size = str.size();
|
||||
char *buffer = new char[size + 1]; //we need extra char for NUL
|
||||
memcpy(buffer, str.c_str(), size + 1);
|
||||
return buffer;
|
||||
}
|
||||
void MONERO_PendingTransaction_signMultisigTx(void* pendingTx_ptr) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
return pendingTx->signMultisigTx();
|
||||
}
|
||||
const char* MONERO_PendingTransaction_signersKeys(void* pendingTx_ptr, const char* separator) {
|
||||
Monero::PendingTransaction *pendingTx = reinterpret_cast<Monero::PendingTransaction*>(pendingTx_ptr);
|
||||
std::vector<std::string> txid = pendingTx->signersKeys();
|
||||
return vectorToString(txid, std::string(separator));
|
||||
}
|
||||
// TransactionInfo
|
||||
int MONERO_TransactionInfo_direction(void* txInfo_ptr) {
|
||||
Monero::TransactionInfo *txInfo = reinterpret_cast<Monero::TransactionInfo*>(txInfo_ptr);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -68,13 +69,19 @@ uint64_t MONERO_PendingTransaction_dust(void* pendingTx_ptr);
|
||||
// virtual uint64_t fee() const = 0;
|
||||
uint64_t MONERO_PendingTransaction_fee(void* pendingTx_ptr);
|
||||
// virtual std::vector<std::string> txid() const = 0;
|
||||
const char* MONERO_PendingTransaction_txid(void* pendingTx_ptr, const char* separator);
|
||||
// virtual uint64_t txCount() const = 0;
|
||||
uint64_t MONERO_PendingTransaction_txCount(void* pendingTx_ptr);
|
||||
// virtual std::vector<uint32_t> subaddrAccount() const = 0;
|
||||
const char* MONERO_PendingTransaction_subaddrAccount(void* pendingTx_ptr, const char* separator);
|
||||
// virtual std::vector<std::set<uint32_t>> subaddrIndices() const = 0;
|
||||
const char* MONERO_PendingTransaction_subaddrIndices(void* pendingTx_ptr, const char* separator);
|
||||
// virtual std::string multisigSignData() = 0;
|
||||
const char* MONERO_PendingTransaction_multisigSignData(void* pendingTx_ptr);
|
||||
// virtual void signMultisigTx() = 0;
|
||||
void MONERO_PendingTransaction_signMultisigTx(void* pendingTx_ptr);
|
||||
// virtual std::vector<std::string> signersKeys() const = 0;
|
||||
const char* MONERO_PendingTransaction_signersKeys(void* pendingTx_ptr, const char* separator);
|
||||
// };
|
||||
|
||||
// struct UnsignedTransaction
|
||||
@@ -86,7 +93,9 @@ uint64_t MONERO_PendingTransaction_txCount(void* pendingTx_ptr);
|
||||
// };
|
||||
// virtual ~UnsignedTransaction() = 0;
|
||||
// virtual int status() const = 0;
|
||||
// int MONERO_UnsignedTransaction_status(void* unsignedTx_ptr);
|
||||
// virtual std::string errorString() const = 0;
|
||||
// const char* MONERO_UnsignedTransaction_errorString(void* unsignedTx_ptr);
|
||||
// virtual std::vector<uint64_t> amount() const = 0;
|
||||
// virtual std::vector<uint64_t> fee() const = 0;
|
||||
// virtual std::vector<uint64_t> mixin() const = 0;
|
||||
|
||||
Reference in New Issue
Block a user