@@ -112,6 +112,32 @@ Handle<Value> convert_blob(const Arguments& args) {
112112 return scope.Close (buff->handle_ );
113113}
114114
115+ Handle<Value> get_block_id (const Arguments& args) {
116+ HandleScope scope;
117+
118+ if (args.Length () < 1 )
119+ return except (" You must provide one argument." );
120+
121+ Local<Object> target = args[0 ]->ToObject ();
122+
123+ if (!Buffer::HasInstance (target))
124+ return except (" Argument should be a buffer object." );
125+
126+ blobdata input = std::string (Buffer::Data (target), Buffer::Length (target));
127+ blobdata output = " " ;
128+
129+ block b = AUTO_VAL_INIT (b);
130+ if (!parse_and_validate_block_from_blob (input, b))
131+ return except (" Failed to parse block" );
132+
133+ crypto::hash block_id;
134+ if (!get_block_hash (b, block_id))
135+ return except (" Failed to calculate hash for block" );
136+
137+ Buffer* buff = Buffer::New (reinterpret_cast <char *>(&block_id), sizeof (block_id));
138+ return scope.Close (buff->handle_ );
139+ }
140+
115141Handle<Value> construct_block_blob (const Arguments& args) {
116142 HandleScope scope;
117143
@@ -216,6 +242,7 @@ Handle<Value> check_address(const Arguments& args) {
216242
217243void init (Handle<Object> exports) {
218244 exports->Set (String::NewSymbol (" construct_block_blob" ), FunctionTemplate::New (construct_block_blob)->GetFunction ());
245+ exports->Set (String::NewSymbol (" get_block_id" ), FunctionTemplate::New (get_block_id)->GetFunction ());
219246 exports->Set (String::NewSymbol (" convert_blob" ), FunctionTemplate::New (convert_blob)->GetFunction ());
220247 exports->Set (String::NewSymbol (" convert_blob_bb" ), FunctionTemplate::New (convert_blob_bb)->GetFunction ());
221248 exports->Set (String::NewSymbol (" check_address" ), FunctionTemplate::New (check_address)->GetFunction ());
0 commit comments