diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index cb6e19d..ad7c04d 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -27,7 +27,6 @@ add_library( ../cpp/GdkHostObject.cpp ../cpp/json.cpp ../cpp/utils.cpp - ../cpp/TwoFactorCall.cpp ../cpp/ThreadPool.cpp ) diff --git a/cpp/GdkHostObject.cpp b/cpp/GdkHostObject.cpp index 5593355..e7c3580 100644 --- a/cpp/GdkHostObject.cpp +++ b/cpp/GdkHostObject.cpp @@ -169,9 +169,11 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_register_user(session, hw_device_json, details_json, &call)); - - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + + json res = utils::resolve(call); + + GA_destroy_json(hw_device_json); + GA_destroy_json(details_json); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -182,6 +184,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop } }); } catch (utils::Exception e) { + GA_destroy_json(hw_device_json); + GA_destroy_json(details_json); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -220,8 +224,10 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop GA_auth_handler *call; utils::wrapCall(GA_login_user(session, hw_device_json, details_json, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + + GA_destroy_json(hw_device_json); + GA_destroy_json(details_json); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -232,6 +238,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop } }); } catch (utils::Exception e) { + GA_destroy_json(hw_device_json); + GA_destroy_json(details_json); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -267,8 +275,9 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop GA_auth_handler *call; utils::wrapCall(GA_get_subaccounts(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -279,6 +288,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop } }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -309,8 +319,9 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_create_subaccount(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -321,13 +332,13 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop } }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } }; - pool->queueWork(task); }; @@ -354,8 +365,9 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_get_receive_address(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -366,6 +378,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop } }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -462,8 +475,9 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_get_transactions(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -474,6 +488,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop } }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -507,8 +522,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_get_unspent_outputs(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -520,6 +535,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -615,8 +631,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_get_previous_addresses(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -628,6 +644,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -661,8 +678,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_get_credentials(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -674,6 +691,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -706,8 +724,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_encrypt_with_pin(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -719,6 +737,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -790,8 +809,9 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_create_transaction(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -809,6 +829,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -841,8 +862,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_blind_transaction(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -854,6 +875,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -886,8 +908,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_sign_transaction(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -899,6 +921,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } @@ -931,8 +954,8 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop try { GA_auth_handler *call; utils::wrapCall(GA_send_transaction(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=, &rt] { @@ -944,6 +967,7 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop }); } catch (utils::Exception e) { + GA_destroy_json(details); std::shared_ptr c = invoker.lock(); c->invokeAsync([=] { p->reject(e.what()); }); } diff --git a/cpp/GdkHostObject.hpp b/cpp/GdkHostObject.hpp index bd09090..0235f79 100644 --- a/cpp/GdkHostObject.hpp +++ b/cpp/GdkHostObject.hpp @@ -5,7 +5,6 @@ #include "utils.hpp" #include "json.hpp" #include -#include "TwoFactorCall.hpp" #include "ThreadPool.hpp" using namespace facebook; diff --git a/cpp/TwoFactorCall.cpp b/cpp/TwoFactorCall.cpp deleted file mode 100644 index 59978f4..0000000 --- a/cpp/TwoFactorCall.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// TwoFactorCall.cpp -// react-native-gdk -// -// Created by Mattia Ferrari on 06/02/24. -// - -#include "TwoFactorCall.hpp" -#include "utils.hpp" - -TwoFactorCall::TwoFactorCall(GA_auth_handler* handler) { - ptr = handler; -} - -//TwoFactorCall::~TwoFactorCall() { -// GA_destroy_auth_handler(ptr); -//} - -void TwoFactorCall::call() { - GA_auth_handler_call(ptr); -} - -json TwoFactorCall::getStatus() { - GA_json *resJson; - GA_auth_handler_get_status(ptr, &resJson); - char *stringJson; - GA_convert_json_to_string(resJson, &stringJson); - std::string s(stringJson); - json res = json::parse(s); - return res; -} diff --git a/cpp/TwoFactorCall.hpp b/cpp/TwoFactorCall.hpp deleted file mode 100644 index 2ee7c09..0000000 --- a/cpp/TwoFactorCall.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// TwoFactorCall.hpp -// react-native-gdk -// -// Created by Mattia Ferrari on 06/02/24. -// - -#ifndef TwoFactorCall_hpp -#define TwoFactorCall_hpp - -#include -#include "json.hpp" - - -using namespace nlohmann; - -class TwoFactorCall { - -public: - TwoFactorCall(GA_auth_handler* handler); -// ~TwoFactorCall(); - - void call(); - json getStatus(); - -private: - GA_auth_handler* ptr; - -}; - -#endif /* TwoFactorCall_hpp */ diff --git a/cpp/utils.cpp b/cpp/utils.cpp index 2e21307..c211495 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -99,17 +99,24 @@ namespace utils { return res; } - - json resolve(TwoFactorCall call) { + json resolve(GA_auth_handler* call) { while (true) { - json obj = call.getStatus(); + GA_json *resJson; + GA_auth_handler_get_status(call, &resJson); + char *stringJson; + GA_convert_json_to_string(resJson, &stringJson); + GA_destroy_json(resJson); + std::string s(stringJson); + json obj = json::parse(s); std::string status = obj["status"]; if (status == "call") { - call.call(); + GA_auth_handler_call(call); } else if (status == "done") { + GA_destroy_auth_handler(call); return obj; } else { + GA_destroy_auth_handler(call); return obj; } } @@ -129,6 +136,7 @@ namespace utils { char *stringJson; GA_convert_json_to_string(details, &stringJson); + GA_destroy_json(details); std::string s(stringJson); json res = json::parse(s); diff --git a/cpp/utils.hpp b/cpp/utils.hpp index b0caf46..3f3da69 100644 --- a/cpp/utils.hpp +++ b/cpp/utils.hpp @@ -10,7 +10,6 @@ #include #include -#include "TwoFactorCall.hpp" #include "GdkHostObject.hpp" #include "json.hpp" @@ -29,8 +28,8 @@ namespace utils { // debug utility int consoleLog(jsi::Runtime &rt, std::string msg); - // resolve a TwoFactorCall - json resolve(TwoFactorCall call); + // resolve a auth handler call + json resolve(GA_auth_handler* call); // handle notifications @@ -42,7 +41,7 @@ namespace utils { // definition of js JSON.stringify and JSON.parse std::string stringify(jsi::Runtime &runtime, const jsi::Value &value); jsi::Value parse(jsi::Runtime &runtime, std::string src); - + // promise definition struct Promise { @@ -56,7 +55,7 @@ namespace utils { jsi::Function reject_; }; using Promised = std::function)>; - + // creates and returns a promise given the funnction to be wrapped in promise jsi::Value makePromise(jsi::Runtime &rt, const Promised func); diff --git a/docs/about_gdk.md b/docs/about_gdk.md index 0545b4a..2d74e54 100644 --- a/docs/about_gdk.md +++ b/docs/about_gdk.md @@ -31,13 +31,12 @@ This is the struct that will hold the session informations. It is created using It is stored as a member of the `GdkHostObject` class and it is used to call all GDK functions that require a session. ## `GA_auth_handler` -This is the struct that holds the reference to an authenticated function/network call. Use it wrapped with `TwoFactorCall` object and complete the call using `utils::resolve(twoFactorCall)`. +This is the struct that holds the reference to an authenticated function/network call. Use it wrapped `utils::resolve(call)`. ```cpp GA_auth_handler *call; // create the auth handler utils::wrapCall(GA_register_user(session, hw_device_json, details_json, &call)); // make the authenticated net call -TwoFactorCall twoFactorCall(call); // pass the call to the TwoFactorCall class -json res = utils::resolve(twoFactorCall); // resolve the call using the utility function +json res = utils::resolve(call); // resolve the call using the utility function ``` diff --git a/docs/about_jsi.md b/docs/about_jsi.md index 3811abf..c0290c4 100644 --- a/docs/about_jsi.md +++ b/docs/about_jsi.md @@ -27,7 +27,6 @@ react-native-gdk │ └── GdkPackage.java └── cpp ├── GdkHostObject.cpp - ├── TwoFactorCall.cpp ├── ThreadPool.cpp ├── json.cpp └── utils.cpp @@ -49,7 +48,6 @@ react-native-gdk `cpp` - Contains all the non platform specific code, the core of the application that will be compiled for both platforms - `GdkHostObject.cpp` is the host object that will be created in the native runtime and exposed to JavaScript, its methods will be callable from JS -- `TwoFactorCall.cpp` resolves a GDK http request that requires authentication - `utils.cpp` contains some utility functions to semplify the workflows - `ThreadPool.cpp` implementation of a thread pool used to execute async tasks, source here https://github.com/OP-Engineering/op-sqlite/blob/main/cpp/ThreadPool.cpp - `json.cpp` contains an implementation of JSON objects in cpp, source here https://github.com/nlohmann/json @@ -136,8 +134,7 @@ return jsi::Function::createFromHostFunction(runtime, // create function using r try { GA_auth_handler *call; utils::wrapCall(GA_get_receive_address(session, details, &call)); - TwoFactorCall twoFactorCall(call); - json res = utils::resolve(twoFactorCall); + json res = utils::resolve(call); // the long running task finishes and we need to resolve or reject the promise // lock the weak pointer to the invoker to gain control of the runtime std::shared_ptr c = invoker.lock(); diff --git a/example/src/App.tsx b/example/src/App.tsx index b72fd3e..e9a9a9f 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -55,7 +55,7 @@ const App: React.FunctionComponent = () => { }} />