From a8d796fe0dc089a9588f8606362edcb01a4657fb Mon Sep 17 00:00:00 2001 From: huiqi Date: Wed, 25 Nov 2020 16:30:34 +0100 Subject: [PATCH] fix Preferencesbackend binding problem --- include/PreferencesBackend.hpp | 6 +- src/NJSPreferencesBackend.cpp | 247 ++++++++++++++ ...ckendCpp.hpp => NJSPreferencesBackend.hpp} | 63 +++- src/NJSPreferencesBackendCpp.cpp | 312 ------------------ src/NJSWalletPoolBuilderCpp.cpp | 8 - src/NJSWalletPoolBuilderCpp.hpp | 2 +- src/ledgercore.cpp | 2 +- 7 files changed, 312 insertions(+), 328 deletions(-) create mode 100644 src/NJSPreferencesBackend.cpp rename src/{NJSPreferencesBackendCpp.hpp => NJSPreferencesBackend.hpp} (52%) delete mode 100644 src/NJSPreferencesBackendCpp.cpp diff --git a/include/PreferencesBackend.hpp b/include/PreferencesBackend.hpp index b766e57..17c062e 100644 --- a/include/PreferencesBackend.hpp +++ b/include/PreferencesBackend.hpp @@ -23,7 +23,7 @@ class RandomNumberGenerator; struct PreferencesChange; /** Interface describing the behaviour of the backend used by Preferences. */ -class LIBCORE_EXPORT PreferencesBackend { +class PreferencesBackend { public: virtual ~PreferencesBackend() {} @@ -32,7 +32,7 @@ class LIBCORE_EXPORT PreferencesBackend { * @param key The data key. * @return The value associated to the key if it exists, an empty option otherwise. */ - virtual std::experimental::optional> get(const std::vector & key) const = 0; + virtual std::experimental::optional> get(const std::vector & key) = 0; /** * Commit a change. @@ -74,7 +74,7 @@ class LIBCORE_EXPORT PreferencesBackend { * Get encryption salt, if any. * @return the encryption salt if it exists, an empty string otherwise. */ - virtual std::string getEncryptionSalt() const = 0; + virtual std::string getEncryptionSalt() = 0; /** Clear all preferences. */ virtual void clear() = 0; diff --git a/src/NJSPreferencesBackend.cpp b/src/NJSPreferencesBackend.cpp new file mode 100644 index 0000000..2d446df --- /dev/null +++ b/src/NJSPreferencesBackend.cpp @@ -0,0 +1,247 @@ +// AUTOGENERATED FILE - DO NOT MODIFY! +// This file generated by Djinni from preferences.djinni + +#include "NJSPreferencesBackend.hpp" +#include "NJSObjectWrapper.hpp" +#include "NJSHexUtils.hpp" + +using namespace v8; +using namespace node; +using namespace std; + +std::experimental::optional> NJSPreferencesBackend::get(const std::vector & key) +{ + Nan::HandleScope scope; + //Wrap parameters + auto arg_0 = Nan::New("0x" + djinni::js::hex::toString(key)).ToLocalChecked(); + + Local args[1] = {arg_0}; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::get fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("get").ToLocalChecked()).ToLocalChecked(); + auto result_get = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,1,args); + if(result_get.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::get call failed"); + } + auto checkedResult_get = result_get.ToLocalChecked(); + auto fResult_get = std::experimental::optional>(); + if(!checkedResult_get->IsNull() && !checkedResult_get->IsUndefined()) + { + if(!checkedResult_get->IsString()) + { + Nan::ThrowError("checkedResult_get should be a hexadecimal string."); + } + std::vector opt_fResult_get; + Nan::Utf8String str_opt_fResult_get(checkedResult_get); + std::string string_opt_fResult_get(*str_opt_fResult_get, str_opt_fResult_get.length()); + if (string_opt_fResult_get.rfind("0x", 0) == 0) + { + opt_fResult_get = djinni::js::hex::toByteArray(string_opt_fResult_get.substr(2)); + } + else + { + opt_fResult_get = std::vector(string_opt_fResult_get.cbegin(), string_opt_fResult_get.cend()); + } + + fResult_get.emplace(opt_fResult_get); + } + + return fResult_get; +} + +bool NJSPreferencesBackend::commit(const std::vector<::ledger::core::api::PreferencesChange> & changes) +{ + Nan::HandleScope scope; + //Wrap parameters + Local arg_0 = Nan::New(); + for(size_t arg_0_id = 0; arg_0_id < changes.size(); arg_0_id++) + { + auto arg_0_elem = Nan::New(); + auto arg_0_elem_1 = Nan::New((int)changes[arg_0_id].type); + Nan::DefineOwnProperty(arg_0_elem, Nan::New("type").ToLocalChecked(), arg_0_elem_1); + auto arg_0_elem_2 = Nan::New("0x" + djinni::js::hex::toString(changes[arg_0_id].key)).ToLocalChecked(); + + Nan::DefineOwnProperty(arg_0_elem, Nan::New("key").ToLocalChecked(), arg_0_elem_2); + auto arg_0_elem_3 = Nan::New("0x" + djinni::js::hex::toString(changes[arg_0_id].value)).ToLocalChecked(); + + Nan::DefineOwnProperty(arg_0_elem, Nan::New("value").ToLocalChecked(), arg_0_elem_3); + + Nan::Set(arg_0, (int)arg_0_id,arg_0_elem); + } + + Local args[1] = {arg_0}; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::commit fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("commit").ToLocalChecked()).ToLocalChecked(); + auto result_commit = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,1,args); + if(result_commit.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::commit call failed"); + } + auto checkedResult_commit = result_commit.ToLocalChecked(); + auto fResult_commit = Nan::To(checkedResult_commit).FromJust(); + return fResult_commit; +} + +void NJSPreferencesBackend::setEncryption(const std::shared_ptr<::ledger::core::api::RandomNumberGenerator> & rng, const std::string & password) +{ + Nan::HandleScope scope; + //Wrap parameters + auto arg_0 = NJSRandomNumberGenerator::wrap(rng); + + auto arg_1 = Nan::New(password).ToLocalChecked(); + Local args[2] = {arg_0,arg_1}; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::setEncryption fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("setEncryption").ToLocalChecked()).ToLocalChecked(); + auto result_setEncryption = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,2,args); + if(result_setEncryption.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::setEncryption call failed"); + } +} + +void NJSPreferencesBackend::unsetEncryption() +{ + Nan::HandleScope scope; + //Wrap parameters + Local args[1]; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::unsetEncryption fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("unsetEncryption").ToLocalChecked()).ToLocalChecked(); + auto result_unsetEncryption = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,0,args); + if(result_unsetEncryption.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::unsetEncryption call failed"); + } +} + +bool NJSPreferencesBackend::resetEncryption(const std::shared_ptr<::ledger::core::api::RandomNumberGenerator> & rng, const std::string & oldPassword, const std::string & newPassword) +{ + Nan::HandleScope scope; + //Wrap parameters + auto arg_0 = NJSRandomNumberGenerator::wrap(rng); + + auto arg_1 = Nan::New(oldPassword).ToLocalChecked(); + auto arg_2 = Nan::New(newPassword).ToLocalChecked(); + Local args[3] = {arg_0,arg_1,arg_2}; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::resetEncryption fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("resetEncryption").ToLocalChecked()).ToLocalChecked(); + auto result_resetEncryption = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,3,args); + if(result_resetEncryption.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::resetEncryption call failed"); + } + auto checkedResult_resetEncryption = result_resetEncryption.ToLocalChecked(); + auto fResult_resetEncryption = Nan::To(checkedResult_resetEncryption).FromJust(); + return fResult_resetEncryption; +} + +std::string NJSPreferencesBackend::getEncryptionSalt() +{ + Nan::HandleScope scope; + //Wrap parameters + Local args[1]; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::getEncryptionSalt fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("getEncryptionSalt").ToLocalChecked()).ToLocalChecked(); + auto result_getEncryptionSalt = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,0,args); + if(result_getEncryptionSalt.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::getEncryptionSalt call failed"); + } + auto checkedResult_getEncryptionSalt = result_getEncryptionSalt.ToLocalChecked(); + Nan::Utf8String string_fResult_getEncryptionSalt(checkedResult_getEncryptionSalt->ToString(Nan::GetCurrentContext()).ToLocalChecked()); + auto fResult_getEncryptionSalt = std::string(*string_fResult_getEncryptionSalt); + return fResult_getEncryptionSalt; +} + +void NJSPreferencesBackend::clear() +{ + Nan::HandleScope scope; + //Wrap parameters + Local args[1]; + Local local_njs_impl = Nan::New(njs_impl); + if(!local_njs_impl->IsObject()) + { + Nan::ThrowError("NJSPreferencesBackend::clear fail to retrieve node implementation"); + } + auto calling_funtion = Nan::Get(local_njs_impl,Nan::New("clear").ToLocalChecked()).ToLocalChecked(); + auto result_clear = Nan::CallAsFunction(calling_funtion->ToObject(Nan::GetCurrentContext()).ToLocalChecked(),local_njs_impl,0,args); + if(result_clear.IsEmpty()) + { + Nan::ThrowError("NJSPreferencesBackend::clear call failed"); + } +} + +NAN_METHOD(NJSPreferencesBackend::New) { + //Only new allowed + if(!info.IsConstructCall()) + { + return Nan::ThrowError("NJSPreferencesBackend function can only be called as constructor (use New)"); + } + + if(!info[0]->IsObject()) + { + return Nan::ThrowError("NJSPreferencesBackend::New requires an implementation from node"); + } + auto node_instance = std::make_shared(info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked()); + djinni::js::ObjectWrapper::Wrap(node_instance, info.This()); + info.GetReturnValue().Set(info.This()); +} + + +Nan::Persistent NJSPreferencesBackend::PreferencesBackend_prototype; + +Local NJSPreferencesBackend::wrap(const std::shared_ptr &object) { + Nan::EscapableHandleScope scope; + Local local_prototype = Nan::New(PreferencesBackend_prototype); + + Local obj; + if(!local_prototype.IsEmpty()) + { + obj = local_prototype->NewInstance(Nan::GetCurrentContext()).ToLocalChecked(); + djinni::js::ObjectWrapper::Wrap(object, obj); + } + else + { + Nan::ThrowError("NJSPreferencesBackend::wrap: object template not valid"); + } + return scope.Escape(obj); +} + +void NJSPreferencesBackend::Initialize(Local target) { + Nan::HandleScope scope; + + Local func_template = Nan::New(NJSPreferencesBackend::New); + Local objectTemplate = func_template->InstanceTemplate(); + objectTemplate->SetInternalFieldCount(1); + + func_template->SetClassName(Nan::New("NJSPreferencesBackend").ToLocalChecked()); + Nan::SetPrototypeMethod(func_template,"New", New); + //Set object prototype + PreferencesBackend_prototype.Reset(objectTemplate); + + //Add template to target + Nan::Set(target, Nan::New("NJSPreferencesBackend").ToLocalChecked(), Nan::GetFunction(func_template).ToLocalChecked()); +} diff --git a/src/NJSPreferencesBackendCpp.hpp b/src/NJSPreferencesBackend.hpp similarity index 52% rename from src/NJSPreferencesBackendCpp.hpp rename to src/NJSPreferencesBackend.hpp index b16d3e9..26d3a9c 100644 --- a/src/NJSPreferencesBackendCpp.hpp +++ b/src/NJSPreferencesBackend.hpp @@ -7,6 +7,7 @@ #include "../include/../utils/optional.hpp" #include "../include/PreferencesChange.hpp" +#include "../include/RandomNumberGenerator.hpp" #include "NJSRandomNumberGenerator.hpp" #include #include @@ -22,14 +23,70 @@ using namespace node; using namespace std; using namespace ledger::core::api; -class NJSPreferencesBackend final { +class NJSPreferencesBackend: public ledger::core::api::PreferencesBackend { public: static void Initialize(Local target); - NJSPreferencesBackend() = delete; static Local wrap(const std::shared_ptr &object); static Nan::Persistent PreferencesBackend_prototype; + ~NJSPreferencesBackend() + { + njs_impl.Reset(); + }; + NJSPreferencesBackend(Local njs_implementation){njs_impl.Reset(njs_implementation);}; + + /** + * Gets the value associated to the given key. + * @param key The data key. + * @return The value associated to the key if it exists, an empty option otherwise. + */ + std::experimental::optional> get(const std::vector & key); + + /** + * Commit a change. + * @param changes The list of changes to commit. + * @return false if unsuccessful (might happen if the underlying DB was destroyed). + */ + bool commit(const std::vector<::ledger::core::api::PreferencesChange> & changes); + + /** + * Turn encryption on for all future uses. + * This method will set encryption on for all future values that will be persisted. + * If this function is called on a plaintext storage (i.e. first encryption for + * instance), it will also encrypt all data already present. + * @param rng Random number generator used to generate the encryption salt. + * @param password The new password. + */ + void setEncryption(const std::shared_ptr<::ledger::core::api::RandomNumberGenerator> & rng, const std::string & password); + + /** + * Turn off encryption by disabling the use of the internal cipher. Data is left + * untouched. + * This method is suitable when you want to get back raw, encrypted data. If you want + * to disable encryption in order to read clear data back without password, consider + * the resetEncryption method instead. + */ + void unsetEncryption(); + + /** + * Reset the encryption with a new password by first decrypting on the + * fly with the old password the data present. + * If the new password is an empty string, after this method is called, the database + * is completely unciphered and no password is required to read from it. + * @return true if the reset occurred correctly, false otherwise (e.g. trying to change + * password with an old password but without a proper salt already persisted). + */ + bool resetEncryption(const std::shared_ptr<::ledger::core::api::RandomNumberGenerator> & rng, const std::string & oldPassword, const std::string & newPassword); + + /** + * Get encryption salt, if any. + * @return the encryption salt if it exists, an empty string otherwise. + */ + std::string getEncryptionSalt(); + + /** Clear all preferences. */ + void clear(); private: /** @@ -86,6 +143,6 @@ class NJSPreferencesBackend final { static NAN_METHOD(New); - static NAN_METHOD(isNull); + Nan::Persistent njs_impl; }; #endif //DJINNI_GENERATED_NJSPREFERENCESBACKEND_HPP diff --git a/src/NJSPreferencesBackendCpp.cpp b/src/NJSPreferencesBackendCpp.cpp deleted file mode 100644 index fcbedab..0000000 --- a/src/NJSPreferencesBackendCpp.cpp +++ /dev/null @@ -1,312 +0,0 @@ -// AUTOGENERATED FILE - DO NOT MODIFY! -// This file generated by Djinni from preferences.djinni - -#include "NJSPreferencesBackendCpp.hpp" -#include "NJSObjectWrapper.hpp" -#include "NJSHexUtils.hpp" - -using namespace v8; -using namespace node; -using namespace std; - -NAN_METHOD(NJSPreferencesBackend::get) { - - //Check if method called with right number of arguments - if(info.Length() != 1) - { - return Nan::ThrowError("NJSPreferencesBackend::get needs 1 arguments"); - } - - //Check if parameters have correct types - if(!info[0]->IsString()) - { - Nan::ThrowError("info[0] should be a hexadecimal string."); - } - std::vector arg_0; - Nan::Utf8String str_arg_0(info[0]); - std::string string_arg_0(*str_arg_0, str_arg_0.length()); - if (string_arg_0.rfind("0x", 0) == 0) - { - arg_0 = djinni::js::hex::toByteArray(string_arg_0.substr(2)); - } - else - { - arg_0 = std::vector(string_arg_0.cbegin(), string_arg_0.cend()); - } - - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::get : implementation of PreferencesBackend is not valid"); - } - - auto result = cpp_impl->get(arg_0); - - //Wrap result in node object - Local arg_1; - if(result) - { - auto arg_1_optional = (result).value(); - auto arg_1_tmp = Nan::New("0x" + djinni::js::hex::toString(arg_1_optional)).ToLocalChecked(); - - arg_1 = arg_1_tmp; - } - - - //Return result - info.GetReturnValue().Set(arg_1); -} -NAN_METHOD(NJSPreferencesBackend::commit) { - - //Check if method called with right number of arguments - if(info.Length() != 1) - { - return Nan::ThrowError("NJSPreferencesBackend::commit needs 1 arguments"); - } - - //Check if parameters have correct types - vector arg_0; - Local arg_0_container = Local::Cast(info[0]); - for(uint32_t arg_0_id = 0; arg_0_id < arg_0_container->Length(); arg_0_id++) - { - if(arg_0_container->Get(Nan::GetCurrentContext(), arg_0_id).ToLocalChecked()->IsObject()) - { - - auto field_arg_0_elem_1 = Nan::Get(arg_0_container->Get(Nan::GetCurrentContext(), arg_0_id).ToLocalChecked()->ToObject(Nan::GetCurrentContext()).ToLocalChecked(), Nan::New("type").ToLocalChecked()).ToLocalChecked(); - auto arg_0_elem_1 = (ledger::core::api::PreferencesChangeType)Nan::To(field_arg_0_elem_1).FromJust(); - - auto field_arg_0_elem_2 = Nan::Get(arg_0_container->Get(Nan::GetCurrentContext(), arg_0_id).ToLocalChecked()->ToObject(Nan::GetCurrentContext()).ToLocalChecked(), Nan::New("key").ToLocalChecked()).ToLocalChecked(); - if(!field_arg_0_elem_2->IsString()) - { - Nan::ThrowError("field_arg_0_elem_2 should be a hexadecimal string."); - } - std::vector arg_0_elem_2; - Nan::Utf8String str_arg_0_elem_2(field_arg_0_elem_2); - std::string string_arg_0_elem_2(*str_arg_0_elem_2, str_arg_0_elem_2.length()); - if (string_arg_0_elem_2.rfind("0x", 0) == 0) - { - arg_0_elem_2 = djinni::js::hex::toByteArray(string_arg_0_elem_2.substr(2)); - } - else - { - arg_0_elem_2 = std::vector(string_arg_0_elem_2.cbegin(), string_arg_0_elem_2.cend()); - } - - - auto field_arg_0_elem_3 = Nan::Get(arg_0_container->Get(Nan::GetCurrentContext(), arg_0_id).ToLocalChecked()->ToObject(Nan::GetCurrentContext()).ToLocalChecked(), Nan::New("value").ToLocalChecked()).ToLocalChecked(); - if(!field_arg_0_elem_3->IsString()) - { - Nan::ThrowError("field_arg_0_elem_3 should be a hexadecimal string."); - } - std::vector arg_0_elem_3; - Nan::Utf8String str_arg_0_elem_3(field_arg_0_elem_3); - std::string string_arg_0_elem_3(*str_arg_0_elem_3, str_arg_0_elem_3.length()); - if (string_arg_0_elem_3.rfind("0x", 0) == 0) - { - arg_0_elem_3 = djinni::js::hex::toByteArray(string_arg_0_elem_3.substr(2)); - } - else - { - arg_0_elem_3 = std::vector(string_arg_0_elem_3.cbegin(), string_arg_0_elem_3.cend()); - } - - PreferencesChange arg_0_elem(arg_0_elem_1, arg_0_elem_2, arg_0_elem_3); - - arg_0.emplace_back(arg_0_elem); - } - } - - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::commit : implementation of PreferencesBackend is not valid"); - } - - auto result = cpp_impl->commit(arg_0); - - //Wrap result in node object - auto arg_1 = Nan::New(result); - - //Return result - info.GetReturnValue().Set(arg_1); -} -NAN_METHOD(NJSPreferencesBackend::setEncryption) { - - //Check if method called with right number of arguments - if(info.Length() != 2) - { - return Nan::ThrowError("NJSPreferencesBackend::setEncryption needs 2 arguments"); - } - - //Check if parameters have correct types - Local njs_arg_0 = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked(); - auto arg_0 = djinni::js::ObjectWrapper::Unwrap(njs_arg_0); - - Nan::Utf8String string_arg_1(info[1]->ToString(Nan::GetCurrentContext()).ToLocalChecked()); - auto arg_1 = std::string(*string_arg_1); - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::setEncryption : implementation of PreferencesBackend is not valid"); - } - cpp_impl->setEncryption(arg_0,arg_1); -} -NAN_METHOD(NJSPreferencesBackend::unsetEncryption) { - - //Check if method called with right number of arguments - if(info.Length() != 0) - { - return Nan::ThrowError("NJSPreferencesBackend::unsetEncryption needs 0 arguments"); - } - - //Check if parameters have correct types - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::unsetEncryption : implementation of PreferencesBackend is not valid"); - } - cpp_impl->unsetEncryption(); -} -NAN_METHOD(NJSPreferencesBackend::resetEncryption) { - - //Check if method called with right number of arguments - if(info.Length() != 3) - { - return Nan::ThrowError("NJSPreferencesBackend::resetEncryption needs 3 arguments"); - } - - //Check if parameters have correct types - Local njs_arg_0 = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked(); - auto arg_0 = djinni::js::ObjectWrapper::Unwrap(njs_arg_0); - - Nan::Utf8String string_arg_1(info[1]->ToString(Nan::GetCurrentContext()).ToLocalChecked()); - auto arg_1 = std::string(*string_arg_1); - Nan::Utf8String string_arg_2(info[2]->ToString(Nan::GetCurrentContext()).ToLocalChecked()); - auto arg_2 = std::string(*string_arg_2); - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::resetEncryption : implementation of PreferencesBackend is not valid"); - } - - auto result = cpp_impl->resetEncryption(arg_0,arg_1,arg_2); - - //Wrap result in node object - auto arg_3 = Nan::New(result); - - //Return result - info.GetReturnValue().Set(arg_3); -} -NAN_METHOD(NJSPreferencesBackend::getEncryptionSalt) { - - //Check if method called with right number of arguments - if(info.Length() != 0) - { - return Nan::ThrowError("NJSPreferencesBackend::getEncryptionSalt needs 0 arguments"); - } - - //Check if parameters have correct types - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::getEncryptionSalt : implementation of PreferencesBackend is not valid"); - } - - auto result = cpp_impl->getEncryptionSalt(); - - //Wrap result in node object - auto arg_0 = Nan::New(result).ToLocalChecked(); - - //Return result - info.GetReturnValue().Set(arg_0); -} -NAN_METHOD(NJSPreferencesBackend::clear) { - - //Check if method called with right number of arguments - if(info.Length() != 0) - { - return Nan::ThrowError("NJSPreferencesBackend::clear needs 0 arguments"); - } - - //Check if parameters have correct types - - //Unwrap current object and retrieve its Cpp Implementation - auto cpp_impl = djinni::js::ObjectWrapper::Unwrap(info.This()); - if(!cpp_impl) - { - return Nan::ThrowError("NJSPreferencesBackend::clear : implementation of PreferencesBackend is not valid"); - } - cpp_impl->clear(); -} - -NAN_METHOD(NJSPreferencesBackend::New) { - //Only new allowed - if(!info.IsConstructCall()) - { - return Nan::ThrowError("NJSPreferencesBackend function can only be called as constructor (use New)"); - } - info.GetReturnValue().Set(info.This()); -} - - -Nan::Persistent NJSPreferencesBackend::PreferencesBackend_prototype; - -Local NJSPreferencesBackend::wrap(const std::shared_ptr &object) { - Nan::EscapableHandleScope scope; - Local local_prototype = Nan::New(PreferencesBackend_prototype); - - Local obj; - if(!local_prototype.IsEmpty()) - { - obj = local_prototype->NewInstance(Nan::GetCurrentContext()).ToLocalChecked(); - djinni::js::ObjectWrapper::Wrap(object, obj); - } - else - { - Nan::ThrowError("NJSPreferencesBackend::wrap: object template not valid"); - } - return scope.Escape(obj); -} - -NAN_METHOD(NJSPreferencesBackend::isNull) { - auto cpp_implementation = djinni::js::ObjectWrapper::Unwrap(info.This()); - auto isNull = !cpp_implementation ? true : false; - return info.GetReturnValue().Set(Nan::New(isNull)); -} - -void NJSPreferencesBackend::Initialize(Local target) { - Nan::HandleScope scope; - - Local func_template = Nan::New(NJSPreferencesBackend::New); - Local objectTemplate = func_template->InstanceTemplate(); - objectTemplate->SetInternalFieldCount(1); - - func_template->SetClassName(Nan::New("NJSPreferencesBackend").ToLocalChecked()); - - //SetPrototypeMethod all methods - Nan::SetPrototypeMethod(func_template,"get", get); - Nan::SetPrototypeMethod(func_template,"commit", commit); - Nan::SetPrototypeMethod(func_template,"setEncryption", setEncryption); - Nan::SetPrototypeMethod(func_template,"unsetEncryption", unsetEncryption); - Nan::SetPrototypeMethod(func_template,"resetEncryption", resetEncryption); - Nan::SetPrototypeMethod(func_template,"getEncryptionSalt", getEncryptionSalt); - Nan::SetPrototypeMethod(func_template,"clear", clear); - Nan::SetPrototypeMethod(func_template,"isNull", isNull); - //Set object prototype - PreferencesBackend_prototype.Reset(objectTemplate); - - //Add template to target - Nan::Set(target, Nan::New("NJSPreferencesBackend").ToLocalChecked(), Nan::GetFunction(func_template).ToLocalChecked()); -} diff --git a/src/NJSWalletPoolBuilderCpp.cpp b/src/NJSWalletPoolBuilderCpp.cpp index d3dc430..e931114 100644 --- a/src/NJSWalletPoolBuilderCpp.cpp +++ b/src/NJSWalletPoolBuilderCpp.cpp @@ -316,10 +316,6 @@ NAN_METHOD(NJSWalletPoolBuilder::setExternalPreferencesBackend) { //Check if parameters have correct types Local njs_arg_0 = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked(); auto arg_0 = djinni::js::ObjectWrapper::Unwrap(njs_arg_0); - if(!arg_0) - { - return Nan::ThrowError("NodeJs Object to NJSPreferencesBackend failed"); - } //Unwrap current object and retrieve its Cpp Implementation @@ -349,10 +345,6 @@ NAN_METHOD(NJSWalletPoolBuilder::setInternalPreferencesBackend) { //Check if parameters have correct types Local njs_arg_0 = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked(); auto arg_0 = djinni::js::ObjectWrapper::Unwrap(njs_arg_0); - if(!arg_0) - { - return Nan::ThrowError("NodeJs Object to NJSPreferencesBackend failed"); - } //Unwrap current object and retrieve its Cpp Implementation diff --git a/src/NJSWalletPoolBuilderCpp.hpp b/src/NJSWalletPoolBuilderCpp.hpp index 6f0632b..55daec5 100644 --- a/src/NJSWalletPoolBuilderCpp.hpp +++ b/src/NJSWalletPoolBuilderCpp.hpp @@ -10,7 +10,7 @@ #include "NJSHttpClient.hpp" #include "NJSLogPrinter.hpp" #include "NJSPathResolver.hpp" -#include "NJSPreferencesBackendCpp.hpp" +#include "NJSPreferencesBackend.hpp" #include "NJSRandomNumberGenerator.hpp" #include "NJSThreadDispatcher.hpp" #include "NJSWalletPoolCallback.hpp" diff --git a/src/ledgercore.cpp b/src/ledgercore.cpp index 263c96b..886b29e 100644 --- a/src/ledgercore.cpp +++ b/src/ledgercore.cpp @@ -146,7 +146,7 @@ #include "NJSRandomNumberGenerator.hpp" #include "NJSPreferencesCpp.hpp" #include "NJSPreferencesEditorCpp.hpp" -#include "NJSPreferencesBackendCpp.hpp" +#include "NJSPreferencesBackend.hpp" #include "NJSBitcoinLikeWalletConfigurationCpp.hpp" #include "NJSBitcoinLikeInputCpp.hpp" #include "NJSBitcoinLikeOutputCpp.hpp"