Skip to content

Commit

Permalink
improves header class
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Sep 22, 2020
1 parent af3eb38 commit f1b87a6
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 55 deletions.
63 changes: 53 additions & 10 deletions include/kth/js-native/chain/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,61 @@

namespace kth::js_native {

// void chain_header_factory_from_data(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_satoshi_fixed_size(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_reset(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_serialized_size(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_to_data(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_construct(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_version(v8::FunctionCallbackInfo<v8::Value> const& args);

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

void chain_header_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_bits(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_proof_str(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_nonce(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_previous_block_hash(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_merkle(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_hash(v8::FunctionCallbackInfo<v8::Value> const& args);

void chain_header_set_version(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_previous_block_hash(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_merkle(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_hash(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_set_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_bits(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_set_bits(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_nonce(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_set_nonce(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_set_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_set_bits(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_set_nonce(v8::FunctionCallbackInfo<v8::Value> const& args);

// void chain_header_previous_block_hash_out(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_merkle_out(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_hash_out(v8::FunctionCallbackInfo<v8::Value> const& args);


// kth_header_t kth_chain_header_factory_from_data(uint32_t version, uint8_t* data, uint64_t n);
// kth_size_t kth_chain_header_satoshi_fixed_size(uint32_t version);
// void kth_chain_header_reset(kth_header_t header);
// kth_size_t kth_chain_header_serialized_size(kth_header_t header, uint32_t version);
// uint8_t const* kth_chain_header_to_data(kth_header_t header, uint32_t version, kth_size_t* out_size);
// kth_header_t kth_chain_header_construct_default(void);
// kth_header_t kth_chain_header_construct(uint32_t version, uint8_t* previous_block_hash, uint8_t* merkle, uint32_t timestamp, uint32_t bits, uint32_t nonce);
// void kth_chain_header_destruct(kth_header_t header);
// int kth_chain_header_is_valid(kth_header_t header);
// uint32_t kth_chain_header_version(kth_header_t header);
// void kth_chain_header_set_version(kth_header_t header, uint32_t version);
// uint32_t kth_chain_header_timestamp(kth_header_t header);
// void kth_chain_header_set_timestamp(kth_header_t header, uint32_t timestamp);
// uint32_t kth_chain_header_bits(kth_header_t header);
// char const* kth_chain_header_proof_str(kth_header_t header);
// void kth_chain_header_set_bits(kth_header_t header, uint32_t bits);
// uint32_t kth_chain_header_nonce(kth_header_t header);
// void kth_chain_header_set_nonce(kth_header_t header, uint32_t nonce);
// kth_hash_t kth_chain_header_previous_block_hash(kth_header_t header);
// void kth_chain_header_previous_block_hash_out(kth_header_t header, kth_hash_t* out_previous_block_hash);
// kth_hash_t kth_chain_header_merkle(kth_header_t header);
// void kth_chain_header_merkle_out(kth_header_t header, kth_hash_t* out_merkle);
// kth_hash_t kth_chain_header_hash(kth_header_t header);
// void kth_chain_header_hash_out(kth_header_t header, kth_hash_t* out_hash);


} // namespace kth::js_native

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kth-bch-native",
"version": "0.5.0",
"version": "0.6.0",
"description": "Bitcoin, Bitcoin Cash and Litecoin development platform for Javascript applications",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/chain/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void chain_fetch_block_height(FunctionCallbackInfo<Value> const& args) {
}

if ( ! args[1]->IsUint8Array()) {
throw_exception(isolate, "Wrong arguments, 1");
throw_exception(isolate, "Wrong arguments, 1");
return;
}

Expand Down
171 changes: 149 additions & 22 deletions src/chain/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <kth/capi/chain/header.h>
#include <kth/js-native/chain/header.hpp>
#include <kth/js-native/chain/tools.hpp>
#include <kth/js-native/helper.hpp>

namespace kth::js_native {
Expand All @@ -29,6 +30,124 @@ using v8::Function;
using v8::Uint8Array;
using v8::ArrayBuffer;

// // kth_header_t kth_chain_header_factory_from_data(uint32_t version, uint8_t* data, uint64_t n);
// void chain_header_factory_from_data(v8::FunctionCallbackInfo<v8::Value> const& args) {

// }

// // kth_size_t kth_chain_header_satoshi_fixed_size(uint32_t version);
// void chain_header_satoshi_fixed_size(v8::FunctionCallbackInfo<v8::Value> const& args) {

// }

// // void kth_chain_header_reset(kth_header_t header);
// // void chain_header_reset(v8::FunctionCallbackInfo<v8::Value> const& args) {}

// // kth_size_t kth_chain_header_serialized_size(kth_header_t header, uint32_t version);
// void chain_header_serialized_size(v8::FunctionCallbackInfo<v8::Value> const& args) {

// }

// uint8_t const* kth_chain_header_to_data(kth_header_t header, uint32_t version, kth_size_t* out_size);
void chain_header_to_data(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 2) {
throw_exception(isolate, "Wrong number of arguments");
return;
}

if ( ! args[0]->IsExternal()) {
throw_exception(isolate, "Wrong arguments");
return;
}

if ( ! args[1]->IsNumber()) {
throw_exception(isolate, "Wrong arguments, 1");
return;
}

void* vptr = v8::External::Cast(*args[0])->Value();
kth_header_t header = (kth_header_t)vptr;

uint32_t version = args[1]->IntegerValue(isolate->GetCurrentContext()).ToChecked();

kth_size_t size;
auto data_c = kth_chain_header_to_data(header, version, &size);

Local<ArrayBuffer> tmp = ArrayBuffer::New(isolate, size);
memcpy(tmp->GetContents().Data(), data_c, size);
Local<Uint8Array> data = Uint8Array::New(tmp, 0, size);
args.GetReturnValue().Set(data);
}

// // kth_header_t kth_chain_header_construct_default(void);
// void chain_header_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args) {
// }

// kth_header_t kth_chain_header_construct(uint32_t version, uint8_t* previous_block_hash, uint8_t* merkle, uint32_t timestamp, uint32_t bits, uint32_t nonce);
void chain_header_construct(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 6) {
throw_exception(isolate, "Wrong number of arguments");
return;
}

if ( ! args[0]->IsNumber()) {
throw_exception(isolate, "Wrong arguments, 0");
return;
}

if ( ! args[1]->IsUint8Array()) {
throw_exception(isolate, "Wrong arguments, 1");
return;
}

if ( ! args[2]->IsUint8Array()) {
throw_exception(isolate, "Wrong arguments, 2");
return;
}

if ( ! args[3]->IsNumber()) {
throw_exception(isolate, "Wrong arguments, 3");
return;
}

if ( ! args[4]->IsNumber()) {
throw_exception(isolate, "Wrong arguments, 4");
return;
}

if ( ! args[5]->IsNumber()) {
throw_exception(isolate, "Wrong arguments, 5");
return;
}

v8::Local<v8::Uint8Array> previous_block_hash_arr = v8::Local<v8::Uint8Array>::Cast(args[1]);
if (previous_block_hash_arr->Length() != 32) {
throw_exception(isolate, "Wrong arguments, 1, number of bytes");
return;
}

v8::Local<v8::Uint8Array> merkle_arr = v8::Local<v8::Uint8Array>::Cast(args[2]);
if (merkle_arr->Length() != 32) {
throw_exception(isolate, "Wrong arguments, 2, number of bytes");
return;
}

uint32_t version = args[0]->IntegerValue(isolate->GetCurrentContext()).ToChecked();
kth_hash_t previous_block_hash = to_native_hash(previous_block_hash_arr);
kth_hash_t merkle = to_native_hash(merkle_arr);
uint32_t timestamp = args[3]->IntegerValue(isolate->GetCurrentContext()).ToChecked();
uint32_t bits = args[4]->IntegerValue(isolate->GetCurrentContext()).ToChecked();
uint32_t nonce = args[5]->IntegerValue(isolate->GetCurrentContext()).ToChecked();

auto res = kth_chain_header_construct(version, previous_block_hash.hash, merkle.hash, timestamp, bits, nonce);
args.GetReturnValue().Set(External::New(isolate, res));
}

// void kth_chain_header_destruct(kth_header_t header);
void chain_header_destruct(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

Expand All @@ -48,7 +167,12 @@ void chain_header_destruct(v8::FunctionCallbackInfo<v8::Value> const& args) {
kth_chain_header_destruct(header);
}

void chain_header_get_version(v8::FunctionCallbackInfo<v8::Value> const& args) {
// // int kth_chain_header_is_valid(kth_header_t header);
// void chain_header_is_valid(v8::FunctionCallbackInfo<v8::Value> const& args) {

// }

void chain_header_version(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand Down Expand Up @@ -97,7 +221,7 @@ void chain_header_set_version(v8::FunctionCallbackInfo<v8::Value> const& args) {
kth_chain_header_set_version(header, version);
}

void chain_header_get_previous_block_hash(v8::FunctionCallbackInfo<v8::Value> const& args) {
void chain_header_previous_block_hash(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand All @@ -122,10 +246,7 @@ void chain_header_get_previous_block_hash(v8::FunctionCallbackInfo<v8::Value> co
args.GetReturnValue().Set(hash);
}



// void chain_header_get_merkle(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_merkle(v8::FunctionCallbackInfo<v8::Value> const& args) {
void chain_header_merkle(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand All @@ -150,7 +271,7 @@ void chain_header_get_merkle(v8::FunctionCallbackInfo<v8::Value> const& args) {
args.GetReturnValue().Set(hash);
}

void chain_header_get_hash(v8::FunctionCallbackInfo<v8::Value> const& args) {
void chain_header_hash(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand All @@ -175,8 +296,7 @@ void chain_header_get_hash(v8::FunctionCallbackInfo<v8::Value> const& args) {
args.GetReturnValue().Set(hash);
}

// void chain_header_get_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args) {
void chain_header_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand All @@ -198,10 +318,7 @@ void chain_header_get_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args)
args.GetReturnValue().Set(num);
}

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

// void chain_header_get_bits(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_bits(v8::FunctionCallbackInfo<v8::Value> const& args) {
void chain_header_bits(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand All @@ -223,12 +340,7 @@ void chain_header_get_bits(v8::FunctionCallbackInfo<v8::Value> const& args) {
args.GetReturnValue().Set(num);
}



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

// void chain_header_get_nonce(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_header_get_nonce(v8::FunctionCallbackInfo<v8::Value> const& args) {
void chain_header_nonce(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
Expand All @@ -251,10 +363,25 @@ void chain_header_get_nonce(v8::FunctionCallbackInfo<v8::Value> const& args) {
}


void chain_header_proof_str(v8::FunctionCallbackInfo<v8::Value> const& args) {
}

// uint32_t kth_chain_header_version(kth_header_t header);
// void kth_chain_header_set_version(kth_header_t header, uint32_t version);
// uint32_t kth_chain_header_timestamp(kth_header_t header);
// void kth_chain_header_set_timestamp(kth_header_t header, uint32_t timestamp);
// uint32_t kth_chain_header_bits(kth_header_t header);
// char const* kth_chain_header_proof_str(kth_header_t header);
// void kth_chain_header_set_bits(kth_header_t header, uint32_t bits);
// uint32_t kth_chain_header_nonce(kth_header_t header);
// void kth_chain_header_set_nonce(kth_header_t header, uint32_t nonce);
// kth_hash_t kth_chain_header_previous_block_hash(kth_header_t header);
// kth_hash_t kth_chain_header_merkle(kth_header_t header);
// kth_hash_t kth_chain_header_hash(kth_header_t header);

// void chain_header_set_version(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_set_timestamp(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_set_bits(v8::FunctionCallbackInfo<v8::Value> const& args);
// void chain_header_set_nonce(v8::FunctionCallbackInfo<v8::Value> const& args);




} // namespace kth::js_native
16 changes: 9 additions & 7 deletions src/kth-native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ void init(v8::Local<v8::Object> exports) {

// NODE_SET_METHOD(exports, "chain_subscribe_blockchain", chain_subscribe_blockchain);

NODE_SET_METHOD(exports, "chain_header_to_data", chain_header_to_data);
NODE_SET_METHOD(exports, "chain_header_construct", chain_header_construct);
NODE_SET_METHOD(exports, "chain_header_destruct", chain_header_destruct);
NODE_SET_METHOD(exports, "chain_header_get_version", chain_header_get_version);
NODE_SET_METHOD(exports, "chain_header_version", chain_header_version);
NODE_SET_METHOD(exports, "chain_header_set_version", chain_header_set_version);
NODE_SET_METHOD(exports, "chain_header_get_previous_block_hash", chain_header_get_previous_block_hash);
NODE_SET_METHOD(exports, "chain_header_get_merkle", chain_header_get_merkle);
NODE_SET_METHOD(exports, "chain_header_get_hash", chain_header_get_hash);
NODE_SET_METHOD(exports, "chain_header_get_timestamp", chain_header_get_timestamp);
NODE_SET_METHOD(exports, "chain_header_previous_block_hash", chain_header_previous_block_hash);
NODE_SET_METHOD(exports, "chain_header_merkle", chain_header_merkle);
NODE_SET_METHOD(exports, "chain_header_hash", chain_header_hash);
NODE_SET_METHOD(exports, "chain_header_timestamp", chain_header_timestamp);
NODE_SET_METHOD(exports, "chain_header_bits", chain_header_bits);
NODE_SET_METHOD(exports, "chain_header_nonce", chain_header_nonce);
// NODE_SET_METHOD(exports, "chain_header_set_timestamp", chain_header_set_timestamp);
NODE_SET_METHOD(exports, "chain_header_get_bits", chain_header_get_bits);
// NODE_SET_METHOD(exports, "chain_header_set_bits", chain_header_set_bits);
NODE_SET_METHOD(exports, "chain_header_get_nonce", chain_header_get_nonce);
// NODE_SET_METHOD(exports, "chain_header_set_nonce", chain_header_set_nonce);

NODE_SET_METHOD(exports, "chain_block_destruct", chain_block_destruct);
Expand Down
Loading

0 comments on commit f1b87a6

Please sign in to comment.