Make accounts there own type

so we can add new functionality  and so they can display properly.
Cleanups and missing helper function.
This commit is contained in:
JoelKatz
2012-03-27 15:51:14 -07:00
parent cbddb42e43
commit cb28fd52a3
5 changed files with 113 additions and 20 deletions

View File

@@ -28,10 +28,8 @@ SerializedType* STObject::makeDefaultObject(SerializedTypeID id, const char *nam
case STI_TL: case STI_TL:
return new STTaggedList(name); return new STTaggedList(name);
#if 0 case STI_ACCOUNT:
case STI_ACCOUNT: // CHECKME: Should an account be variable length? return new STAccount(name);
return new STVariableLength(name);
#endif
default: default:
return NULL; return NULL;
@@ -63,11 +61,9 @@ SerializedType* STObject::makeDeserializedObject(SerializedTypeID id, const char
case STI_TL: case STI_TL:
return STTaggedList::construct(sit, name); return STTaggedList::construct(sit, name);
#if 0 case STI_ACCOUNT:
case STI_ACCOUNT: // CHECKME: Should an account be variable length? return STAccount::construct(sit, name);
return STVariableLength::construct(sit, name);
#endif
default: default:
return NULL; return NULL;
} }

View File

@@ -99,6 +99,11 @@ std::vector<unsigned char> SerializedTransaction::getSignature() const
return mSignature.getValue(); return mSignature.getValue();
} }
const std::vector<unsigned char>& SerializedTransaction::peekSignature() const
{
return mSignature.peekValue();
}
bool SerializedTransaction::sign(CKey& key) bool SerializedTransaction::sign(CKey& key)
{ {
return key.Sign(getSigningHash(), mSignature.peekValue()); return key.Sign(getSigningHash(), mSignature.peekValue());
@@ -156,6 +161,36 @@ void SerializedTransaction::setSequence(uint32 seq)
v->setValue(seq); v->setValue(seq);
} }
std::vector<unsigned char> SerializedTransaction::getSigningAccount() const
{
const STVariableLength* v=
dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
return v->getValue();
}
const std::vector<unsigned char>& SerializedTransaction::peekSigningAccount() const
{
const STVariableLength* v=
dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
return v->peekValue();
}
std::vector<unsigned char>& SerializedTransaction::peekSigningAccount()
{
STVariableLength* v=dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
return v->peekValue();
}
void SerializedTransaction::setSigningAccount(const std::vector<unsigned char>& s)
{
STVariableLength* v=dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
v->setValue(s);
}
int SerializedTransaction::getITFieldIndex(SOE_Field field) const int SerializedTransaction::getITFieldIndex(SOE_Field field) const
{ {
return mInnerTxn.getFieldIndex(field); return mInnerTxn.getFieldIndex(field);

View File

@@ -34,6 +34,7 @@ public:
// outer transaction functions / signature functions // outer transaction functions / signature functions
std::vector<unsigned char> getSignature() const; std::vector<unsigned char> getSignature() const;
const std::vector<unsigned char>& peekSignature() const;
void setSignature(const std::vector<unsigned char>& s); void setSignature(const std::vector<unsigned char>& s);
uint256 getSigningHash() const; uint256 getSigningHash() const;
@@ -44,6 +45,8 @@ public:
uint64 getTransactionFee() const; uint64 getTransactionFee() const;
void setTransactionFee(uint64); void setTransactionFee(uint64);
std::vector<unsigned char> getSigningAccount() const; std::vector<unsigned char> getSigningAccount() const;
const std::vector<unsigned char>& peekSigningAccount() const;
std::vector<unsigned char>& peekSigningAccount();
void setSigningAccount(const std::vector<unsigned char>& s); void setSigningAccount(const std::vector<unsigned char>& s);
// inner transaction functions // inner transaction functions

View File

@@ -4,6 +4,7 @@
#include "SerializedTypes.h" #include "SerializedTypes.h"
#include "SerializedObject.h" #include "SerializedObject.h"
#include "TransactionFormats.h" #include "TransactionFormats.h"
#include "NewcoinAddress.h"
std::string SerializedType::getFullText() const std::string SerializedType::getFullText() const
{ {
@@ -104,6 +105,44 @@ int STVariableLength::getLength() const
return Serializer::encodeLengthLength(value.size()) + value.size(); return Serializer::encodeLengthLength(value.size()) + value.size();
} }
std::string STAccount::getText() const
{
uint160 u;
NewcoinAddress a;
if(!getValueH160(u)) return STVariableLength::getText();
a.setAccountID(u);
return a.humanAccountPublic();
}
STAccount* STAccount::construct(SerializerIterator& u, const char *name)
{
STAccount *ret=new STAccount(u.getVL());
if(!ret->isValueH160())
{
delete ret;
throw(std::runtime_error("invalid account in transaction"));
}
return ret;
}
bool STAccount::isValueH160() const
{
return peekValue().size() == (160/8);
}
void STAccount::setValueH160(const uint160& v)
{
peekValue().empty();
peekValue().insert(peekValue().end(), v.begin(), v.end());
}
bool STAccount::getValueH160(uint160& v) const
{
if(!isValueH160()) return false;
memcpy(v.begin(), &(peekValue().front()), 32);
return true;
}
std::string STTaggedList::getText() const std::string STTaggedList::getText() const
{ {
std::string ret; std::string ret;
@@ -128,3 +167,4 @@ int STTaggedList::getLength() const
return ret; return ret;
} }

View File

@@ -17,7 +17,7 @@ enum SerializedTypeID
STI_HASH160=6, STI_HASH256=7, STI_VL=8, STI_TL=9, STI_HASH160=6, STI_HASH256=7, STI_VL=8, STI_TL=9,
// high level types // high level types
STI_TRANSACTION=10 STI_ACCOUNT=10, STI_TRANSACTION=10
}; };
class SerializedType class SerializedType
@@ -61,7 +61,7 @@ public:
int getLength() const { return 1; } int getLength() const { return 1; }
SerializedTypeID getType() const { return STI_UINT8; } SerializedTypeID getType() const { return STI_UINT8; }
STUInt8 *duplicate() const { return new STUInt8(name, value); } STUInt8* duplicate() const { return new STUInt8(name, value); }
std::string getText() const; std::string getText() const;
void add(Serializer& s) const { s.add8(value); } void add(Serializer& s) const { s.add8(value); }
@@ -85,7 +85,7 @@ public:
int getLength() const { return 2; } int getLength() const { return 2; }
SerializedTypeID getType() const { return STI_UINT16; } SerializedTypeID getType() const { return STI_UINT16; }
STUInt16 *duplicate() const { return new STUInt16(name, value); } STUInt16* duplicate() const { return new STUInt16(name, value); }
std::string getText() const; std::string getText() const;
void add(Serializer& s) const { s.add16(value); } void add(Serializer& s) const { s.add16(value); }
@@ -109,7 +109,7 @@ public:
int getLength() const { return 4; } int getLength() const { return 4; }
SerializedTypeID getType() const { return STI_UINT32; } SerializedTypeID getType() const { return STI_UINT32; }
STUInt32 *duplicate() const { return new STUInt32(name, value); } STUInt32* duplicate() const { return new STUInt32(name, value); }
std::string getText() const; std::string getText() const;
void add(Serializer& s) const { s.add32(value); } void add(Serializer& s) const { s.add32(value); }
@@ -133,7 +133,7 @@ public:
int getLength() const { return 8; } int getLength() const { return 8; }
SerializedTypeID getType() const { return STI_UINT64; } SerializedTypeID getType() const { return STI_UINT64; }
STUInt64 *duplicate() const { return new STUInt64(name, value); } STUInt64* duplicate() const { return new STUInt64(name, value); }
std::string getText() const; std::string getText() const;
void add(Serializer& s) const { s.add64(value); } void add(Serializer& s) const { s.add64(value); }
@@ -158,8 +158,8 @@ public:
int getLength() const { return 20; } int getLength() const { return 20; }
SerializedTypeID getType() const { return STI_HASH160; } SerializedTypeID getType() const { return STI_HASH160; }
STHash160 *duplicate() const { return new STHash160(name, value); } STHash160* duplicate() const { return new STHash160(name, value); }
std::string getText() const; virtual std::string getText() const;
void add(Serializer& s) const { s.add160(value); } void add(Serializer& s) const { s.add160(value); }
const uint160& getValue() const { return value; } const uint160& getValue() const { return value; }
@@ -183,7 +183,7 @@ public:
int getLength() const { return 32; } int getLength() const { return 32; }
SerializedTypeID getType() const { return STI_HASH256; } SerializedTypeID getType() const { return STI_HASH256; }
STHash256 *duplicate() const { return new STHash256(name, value); } STHash256* duplicate() const { return new STHash256(name, value); }
std::string getText() const; std::string getText() const;
void add(Serializer& s) const { s.add256(value); } void add(Serializer& s) const { s.add256(value); }
@@ -208,9 +208,9 @@ public:
static STVariableLength* construct(SerializerIterator&, const char *name=NULL); static STVariableLength* construct(SerializerIterator&, const char *name=NULL);
int getLength() const; int getLength() const;
SerializedTypeID getType() const { return STI_VL; } virtual SerializedTypeID getType() const { return STI_VL; }
STVariableLength *duplicate() const { return new STVariableLength(name, value); } virtual STVariableLength* duplicate() const { return new STVariableLength(name, value); }
std::string getText() const; virtual std::string getText() const;
void add(Serializer& s) const { s.addVL(value); } void add(Serializer& s) const { s.addVL(value); }
const std::vector<unsigned char>& peekValue() const { return value; } const std::vector<unsigned char>& peekValue() const { return value; }
@@ -222,6 +222,25 @@ public:
STVariableLength& operator=(const std::vector<unsigned char>& v) { value=v; return *this; } STVariableLength& operator=(const std::vector<unsigned char>& v) { value=v; return *this; }
}; };
class STAccount : public STVariableLength
{
public:
STAccount(const std::vector<unsigned char>& v) : STVariableLength(v) { ; }
STAccount(const char *n, const std::vector<unsigned char>& v) : STVariableLength(n, v) { ; }
STAccount(const char *n) : STVariableLength(n) { ; }
STAccount() { ; }
static STAccount* construct(SerializerIterator&, const char *name=NULL);
SerializedTypeID getType() const { return STI_ACCOUNT; }
virtual STAccount* duplicate() const { return new STAccount(name, value); }
std::string getText() const;
void setValueH160(const uint160& v);
bool getValueH160(uint160&) const;
bool isValueH160() const;
};
class STTaggedList : public SerializedType class STTaggedList : public SerializedType
{ {
protected: protected:
@@ -237,7 +256,7 @@ public:
int getLength() const; int getLength() const;
SerializedTypeID getType() const { return STI_TL; } SerializedTypeID getType() const { return STI_TL; }
STTaggedList *duplicate() const { return new STTaggedList(name, value); } STTaggedList* duplicate() const { return new STTaggedList(name, value); }
std::string getText() const; std::string getText() const;
void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); } void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); }