diff --git a/RPCServer.cpp b/RPCServer.cpp index e3f6e45c7c..f9093d393d 100644 --- a/RPCServer.cpp +++ b/RPCServer.cpp @@ -151,6 +151,18 @@ bool RPCServer::extractString(std::string& param, const Json::Value& params, int return true; } +uint160 RPCServer::parseFamily(const std::string& fParam) +{ + uint160 family; + if(Wallet::isHexFamily(fParam)) + family.SetHex(fParam); + else if(Wallet::isHexPublicKey(fParam)) + family=theApp->getWallet().findFamilyPK(fParam); + else + family=theApp->getWallet().findFamilySN(fParam); + return family; +} + Json::Value RPCServer::doCreateFamily(Json::Value& params) { // createfamily @@ -206,6 +218,7 @@ Json::Value RPCServer::doAccountInfo(Json::Value ¶ms) return JSONRPCError(500, "Account not found"); Json::Value ret(Json::objectValue); + ret["Family"]=account->getFamilyName(); ret["ShortName"]=account->getShortName(); ret["FullName"]=account->getFullName(); ret["AccountID"]=NewcoinAddress(account->getAddress()).GetString(); @@ -214,8 +227,25 @@ Json::Value RPCServer::doAccountInfo(Json::Value ¶ms) } Json::Value RPCServer::doNewAccount(Json::Value ¶ms) -{ // newaccount - return "Not yet"; +{ // newaccount [] + std::string fParam; + if(!extractString(fParam, params, 0)) + return JSONRPCError(500, "Family required"); + + uint160 family = parseFamily(fParam); + if(!family) return JSONRPCError(500, "No such family"); + + LocalAccount::pointer account(theApp->getWallet().getNewLocalAccount(family)); + if(!account) + return JSONRPCError(500, "Family not found"); + + Json::Value ret(Json::objectValue); + ret["Family"]=account->getFamilyName(); + ret["ShortName"]=account->getShortName(); + ret["FullName"]=account->getFullName(); + ret["AccountID"]=NewcoinAddress(account->getAddress()).GetString(); + ret["Issued"]=Json::Value(account->isIssued()); + return ret; } Json::Value RPCServer::doLock(Json::Value ¶ms) @@ -263,13 +293,7 @@ Json::Value RPCServer::doFamilyInfo(Json::Value ¶ms) std::string fParam; extractString(fParam, params, 0); - uint160 family; - if(Wallet::isHexFamily(fParam)) - family.SetHex(fParam); - else if(Wallet::isHexPublicKey(fParam)) - family=theApp->getWallet().findFamilyPK(fParam); - else - family=theApp->getWallet().findFamilySN(fParam); + uint160 family=parseFamily(fParam); if(!family) return JSONRPCError(500, "No such family"); std::string name, comment, pubGen; diff --git a/RPCServer.h b/RPCServer.h index ecd8b8e365..cb62ada19d 100644 --- a/RPCServer.h +++ b/RPCServer.h @@ -31,6 +31,8 @@ class RPCServer : public boost::enable_shared_from_this int getParamCount(const Json::Value& params); bool extractString(std::string& param, const Json::Value& params, int index); + uint160 parseFamily(const std::string& family); + Json::Value doCreateFamily(Json::Value& params); Json::Value doFamilyInfo(Json::Value& params); Json::Value doAccountInfo(Json::Value& params); diff --git a/Wallet.cpp b/Wallet.cpp index fa7bc82018..52272c4558 100644 --- a/Wallet.cpp +++ b/Wallet.cpp @@ -350,6 +350,11 @@ std::string LocalAccount::getFullName() const return ret; } +std::string LocalAccount::getFamilyName() const +{ + return mFamily->getShortName(); +} + bool LocalAccount::isIssued() const { return mSeq < mFamily->getSeq(); @@ -471,6 +476,24 @@ std::string Wallet::getShortName(const uint160& famBase) return fit->second->getShortName(); } +LocalAccount::pointer Wallet::getNewLocalAccount(const uint160& family) +{ + boost::recursive_mutex::scoped_lock sl(mLock); + std::map::iterator fit=mFamilies.find(family); + if(fit==mFamilies.end()) return LocalAccount::pointer(); + + uint32 seq=fit->second->getSeq(); + uint160 acct=fit->second->getAccount(seq, true); + fit->second->setSeq(seq+1); // FIXME: writeout new seq + + std::map::iterator ait=mAccounts.find(acct); + if(ait!=mAccounts.end()) return ait->second; + + LocalAccount::pointer lac(new LocalAccount(fit->second, seq)); + mAccounts.insert(std::make_pair(acct, lac)); + return lac; +} + LocalAccount::pointer Wallet::getLocalAccount(const uint160& family, int seq) { boost::recursive_mutex::scoped_lock sl(mLock); diff --git a/Wallet.h b/Wallet.h index 66b00e6f2b..2cef3a5f9f 100644 --- a/Wallet.h +++ b/Wallet.h @@ -121,6 +121,7 @@ public: std::string getShortName() const; std::string getFullName() const; + std::string getFamilyName() const; bool isIssued() const; bool signRaw(Serializer::pointer); @@ -173,6 +174,7 @@ public: LocalAccount::pointer getLocalAccount(const uint160& famBase, int seq); LocalAccount::pointer getLocalAccount(const uint160& acctID); + LocalAccount::pointer getNewLocalAccount(const uint160& family); uint160 peekKey(const uint160& family, int seq); std::string getPubGenHex(const uint160& famBase); std::string getShortName(const uint160& famBase);