Compare commits
8 Commits
master
...
forknote_c
Author | SHA1 | Date |
---|---|---|
clintar | 8d74079e59 | |
clintar | 3e9fd69c9c | |
clintar | 15d682bfb6 | |
clintar | 0a259628ad | |
clintar | 81f2e15516 | |
clintar | 54f5563ebb | |
clintar | 0ac11dca8b | |
clintar | da133cd384 |
38
package.json
38
package.json
|
@ -1,21 +1,21 @@
|
||||||
{
|
{
|
||||||
"name": "cryptonote-util",
|
"name": "cryptonote-util",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"main": "cryptonote",
|
"main": "cryptonote",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "LucasJones",
|
"name": "LucasJones",
|
||||||
"email": "lucasjonesdev@hotmail.co.uk"
|
"email": "lucasjonesdev@hotmail.co.uk"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/LucasJones/node-cryptonote-util.git"
|
"url": "https://github.com/clintar/node-cryptonote-util.git"
|
||||||
},
|
},
|
||||||
"dependencies" : {
|
"dependencies": {
|
||||||
"bindings" : "*",
|
"bindings": "*",
|
||||||
"nan" : "1"
|
"nan": "^2.0.0"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"cryptonight",
|
"cryptonight",
|
||||||
"cryptonote"
|
"cryptonote"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -458,15 +458,9 @@ namespace cryptonote
|
||||||
VARINT_FIELD(major_version)
|
VARINT_FIELD(major_version)
|
||||||
if(major_version > BLOCK_MAJOR_VERSION_2) return false;
|
if(major_version > BLOCK_MAJOR_VERSION_2) return false;
|
||||||
VARINT_FIELD(minor_version)
|
VARINT_FIELD(minor_version)
|
||||||
if (BLOCK_MAJOR_VERSION_1 == major_version)
|
VARINT_FIELD(timestamp)
|
||||||
{
|
|
||||||
VARINT_FIELD(timestamp)
|
|
||||||
}
|
|
||||||
FIELD(prev_id)
|
FIELD(prev_id)
|
||||||
if (BLOCK_MAJOR_VERSION_1 == major_version)
|
FIELD(nonce)
|
||||||
{
|
|
||||||
FIELD(nonce)
|
|
||||||
}
|
|
||||||
END_SERIALIZE()
|
END_SERIALIZE()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -479,11 +473,11 @@ namespace cryptonote
|
||||||
|
|
||||||
BEGIN_SERIALIZE_OBJECT()
|
BEGIN_SERIALIZE_OBJECT()
|
||||||
FIELDS(*static_cast<block_header *>(this))
|
FIELDS(*static_cast<block_header *>(this))
|
||||||
if (BLOCK_MAJOR_VERSION_2 <= major_version)
|
// if (BLOCK_MAJOR_VERSION_2 <= major_version)
|
||||||
{
|
// {
|
||||||
auto sbb = make_serializable_bytecoin_block(*this, false, false);
|
// auto sbb = make_serializable_bytecoin_block(*this, false, false);
|
||||||
FIELD_N("parent_block", sbb);
|
// FIELD_N("parent_block", sbb);
|
||||||
}
|
// }
|
||||||
FIELD(miner_tx)
|
FIELD(miner_tx)
|
||||||
FIELD(tx_hashes)
|
FIELD(tx_hashes)
|
||||||
END_SERIALIZE()
|
END_SERIALIZE()
|
||||||
|
|
|
@ -650,15 +650,15 @@ namespace cryptonote
|
||||||
if (!get_block_hashing_blob(b, blob))
|
if (!get_block_hashing_blob(b, blob))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (BLOCK_MAJOR_VERSION_2 <= b.major_version)
|
// if (BLOCK_MAJOR_VERSION_2 <= b.major_version)
|
||||||
{
|
// {
|
||||||
blobdata parent_blob;
|
// blobdata parent_blob;
|
||||||
auto sbb = make_serializable_bytecoin_block(b, true, false);
|
// auto sbb = make_serializable_bytecoin_block(b, true, false);
|
||||||
if (!t_serializable_object_to_blob(sbb, parent_blob))
|
// if (!t_serializable_object_to_blob(sbb, parent_blob))
|
||||||
return false;
|
// return false;
|
||||||
|
|
||||||
blob.append(parent_blob);
|
// blob.append(parent_blob);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return get_object_hash(blob, res);
|
return get_object_hash(blob, res);
|
||||||
}
|
}
|
||||||
|
@ -851,8 +851,8 @@ namespace cryptonote
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
bool check_proof_of_work_v1(const block& bl, difficulty_type current_diffic, crypto::hash& proof_of_work)
|
bool check_proof_of_work_v1(const block& bl, difficulty_type current_diffic, crypto::hash& proof_of_work)
|
||||||
{
|
{
|
||||||
if (BLOCK_MAJOR_VERSION_1 != bl.major_version)
|
// if (BLOCK_MAJOR_VERSION_1 != bl.major_version)
|
||||||
return false;
|
// return false;
|
||||||
|
|
||||||
proof_of_work = get_block_longhash(bl, 0);
|
proof_of_work = get_block_longhash(bl, 0);
|
||||||
return check_hash(proof_of_work, current_diffic);
|
return check_hash(proof_of_work, current_diffic);
|
||||||
|
@ -899,7 +899,7 @@ namespace cryptonote
|
||||||
switch (bl.major_version)
|
switch (bl.major_version)
|
||||||
{
|
{
|
||||||
case BLOCK_MAJOR_VERSION_1: return check_proof_of_work_v1(bl, current_diffic, proof_of_work);
|
case BLOCK_MAJOR_VERSION_1: return check_proof_of_work_v1(bl, current_diffic, proof_of_work);
|
||||||
case BLOCK_MAJOR_VERSION_2: return check_proof_of_work_v2(bl, current_diffic, proof_of_work);
|
case BLOCK_MAJOR_VERSION_2: return check_proof_of_work_v1(bl, current_diffic, proof_of_work);
|
||||||
}
|
}
|
||||||
|
|
||||||
CHECK_AND_ASSERT_MES(false, false, "unknown block major version: " << bl.major_version << "." << bl.minor_version);
|
CHECK_AND_ASSERT_MES(false, false, "unknown block major version: " << bl.major_version << "." << bl.minor_version);
|
||||||
|
|
176
src/main.cc
176
src/main.cc
|
@ -14,9 +14,12 @@
|
||||||
#include "serialization/binary_utils.h"
|
#include "serialization/binary_utils.h"
|
||||||
#include <nan.h>
|
#include <nan.h>
|
||||||
|
|
||||||
#define THROW_ERROR_EXCEPTION(x) NanThrowError(x)
|
#define THROW_ERROR_EXCEPTION(x) Nan::ThrowError(x)
|
||||||
#define THROW_ERROR_EXCEPTION_WITH_STATUS_CODE(x, y) NanThrowError(x, y)
|
|
||||||
|
void callback(char* data, void* hint) {
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
using namespace node;
|
using namespace node;
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
using namespace cryptonote;
|
using namespace cryptonote;
|
||||||
|
@ -78,12 +81,38 @@ static bool construct_parent_block(const cryptonote::block& b, cryptonote::block
|
||||||
}
|
}
|
||||||
|
|
||||||
NAN_METHOD(convert_blob) {
|
NAN_METHOD(convert_blob) {
|
||||||
NanScope();
|
|
||||||
|
|
||||||
if (args.Length() < 1)
|
if (info.Length() < 1)
|
||||||
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
||||||
|
|
||||||
Local<Object> target = args[0]->ToObject();
|
Local<Object> 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");
|
||||||
|
|
||||||
|
if (!get_block_hashing_blob(b, output))
|
||||||
|
return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
||||||
|
|
||||||
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked();
|
||||||
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
NAN_METHOD(convert_blob_fa) {
|
||||||
|
|
||||||
|
if (info.Length() < 1)
|
||||||
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
||||||
|
|
||||||
|
Local<Object> target = info[0]->ToObject();
|
||||||
|
|
||||||
if (!Buffer::HasInstance(target))
|
if (!Buffer::HasInstance(target))
|
||||||
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
||||||
|
@ -107,19 +136,24 @@ NAN_METHOD(convert_blob) {
|
||||||
if (!get_block_hashing_blob(parent_block, output))
|
if (!get_block_hashing_blob(parent_block, output))
|
||||||
return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
return THROW_ERROR_EXCEPTION("Failed to create mining block");
|
||||||
}
|
}
|
||||||
|
// Local<Object> v8::Local<v8::Value> returnValue = Nan::NewBuffer(output.length()).ToLocalChecked();
|
||||||
NanReturnValue(
|
// memcpy(Buffer::Data(returnValue), output.c_str(), output.length());
|
||||||
NanNewBufferHandle(output.data(), output.size())
|
// info.GetReturnValue().Set(
|
||||||
|
// returnValue
|
||||||
|
// );
|
||||||
|
|
||||||
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked();
|
||||||
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
NAN_METHOD(get_block_id) {
|
void get_block_id(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||||
NanScope();
|
|
||||||
|
|
||||||
if (args.Length() < 1)
|
if (info.Length() < 1)
|
||||||
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
||||||
|
|
||||||
Local<Object> target = args[0]->ToObject();
|
Local<Object> target = info[0]->ToObject();
|
||||||
|
|
||||||
if (!Buffer::HasInstance(target))
|
if (!Buffer::HasInstance(target))
|
||||||
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
||||||
|
@ -134,20 +168,53 @@ NAN_METHOD(get_block_id) {
|
||||||
crypto::hash block_id;
|
crypto::hash block_id;
|
||||||
if (!get_block_hash(b, block_id))
|
if (!get_block_hash(b, block_id))
|
||||||
return THROW_ERROR_EXCEPTION("Failed to calculate hash for block");
|
return THROW_ERROR_EXCEPTION("Failed to calculate hash for block");
|
||||||
|
|
||||||
NanReturnValue(
|
char *cstr = reinterpret_cast<char*>(&block_id);
|
||||||
NanNewBufferHandle(reinterpret_cast<char*>(&block_id), sizeof(block_id))
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer(cstr, 32).ToLocalChecked();
|
||||||
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
NAN_METHOD(construct_block_blob) {
|
void construct_block_blob(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||||
NanScope();
|
|
||||||
|
|
||||||
if (args.Length() < 2)
|
if (info.Length() < 2)
|
||||||
return THROW_ERROR_EXCEPTION("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 = info[0]->ToObject();
|
||||||
Local<Object> nonce_buf = args[1]->ToObject();
|
Local<Object> nonce_buf = info[1]->ToObject();
|
||||||
|
|
||||||
|
if (!Buffer::HasInstance(block_template_buf) || !Buffer::HasInstance(nonce_buf))
|
||||||
|
return THROW_ERROR_EXCEPTION("Both arguments should be buffer objects.");
|
||||||
|
|
||||||
|
if (Buffer::Length(nonce_buf) != 4)
|
||||||
|
return THROW_ERROR_EXCEPTION("Nonce buffer has invalid size.");
|
||||||
|
|
||||||
|
uint32_t nonce = *reinterpret_cast<uint32_t*>(Buffer::Data(nonce_buf));
|
||||||
|
|
||||||
|
blobdata block_template_blob = std::string(Buffer::Data(block_template_buf), Buffer::Length(block_template_buf));
|
||||||
|
blobdata output = "";
|
||||||
|
|
||||||
|
block b = AUTO_VAL_INIT(b);
|
||||||
|
if (!parse_and_validate_block_from_blob(block_template_blob, b))
|
||||||
|
return THROW_ERROR_EXCEPTION("Failed to parse block");
|
||||||
|
b.nonce = nonce;
|
||||||
|
if (!block_to_blob(b, output))
|
||||||
|
return THROW_ERROR_EXCEPTION("Failed to convert block to blob");
|
||||||
|
|
||||||
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked();
|
||||||
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void construct_block_blob_fa(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||||
|
|
||||||
|
if (info.Length() < 2)
|
||||||
|
return THROW_ERROR_EXCEPTION("You must provide two arguments.");
|
||||||
|
|
||||||
|
Local<Object> block_template_buf = info[0]->ToObject();
|
||||||
|
Local<Object> nonce_buf = info[1]->ToObject();
|
||||||
|
|
||||||
if (!Buffer::HasInstance(block_template_buf) || !Buffer::HasInstance(nonce_buf))
|
if (!Buffer::HasInstance(block_template_buf) || !Buffer::HasInstance(nonce_buf))
|
||||||
return THROW_ERROR_EXCEPTION("Both arguments should be buffer objects.");
|
return THROW_ERROR_EXCEPTION("Both arguments should be buffer objects.");
|
||||||
|
@ -178,18 +245,18 @@ NAN_METHOD(construct_block_blob) {
|
||||||
if (!block_to_blob(b, output))
|
if (!block_to_blob(b, output))
|
||||||
return THROW_ERROR_EXCEPTION("Failed to convert block to blob");
|
return THROW_ERROR_EXCEPTION("Failed to convert block to blob");
|
||||||
|
|
||||||
NanReturnValue(
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked();
|
||||||
NanNewBufferHandle(output.data(), output.size())
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
NAN_METHOD(convert_blob_bb) {
|
void convert_blob_bb(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||||
NanScope();
|
|
||||||
|
|
||||||
if (args.Length() < 1)
|
if (info.Length() < 1)
|
||||||
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
||||||
|
|
||||||
Local<Object> target = args[0]->ToObject();
|
Local<Object> target = info[0]->ToObject();
|
||||||
|
|
||||||
if (!Buffer::HasInstance(target))
|
if (!Buffer::HasInstance(target))
|
||||||
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
||||||
|
@ -204,18 +271,18 @@ NAN_METHOD(convert_blob_bb) {
|
||||||
}
|
}
|
||||||
output = get_block_hashing_blob(b);
|
output = get_block_hashing_blob(b);
|
||||||
|
|
||||||
NanReturnValue(
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)output.data(), output.size()).ToLocalChecked();
|
||||||
NanNewBufferHandle(output.data(), output.size())
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
NAN_METHOD(address_decode) {
|
void address_decode(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||||
NanEscapableScope();
|
|
||||||
|
|
||||||
if (args.Length() < 1)
|
if (info.Length() < 1)
|
||||||
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
return THROW_ERROR_EXCEPTION("You must provide one argument.");
|
||||||
|
|
||||||
Local<Object> target = args[0]->ToObject();
|
Local<Object> target = info[0]->ToObject();
|
||||||
|
|
||||||
if (!Buffer::HasInstance(target))
|
if (!Buffer::HasInstance(target))
|
||||||
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
|
||||||
|
@ -225,24 +292,41 @@ NAN_METHOD(address_decode) {
|
||||||
blobdata data;
|
blobdata data;
|
||||||
uint64_t prefix;
|
uint64_t prefix;
|
||||||
if (!tools::base58::decode_addr(input, prefix, data))
|
if (!tools::base58::decode_addr(input, prefix, data))
|
||||||
NanReturnUndefined();
|
{
|
||||||
|
info.GetReturnValue().Set(Nan::Undefined());
|
||||||
|
}
|
||||||
|
// info.GetReturnValue().Set(Nan::Undefined());
|
||||||
|
|
||||||
|
|
||||||
account_public_address adr;
|
account_public_address adr;
|
||||||
if (!::serialization::parse_binary(data, adr))
|
if (!::serialization::parse_binary(data, adr) || !crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key))
|
||||||
NanReturnUndefined();
|
{
|
||||||
|
if(data.length())
|
||||||
|
{
|
||||||
|
data = uint64be_to_blob(prefix) + data;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
info.GetReturnValue().Set(Nan::Undefined());
|
||||||
|
}
|
||||||
|
v8::Local<v8::Value> returnValue = Nan::CopyBuffer((char*)data.data(), data.size()).ToLocalChecked();
|
||||||
|
info.GetReturnValue().Set(
|
||||||
|
returnValue
|
||||||
|
);
|
||||||
|
|
||||||
if (!crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key))
|
}
|
||||||
NanReturnUndefined();
|
else
|
||||||
|
{
|
||||||
NanReturnValue(NanNew(static_cast<uint32_t>(prefix)));
|
info.GetReturnValue().Set(Nan::New(static_cast<uint32_t>(prefix)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(Handle<Object> exports) {
|
NAN_MODULE_INIT(init) {
|
||||||
exports->Set(NanNew<String>("construct_block_blob"), NanNew<FunctionTemplate>(construct_block_blob)->GetFunction());
|
Nan::Set(target, Nan::New("construct_block_blob").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(construct_block_blob)).ToLocalChecked());
|
||||||
exports->Set(NanNew<String>("get_block_id"), NanNew<FunctionTemplate>(get_block_id)->GetFunction());
|
Nan::Set(target, Nan::New("get_block_id").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(get_block_id)).ToLocalChecked());
|
||||||
exports->Set(NanNew<String>("convert_blob"), NanNew<FunctionTemplate>(convert_blob)->GetFunction());
|
Nan::Set(target, Nan::New("convert_blob").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(convert_blob)).ToLocalChecked());
|
||||||
exports->Set(NanNew<String>("convert_blob_bb"), NanNew<FunctionTemplate>(convert_blob_bb)->GetFunction());
|
Nan::Set(target, Nan::New("convert_blob_bb").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(convert_blob_bb)).ToLocalChecked());
|
||||||
exports->Set(NanNew<String>("address_decode"), NanNew<FunctionTemplate>(address_decode)->GetFunction());
|
Nan::Set(target, Nan::New("address_decode").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(address_decode)).ToLocalChecked());
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_MODULE(cryptonote, init)
|
NODE_MODULE(cryptonote, init)
|
||||||
|
|
Loading…
Reference in New Issue