#pragma once #include "serde.hpp" #include "bincode.hpp" namespace WitnessStack { struct Witness { uint32_t value; friend bool operator==(const Witness&, const Witness&); std::vector bincodeSerialize() const; static Witness bincodeDeserialize(std::vector); }; struct WitnessMap { std::map value; friend bool operator==(const WitnessMap&, const WitnessMap&); std::vector bincodeSerialize() const; static WitnessMap bincodeDeserialize(std::vector); }; struct StackItem { uint32_t index; WitnessStack::WitnessMap witness; friend bool operator==(const StackItem&, const StackItem&); std::vector bincodeSerialize() const; static StackItem bincodeDeserialize(std::vector); }; struct WitnessStack { std::vector stack; friend bool operator==(const WitnessStack&, const WitnessStack&); std::vector bincodeSerialize() const; static WitnessStack bincodeDeserialize(std::vector); }; } // end of namespace WitnessStack namespace WitnessStack { inline bool operator==(const StackItem &lhs, const StackItem &rhs) { if (!(lhs.index == rhs.index)) { return false; } if (!(lhs.witness == rhs.witness)) { return false; } return true; } inline std::vector StackItem::bincodeSerialize() const { auto serializer = serde::BincodeSerializer(); serde::Serializable::serialize(*this, serializer); return std::move(serializer).bytes(); } inline StackItem StackItem::bincodeDeserialize(std::vector input) { auto deserializer = serde::BincodeDeserializer(input); auto value = serde::Deserializable::deserialize(deserializer); if (deserializer.get_buffer_offset() < input.size()) { throw serde::deserialization_error("Some input bytes were not read"); } return value; } } // end of namespace WitnessStack template <> template void serde::Serializable::serialize(const WitnessStack::StackItem &obj, Serializer &serializer) { serializer.increase_container_depth(); serde::Serializable::serialize(obj.index, serializer); serde::Serializable::serialize(obj.witness, serializer); serializer.decrease_container_depth(); } template <> template WitnessStack::StackItem serde::Deserializable::deserialize(Deserializer &deserializer) { deserializer.increase_container_depth(); WitnessStack::StackItem obj; obj.index = serde::Deserializable::deserialize(deserializer); obj.witness = serde::Deserializable::deserialize(deserializer); deserializer.decrease_container_depth(); return obj; } namespace WitnessStack { inline bool operator==(const Witness &lhs, const Witness &rhs) { if (!(lhs.value == rhs.value)) { return false; } return true; } inline std::vector Witness::bincodeSerialize() const { auto serializer = serde::BincodeSerializer(); serde::Serializable::serialize(*this, serializer); return std::move(serializer).bytes(); } inline Witness Witness::bincodeDeserialize(std::vector input) { auto deserializer = serde::BincodeDeserializer(input); auto value = serde::Deserializable::deserialize(deserializer); if (deserializer.get_buffer_offset() < input.size()) { throw serde::deserialization_error("Some input bytes were not read"); } return value; } } // end of namespace WitnessStack template <> template void serde::Serializable::serialize(const WitnessStack::Witness &obj, Serializer &serializer) { serializer.increase_container_depth(); serde::Serializable::serialize(obj.value, serializer); serializer.decrease_container_depth(); } template <> template WitnessStack::Witness serde::Deserializable::deserialize(Deserializer &deserializer) { deserializer.increase_container_depth(); WitnessStack::Witness obj; obj.value = serde::Deserializable::deserialize(deserializer); deserializer.decrease_container_depth(); return obj; } namespace WitnessStack { inline bool operator==(const WitnessMap &lhs, const WitnessMap &rhs) { if (!(lhs.value == rhs.value)) { return false; } return true; } inline std::vector WitnessMap::bincodeSerialize() const { auto serializer = serde::BincodeSerializer(); serde::Serializable::serialize(*this, serializer); return std::move(serializer).bytes(); } inline WitnessMap WitnessMap::bincodeDeserialize(std::vector input) { auto deserializer = serde::BincodeDeserializer(input); auto value = serde::Deserializable::deserialize(deserializer); if (deserializer.get_buffer_offset() < input.size()) { throw serde::deserialization_error("Some input bytes were not read"); } return value; } } // end of namespace WitnessStack template <> template void serde::Serializable::serialize(const WitnessStack::WitnessMap &obj, Serializer &serializer) { serializer.increase_container_depth(); serde::Serializable::serialize(obj.value, serializer); serializer.decrease_container_depth(); } template <> template WitnessStack::WitnessMap serde::Deserializable::deserialize(Deserializer &deserializer) { deserializer.increase_container_depth(); WitnessStack::WitnessMap obj; obj.value = serde::Deserializable::deserialize(deserializer); deserializer.decrease_container_depth(); return obj; } namespace WitnessStack { inline bool operator==(const WitnessStack &lhs, const WitnessStack &rhs) { if (!(lhs.stack == rhs.stack)) { return false; } return true; } inline std::vector WitnessStack::bincodeSerialize() const { auto serializer = serde::BincodeSerializer(); serde::Serializable::serialize(*this, serializer); return std::move(serializer).bytes(); } inline WitnessStack WitnessStack::bincodeDeserialize(std::vector input) { auto deserializer = serde::BincodeDeserializer(input); auto value = serde::Deserializable::deserialize(deserializer); if (deserializer.get_buffer_offset() < input.size()) { throw serde::deserialization_error("Some input bytes were not read"); } return value; } } // end of namespace WitnessStack template <> template void serde::Serializable::serialize(const WitnessStack::WitnessStack &obj, Serializer &serializer) { serializer.increase_container_depth(); serde::Serializable::serialize(obj.stack, serializer); serializer.decrease_container_depth(); } template <> template WitnessStack::WitnessStack serde::Deserializable::deserialize(Deserializer &deserializer) { deserializer.increase_container_depth(); WitnessStack::WitnessStack obj; obj.stack = serde::Deserializable::deserialize(deserializer); deserializer.decrease_container_depth(); return obj; }