Make STAccount only constructible with 160 bits (RIPD-994):

If someone attempts to construct an STAccount with something
other than 160 bits the constructor now throws.

Since an STAccount now enforces that it always stores exactly
160 bits, we use a fixed-sized uint160 for the storage, replacing
a variable sized STBlob.

In order to leave the ledger and wire formats unaffected, the
STAccount still serializes and deserializes itself as though
it were variable length.
This commit is contained in:
Scott Schurr
2015-10-30 17:48:42 -07:00
committed by Nik Bougalis
parent f63867e958
commit f72b14ec36
3 changed files with 104 additions and 44 deletions

View File

@@ -19,32 +19,58 @@
#include <BeastConfig.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/types.h>
namespace ripple {
STAccount::STAccount ()
: STBase ()
, value_ (beast::zero)
, default_ (true)
{
}
STAccount::STAccount (SField const& n)
: STBase (n)
, value_ (beast::zero)
, default_ (true)
{
}
STAccount::STAccount (SField const& n, Buffer&& v)
: STAccount (n)
{
if (v.empty())
return; // Zero is a valid size for a defaulted STAccount.
// Is it safe to throw from this constructor? Today (November 2015)
// the only place that calls this constructor is
// STVar::STVar (SerialIter&, SField const&)
// which throws. If STVar can throw in its constructor, then so can
// STAccount.
if (v.size() != uint160::bytes)
throw std::runtime_error ("Invalid STAccount size");
default_ = false;
memcpy (value_.begin(), v.data (), uint160::bytes);
}
STAccount::STAccount (SerialIter& sit, SField const& name)
: STAccount(name, sit.getVLBuffer())
{
}
STAccount::STAccount (SField const& n, AccountID const& v)
: STBase (n)
, default_ (false)
{
value_.copyFrom (v);
}
std::string STAccount::getText () const
{
AccountID u;
RippleAddress a;
if (! getValueH160 (u))
return STBlob::getText ();
return toBase58(u);
}
STAccount::STAccount (SField const& n, AccountID const& v)
: STBlob (n, v.data (), v.size ())
{
}
bool STAccount::isValueH160 () const
{
return peekValue ().size () == (160 / 8);
if (isDefault())
return "";
return toBase58 (value());
}
} // ripple