@@ -110,6 +110,32 @@ Handle<Value> convert_blob(const Arguments& args) {
110110 return scope.Close (buff->handle_ );
111111}
112112
113+ Handle<Value> get_block_id (const Arguments& args) {
114+ HandleScope scope;
115+
116+ if (args.Length () < 1 )
117+ return except (" You must provide one argument." );
118+
119+ Local<Object> target = args[0 ]->ToObject ();
120+
121+ if (!Buffer::HasInstance (target))
122+ return except (" Argument should be a buffer object." );
123+
124+ blobdata input = std::string (Buffer::Data (target), Buffer::Length (target));
125+ blobdata output = " " ;
126+
127+ block b = AUTO_VAL_INIT (b);
128+ if (!parse_and_validate_block_from_blob (input, b))
129+ return except (" Failed to parse block" );
130+
131+ crypto::hash block_id;
132+ if (!get_block_hash (b, block_id))
133+ return except (" Failed to calculate hash for block" );
134+
135+ Buffer* buff = Buffer::New (reinterpret_cast <char *>(&block_id), sizeof (block_id));
136+ return scope.Close (buff->handle_ );
137+ }
138+
113139Handle<Value> construct_block_blob (const Arguments& args) {
114140 HandleScope scope;
115141
@@ -204,6 +230,7 @@ Handle<Value> address_decode(const Arguments& args) {
204230
205231void init (Handle<Object> exports) {
206232 exports->Set (String::NewSymbol (" construct_block_blob" ), FunctionTemplate::New (construct_block_blob)->GetFunction ());
233+ exports->Set (String::NewSymbol (" get_block_id" ), FunctionTemplate::New (get_block_id)->GetFunction ());
207234 exports->Set (String::NewSymbol (" convert_blob" ), FunctionTemplate::New (convert_blob)->GetFunction ());
208235 exports->Set (String::NewSymbol (" convert_blob_bb" ), FunctionTemplate::New (convert_blob_bb)->GetFunction ());
209236 exports->Set (String::NewSymbol (" address_decode" ), FunctionTemplate::New (address_decode)->GetFunction ());
0 commit comments