From 6ddb152cb086f4bee4bc67d45454bb13b3a1de04 Mon Sep 17 00:00:00 2001 From: nmateo Date: Wed, 24 Apr 2024 16:56:41 +0200 Subject: [PATCH] fix main.cc (compatible node 14+) --- main.cc | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/main.cc b/main.cc index 58755db..c620df1 100644 --- a/main.cc +++ b/main.cc @@ -119,6 +119,102 @@ void address_decode(const Nan::FunctionCallbackInfo& info) { #define SET_BUFFER_RETURN(x, len) \ args.GetReturnValue().Set(Buffer::Copy(isolate, x, len).ToLocalChecked()); + +/* +Arguments: +1: block_header_hash - 32-byte buffer +2: nonce - 8-byte buffer +2: height - 8-byte buffer +*/ +void get_pow_hash(const Nan::FunctionCallbackInfo& args) { + + if (args.Length() < 3) + return THROW_ERROR_EXCEPTION("You must provide 3 arguments."); + + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + Local block_header_hash = args[0]->ToObject(isolate->GetCurrentContext()).ToLocalChecked(); + + //Local nonce = args[1]->ToObject(); + Local nonce = args[1]->ToObject(isolate->GetCurrentContext()).ToLocalChecked(); + + //Local height = args[2]->ToObject(); + Local height = args[2]->ToObject(isolate->GetCurrentContext()).ToLocalChecked(); + if(!Buffer::HasInstance(block_header_hash)) + return THROW_ERROR_EXCEPTION("Argument 1 should be a buffer object."); + + if(!Buffer::HasInstance(nonce)) + return THROW_ERROR_EXCEPTION("Argument 2 should be a buffer object."); + + if (!Buffer::HasInstance(height)) + return THROW_ERROR_EXCEPTION("Argument 3 should be a buffer object."); + + uint32_t block_header_hash_len = Buffer::Length(block_header_hash); + uint64_t nonce_len = Buffer::Length(nonce); + uint64_t height_len = Buffer::Length(height); + + if(block_header_hash_len != 32) + return THROW_ERROR_EXCEPTION("Argument 1 should be a buffer object of 32 bytes long."); + + if (nonce_len != 8) + return THROW_ERROR_EXCEPTION("Argument 2 should be a buffer object of 8 bytes long."); + + if (height_len != 8) + return THROW_ERROR_EXCEPTION("Argument 3 should be a buffer object of 8 bytes long."); + + crypto::hash block_header_hash_val = *(crypto::hash*)Buffer::Data(block_header_hash); + uint64_t nonce_val = *(uint64_t*)Buffer::Data(nonce); + uint64_t height_val = *(uint64_t*)Buffer::Data(height); + + + crypto::hash h = currency::get_block_longhash(height_val, block_header_hash_val, nonce_val); + + SET_BUFFER_RETURN((const char*)&h, 32); +} + +/* +Arguments: +1: block_template_buffer - n-byte buffer +2: extra_data - n-byte buffer(job identification) +*/ +void get_hash_from_block_template_with_extra(const Nan::FunctionCallbackInfo& args) { + + if (args.Length() < 2) + return THROW_ERROR_EXCEPTION("You must provide 2 arguments."); + + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + Local block_template_buffer = args[0]->ToObject(isolate->GetCurrentContext()).ToLocalChecked(); + + //Local extra_data = args[1]->ToObject(); + Local extra_data = args[1]->ToObject(isolate->GetCurrentContext()).ToLocalChecked(); + + if (!Buffer::HasInstance(block_template_buffer)) + return THROW_ERROR_EXCEPTION("Argument 1 should be a buffer object."); + + if (!Buffer::HasInstance(extra_data)) + return THROW_ERROR_EXCEPTION("Argument 2 should be a buffer object."); + + uint64_t block_template_buffer_len = Buffer::Length(block_template_buffer); + uint64_t extra_data_len = Buffer::Length(extra_data); + + char* block_template_buffer_ptr = Buffer::Data(block_template_buffer); + std::string blob(block_template_buffer_ptr, block_template_buffer_len); + + char* extra_data_ptr = Buffer::Data(extra_data); + std::string extra(extra_data_ptr, extra_data_len); + + currency::block b = AUTO_VAL_INIT(b); + bool res = currency::parse_and_validate_block_from_blob(blob, b); + if (!res) + return THROW_ERROR_EXCEPTION("Unable to parse block"); + + //if (extra.size()) + // b.miner_tx.extra.push_back(extra); + + crypto::hash h = currency::get_block_header_mining_hash(b); + + SET_BUFFER_RETURN((const char*)&h, 32); +} + /* Arguments: 1: block_template_buffer - n-byte buffer