From 83ead9fba9b0bbc55bfb3807434ab7c09241ab2b Mon Sep 17 00:00:00 2001 From: "cr.zoidberg" Date: Sat, 6 Apr 2024 18:30:29 +0200 Subject: [PATCH] Updated node utils files --- .gitmodules | 3 + Zano | 1 + binding.gyp | 58 +++++----- main.cc | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 361 insertions(+), 29 deletions(-) create mode 100644 .gitmodules create mode 160000 Zano create mode 100644 main.cc diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8325dbf --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Zano"] + path = Zano + url = https://github.com/hyle-team/zano.git diff --git a/Zano b/Zano new file mode 160000 index 0000000..23ac357 --- /dev/null +++ b/Zano @@ -0,0 +1 @@ +Subproject commit 23ac3572cf7535214369e254e2631c6cd32b4519 diff --git a/binding.gyp b/binding.gyp index f701ea7..e29cfdd 100644 --- a/binding.gyp +++ b/binding.gyp @@ -3,37 +3,37 @@ { "target_name": "cryptonote", "sources": [ - "src/main.cc", - "src/currency_core/currency_format_utils.cpp", - "src/currency_core/currency_format_utils_blocks.cpp", - "src/currency_core/basic_pow_helpers.cpp", - "src/currency_core/basic_pow_helpers.cpp", - "src/crypto/tree-hash.c", - "src/crypto/crypto.cpp", - "src/crypto/crypto-ops.c", - "src/crypto/crypto-ops-data.c", - "src/crypto/hash.c", - "src/crypto/keccak.c", - "src/common/base58.cpp", - "src/contrib/ethereum/libethash/ethash.cpp", - "src/contrib/ethereum/libethash/keccak.c", - "src/contrib/ethereum/libethash/keccakf800.c", - "src/contrib/ethereum/libethash/progpow.cpp", - "src/contrib/ethereum/libethash/managed.cpp", - "src/currency_core/currency_format_utils_transactions.cpp", - "src/currency_core/genesis.cpp", - "src/currency_core/genesis_acc.cpp", - "src/crypto/random.c", - "src/contrib/ethereum/libethash/keccakf1600.c", - "src/contrib/ethereum/libethash/managed.cpp", - "src/contrib/ethereum/libethash/primes.c" + "main.cc", + "Zano/src/currency_core/currency_format_utils.cpp", + "Zano/src/currency_core/currency_format_utils_blocks.cpp", + "Zano/src/currency_core/basic_pow_helpers.cpp", + "Zano/src/currency_core/basic_pow_helpers.cpp", + "Zano/src/crypto/tree-hash.c", + "Zano/src/crypto/crypto.cpp", + "Zano/src/crypto/crypto-ops.c", + "Zano/src/crypto/crypto-ops-data.c", + "Zano/src/crypto/hash.c", + "Zano/src/crypto/keccak.c", + "Zano/src/common/base58.cpp", + "Zano/src/contrib/ethereum/libethash/ethash.cpp", + "Zano/src/contrib/ethereum/libethash/keccak.c", + "Zano/src/contrib/ethereum/libethash/keccakf800.c", + "Zano/src/contrib/ethereum/libethash/progpow.cpp", + "Zano/src/contrib/ethereum/libethash/managed.cpp", + "Zano/src/currency_core/currency_format_utils_transactions.cpp", + "Zano/src/currency_core/genesis.cpp", + "Zano/src/currency_core/genesis_acc.cpp", + "Zano/src/crypto/random.c", + "Zano/src/contrib/ethereum/libethash/keccakf1600.c", + "Zano/src/contrib/ethereum/libethash/managed.cpp", + "Zano/src/contrib/ethereum/libethash/primes.c" ], "include_dirs": [ - "src", - "src/contrib", - "src/contrib/epee/include", - "src/contrib/eos_portable_archive", - "src/contrib/ethereum/libethash", + "Zano/src", + "Zano/src/contrib", + "Zano/src/contrib/epee/include", + "Zano/src/contrib/eos_portable_archive", + "Zano/src/contrib/ethereum/libethash", " +#include +#include +#include +#include +#include +#include "currency_core/currency_basic.h" +#include "currency_core/currency_format_utils.h" +#include "currency_protocol/blobdatatype.h" +#include "crypto/crypto.h" +#include "crypto/hash.h" +#include "common/base58.h" +#include "serialization/binary_utils.h" +#include "currency_core/basic_pow_helpers.h" +#include + +#define THROW_ERROR_EXCEPTION(x) Nan::ThrowError(x) + +void callback(char* data, void* hint) { + free(data); +} + +using namespace node; +using namespace v8; +using namespace currency; + +blobdata uint64be_to_blob(uint64_t num) { + blobdata res = " "; + res[0] = num >> 56 & 0xff; + res[1] = num >> 48 & 0xff; + res[2] = num >> 40 & 0xff; + res[3] = num >> 32 & 0xff; + res[4] = num >> 24 & 0xff; + res[5] = num >> 16 & 0xff; + res[6] = num >> 8 & 0xff; + res[7] = num & 0xff; + return res; +} + + + +NAN_METHOD(convert_blob) { + + if (info.Length() < 1) + return THROW_ERROR_EXCEPTION("You must provide one argument."); + + Local target = info[0]->ToObject(); + + if (!Buffer::HasInstance(target)) + return THROW_ERROR_EXCEPTION("Argument should be a buffer object."); + + blobdata input = std::string(Buffer::Data(target), Buffer::Length(target)); + blobdata output = ""; + + //convert + block b = AUTO_VAL_INIT(b); + if (!parse_and_validate_block_from_blob(input, b)) + return THROW_ERROR_EXCEPTION("Failed to parse block"); + + output = get_block_hashing_blob(b); + + v8::Local returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked(); + info.GetReturnValue().Set( + returnValue + ); +} + + +void address_decode(const Nan::FunctionCallbackInfo& info) { + + if (info.Length() < 1) + return THROW_ERROR_EXCEPTION("You must provide one argument."); + + Local target = info[0]->ToObject(); + + if (!Buffer::HasInstance(target)) + return THROW_ERROR_EXCEPTION("Argument should be a buffer object."); + + blobdata input = std::string(Buffer::Data(target), Buffer::Length(target)); + + blobdata data; + uint64_t prefix; + if (!tools::base58::decode_addr(input, prefix, data)) + { + info.GetReturnValue().Set(Nan::Undefined()); + } + // info.GetReturnValue().Set(Nan::Undefined()); + + + account_public_address adr; + if (!::serialization::parse_binary(data, adr) || !crypto::check_key(adr.spend_public_key) || !crypto::check_key(adr.view_public_key)) + { + if(data.length()) + { + data = uint64be_to_blob(prefix) + data; + } + else + { + info.GetReturnValue().Set(Nan::Undefined()); + } + + v8::Local returnValue = Nan::CopyBuffer((char*)data.data(), data.size()).ToLocalChecked(); + + info.GetReturnValue().Set( returnValue); + + } + else + { + info.GetReturnValue().Set(Nan::New(static_cast(prefix))); + } +} + +#define SET_BUFFER_RETURN(x, len) \ + v8::Isolate* isolate = args.GetIsolate(); \ + 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."); + + Local block_header_hash = args[0]->ToObject(); + Local nonce = args[1]->ToObject(); + Local height = args[2]->ToObject(); + + 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."); + + Local block_template_buffer = args[0]->ToObject(); + Local extra_data = args[1]->ToObject(); + + + 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 +2: extra_data - n-byte buffer(job identification) +3: nonce - 8-byte buffer - nonce +*/ +void get_blob_from_block_template(const Nan::FunctionCallbackInfo& args) { + + if (args.Length() < 3) + return THROW_ERROR_EXCEPTION("You must provide 3 arguments."); + + Local block_template_buffer = args[0]->ToObject(); + Local extra_data = args[1]->ToObject(); + Local nonce = args[2]->ToObject(); + + + 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."); + + if (!Buffer::HasInstance(nonce)) + return THROW_ERROR_EXCEPTION("Argument 3 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); + uint64_t nonce_len = Buffer::Length(nonce); + + if (nonce_len != 8) + return THROW_ERROR_EXCEPTION("Argument 3 should be a buffer object of 8 bytes long."); + + 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); + + uint64_t nonce_val = *(uint64_t* )Buffer::Data(nonce); + + 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); + + b.nonce = nonce_val; + + std::string result_blob = currency::block_to_blob(b); + + crypto::hash h = currency::get_block_hash(b); + + SET_BUFFER_RETURN(result_blob.data(), result_blob.size()); +} + + +void get_id_hash(const Nan::FunctionCallbackInfo& args) { + + if (args.Length() < 1) + return THROW_ERROR_EXCEPTION("You must provide 2 arguments."); + + Local block_buffer = args[0]->ToObject(); + + + if (!Buffer::HasInstance(block_buffer)) + return THROW_ERROR_EXCEPTION("Argument 1 should be a buffer object."); + + uint64_t block_buffer_len = Buffer::Length(block_buffer); + + char* block_buffer_ptr = Buffer::Data(block_buffer); + std::string blob(block_buffer_ptr, block_buffer_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"); + + crypto::hash h = currency::get_block_hash(b); + + SET_BUFFER_RETURN((const char*)&h, 32); +} + + + +void is_address_valid(const Nan::FunctionCallbackInfo& info) +{ + + if (info.Length() < 1) + return THROW_ERROR_EXCEPTION("You must provide one argument."); + Local target = info[0]->ToObject(); + + if (!Buffer::HasInstance(target)) + return THROW_ERROR_EXCEPTION("Argument should be a buffer object."); + + blobdata input = std::string(Buffer::Data(target), Buffer::Length(target)); + + account_public_address adr; + bool r = get_account_address_from_str(adr, input); + if(!r) + { + info.GetReturnValue().Set(Nan::Undefined()); + } + else + { + info.GetReturnValue().Set(Nan::True()); + } +} + + +NAN_MODULE_INIT(init) { + Nan::Set(target, Nan::New("convert_blob").ToLocalChecked(), Nan::GetFunction(Nan::New(convert_blob)).ToLocalChecked()); + Nan::Set(target, Nan::New("address_decode").ToLocalChecked(), Nan::GetFunction(Nan::New(address_decode)).ToLocalChecked()); + Nan::Set(target, Nan::New("get_pow_hash").ToLocalChecked(), Nan::GetFunction(Nan::New(get_pow_hash)).ToLocalChecked()); + Nan::Set(target, Nan::New("get_hash_from_block_template_with_extra").ToLocalChecked(), Nan::GetFunction(Nan::New(get_hash_from_block_template_with_extra)).ToLocalChecked()); + Nan::Set(target, Nan::New("get_blob_from_block_template").ToLocalChecked(), Nan::GetFunction(Nan::New(get_blob_from_block_template)).ToLocalChecked()); + Nan::Set(target, Nan::New("get_id_hash").ToLocalChecked(), Nan::GetFunction(Nan::New(get_id_hash)).ToLocalChecked()); + Nan::Set(target, Nan::New("is_address_valid").ToLocalChecked(), Nan::GetFunction(Nan::New(is_address_valid)).ToLocalChecked()); +} + +NODE_MODULE(cryptonote, init)