diff --git a/AccountState.cpp b/AccountState.cpp index b24c779d5..d5612eee4 100644 --- a/AccountState.cpp +++ b/AccountState.cpp @@ -1,3 +1,6 @@ + +#include + #include "AccountState.h" #include "Serializer.h" @@ -22,3 +25,30 @@ std::vector AccountState::getRaw() const s.add32(mAccountSeq); return s.getData(); } + +static bool isHex(char j) +{ + if((j>='0') && (j<='9')) return true; + if((j>='A') && (j<='F')) return true; + if((j>='a') && (j<='f')) return true; + return false; +} + +bool AccountState::isHexAccountID(const std::string& acct) +{ + if(acct.size()!=40) return false; + for(int i=1; i<40; i++) + if(!isHex(acct[i])) return false; + return true; +} + +void AccountState::addJson(Json::Value& val) +{ + Json::Value as(Json::objectValue); + as["Account"]=mAccountID.GetHex(); + as["Balance"]=boost::lexical_cast(mBalance); + as["SendSequence"]=mAccountSeq; + if(!mValid) as["Invalid"]=true; + NewcoinAddress nad(mAccountID); + val[nad.GetString()]=as; +} diff --git a/AccountState.h b/AccountState.h index cd144f5be..7459f3b52 100644 --- a/AccountState.h +++ b/AccountState.h @@ -7,6 +7,8 @@ #include +#include "json/value.h" + #include "types.h" #include "uint256.h" @@ -48,8 +50,10 @@ public: assert(mAccountSeq!=0); mAccountSeq--; } + static bool isHexAccountID(const std::string& acct); std::vector getRaw() const; + void addJson(Json::Value& value); }; #endif diff --git a/RPCServer.cpp b/RPCServer.cpp index c519fd7c7..ae20a0d02 100644 --- a/RPCServer.cpp +++ b/RPCServer.cpp @@ -17,6 +17,8 @@ #include "Wallet.h" #include "Conversion.h" #include "LocalTransaction.h" +#include "NewcoinAddress.h" +#include "AccountState.h" /* Just read from wire until the entire request is in. @@ -198,15 +200,36 @@ Json::Value RPCServer::doCreateFamily(Json::Value& params) Json::Value RPCServer::doAccountInfo(Json::Value ¶ms) { // accountinfo : + // accountinfo std::string acct; if(!extractString(acct, params, 0)) return JSONRPCError(500, "Invalid account identifier"); LocalAccount::pointer account=theApp->getWallet().parseAccount(acct); - if(!account) - return JSONRPCError(500, "Account not found"); + if(account) return account->getJson(); - return account->getJson(); + uint160 acctid; + if(AccountState::isHexAccountID(acct)) acctid.SetHex(acct); + else + { + NewcoinAddress ad(acct); + if(ad.IsValid()) acctid=ad.GetHash160(); + } + if(!acctid) return JSONRPCError(500, "Unable to parse account"); + + LocalAccount::pointer lac(theApp->getWallet().getLocalAccount(acctid)); + if(!!lac) return lac->getJson(); + + AccountState::pointer as=theApp->getMasterLedger().getCurrentLedger()->getAccountState(acctid); + Json::Value ret(Json::objectValue); + if(as) + as->addJson(ret); + else + { + NewcoinAddress ad(acctid); + ret[ad.GetString()]="NotFound"; + } + return ret; } Json::Value RPCServer::doNewAccount(Json::Value ¶ms) diff --git a/RPCServer.h b/RPCServer.h index fa65b6dcc..7787b30a4 100644 --- a/RPCServer.h +++ b/RPCServer.h @@ -43,6 +43,7 @@ class RPCServer : public boost::enable_shared_from_this Json::Value doConnect(Json::Value& params); Json::Value doTx(Json::Value& params); Json::Value doLedger(Json::Value& params); + Json::Value doAccount(Json::Value& params); // parses a string account name into a uint160 // can be local or remote