Make the Json code more logical, avoid duplication in RPC calls.

This commit is contained in:
JoelKatz
2012-01-07 14:32:47 -08:00
parent 79808e9835
commit 7935889f00
3 changed files with 100 additions and 57 deletions

View File

@@ -165,50 +165,38 @@ uint160 RPCServer::parseFamily(const std::string& fParam)
Json::Value RPCServer::doCreateFamily(Json::Value& params)
{
// createfamily <hexPrivateKey>
// createfamily <hexPublicKey>
// createfamily "<pass phrase>"
// createfamily
// createfamily <hexPrivateKey>
// createfamily <hexPublicKey>
// createfamily "<pass phrase>"
// 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 &params)
{ // accountinfo <family>:<number>
{ // accountinfo <family>:<number>
std::string acct;
if(!extractString(acct, params, 0))
return JSONRPCError(500, "Invalid account identifier");
@@ -227,7 +215,7 @@ Json::Value RPCServer::doAccountInfo(Json::Value &params)
}
Json::Value RPCServer::doNewAccount(Json::Value &params)
{ // newaccount <family> [<name>]
{ // newaccount <family> [<name>]
std::string fParam;
if(!extractString(fParam, params, 0))
return JSONRPCError(500, "Family required");
@@ -249,15 +237,42 @@ Json::Value RPCServer::doNewAccount(Json::Value &params)
}
Json::Value RPCServer::doLock(Json::Value &params)
{ // lock <family>
// lock
return "Not yet";
{ // lock <family>
// lock
std::string fParam;
if(extractString(fParam, params, 0))
{ // local <family>
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 &params)
{ // unlock <hexPrivateKey>
// unlock "<pass phrase>"
return "Not yet";
{ // unlock <hexPrivateKey>
// unlock "<pass phrase>"
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 &params)
@@ -275,16 +290,8 @@ Json::Value RPCServer::doFamilyInfo(Json::Value &params)
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 &params)
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)
{

View File

@@ -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<uint160, LocalAccountFamily::pointer>::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<uint160, LocalAccountFamily::pointer>::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<uint160, LocalAccountFamily::pointer>::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;

View File

@@ -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&);