From 7935889f0059f21b21fe41a3e4c6f2e111840e8d Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sat, 7 Jan 2012 14:32:47 -0800 Subject: [PATCH] Make the Json code more logical, avoid duplication in RPC calls. --- RPCServer.cpp | 106 +++++++++++++++++++++++++------------------------- Wallet.cpp | 37 ++++++++++++++++++ Wallet.h | 14 +++++-- 3 files changed, 100 insertions(+), 57 deletions(-) diff --git a/RPCServer.cpp b/RPCServer.cpp index f9093d393d..465aaa7161 100644 --- a/RPCServer.cpp +++ b/RPCServer.cpp @@ -165,50 +165,38 @@ uint160 RPCServer::parseFamily(const std::string& fParam) Json::Value RPCServer::doCreateFamily(Json::Value& params) { -// createfamily -// createfamily -// createfamily "" -// createfamily + // createfamily + // createfamily + // createfamily "" + // createfamily std::string query; uint160 family; - Json::Value ret(Json::objectValue); + uint256 privKey; if(!extractString(query, params, 0)) - { - std::cerr << "empty" << std::endl; - uint256 privKey; family=theApp->getWallet().addRandomFamily(privKey); - ret["PrivateGenerator"]=Wallet::privKeyToText(privKey); - } else if(Wallet::isHexPrivateKey(query)) - { - std::cerr << "hprivk" << std::endl; - uint256 pk; - pk.SetHex(query); - family=theApp->getWallet().addFamily(pk, false); - } + family=theApp->getWallet().addFamily(Wallet::textToPrivKey(query), false); else if(Wallet::isHexPublicKey(query)) - { - std::cerr << "hpubk" << std::endl; family=theApp->getWallet().addFamily(query); - } else - { - std::cerr << "PassPhrase(" << query << ")" << std::endl; family=theApp->getWallet().addFamily(query, false); - } if(!family) return JSONRPCError(500, "Invalid family specifier"); - ret["FamilyIdentifier"]=family.GetHex(); - ret["ShortName"]=theApp->getWallet().getShortName(family); - ret["PublicGenerator"]=theApp->getWallet().getPubGenHex(family); + Json::Value ret(theApp->getWallet().getFamilyJson(family)); + if(ret.isNull()) return JSONRPCError(500, "Invalid family"); + if(!!privKey) + { + ret["PrivateGenerator"]=Wallet::privKeyToText(privKey); + privKey.zero(); + } return ret; } Json::Value RPCServer::doAccountInfo(Json::Value ¶ms) -{ // accountinfo : +{ // accountinfo : std::string acct; if(!extractString(acct, params, 0)) return JSONRPCError(500, "Invalid account identifier"); @@ -227,7 +215,7 @@ Json::Value RPCServer::doAccountInfo(Json::Value ¶ms) } Json::Value RPCServer::doNewAccount(Json::Value ¶ms) -{ // newaccount [] +{ // newaccount [] std::string fParam; if(!extractString(fParam, params, 0)) return JSONRPCError(500, "Family required"); @@ -249,15 +237,42 @@ Json::Value RPCServer::doNewAccount(Json::Value ¶ms) } Json::Value RPCServer::doLock(Json::Value ¶ms) -{ // lock - // lock - return "Not yet"; +{ // lock + // lock + std::string fParam; + if(extractString(fParam, params, 0)) + { // local + uint160 family = parseFamily(fParam); + if(!family) return JSONRPCError(500, "Family not found"); + theApp->getWallet().lock(family); + return "locked"; + } + else + { + theApp->getWallet().lock(); + return "locked"; + } } Json::Value RPCServer::doUnlock(Json::Value ¶ms) -{ // unlock - // unlock "" - return "Not yet"; +{ // unlock + // unlock "" + std::string param; + if(!extractString(param, params, 0) || Wallet::isHexPublicKey(param)) + return JSONRPCError(500, "Private key required"); + + uint160 family; + if(Wallet::isHexPrivateKey(param)) + family=theApp->getWallet().addFamily(Wallet::textToPrivKey(param), false); + else + family=theApp->getWallet().addFamily(param); + + if(!family) + return JSONRPCError(500, "Bad family"); + + Json::Value ret(theApp->getWallet().getFamilyJson(family)); + if(ret.isNull()) return JSONRPCError(500, "Invalid family"); + return ret; } Json::Value RPCServer::doFamilyInfo(Json::Value ¶ms) @@ -275,16 +290,8 @@ Json::Value RPCServer::doFamilyInfo(Json::Value ¶ms) Json::Value ret(Json::arrayValue); BOOST_FOREACH(const uint160& fid, familyIDs) { - Json::Value obj(Json::objectValue); - std::string name, comment; - if(theApp->getWallet().getFamilyInfo(fid, name, comment)) - { - obj["FamilyIdentifier"]=fid.GetHex(); - obj["ShortName"]=name; - if(!comment.empty()) - obj["Comment"]=comment; - ret.append(obj); - } + Json::Value obj(theApp->getWallet().getFamilyJson(fid)); + if(!obj.isNull()) ret.append(obj); } return ret; } @@ -295,18 +302,9 @@ Json::Value RPCServer::doFamilyInfo(Json::Value ¶ms) uint160 family=parseFamily(fParam); if(!family) return JSONRPCError(500, "No such family"); - - std::string name, comment, pubGen; - bool isLocked; - if(!theApp->getWallet().getFullFamilyInfo(family, name, comment, pubGen, isLocked)) + Json::Value obj(theApp->getWallet().getFamilyJson(family)); + if(obj.isNull()) return JSONRPCError(500, "Family not found"); - Json::Value obj(Json::objectValue); - obj["FamilyIdentifier"]=family.GetHex(); - obj["ShortName"]=name; - if(!comment.empty()) - obj["Comment"]=comment; - obj["PublicGenerator"]=pubGen; - obj["Locked"]=Json::Value(isLocked); if(paramCount==2) { diff --git a/Wallet.cpp b/Wallet.cpp index 52272c4558..7b0c80ba5b 100644 --- a/Wallet.cpp +++ b/Wallet.cpp @@ -128,6 +128,17 @@ static bool isHex(char j) return false; } +Json::Value LocalAccountFamily::getJson() const +{ + Json::Value ret(Json::objectValue); + ret["ShortName"]=getShortName(); + ret["FullName"]=getFamily().GetHex(); + ret["PublicGenerator"]=getPubGenHex(); + ret["IsLocked"]=isLocked(); + if(!getComment().empty()) ret["Comment"]=getComment(); + return ret; +} + LocalAccountFamily::pointer LocalAccountFamily::readFamily(const uint160& family) { std::string sql="SELECT * from LocalAcctFamilies WHERE FamilyName='"; @@ -419,6 +430,15 @@ bool Wallet::getFullFamilyInfo(const uint160& family, std::string& name, std::st return true; } +Json::Value Wallet::getFamilyJson(const uint160& family) +{ + boost::recursive_mutex::scoped_lock sl(mLock); + std::map::iterator fit=mFamilies.find(family); + if(fit==mFamilies.end()) return Json::Value(Json::nullValue); + assert(fit->second->getFamily()==family); + return fit->second->getJson(); +} + void Wallet::load() { std::string sql("SELECT * FROM LocalAcctFamilies;"); @@ -656,6 +676,23 @@ LocalAccountFamily::pointer Wallet::doPrivate(const uint256& passPhrase, bool do return fam; } +bool Wallet::lock(const uint160& family) +{ + boost::recursive_mutex::scoped_lock sl(mLock); + std::map::iterator fit=mFamilies.find(family); + if(fit==mFamilies.end()) return false; + fit->second->lock(); + return true; +} + +void Wallet::lock() +{ + boost::recursive_mutex::scoped_lock sl(mLock); + for(std::map::iterator fit=mFamilies.begin(); + fit!=mFamilies.end(); ++fit) + fit->second->lock(); +} + bool Wallet::unitTest() { // Create 100 keys for each of 1,000 families and ensure all keys match Wallet pub, priv; diff --git a/Wallet.h b/Wallet.h index 2cef3a5f9f..93e77a62d9 100644 --- a/Wallet.h +++ b/Wallet.h @@ -10,6 +10,8 @@ #include "openssl/ec.h" +#include "json/value.h" + #include "uint256.h" #include "Serializer.h" @@ -79,14 +81,14 @@ public: LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey); ~LocalAccountFamily(); - const uint160& getFamily() { return mFamily; } + const uint160& getFamily() const { return mFamily; } void unlock(const BIGNUM* privateKey); void lock(); bool isLocked() const { return mRootPrivateKey==NULL; } void setSeq(uint32 s) { mLastSeq=s; } - uint32 getSeq(void) { return mLastSeq; } + uint32 getSeq() { return mLastSeq; } void setName(const std::string& n) { mName=n; } void setComment(const std::string& c) { mComment=c; } @@ -98,6 +100,7 @@ public: std::string getPubGenHex() const; // The text name of the public key std::string getShortName() const { return mName; } std::string getComment() const { return mComment; } + Json::Value getJson() const; static std::string getSQLFields(); std::string getSQL() const; @@ -134,6 +137,8 @@ public: CKey::pointer getPublicKey(); CKey::pointer getPrivateKey(); + + Json::Value getJson(); }; class Wallet @@ -166,8 +171,9 @@ public: uint160 unlock(const uint256& passPhrase); bool lock(const uint160& familyName); + void lock(); - void load(void); + void load(); // must be a known local account LocalAccount::pointer parseAccount(const std::string& accountSpecifier); @@ -178,9 +184,11 @@ public: uint160 peekKey(const uint160& family, int seq); std::string getPubGenHex(const uint160& famBase); std::string getShortName(const uint160& famBase); + bool getFamilyInfo(const uint160& family, std::string& name, std::string& comment); bool getFullFamilyInfo(const uint160& family, std::string& name, std::string& comment, std::string& pubGen, bool& isLocked); + Json::Value getFamilyJson(const uint160& family); static bool isHexPrivateKey(const std::string&); static bool isHexPublicKey(const std::string&);