Add issuerFromString() and bSetJson() to STAmount.

This commit is contained in:
Arthur Britto
2012-11-21 16:22:51 -08:00
parent f98741af3c
commit 8e35265922
2 changed files with 45 additions and 15 deletions

View File

@@ -16,6 +16,31 @@ SETUP_LOG();
uint64 STAmount::uRateOne = STAmount::getRate(STAmount(1), STAmount(1));
bool STAmount::issuerFromString(uint160& uDstIssuer, const std::string& sIssuer)
{
bool bSuccess = true;
if (sIssuer.size() == (160/4))
{
uDstIssuer.SetHex(sIssuer);
}
else
{
RippleAddress raIssuer;
if (raIssuer.setAccountID(sIssuer))
{
uDstIssuer = raIssuer.getAccountID();
}
else
{
bSuccess = false;
}
}
return bSuccess;
}
// --> sCurrency: "", "XRP", or three letter ISO code.
bool STAmount::currencyFromString(uint160& uDstCurrency, const std::string& sCurrency)
{
@@ -58,6 +83,21 @@ std::string STAmount::getHumanCurrency() const
return createHumanCurrency(mCurrency);
}
bool STAmount::bSetJson(const Json::Value& jvSource)
{
try {
STAmount saParsed(sfGeneric, jvSource);
*this = saParsed;
return true;
}
catch (...)
{
return false;
}
}
STAmount::STAmount(SField::ref n, const Json::Value& v)
: SerializedType(n), mValue(0), mOffset(0), mIsNegative(false)
{
@@ -105,23 +145,10 @@ STAmount::STAmount(SField::ref n, const Json::Value& v)
if (!currencyFromString(mCurrency, currency.asString()))
throw std::runtime_error("invalid currency");
if (!issuer.isString())
if (!issuer.isString()
|| !issuerFromString(mIssuer, issuer.asString()))
throw std::runtime_error("invalid issuer");
if (issuer.size() == (160/4))
{
mIssuer.SetHex(issuer.asString());
}
else
{
RippleAddress is;
if(!is.setAccountID(issuer.asString()))
throw std::runtime_error("invalid issuer");
mIssuer = is.getAccountID();
}
if (mIssuer.isZero())
throw std::runtime_error("invalid issuer");
}