#include #include #include #include #include #include "crypto/equihash.h" #include using namespace v8; int verifyEH(const char *hdr, const std::vector &soln, unsigned int n = 200, unsigned int k = 9){ // Hash state crypto_generichash_blake2b_state state; EhInitialiseState(n, k, state); crypto_generichash_blake2b_update(&state, (const unsigned char*)hdr, 140); bool isValid; if (n == 96 && k == 3) { isValid = Eh96_3.IsValidSolution(state, soln); } else if (n == 200 && k == 9) { isValid = Eh200_9.IsValidSolution(state, soln); } else if (n == 144 && k == 5) { isValid = Eh144_5.IsValidSolution(state, soln); } else if (n == 192 && k == 7) { isValid = Eh192_7.IsValidSolution(state, soln); } else if (n == 96 && k == 5) { isValid = Eh96_5.IsValidSolution(state, soln); } else if (n == 48 && k == 5) { isValid = Eh48_5.IsValidSolution(state, soln); } else { throw std::invalid_argument("Unsupported Equihash parameters"); } return isValid; } void Verify(const v8::FunctionCallbackInfo& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); unsigned int n = 200; unsigned int k = 9; if (args.Length() < 2) { isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong number of arguments"))); return; } Local header = args[0]->ToObject(); Local solution = args[1]->ToObject(); if (args.Length() == 4) { n = args[2]->Uint32Value(); k = args[3]->Uint32Value(); } if(!node::Buffer::HasInstance(header) || !node::Buffer::HasInstance(solution)) { isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Arguments should be buffer objects."))); return; } const char *hdr = node::Buffer::Data(header); if(node::Buffer::Length(header) != 140) { //invalid hdr length args.GetReturnValue().Set(false); return; } const char *soln = node::Buffer::Data(solution); std::vector vecSolution(soln, soln + node::Buffer::Length(solution)); bool result = verifyEH(hdr, vecSolution, n, k); args.GetReturnValue().Set(result); } void Init(Handle exports) { NODE_SET_METHOD(exports, "verify", Verify); } NODE_MODULE(equihashverify, Init)