From bd9780e5033ff4e6160f0d2592b76c7b9a2428c4 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 26 Mar 2012 20:31:03 -0700 Subject: [PATCH] Tie the new serialization code into the Json code. Small cleanups. --- src/SerializedObject.cpp | 18 ++++++++++++++++++ src/SerializedObject.h | 3 +++ src/SerializedTransaction.cpp | 22 +++++++++++++++++++++- src/SerializedTransaction.h | 5 +++++ src/SerializedTypes.h | 18 +++++++++--------- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index cce8db74c9..6495d5555e 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -1,6 +1,8 @@ #include "SerializedObject.h" +#include "boost/lexical_cast.hpp" + SerializedType* STObject::makeDefaultObject(SerializedTypeID id, const char *name) { switch(id) @@ -455,3 +457,19 @@ void STObject::setValueFieldTL(SOE_Field field, const std::vectorsetValue(v); } + +Json::Value STObject::getJson(int options) const +{ + Json::Value ret(Json::objectValue); + int index=1; + for(boost::ptr_vector::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it, ++index) + { + if(it->getType()!=STI_NOTPRESENT) + { + if(it->getName()==NULL) + ret[boost::lexical_cast(index)]=it->getText(); + else ret[it->getName()]=it->getText(); + } + } + return ret; +} diff --git a/src/SerializedObject.h b/src/SerializedObject.h index 289fd4f310..e48c989c0b 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -5,6 +5,8 @@ #include +#include "../json/value.h" + #include "SerializedTypes.h" enum SOE_Type @@ -57,6 +59,7 @@ public: void add(Serializer& s) const; std::string getFullText() const; std::string getText() const; + virtual Json::Value getJson(int options) const; int addObject(const SerializedType& t) { mData.push_back(t.duplicate()); return mData.size()-1; } int giveObject(SerializedType* t) { mData.push_back(t); return mData.size()-1; } diff --git a/src/SerializedTransaction.cpp b/src/SerializedTransaction.cpp index df57ded23c..126be6cd15 100644 --- a/src/SerializedTransaction.cpp +++ b/src/SerializedTransaction.cpp @@ -99,6 +99,16 @@ std::vector SerializedTransaction::getSignature() const return mSignature.getValue(); } +bool SerializedTransaction::sign(CKey& key) +{ + return key.Sign(getSigningHash(), mSignature.peekValue()); +} + +bool SerializedTransaction::checkSign(const CKey& key) const +{ + return key.Verify(getSigningHash(), mSignature.getValue()); +} + void SerializedTransaction::setSignature(const std::vector& sig) { mSignature.setValue(sig); @@ -180,4 +190,14 @@ void SerializedTransaction::makeITFieldAbsent(SOE_Field field) { return mInnerTxn.makeFieldAbsent(field); } - \ No newline at end of file + +Json::Value SerializedTransaction::getJson(int options) const +{ + Json::Value ret(Json::objectValue); + ret["Type"]=mFormat->t_name; + ret["ID"]=getTransactionID().GetHex(); + ret["Signature"]=mSignature.getText(); + ret["Middle"]=mMiddleTxn.getJson(options); + ret["Inner"]=mInnerTxn.getJson(options); + return ret; +} diff --git a/src/SerializedTransaction.h b/src/SerializedTransaction.h index 7b040de4dc..796b759b19 100644 --- a/src/SerializedTransaction.h +++ b/src/SerializedTransaction.h @@ -89,6 +89,11 @@ public: // whole transaction functions int getTransaction(Serializer& s, bool include_length) const; uint256 getTransactionID() const; + + virtual Json::Value getJson(int options) const; + + bool sign(CKey& key); + bool checkSign(const CKey& key) const; }; #endif diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 8a9fced78f..f9dcbb11c9 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -32,7 +32,7 @@ public: virtual ~SerializedType() { ; } void setName(const char *n) { name=n; } - const char *getName() { return name; } + const char *getName() const { return name; } virtual int getLength() const { return 0; } virtual SerializedTypeID getType() const { return STI_NOTPRESENT; } @@ -63,7 +63,7 @@ public: SerializedTypeID getType() const { return STI_UINT8; } STUInt8 *duplicate() const { return new STUInt8(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.add8(value); } + void add(Serializer& s) const { s.add8(value); } unsigned char getValue() const { return value; } void setValue(unsigned char v) { value=v; } @@ -87,7 +87,7 @@ public: SerializedTypeID getType() const { return STI_UINT16; } STUInt16 *duplicate() const { return new STUInt16(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.add16(value); } + void add(Serializer& s) const { s.add16(value); } uint16 getValue() const { return value; } void setValue(uint16 v) { value=v; } @@ -111,7 +111,7 @@ public: SerializedTypeID getType() const { return STI_UINT32; } STUInt32 *duplicate() const { return new STUInt32(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.add32(value); } + void add(Serializer& s) const { s.add32(value); } uint32 getValue() const { return value; } void setValue(uint32 v) { value=v; } @@ -135,7 +135,7 @@ public: SerializedTypeID getType() const { return STI_UINT64; } STUInt64 *duplicate() const { return new STUInt64(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.add64(value); } + void add(Serializer& s) const { s.add64(value); } uint64 getValue() const { return value; } void setValue(uint64 v) { value=v; } @@ -160,7 +160,7 @@ public: SerializedTypeID getType() const { return STI_HASH160; } STHash160 *duplicate() const { return new STHash160(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.add160(value); } + void add(Serializer& s) const { s.add160(value); } const uint160& getValue() const { return value; } void setValue(const uint160& v) { value=v; } @@ -185,7 +185,7 @@ public: SerializedTypeID getType() const { return STI_HASH256; } STHash256 *duplicate() const { return new STHash256(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.add256(value); } + void add(Serializer& s) const { s.add256(value); } const uint256& getValue() const { return value; } void setValue(const uint256& v) { value=v; } @@ -211,7 +211,7 @@ public: SerializedTypeID getType() const { return STI_VL; } STVariableLength *duplicate() const { return new STVariableLength(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { s.addVL(value); } + void add(Serializer& s) const { s.addVL(value); } const std::vector& peekValue() const { return value; } std::vector& peekValue() { return value; } @@ -239,7 +239,7 @@ public: SerializedTypeID getType() const { return STI_TL; } STTaggedList *duplicate() const { return new STTaggedList(name, value); } std::string getText() const; - virtual void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); } + void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); } const std::vector& peekValue() const { return value; } std::vector& peekValue() { return value; }