Skip to content

feat: support data in both gzip and json #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ios/Headers/native-crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern "C" {
#endif

const char *create_blocks_request(int height, size_t *length);
const char * decompress(const char *buffer, size_t length);
void extract_utxos_from_blocks_response(const char *buffer, size_t length, const char *szJsonParams, char **pszResult);
void extract_utxos_from_clarity_blocks_response(const char *buffer, size_t length, const char *szJsonParams, char **pszResult);
void get_transaction_pool_hashes(const char *buffer, size_t length, char **pszResult);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
depends="boost monerocorecustom"
inherit lib

version="db133d3eda862e4d3879628e9a2b9ac68f077895"
version="2a678f08ec3f335c99cd4079287aafaa8f53a62d"
source="https://github.com/ExodusMovement/mymonero-core-cpp.git#${version}"

build() {
Expand Down
8 changes: 8 additions & 0 deletions native-libs/src/native-crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const char *create_blocks_request(int height, size_t *length) {
return serial_bridge::create_blocks_request(height, length);
}

const char *decompress(const char *buffer, size_t length) {
return serial_bridge::decompress(buffer, length);
}

bool is_gzip_compressed(const char *buffer, size_t length) {
return serial_bridge::is_gzip_compressed(buffer, length);
}

void extract_utxos_from_blocks_response(const char *buffer, size_t length, const char *szJsonParams, char **pszResult) {
std::string strParams = szJsonParams;
std::string result = serial_bridge::extract_data_from_blocks_response_str(buffer, length, strParams);
Expand Down
1 change: 1 addition & 0 deletions native-libs/src/native-crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern "C" {
#endif

const char *create_blocks_request(int height, size_t *length);
const char * decompress(const char *buffer, size_t length);
void extract_utxos_from_blocks_response(const char *buffer, size_t length, const char *szJsonParams, char **pszResult);
void extract_utxos_from_clarity_blocks_response(const char *buffer, size_t length, const char *szJsonParams, char **pszResult);
void get_transaction_pool_hashes(const char *buffer, size_t length, char **pszResult);
Expand Down
107 changes: 75 additions & 32 deletions native-libs/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <vector>
#include <cstdlib>
#include <chrono>
#include <serial_bridge_index.hpp>
#include <storages/portable_storage_template_helper.h>
#include <cryptonote_basic/cryptonote_format_utils.h>
Expand Down Expand Up @@ -48,26 +49,47 @@ void test_decode() {
delete[] input;
}

void test_decompress() {
std::ifstream file("test/input/blocks.json.gzip", std::ios::binary | std::ios::ate);
void test_decompress(const std::string &filePath) {
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file) {
throw std::runtime_error("Failed to open file");
throw std::runtime_error("Failed to open file: " + filePath);
}

file.seekg(0, std::ios::end);
// Get file size
size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);

// Read the file into a vector<char>
char *buffer = new char[fileSize];
if (!file.read(buffer, fileSize)) {
throw std::runtime_error("Failed to read file");
// Read file into a buffer
std::vector<char> buffer(fileSize);
if (!file.read(buffer.data(), fileSize)) {
throw std::runtime_error("Failed to read file: " + filePath);
}

size_t length = 0;
std::string decompressedData = serial_bridge::decompress(buffer, fileSize);
// Check if the file is Gzip compressed
bool isGzip = serial_bridge::is_gzip_compressed(buffer.data(), fileSize);
std::cout << "File: " << filePath << " | Is Gzip Compressed: " << (isGzip ? "Yes" : "No") << '\n';

// Process the content based on type
std::string jsonData;
if (isGzip) {
try {
jsonData = serial_bridge::decompress(buffer.data(), fileSize);
} catch (const std::exception &e) {
std::cerr << "Decompression failed for " << filePath << ": " << e.what() << '\n';
return;
}
std::cout << "Decompressed JSON Length: " << jsonData.size() << '\n';
} else {
jsonData.assign(buffer.begin(), buffer.end());
std::cout << "Plain JSON Length: " << jsonData.size() << '\n';
}

delete[] buffer;
// Validate JSON format (basic check)
assert(!jsonData.empty() && "JSON data should not be empty!");
assert(jsonData.front() == '{' || jsonData.front() == '[' && "JSON must start with { or [");

// Print a small portion of the JSON for validation
std::cout << "JSON Preview: " << jsonData.substr(0, 200) << "...\n\n";
}

void test_decode_with_clarity() {
Expand Down Expand Up @@ -105,33 +127,41 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use
}

bool downloadFile(const std::string& url, std::vector<char>& buffer) {
CURL *curl;
CURL *curl = curl_easy_init();
if (!curl) {
std::cerr << "Failed to initialize CURL\n";
return false;
}

CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return (res == CURLE_OK);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

if (res != CURLE_OK) {
std::cerr << "CURL error: " << curl_easy_strerror(res) << '\n';
return false;
}
curl_easy_cleanup(curl); // Clean up even if curl_easy_init fails
return false;

return true;
}

void test_full_flow_with_clarity() {
std::string url = "https://xmr-proxy-d.a.exodus.io/v1/monero/get_blocks_file/3148308.json.gzip";
// Generic function to test JSON/Gzip processing
void test_full_flow_with_url_clarity(const std::string& url) {
auto start_time = std::chrono::high_resolution_clock::now();
std::vector<char> buffer;

std::cout << "Start download blocks file" << '\n';
downloadFile(url, buffer);
std::cout << "Start downloading file from: " << url << '\n';
if (!downloadFile(url, buffer)) {
std::cerr << "Failed to download blocks file\n";
std::cerr << "Failed to download file\n";
return;
}
std::cout << "Done download file" << '\n';
std::cout << "Download complete. File size: " << buffer.size() << " bytes\n";

// Read input parameters from a file
std::ifstream paramsFile("test/input/input.json");
if (!paramsFile) {
std::cerr << "Failed to open parameter file\n";
Expand All @@ -142,17 +172,30 @@ void test_full_flow_with_clarity() {
paramsStream << paramsFile.rdbuf();
std::string params = paramsStream.str();

// Pass the downloaded buffer directly to the function
auto resp = serial_bridge::extract_data_from_clarity_blocks_response_str(buffer.data(), buffer.size(), params);
std::cout << resp << '\n';
return;

std::cout << "Response: " << resp << '\n';

auto end_time = std::chrono::high_resolution_clock::now(); // End timer
std::chrono::duration<double> duration = end_time - start_time;
std::cout << "Execution Time: " << duration.count() << " seconds\n";
}


int main() {
test_encode();
test_decode();
test_decompress();

std::cout << "Testing Gzip JSON File...\n";
test_decompress("test/input/blocks.json.gzip");

std::cout << "\nTesting Plain JSON File...\n";
test_decompress("test/input/blocks.json");

// test_decode_with_clarity();
test_full_flow_with_clarity();
test_full_flow_with_url_clarity("https://xmr-proxy-d.a.exodus.io/v1/monero/get_blocks_file/3148308.json.gzip");
test_full_flow_with_url_clarity("https://xmr-proxy-d.a.exodus.io/v1/monero/get_blocks_file/3148308.json");

return 0;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exodus/react-native-fast-crypto",
"version": "18.3.5-rc.1",
"version": "18.3.6-rc.1",
"description": "Native C/C++ implemented crypto libraries for React Native apps",
"keywords": [
"react-native",
Expand Down
16 changes: 8 additions & 8 deletions shasums.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ dff0a485e8499b7b5a8010d9b0d851fb2163b3553d04237af212174a529884d3 ios/Frameworks
b6e45f0a4e7aadb215c1704ab3d1cb2b2fb2c3d315390b488a38fc48bf7bf0cc ios/Frameworks/libboost_system.a.xcframework/ios-arm64_i386_x86_64-simulator/libboost_system.a
575e148ea01773085573ccfdd651592179c041d7bd322785b59312b7e96ddba0 ios/Frameworks/libboost_thread.a.xcframework/ios-arm64_armv7_armv7s/libboost_thread.a
11caf18ecdd380f005bc80e6901c7fe0919b3d03b040cc5b20cb33543da3e416 ios/Frameworks/libboost_thread.a.xcframework/ios-arm64_i386_x86_64-simulator/libboost_thread.a
c242c51c3230c1e3d6162708299b691ffc3c04d71aa28dc4fa360025a0344430 ios/Frameworks/libmymonerocorecpp.a.xcframework/ios-arm64_armv7_armv7s/libmymonerocorecpp.a
c35aaaed386512c24b39e77dded75acd26804ad666662339ece35056d0aa22fc ios/Frameworks/libmymonerocorecpp.a.xcframework/ios-arm64_i386_x86_64-simulator/libmymonerocorecpp.a
e05026534351be7ec8891c2e7ea5dbba34f4eaacaf7a8285095641864b67b37a ios/Frameworks/libnativecrypto.a.xcframework/ios-arm64_armv7_armv7s/libnativecrypto.a
eea6c3414ec15395c0e499b70e331a4cf0dfdbaa4bb0170ce1c9a39836e02347 ios/Frameworks/libnativecrypto.a.xcframework/ios-arm64_i386_x86_64-simulator/libnativecrypto.a
d0a1870881911281434fcf7cb29b5f28bf635767371690bd9b8919f2d0abf46c ios/Frameworks/libmymonerocorecpp.a.xcframework/ios-arm64_armv7_armv7s/libmymonerocorecpp.a
e39dee51c8e782d2409a89d57fdc9e1e06202486af31030ef4c1df78f085ba88 ios/Frameworks/libmymonerocorecpp.a.xcframework/ios-arm64_i386_x86_64-simulator/libmymonerocorecpp.a
c31ce8642b696d4f86df9a4f2202df88b93addde8b0007afaac87144f3947d4c ios/Frameworks/libnativecrypto.a.xcframework/ios-arm64_armv7_armv7s/libnativecrypto.a
ace05410b447f92d6e45ae194e44c45b5afddd7a1c5b297a1f136ef088b36679 ios/Frameworks/libnativecrypto.a.xcframework/ios-arm64_i386_x86_64-simulator/libnativecrypto.a
46640a59840be914f759beb99382ad8d6ed94a255660b6c220bcd600632be6b3 android/jni/libs/arm64-v8a/libcrypto_bridge.so
e3bf23fc8a6cd2dc9c15f5e6c1b18f8b72ca407bfac063b5d16b3079eb279e2e android/jni/libs/arm64-v8a/libnativecrypto.so
5f9358aba03f54bdfb0b76efe75f294e831acfa7344df9d25abecb1882dcbc26 android/jni/libs/arm64-v8a/libnativecrypto.so
498b3d918352c573188054d5fd5b843f666bef003662efbeed802eb57400faac android/jni/libs/armeabi-v7a/libcrypto_bridge.so
15c706575f6fb14923d2075fa904b49e760845e3412aa25bd197c24d0d43a8ff android/jni/libs/armeabi-v7a/libnativecrypto.so
69de848b86a5a884b31090f43ff9509b12c87481c4da04e290dcace93df709f9 android/jni/libs/armeabi-v7a/libnativecrypto.so
6ad211e4e5ef3dc24b0d26bd6d1ce364add0160f76c87322adee1d03dac2c0f4 android/jni/libs/x86/libcrypto_bridge.so
2cf23134a276eeea5fcd320bc3079f5c9a484a17567e16d8e37318992d8b00a6 android/jni/libs/x86/libnativecrypto.so
113cb0b3e4d32f78b24bc6aa607e34ffabf33a15f5ed7d52e4ec2541098029e7 android/jni/libs/x86/libnativecrypto.so
1152558c5ed6324a3a935dd9acab7e91fade049507a99ca88ccbff05319c475f android/jni/libs/x86_64/libcrypto_bridge.so
1987c9a6792d4277ae8c1fa3328fe1ca577cc347cef3d597bb5dc63cf5cdcdd1 android/jni/libs/x86_64/libnativecrypto.so
03812db74e9d6fccbd8c41e5eec76b8cbc915ef92ecca804f1831ba562bfd110 android/jni/libs/x86_64/libnativecrypto.so