diff --git a/src/main.cc b/src/main.cc index e88852d..4445ad1 100644 --- a/src/main.cc +++ b/src/main.cc @@ -59,7 +59,7 @@ NAN_METHOD(convert_blob) { 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 @@ -86,7 +86,7 @@ void address_decode(const Nan::FunctionCallbackInfo& info) { info.GetReturnValue().Set(Nan::Undefined()); } // info.GetReturnValue().Set(Nan::Undefined()); - + account_public_address adr; if (!::serialization::parse_binary(data, adr) || !crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key)) @@ -99,9 +99,9 @@ void address_decode(const Nan::FunctionCallbackInfo& info) { { info.GetReturnValue().Set(Nan::Undefined()); } - + v8::Local returnValue = Nan::CopyBuffer((char*)data.data(), data.size()).ToLocalChecked(); - + info.GetReturnValue().Set( returnValue); } @@ -116,7 +116,7 @@ void address_decode(const Nan::FunctionCallbackInfo& info) { args.GetReturnValue().Set(Buffer::Copy(isolate, x, len).ToLocalChecked()); /* -Arguments: +Arguments: 1: block_header_hash - 32-byte buffer 2: nonce - 8-byte buffer 2: height - 8-byte buffer @@ -139,7 +139,6 @@ void get_pow_hash(const Nan::FunctionCallbackInfo& args) { 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); @@ -153,13 +152,13 @@ void get_pow_hash(const Nan::FunctionCallbackInfo& args) { 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); - crypto::hash* block_header_hash_ptr = (crypto::hash*)Buffer::Data(block_header_hash); - uint64_t* nonce_ptr = (uint64_t*)Buffer::Data(nonce); - uint64_t* height_ptr = (uint64_t*)Buffer::Data(height); - - - crypto::hash h = currency::get_block_longhash(*height_ptr, *block_header_hash_ptr, *nonce_ptr); SET_BUFFER_RETURN((const char*)&h, 32); } @@ -187,9 +186,9 @@ void get_hash_from_block_template_with_extra(const Nan::FunctionCallbackInfo& ar 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); - char* extra_data_ptr = Buffer::Data(extra_data); - uint64_t* nonce_ptr = (uint64_t* )Buffer::Data(nonce); - 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) @@ -259,53 +252,67 @@ void get_blob_from_block_template(const Nan::FunctionCallbackInfo& ar if (extra.size()) b.miner_tx.extra.push_back(extra); - b.nonce = *nonce_ptr; + b.nonce = nonce_val; std::string result_blob = currency::block_to_blob(b); crypto::hash h = currency::get_block_hash(b); - - //@#@ debug output - std::cout << "[get_blob_from_block_template]: " << ENDL - << "blob: [" << epee::string_tools::buff_to_hex_nodelimer(blob) << "]" << ENDL - << "extra: [" << epee::string_tools::buff_to_hex_nodelimer(extra) << "]" << ENDL - << "nonce: [" << *nonce_ptr << "]" << ENDL - << "header_mining_hash: [" << currency::get_block_header_mining_hash(b) << "]" << ENDL - << "res blob: [" << epee::string_tools::buff_to_hex_nodelimer(result_blob) << "]" << ENDL; - //@#@ - - 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 two arguments."); -// -// Local target = args[0]->ToObject(); -// -// if(!Buffer::HasInstance(target)) -// return THROW_ERROR_EXCEPTION("Argument 1 should be a buffer object."); -// -// -// char * input = Buffer::Data(target); -// -// crypto::hash h = AUTO_VAL_INIT(h); -// char* output = reinterpret_cast(&h); -// -// uint32_t input_len = Buffer::Length(target); -// -// crypto::cn_fast_hash(input, input_len, h); -// -// v8::Isolate* isolate = args.GetIsolate(); -// -// SET_BUFFER_RETURN(output, 32); -// } +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()); + } +} @@ -315,8 +322,9 @@ NAN_MODULE_INIT(init) { 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("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)