From 5e649921bf235cdeff7319582ad67c67acf44b2c Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Sun, 12 May 2013 13:51:16 +0200 Subject: [PATCH] RPC: Add ability to specify a custom secret in proof_create. --- src/cpp/ripple/CallRPC.cpp | 9 ++++++--- src/cpp/ripple/ProofOfWork.h | 1 + src/cpp/ripple/RPCHandler.cpp | 37 +++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/cpp/ripple/CallRPC.cpp b/src/cpp/ripple/CallRPC.cpp index 4160dfea8..1eb4d919f 100644 --- a/src/cpp/ripple/CallRPC.cpp +++ b/src/cpp/ripple/CallRPC.cpp @@ -471,14 +471,17 @@ Json::Value RPCParser::parseAccountRaw(const Json::Value& jvParams, bool bPeer) return jvRequest; } -// proof_create [] +// proof_create [] [] Json::Value RPCParser::parseProofCreate(const Json::Value& jvParams) { Json::Value jvRequest; - if (1 == jvParams.size()) + if (jvParams.size() >= 1) jvRequest["difficulty"] = jvParams[0u].asInt(); + if (jvParams.size() >= 2) + jvRequest["secret"] = jvParams[1u].asString(); + return jvRequest; } @@ -730,7 +733,7 @@ Json::Value RPCParser::parseCommand(std::string strMethod, Json::Value jvParams) { "peers", &RPCParser::parseAsIs, 0, 0 }, { "ping", &RPCParser::parseAsIs, 0, 0 }, // { "profile", &RPCParser::parseProfile, 1, 9 }, - { "proof_create", &RPCParser::parseProofCreate, 0, 1 }, + { "proof_create", &RPCParser::parseProofCreate, 0, 2 }, { "proof_solve", &RPCParser::parseProofSolve, 1, 1 }, { "proof_verify", &RPCParser::parseProofVerify, 2, 2 }, { "random", &RPCParser::parseAsIs, 0, 0 }, diff --git a/src/cpp/ripple/ProofOfWork.h b/src/cpp/ripple/ProofOfWork.h index 1e29ada1d..0d476bbd7 100644 --- a/src/cpp/ripple/ProofOfWork.h +++ b/src/cpp/ripple/ProofOfWork.h @@ -90,6 +90,7 @@ public: void sweep(void); const uint256& getSecret() const { return mSecret; } + void setSecret(const uint256& secret) { mSecret = secret; } static int getPowEntry(const uint256& target, int iterations); }; diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index e39c85c3f..078e21965 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -879,27 +879,38 @@ Json::Value RPCHandler::doProfile(Json::Value jvRequest, int& cost, ScopedLock& } // { -// difficulty: // optional, if set, a temporary generator is -// // instantiated and its secret included +// // if either of these parameters is set, a custom generator is used +// difficulty: // optional +// secret: // optional // } Json::Value RPCHandler::doProofCreate(Json::Value jvRequest, int& cost, ScopedLock& MasterLockHolder) { - // XXX: Add ability to create proof with arbitrary secret and time + // XXX: Add ability to create proof with arbitrary time Json::Value jvResult(Json::objectValue); - if (jvRequest.isMember("difficulty")) + if (jvRequest.isMember("difficulty") || jvRequest.isMember("secret")) { - if (!jvRequest["difficulty"].isIntegral()) - return rpcError(rpcINVALID_PARAMS); - - int iDifficulty = jvRequest["difficulty"].asInt(); - - if (iDifficulty < 0 || iDifficulty > ProofOfWork::sMaxDifficulty) - return rpcError(rpcINVALID_PARAMS); - ProofOfWorkGenerator pgGen; - pgGen.setDifficulty(iDifficulty); + + if (jvRequest.isMember("difficulty")) + { + if (!jvRequest["difficulty"].isIntegral()) + return rpcError(rpcINVALID_PARAMS); + + int iDifficulty = jvRequest["difficulty"].asInt(); + + if (iDifficulty < 0 || iDifficulty > ProofOfWork::sMaxDifficulty) + return rpcError(rpcINVALID_PARAMS); + + pgGen.setDifficulty(iDifficulty); + } + + if (jvRequest.isMember("secret")) + { + uint256 uSecret(jvRequest["secret"].asString()); + pgGen.setSecret(uSecret); + } jvResult["token"] = pgGen.getProof().getToken(); jvResult["secret"] = pgGen.getSecret().GetHex();