From 8e35265922f23e6b269336faa2b619c8aae6a013 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Wed, 21 Nov 2012 16:22:51 -0800 Subject: [PATCH] Add issuerFromString() and bSetJson() to STAmount. --- src/cpp/ripple/Amount.cpp | 57 +++++++++++++++++++++++--------- src/cpp/ripple/SerializedTypes.h | 3 ++ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/cpp/ripple/Amount.cpp b/src/cpp/ripple/Amount.cpp index 52ff6f8d3c..80027b11ee 100644 --- a/src/cpp/ripple/Amount.cpp +++ b/src/cpp/ripple/Amount.cpp @@ -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"); } diff --git a/src/cpp/ripple/SerializedTypes.h b/src/cpp/ripple/SerializedTypes.h index 3745a92190..438145f38b 100644 --- a/src/cpp/ripple/SerializedTypes.h +++ b/src/cpp/ripple/SerializedTypes.h @@ -259,6 +259,8 @@ public: static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) { return std::auto_ptr(construct(sit, name)); } + bool bSetJson(const Json::Value& jvSource); + static STAmount saFromRate(uint64 uRate = 0) { return STAmount(CURRENCY_ONE, ACCOUNT_ONE, uRate, -9, false); } @@ -379,6 +381,7 @@ public: static std::string createHumanCurrency(const uint160& uCurrency); static STAmount deserialize(SerializerIterator&); static bool currencyFromString(uint160& uDstCurrency, const std::string& sCurrency); + static bool issuerFromString(uint160& uDstIssuer, const std::string& sIssuer); Json::Value getJson(int) const; };