|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <node.h>
|
|
|
|
#include <node.h>
|
|
|
|
#include <node_buffer.h>
|
|
|
|
#include <node_buffer.h>
|
|
|
|
#include <v8.h>
|
|
|
|
#include <v8.h>
|
|
|
@ -10,15 +11,16 @@
|
|
|
|
#include "crypto/crypto.h"
|
|
|
|
#include "crypto/crypto.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
#include "common/base58.h"
|
|
|
|
#include "common/base58.h"
|
|
|
|
|
|
|
|
#include "serialization/binary_utils.h"
|
|
|
|
|
|
|
|
#include <nan.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define THROW_ERROR_EXCEPTION(x) NanThrowError(x)
|
|
|
|
|
|
|
|
#define THROW_ERROR_EXCEPTION_WITH_STATUS_CODE(x, y) NanThrowError(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
using namespace node;
|
|
|
|
using namespace node;
|
|
|
|
using namespace v8;
|
|
|
|
using namespace v8;
|
|
|
|
using namespace cryptonote;
|
|
|
|
using namespace cryptonote;
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> except(const char* msg) {
|
|
|
|
|
|
|
|
return ThrowException(Exception::Error(String::New(msg)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blobdata uint64be_to_blob(uint64_t num) {
|
|
|
|
blobdata uint64be_to_blob(uint64_t num) {
|
|
|
|
blobdata res = " ";
|
|
|
|
blobdata res = " ";
|
|
|
|
res[0] = num >> 56 & 0xff;
|
|
|
|
res[0] = num >> 56 & 0xff;
|
|
|
@ -75,16 +77,16 @@ static bool construct_parent_block(const cryptonote::block& b, cryptonote::block
|
|
|
|
return fillExtra(parent_block, b);
|
|
|
|
return fillExtra(parent_block, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> convert_blob(const Arguments& args) {
|
|
|
|
NAN_METHOD(convert_blob) {
|
|
|
|
HandleScope scope;
|
|
|
|
NanScope();
|
|
|
|
|
|
|
|
|
|
|
|
if (args.Length() < 1)
|
|
|
|
if (args.Length() < 1)
|
|
|
|
return except("You must provide one argument.");
|
|
|
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
|
|
|
|
|
|
|
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
return except("Argument should be a buffer object.");
|
|
|
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
|
|
|
|
|
|
|
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata output = "";
|
|
|
|
blobdata output = "";
|
|
|
@ -92,64 +94,66 @@ Handle<Value> convert_blob(const Arguments& args) {
|
|
|
|
//convert
|
|
|
|
//convert
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
if (!parse_and_validate_block_from_blob(input, b))
|
|
|
|
if (!parse_and_validate_block_from_blob(input, b))
|
|
|
|
return except("Failed to parse block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to parse block");
|
|
|
|
|
|
|
|
|
|
|
|
if (b.major_version < BLOCK_MAJOR_VERSION_2) {
|
|
|
|
if (b.major_version < BLOCK_MAJOR_VERSION_2) {
|
|
|
|
if (!get_block_hashing_blob(b, output))
|
|
|
|
if (!get_block_hashing_blob(b, output))
|
|
|
|
return except("Failed to create mining block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
block parent_block;
|
|
|
|
block parent_block;
|
|
|
|
if (!construct_parent_block(b, parent_block))
|
|
|
|
if (!construct_parent_block(b, parent_block))
|
|
|
|
return except("Failed to construct parent block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to construct parent block");
|
|
|
|
|
|
|
|
|
|
|
|
if (!get_block_hashing_blob(parent_block, output))
|
|
|
|
if (!get_block_hashing_blob(parent_block, output))
|
|
|
|
return except("Failed to create mining block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Buffer* buff = Buffer::New(output.data(), output.size());
|
|
|
|
NanReturnValue(
|
|
|
|
return scope.Close(buff->handle_);
|
|
|
|
NanNewBufferHandle(output.data(), output.size())
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> get_block_id(const Arguments& args) {
|
|
|
|
NAN_METHOD(get_block_id) {
|
|
|
|
HandleScope scope;
|
|
|
|
NanScope();
|
|
|
|
|
|
|
|
|
|
|
|
if (args.Length() < 1)
|
|
|
|
if (args.Length() < 1)
|
|
|
|
return except("You must provide one argument.");
|
|
|
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
|
|
|
|
|
|
|
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
return except("Argument should be a buffer object.");
|
|
|
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
|
|
|
|
|
|
|
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata output = "";
|
|
|
|
blobdata output = "";
|
|
|
|
|
|
|
|
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
if (!parse_and_validate_block_from_blob(input, b))
|
|
|
|
if (!parse_and_validate_block_from_blob(input, b))
|
|
|
|
return except("Failed to parse block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to parse block");
|
|
|
|
|
|
|
|
|
|
|
|
crypto::hash block_id;
|
|
|
|
crypto::hash block_id;
|
|
|
|
if (!get_block_hash(b, block_id))
|
|
|
|
if (!get_block_hash(b, block_id))
|
|
|
|
return except("Failed to calculate hash for block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to calculate hash for block");
|
|
|
|
|
|
|
|
|
|
|
|
Buffer* buff = Buffer::New(reinterpret_cast<char*>(&block_id), sizeof(block_id));
|
|
|
|
NanReturnValue(
|
|
|
|
return scope.Close(buff->handle_);
|
|
|
|
NanNewBufferHandle(reinterpret_cast<char*>(&block_id), sizeof(block_id))
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> construct_block_blob(const Arguments& args) {
|
|
|
|
NAN_METHOD(construct_block_blob) {
|
|
|
|
HandleScope scope;
|
|
|
|
NanScope();
|
|
|
|
|
|
|
|
|
|
|
|
if (args.Length() < 2)
|
|
|
|
if (args.Length() < 2)
|
|
|
|
return except("You must provide two arguments.");
|
|
|
|
return THROW_ERROR_EXCEPTION("You must provide two arguments.");
|
|
|
|
|
|
|
|
|
|
|
|
Local<Object> block_template_buf = args[0]->ToObject();
|
|
|
|
Local<Object> block_template_buf = args[0]->ToObject();
|
|
|
|
Local<Object> nonce_buf = args[1]->ToObject();
|
|
|
|
Local<Object> nonce_buf = args[1]->ToObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Buffer::HasInstance(block_template_buf) || !Buffer::HasInstance(nonce_buf))
|
|
|
|
if (!Buffer::HasInstance(block_template_buf) || !Buffer::HasInstance(nonce_buf))
|
|
|
|
return except("Both arguments should be buffer objects.");
|
|
|
|
return THROW_ERROR_EXCEPTION("Both arguments should be buffer objects.");
|
|
|
|
|
|
|
|
|
|
|
|
if (Buffer::Length(nonce_buf) != 4)
|
|
|
|
if (Buffer::Length(nonce_buf) != 4)
|
|
|
|
return except("Nonce buffer has invalid size.");
|
|
|
|
return THROW_ERROR_EXCEPTION("Nonce buffer has invalid size.");
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t nonce = *reinterpret_cast<uint32_t*>(Buffer::Data(nonce_buf));
|
|
|
|
uint32_t nonce = *reinterpret_cast<uint32_t*>(Buffer::Data(nonce_buf));
|
|
|
|
|
|
|
|
|
|
|
@ -158,36 +162,37 @@ Handle<Value> construct_block_blob(const Arguments& args) {
|
|
|
|
|
|
|
|
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
block b = AUTO_VAL_INIT(b);
|
|
|
|
if (!parse_and_validate_block_from_blob(block_template_blob, b))
|
|
|
|
if (!parse_and_validate_block_from_blob(block_template_blob, b))
|
|
|
|
return except("Failed to parse block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to parse block");
|
|
|
|
|
|
|
|
|
|
|
|
b.nonce = nonce;
|
|
|
|
b.nonce = nonce;
|
|
|
|
if (b.major_version == BLOCK_MAJOR_VERSION_2) {
|
|
|
|
if (b.major_version == BLOCK_MAJOR_VERSION_2) {
|
|
|
|
block parent_block;
|
|
|
|
block parent_block;
|
|
|
|
b.parent_block.nonce = nonce;
|
|
|
|
b.parent_block.nonce = nonce;
|
|
|
|
if (!construct_parent_block(b, parent_block))
|
|
|
|
if (!construct_parent_block(b, parent_block))
|
|
|
|
return except("Failed to construct parent block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to construct parent block");
|
|
|
|
|
|
|
|
|
|
|
|
if (!mergeBlocks(parent_block, b, std::vector<crypto::hash>()))
|
|
|
|
if (!mergeBlocks(parent_block, b, std::vector<crypto::hash>()))
|
|
|
|
return except("Failed to postprocess mining block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to postprocess mining block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!block_to_blob(b, output))
|
|
|
|
if (!block_to_blob(b, output))
|
|
|
|
return except("Failed to convert block to blob");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to convert block to blob");
|
|
|
|
|
|
|
|
|
|
|
|
Buffer* buff = Buffer::New(output.data(), output.size());
|
|
|
|
NanReturnValue(
|
|
|
|
return scope.Close(buff->handle_);
|
|
|
|
NanNewBufferHandle(output.data(), output.size())
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> convert_blob_bb(const Arguments& args) {
|
|
|
|
NAN_METHOD(convert_blob_bb) {
|
|
|
|
HandleScope scope;
|
|
|
|
NanScope();
|
|
|
|
|
|
|
|
|
|
|
|
if (args.Length() < 1)
|
|
|
|
if (args.Length() < 1)
|
|
|
|
return except("You must provide one argument.");
|
|
|
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
|
|
|
|
|
|
|
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
return except("Argument should be a buffer object.");
|
|
|
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
|
|
|
|
|
|
|
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata output = "";
|
|
|
|
blobdata output = "";
|
|
|
@ -195,45 +200,49 @@ Handle<Value> convert_blob_bb(const Arguments& args) {
|
|
|
|
//convert
|
|
|
|
//convert
|
|
|
|
bb_block b = AUTO_VAL_INIT(b);
|
|
|
|
bb_block b = AUTO_VAL_INIT(b);
|
|
|
|
if (!parse_and_validate_block_from_blob(input, b)) {
|
|
|
|
if (!parse_and_validate_block_from_blob(input, b)) {
|
|
|
|
return except("Failed to parse block");
|
|
|
|
return THROW_ERROR_EXCEPTION("Failed to parse block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output = get_block_hashing_blob(b);
|
|
|
|
output = get_block_hashing_blob(b);
|
|
|
|
|
|
|
|
|
|
|
|
Buffer* buff = Buffer::New(output.data(), output.size());
|
|
|
|
NanReturnValue(
|
|
|
|
return scope.Close(buff->handle_);
|
|
|
|
NanNewBufferHandle(output.data(), output.size())
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> address_decode(const Arguments& args) {
|
|
|
|
NAN_METHOD(address_decode) {
|
|
|
|
HandleScope scope;
|
|
|
|
NanEscapableScope();
|
|
|
|
|
|
|
|
|
|
|
|
if (args.Length() < 1)
|
|
|
|
if (args.Length() < 1)
|
|
|
|
return except("You must provide one argument.");
|
|
|
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
|
|
|
|
|
|
|
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
Local<Object> target = args[0]->ToObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
if (!Buffer::HasInstance(target))
|
|
|
|
return except("Argument should be a buffer object.");
|
|
|
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
|
|
|
|
|
|
|
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
|
|
|
|
blobdata output = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blobdata data;
|
|
|
|
uint64_t prefix;
|
|
|
|
uint64_t prefix;
|
|
|
|
|
|
|
|
if (!tools::base58::decode_addr(input, prefix, data))
|
|
|
|
|
|
|
|
NanReturnUndefined();
|
|
|
|
|
|
|
|
|
|
|
|
tools::base58::decode_addr(input, prefix, output);
|
|
|
|
account_public_address adr;
|
|
|
|
|
|
|
|
if (!::serialization::parse_binary(data, adr))
|
|
|
|
|
|
|
|
NanReturnUndefined();
|
|
|
|
|
|
|
|
|
|
|
|
if(output.length())
|
|
|
|
if (!crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key))
|
|
|
|
output = uint64be_to_blob(prefix) + output;
|
|
|
|
NanReturnUndefined();
|
|
|
|
|
|
|
|
|
|
|
|
Buffer* buff = Buffer::New(output.data(), output.size());
|
|
|
|
NanReturnValue(NanNew(static_cast<uint32_t>(prefix)));
|
|
|
|
return scope.Close(buff->handle_);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void init(Handle<Object> exports) {
|
|
|
|
void init(Handle<Object> exports) {
|
|
|
|
exports->Set(String::NewSymbol("construct_block_blob"), FunctionTemplate::New(construct_block_blob)->GetFunction());
|
|
|
|
exports->Set(NanNew<String>("construct_block_blob"), NanNew<FunctionTemplate>(construct_block_blob)->GetFunction());
|
|
|
|
exports->Set(String::NewSymbol("get_block_id"), FunctionTemplate::New(get_block_id)->GetFunction());
|
|
|
|
exports->Set(NanNew<String>("get_block_id"), NanNew<FunctionTemplate>(get_block_id)->GetFunction());
|
|
|
|
exports->Set(String::NewSymbol("convert_blob"), FunctionTemplate::New(convert_blob)->GetFunction());
|
|
|
|
exports->Set(NanNew<String>("convert_blob"), NanNew<FunctionTemplate>(convert_blob)->GetFunction());
|
|
|
|
exports->Set(String::NewSymbol("convert_blob_bb"), FunctionTemplate::New(convert_blob_bb)->GetFunction());
|
|
|
|
exports->Set(NanNew<String>("convert_blob_bb"), NanNew<FunctionTemplate>(convert_blob_bb)->GetFunction());
|
|
|
|
exports->Set(String::NewSymbol("address_decode"), FunctionTemplate::New(address_decode)->GetFunction());
|
|
|
|
exports->Set(NanNew<String>("address_decode"), NanNew<FunctionTemplate>(address_decode)->GetFunction());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NODE_MODULE(cryptonote, init)
|
|
|
|
NODE_MODULE(cryptonote, init)
|
|
|
|