Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

fix Preferencesbackend binding problem #93

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions include/PreferencesBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -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<std::vector<uint8_t>> get(const std::vector<uint8_t> & key) const = 0;
virtual std::experimental::optional<std::vector<uint8_t>> get(const std::vector<uint8_t> & key) = 0;

/**
* Commit a change.
Expand Down Expand Up @@ -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;
Expand Down
247 changes: 247 additions & 0 deletions src/NJSPreferencesBackend.cpp
Original file line number Diff line number Diff line change
@@ -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<std::vector<uint8_t>> NJSPreferencesBackend::get(const std::vector<uint8_t> & key)
{
Nan::HandleScope scope;
//Wrap parameters
auto arg_0 = Nan::New<String>("0x" + djinni::js::hex::toString(key)).ToLocalChecked();

Local<Value> args[1] = {arg_0};
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<std::vector<uint8_t>>();
if(!checkedResult_get->IsNull() && !checkedResult_get->IsUndefined())
{
if(!checkedResult_get->IsString())
{
Nan::ThrowError("checkedResult_get should be a hexadecimal string.");
}
std::vector<uint8_t> 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<uint8_t>(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<Array> arg_0 = Nan::New<Array>();
for(size_t arg_0_id = 0; arg_0_id < changes.size(); arg_0_id++)
{
auto arg_0_elem = Nan::New<Object>();
auto arg_0_elem_1 = Nan::New<Integer>((int)changes[arg_0_id].type);
Nan::DefineOwnProperty(arg_0_elem, Nan::New<String>("type").ToLocalChecked(), arg_0_elem_1);
auto arg_0_elem_2 = Nan::New<String>("0x" + djinni::js::hex::toString(changes[arg_0_id].key)).ToLocalChecked();

Nan::DefineOwnProperty(arg_0_elem, Nan::New<String>("key").ToLocalChecked(), arg_0_elem_2);
auto arg_0_elem_3 = Nan::New<String>("0x" + djinni::js::hex::toString(changes[arg_0_id].value)).ToLocalChecked();

Nan::DefineOwnProperty(arg_0_elem, Nan::New<String>("value").ToLocalChecked(), arg_0_elem_3);

Nan::Set(arg_0, (int)arg_0_id,arg_0_elem);
}

Local<Value> args[1] = {arg_0};
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<bool>(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<String>(password).ToLocalChecked();
Local<Value> args[2] = {arg_0,arg_1};
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<Value> args[1];
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<String>(oldPassword).ToLocalChecked();
auto arg_2 = Nan::New<String>(newPassword).ToLocalChecked();
Local<Value> args[3] = {arg_0,arg_1,arg_2};
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<bool>(checkedResult_resetEncryption).FromJust();
return fResult_resetEncryption;
}

std::string NJSPreferencesBackend::getEncryptionSalt()
{
Nan::HandleScope scope;
//Wrap parameters
Local<Value> args[1];
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<Value> args[1];
Local<Object> local_njs_impl = Nan::New<Object>(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<String>("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<NJSPreferencesBackend>(info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked());
djinni::js::ObjectWrapper<NJSPreferencesBackend>::Wrap(node_instance, info.This());
info.GetReturnValue().Set(info.This());
}


Nan::Persistent<ObjectTemplate> NJSPreferencesBackend::PreferencesBackend_prototype;

Local<Object> NJSPreferencesBackend::wrap(const std::shared_ptr<ledger::core::api::PreferencesBackend> &object) {
Nan::EscapableHandleScope scope;
Local<ObjectTemplate> local_prototype = Nan::New(PreferencesBackend_prototype);

Local<Object> obj;
if(!local_prototype.IsEmpty())
{
obj = local_prototype->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
djinni::js::ObjectWrapper<ledger::core::api::PreferencesBackend>::Wrap(object, obj);
}
else
{
Nan::ThrowError("NJSPreferencesBackend::wrap: object template not valid");
}
return scope.Escape(obj);
}

void NJSPreferencesBackend::Initialize(Local<Object> target) {
Nan::HandleScope scope;

Local<FunctionTemplate> func_template = Nan::New<FunctionTemplate>(NJSPreferencesBackend::New);
Local<ObjectTemplate> objectTemplate = func_template->InstanceTemplate();
objectTemplate->SetInternalFieldCount(1);

func_template->SetClassName(Nan::New<String>("NJSPreferencesBackend").ToLocalChecked());
Nan::SetPrototypeMethod(func_template,"New", New);
//Set object prototype
PreferencesBackend_prototype.Reset(objectTemplate);

//Add template to target
Nan::Set(target, Nan::New<String>("NJSPreferencesBackend").ToLocalChecked(), Nan::GetFunction(func_template).ToLocalChecked());
}
63 changes: 60 additions & 3 deletions src/NJSPreferencesBackendCpp.hpp → src/NJSPreferencesBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "../include/../utils/optional.hpp"
#include "../include/PreferencesChange.hpp"
#include "../include/RandomNumberGenerator.hpp"
#include "NJSRandomNumberGenerator.hpp"
#include <cstdint>
#include <memory>
Expand All @@ -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<Object> target);
NJSPreferencesBackend() = delete;

static Local<Object> wrap(const std::shared_ptr<ledger::core::api::PreferencesBackend> &object);
static Nan::Persistent<ObjectTemplate> PreferencesBackend_prototype;
~NJSPreferencesBackend()
{
njs_impl.Reset();
};
NJSPreferencesBackend(Local<Object> 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<std::vector<uint8_t>> get(const std::vector<uint8_t> & 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:
/**
Expand Down Expand Up @@ -86,6 +143,6 @@ class NJSPreferencesBackend final {

static NAN_METHOD(New);

static NAN_METHOD(isNull);
Nan::Persistent<Object> njs_impl;
};
#endif //DJINNI_GENERATED_NJSPREFERENCESBACKEND_HPP
Loading