Skip to content

Commit

Permalink
new wallet func
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Aug 8, 2023
1 parent db2a85e commit 1b16d31
Show file tree
Hide file tree
Showing 29 changed files with 2,115 additions and 167 deletions.
5 changes: 5 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
"src/config/settings.cpp",

"src/wallet/ec_private.cpp",
"src/wallet/ec_public.cpp",
"src/wallet/elliptic_curve.cpp",
"src/wallet/hd_private.cpp",
"src/wallet/hd_public.cpp",
"src/wallet/payment_address.cpp",
"src/wallet/wallet.cpp",

],

Expand Down
4 changes: 2 additions & 2 deletions include/kth/js-native/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void chain_block_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_is_valid(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_header(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_hash(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_hash_out(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_block_hash_out(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_proof_str(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_transactions(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_serialized_size(v8::FunctionCallbackInfo<v8::Value> const& args);
Expand All @@ -25,7 +25,7 @@ void chain_block_fees(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_claim(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_reward(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_generate_merkle_root(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_generate_merkle_root_out(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_block_generate_merkle_root_out(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_signature_operations(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_signature_operations_bip16_active(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_block_total_inputs(v8::FunctionCallbackInfo<v8::Value> const& args);
Expand Down
6 changes: 2 additions & 4 deletions include/kth/js-native/chain/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
namespace kth::js_native {

kth_hash_t to_native_hash(v8::Local<v8::Uint8Array> const& arr);
kth_shorthash_t to_native_shorthash(v8::Local<v8::Uint8Array> arr);
kth_wif_compressed_t to_native_wif_compressed(v8::Local<v8::Uint8Array> arr);
kth_wif_uncompressed_t to_native_wif_uncompressed(v8::Local<v8::Uint8Array> arr);
kth_ec_secret_t to_native_ec_secret(v8::Local<v8::Uint8Array> arr);
kth_shorthash_t to_native_shorthash(v8::Local<v8::Uint8Array> const& arr);
kth_longhash_t to_native_longhash(v8::Local<v8::Uint8Array> const& arr);

} // namespace kth::js_native

Expand Down
4 changes: 2 additions & 2 deletions include/kth/js-native/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ void chain_transaction_is_valid(v8::FunctionCallbackInfo<v8::Value> const& args)
void chain_transaction_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_set_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_hash(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_hash_out(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_transaction_hash_out(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_hash_sighash_type(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_hash_sighash_type_out(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_transaction_hash_sighash_type_out(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_locktime(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_serialized_size(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_transaction_fees(v8::FunctionCallbackInfo<v8::Value> const& args);
Expand Down
22 changes: 13 additions & 9 deletions include/kth/js-native/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ T copy_data_and_free(context_t& context) {

#if defined(_WIN32)
inline
v8::Local<v8::String> string_to_js(v8::Isolate* isolate, wchar_t const* str) {
v8::Local<v8::Value> string_to_js(v8::Isolate* isolate, wchar_t const* str) {
if (str == nullptr) {
return v8::Null(isolate);
}
return v8::String::NewFromTwoByte(isolate, reinterpret_cast<uint16_t const*>(str), v8::NewStringType::kNormal).ToLocalChecked();
}
#endif // defined(_WIN32)

inline
v8::Local<v8::String> string_to_js(v8::Isolate* isolate, char const* str) {
v8::Local<v8::Value> string_to_js(v8::Isolate* isolate, char const* str) {
if (str == nullptr) {
return v8::Null(isolate);
}
return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal).ToLocalChecked();
}

Expand Down Expand Up @@ -119,6 +125,11 @@ v8::Local<v8::Uint8Array> shorthash_to_js(v8::Isolate* isolate, kth_shorthash_t
return byte_array_to_js(isolate, hash.hash, 16);
}

inline
v8::Local<v8::Uint8Array> longhash_to_js(v8::Isolate* isolate, kth_longhash_t const& hash) {
return byte_array_to_js(isolate, hash.hash, 64);
}

inline
kth_bool_t bool_to_cpp(v8::Isolate* isolate, v8::Local<v8::Value> const& x) {
bool b = x->BooleanValue(isolate);
Expand Down Expand Up @@ -164,13 +175,6 @@ kth_db_mode_t db_mode_to_cpp(v8::Isolate* isolate, v8::Local<v8::Value> const& x
return res;
}

// Wallet
inline
v8::Local<v8::Uint8Array> ec_secret_to_js(v8::Isolate* isolate, kth_ec_secret_t const& x) {
return byte_array_to_js(isolate, x.data, 32);
}


} // namespace kth::js_native

#endif // KTH_JS_NATIVE_HELPER_HPP_
124 changes: 124 additions & 0 deletions include/kth/js-native/wallet/convertions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_NATIVE_WALLET_CONVERTIONS_HPP_
#define KTH_JS_NATIVE_WALLET_CONVERTIONS_HPP_

#include <iostream>
#include <string>

#include <node.h>
#include <nan.h>

#include <kth/capi/wallet/hd_lineage.h>

namespace kth::js_native {

inline
v8::Local<v8::Uint8Array> ec_secret_to_js(v8::Isolate* isolate, kth_ec_secret_t const& x) {
return byte_array_to_js(isolate, x.hash, KTH_BITCOIN_EC_SECRET_SIZE);
}

inline
v8::Local<v8::Uint8Array> hd_key_to_js(v8::Isolate* isolate, kth_hd_key_t const& x) {
return byte_array_to_js(isolate, x.data, KTH_HD_KEY_SIZE);
}

inline
v8::Local<v8::Uint8Array> ec_compressed_to_js(v8::Isolate* isolate, kth_ec_compressed_t const& x) {
return byte_array_to_js(isolate, x.data, KTH_EC_COMPRESSED_SIZE);
}

inline
v8::Local<v8::Uint8Array> ec_uncompressed_to_js(v8::Isolate* isolate, kth_ec_uncompressed_t const& x) {
return byte_array_to_js(isolate, x.data, KTH_BITCOIN_EC_UNCOMPRESSED_SIZE);
}

inline
v8::Local<v8::Uint8Array> hd_chain_code_to_js(v8::Isolate* isolate, kth_hd_chain_code_t const& x) {
return byte_array_to_js(isolate, x.data, KTH_HD_CHAIN_CODE_SIZE);
}

inline
v8::Local<v8::Object> hd_lineage_to_js(v8::Isolate* isolate, kth_hd_lineage const& x) {
auto ctx = isolate->GetCurrentContext();
v8::Local<v8::Object> res = v8::Object::New(isolate);
res->Set(ctx, string_to_js(isolate, "prefixes"), v8::Number::New(isolate, x.prefixes));
res->Set(ctx, string_to_js(isolate, "depth"), v8::Number::New(isolate, x.depth));
res->Set(ctx, string_to_js(isolate, "parent_fingerprint"), v8::Number::New(isolate, x.parent_fingerprint));
res->Set(ctx, string_to_js(isolate, "child_number"), v8::Number::New(isolate, x.child_number));
return res;
}

inline
kth_hd_key_t to_native_hd_key(v8::Local<v8::Uint8Array> const& arr) {
//precondition: arr->Length() == KTH_HD_KEY_SIZE
kth_hd_key_t res;

#if (V8_MAJOR_VERSION >= 8)
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetBackingStore()->Data();
#else
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetContents().Data();
#endif

for (uint32_t i = 0; i < arr->Length(); ++i) {
res.data[i] = native_arr[i];
}
return res;
}

inline
kth_ec_secret_t to_native_ec_secret(v8::Local<v8::Uint8Array> const& arr) {
//precondition: arr->Length() == 32
kth_ec_secret_t res;

#if (V8_MAJOR_VERSION >= 8)
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetBackingStore()->Data();
#else
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetContents().Data();
#endif

for (uint32_t i = 0; i < arr->Length(); ++i) {
res.hash[i] = native_arr[i];
}
return res;
}

inline
kth_wif_compressed_t to_native_wif_compressed(v8::Local<v8::Uint8Array> arr) {
//precondition: arr->Length() == 38
kth_wif_compressed_t res;

#if (V8_MAJOR_VERSION >= 8)
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetBackingStore()->Data();
#else
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetContents().Data();
#endif

for (uint32_t i = 0; i < arr->Length(); ++i) {
res.data[i] = native_arr[i];
}
return res;
}

inline
kth_wif_uncompressed_t to_native_wif_uncompressed(v8::Local<v8::Uint8Array> arr) {
//precondition: arr->Length() == 37
kth_wif_uncompressed_t res;

#if (V8_MAJOR_VERSION >= 8)
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetBackingStore()->Data();
#else
uint8_t* native_arr = (uint8_t*)arr->Buffer()->GetContents().Data();
#endif

for (uint32_t i = 0; i < arr->Length(); ++i) {
res.data[i] = native_arr[i];
}
return res;
}

} // namespace kth::js_native

#endif // KTH_JS_NATIVE_WALLET_CONVERTIONS_HPP_
30 changes: 30 additions & 0 deletions include/kth/js-native/wallet/ec_private.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_WALLET_EC_PRIVATE_HPP_
#define KTH_JS_WALLET_EC_PRIVATE_HPP_

#include <node.h>

namespace kth::js_native {

void wallet_ec_private_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_construct_string(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_construct_compressed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_construct_uncompressed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_construct_secret(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_is_valid(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_encoded(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_secret(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_payment_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_wif_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_compressed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_to_public(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_private_to_payment_address(v8::FunctionCallbackInfo<v8::Value> const& args);

} // namespace kth::js_native

#endif //KTH_JS_WALLET_EC_PRIVATE_HPP_
28 changes: 28 additions & 0 deletions include/kth/js-native/wallet/ec_public.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_WALLET_EC_PUBLIC_HPP_
#define KTH_JS_WALLET_EC_PUBLIC_HPP_

#include <node.h>

namespace kth::js_native {

void wallet_ec_public_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_construct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_construct_from_decoded(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_construct_from_base16(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_construct_from_point(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_is_valid(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_encoded(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_point(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_compressed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_to_data(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_to_uncompressed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_public_to_payment_address(v8::FunctionCallbackInfo<v8::Value> const& args);

} // namespace kth::js_native

#endif //KTH_JS_WALLET_EC_PUBLIC_HPP_
16 changes: 16 additions & 0 deletions include/kth/js-native/wallet/elliptic_curve.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_WALLET_ELLIPTIC_CURVE_HPP
#define KTH_JS_WALLET_ELLIPTIC_CURVE_HPP

#include <node.h>

namespace kth::js_native {

void wallet_secret_to_public(v8::FunctionCallbackInfo<v8::Value> const& args);

} // namespace kth::js_native

#endif //KTH_JS_WALLET_ELLIPTIC_CURVE_HPP
31 changes: 31 additions & 0 deletions include/kth/js-native/wallet/hd_private.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_WALLET_HD_PRIVATE_HPP_
#define KTH_JS_WALLET_HD_PRIVATE_HPP_

#include <node.h>

namespace kth::js_native {

void wallet_hd_private_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct_with_prefix(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct_with_prefixes(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct_with_seed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct_string(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct_string_with_prefix(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_construct_string_with_prefixes(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_encoded(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_secret(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_to_hd_key(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_to_public(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_derive_private(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_derive_public(v8::FunctionCallbackInfo<v8::Value> const& args);


} // namespace kth::js_native

#endif //KTH_JS_WALLET_HD_PRIVATE_HPP_
29 changes: 29 additions & 0 deletions include/kth/js-native/wallet/hd_public.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_WALLET_HD_PUBLIC_HPP_
#define KTH_JS_WALLET_HD_PUBLIC_HPP_

#include <node.h>

namespace kth::js_native {

void wallet_hd_public_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_construct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_construct_with_prefix(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_construct_string(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_construct_string_with_prefix(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_is_valid(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_encoded(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_chain_code(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_lineage(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_point(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_to_hd_key(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_public_derive_public(v8::FunctionCallbackInfo<v8::Value> const& args);


} // namespace kth::js_native

#endif //KTH_JS_WALLET_HD_PUBLIC_HPP_
20 changes: 20 additions & 0 deletions include/kth/js-native/wallet/wallet.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2016-2023 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_JS_WALLET_WALLET_HPP_
#define KTH_JS_WALLET_WALLET_HPP_

#include <node.h>

namespace kth::js_native {

void wallet_mnemonics_to_seed(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_new(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_hd_private_to_ec(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_to_public(v8::FunctionCallbackInfo<v8::Value> const& args);
void wallet_ec_to_address(v8::FunctionCallbackInfo<v8::Value> const& args);

} // namespace kth::js_native

#endif //KTH_JS_WALLET_WALLET_HPP_
Loading

0 comments on commit 1b16d31

Please sign in to comment.