diff --git a/src/main.cc b/src/main.cc index c0dffcb..21bab6c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -110,6 +110,32 @@ Handle convert_blob(const Arguments& args) { return scope.Close(buff->handle_); } +Handle get_block_id(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 1) + return except("You must provide one argument."); + + Local target = args[0]->ToObject(); + + if (!Buffer::HasInstance(target)) + return except("Argument should be a buffer object."); + + blobdata input = std::string(Buffer::Data(target), Buffer::Length(target)); + blobdata output = ""; + + block b = AUTO_VAL_INIT(b); + if (!parse_and_validate_block_from_blob(input, b)) + return except("Failed to parse block"); + + crypto::hash block_id; + if (!get_block_hash(b, block_id)) + return except("Failed to calculate hash for block"); + + Buffer* buff = Buffer::New(reinterpret_cast(&block_id), sizeof(block_id)); + return scope.Close(buff->handle_); +} + Handle construct_block_blob(const Arguments& args) { HandleScope scope; @@ -204,6 +230,7 @@ Handle address_decode(const Arguments& args) { void init(Handle exports) { exports->Set(String::NewSymbol("construct_block_blob"), FunctionTemplate::New(construct_block_blob)->GetFunction()); + exports->Set(String::NewSymbol("get_block_id"), FunctionTemplate::New(get_block_id)->GetFunction()); exports->Set(String::NewSymbol("convert_blob"), FunctionTemplate::New(convert_blob)->GetFunction()); exports->Set(String::NewSymbol("convert_blob_bb"), FunctionTemplate::New(convert_blob_bb)->GetFunction()); exports->Set(String::NewSymbol("address_decode"), FunctionTemplate::New(address_decode)->GetFunction());