adding signature serialization and deserializiation

This commit is contained in:
Gabe Kaptchuk 2019-04-01 19:58:59 -04:00
parent d93210dd8a
commit 3bb34bbd1f
2 changed files with 344 additions and 0 deletions

327
include/ChannelClosure.h Normal file
View File

@ -0,0 +1,327 @@
#ifndef __CHANNELCLOSURE_H__
#define __CHANNELCLOSURE_H__
#include <cstring>
#include <vector>
#include <ostream>
#include <sstream>
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#define HEX_CHARS "0123456789abcdefABCDEF"
#define CHANNEL_CLOSURE_VECTOR_LENGTH 1426
#define CHANNEL_CLOSURE_HEX_STRING_LENGTH CHANNEL_CLOSURE_VECTOR_LENGTH*2
#define CHANNEL_CLOSURE_G2_LENGTH 129
#define CHANNEL_CLOSURE_A_ARRAY_SIZE 4 //2 <--- TODO GABE I thought these are supposed to be 2 long?
#define CHANNEL_CLOSURE_B_ARRAY_SIZE 4 //2
using namespace rapidjson;
class ChannelClosure : public std::vector<uint8_t> {
public:
std::string toJson() {
Document json;
// allocator for memory management
Document::AllocatorType& allocator = json.GetAllocator();
// define the document as an object rather than an array
json.SetObject();
if( CHANNEL_CLOSURE_VECTOR_LENGTH != this->size())
return("{}");
// iterator we are going to be using the whole time
std::vector<uint8_t>::iterator it = this->begin();
// Double check the a length
if( CHANNEL_CLOSURE_G2_LENGTH != *it )
return("{}");
it++;
Value a(kArrayType);
for( int j = 0; j< CHANNEL_CLOSURE_G2_LENGTH; j++)
{
a.PushBack(*it, allocator);
it++;
}
// Double check the b length
if( CHANNEL_CLOSURE_G2_LENGTH != *it )
return("{}");
it++;
Value b(kArrayType);
for( int j = 0; j< CHANNEL_CLOSURE_G2_LENGTH; j++)
{
b.PushBack(*it, allocator);
it++;
}
// Double check the c length
if( CHANNEL_CLOSURE_G2_LENGTH != *it )
return("{}");
it++;
Value c(kArrayType);
for( int j = 0; j< CHANNEL_CLOSURE_G2_LENGTH; j++)
{
c.PushBack(*it, allocator);
it++;
}
json.AddMember("a", a, allocator);
json.AddMember("b", b, allocator);
json.AddMember("c", c, allocator);
Value A(kArrayType);
// Check how many things there are
if( CHANNEL_CLOSURE_A_ARRAY_SIZE != *it )
return("{}");
it++;
// Check the size of the things
if( CHANNEL_CLOSURE_G2_LENGTH != *it )
return("{}");
it++;
for (int i =0; i <CHANNEL_CLOSURE_A_ARRAY_SIZE; i++)
{
Value temp(kArrayType);
for( int j = 0; j< CHANNEL_CLOSURE_G2_LENGTH; j++)
{
temp.PushBack(*it, allocator);
it++;
}
A.PushBack(temp, allocator);
}
Value B(kArrayType);
// Check how many things there are
if( CHANNEL_CLOSURE_B_ARRAY_SIZE != *it )
return("{}");
it++;
// Check the size of the things
if( CHANNEL_CLOSURE_G2_LENGTH != *it )
return("{}");
it++;
for (int i =0; i <CHANNEL_CLOSURE_A_ARRAY_SIZE; i++)
{
Value temp(kArrayType);
for( int j = 0; j< CHANNEL_CLOSURE_G2_LENGTH; j++)
{
temp.PushBack(*it, allocator);
it++;
}
B.PushBack(temp, allocator);
}
json.AddMember("A", A, allocator);
json.AddMember("B", B, allocator);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
json.Accept(writer);
return sb.GetString();
}
bool fromJson(std::string s) {
Document json;
json.Parse(s.c_str());
eraseAll();
// // Make sure we arent going to get an error when indexing into the JSON
if(!json.HasMember("a"))
return false;
if(!json.HasMember("b"))
return false;
if(!json.HasMember("c"))
return false;
if(!json.HasMember("A"))
return false;
if(!json.HasMember("B"))
return false;
const Value& a = json["a"];
const Value& b = json["b"];
const Value& c = json["c"];
const Value& A = json["A"];
const Value& B = json["B"];
if(!a.IsArray())
return false;
if(!b.IsArray())
return false;
if(!c.IsArray())
return false;
if(!A.IsArray())
return false;
if(!B.IsArray())
return false;
if(!(a.Size() == SizeType(CHANNEL_CLOSURE_G2_LENGTH)))
return false;
if(!(b.Size() == SizeType(CHANNEL_CLOSURE_G2_LENGTH)))
return false;
if(!(c.Size() == SizeType(CHANNEL_CLOSURE_G2_LENGTH)))
return false;
if(!(A.Size() == SizeType(CHANNEL_CLOSURE_A_ARRAY_SIZE)))
return false;
if(!(B.Size() == SizeType(CHANNEL_CLOSURE_B_ARRAY_SIZE)))
return false;
// Add the header information
// From here on out, make sure to call cleanupAndFalse() instead of false
// a
this->push_back(CHANNEL_CLOSURE_G2_LENGTH);
for(SizeType j = 0; j < a.Size(); j++)
this->push_back(a[j].GetUint64());
// b
this->push_back(CHANNEL_CLOSURE_G2_LENGTH);
for(SizeType j = 0; j < c.Size(); j++)
this->push_back(b[j].GetUint64());
// c
this->push_back(CHANNEL_CLOSURE_G2_LENGTH);
for(SizeType j = 0; j < c.Size(); j++)
this->push_back(c[j].GetUint64());
// A
this->push_back(CHANNEL_CLOSURE_A_ARRAY_SIZE);
this->push_back(CHANNEL_CLOSURE_G2_LENGTH);
for (SizeType i = 0; i < A.Size(); i++)
{
const Value& vec = A[i];
if(!vec.IsArray())
return cleanupAndFalse();
if(!(vec.Size() == SizeType(CHANNEL_CLOSURE_G2_LENGTH)))
return cleanupAndFalse();
for(SizeType j = 0; j < vec.Size(); j++)
{
this->push_back(vec[j].GetUint64());
}
}
// B
this->push_back(CHANNEL_CLOSURE_B_ARRAY_SIZE);
this->push_back(CHANNEL_CLOSURE_G2_LENGTH);
for (SizeType i = 0; i < B.Size(); i++)
{
const Value& vec = B[i];
if(!vec.IsArray())
return cleanupAndFalse();
if(!(vec.Size() == SizeType(CHANNEL_CLOSURE_G2_LENGTH)))
return cleanupAndFalse();
for(SizeType j = 0; j < vec.Size(); j++)
{
this->push_back(vec[j].GetUint64());
}
}
// Make sure the final length is good
if(!(this->size() == CHANNEL_CLOSURE_VECTOR_LENGTH))
return cleanupAndFalse();
return true;
}
// Stolen from https://github.com/zeutro/openabe/blob/master/src/include/openabe/utils/zbytestring.h
bool fromHex(std::string s) {
if((s.find_first_not_of(HEX_CHARS) != std::string::npos) ||
(s.size() % 2 != 0)) {
return false;
}
if ( s.length() != CHANNEL_CLOSURE_HEX_STRING_LENGTH)
return false;
std::string hex_str;
std::stringstream ss;
int tmp;
this->clear();
for (size_t i = 0; i < s.size(); i += 2) {
hex_str = s[i];
hex_str += s[i+1];
ss << hex_str;
ss >> std::hex >> tmp;
this->push_back(tmp & 0xFF);
ss.clear();
}
return true;
}
std::string toHex() const {
std::stringstream ss;
int hex_len = 2;
char hex[hex_len+1];
std::memset(hex, 0, hex_len+1);
for (std::vector<uint8_t>::const_iterator it = this->begin();
it != this->end(); ++it) {
sprintf(hex, "%02X", *it);
ss << hex;
}
return ss.str();
}
std::string toLowerHex() const {
std::stringstream ss;
int hex_len = 2;
char hex[hex_len+1];
std::memset(hex, 0, hex_len+1);
for (std::vector<uint8_t>::const_iterator it = this->begin() ; it != this->end(); ++it) {
sprintf(hex, "%02x", *it);
ss << hex;
}
return ss.str();
}
void eraseAll() {
this->erase(this->begin(), this->end());
}
private:
bool cleanupAndFalse() {
eraseAll();
return false;
}
};
#endif

View File

@ -5,6 +5,7 @@
#include "PublicKey.h"
#include "PublicParams.h"
#include "Wallet.h"
#include "ChannelClosure.h"
int main()
{
@ -87,5 +88,21 @@ int main()
std::cout << w.toJson() << std::endl;
std::string channel_closure = "810407e742f729215b4aa2456b78aeb502f8153442aa64ebec0f5a468e15bf2c5f2284ede6da4001b4d941745be42985d811d4d435c9d6f09489a18caf9a8ec1fd290125b1085d96535351662a35e09165cbdeb146cc6a49f9772332cbd4c24314d6571ac1f798c2bc2b4824d0bb03396f1be04b84ad6a7a51cc29833e8501cc3a98810407537ad2085a8c5a30825414f494360b81b773f31a4a646eee07725cfd9b12a70593a40ecaa54bce6f7668f66373967dde8cd0d77298706175d2f2130899e2fa06072488da670045ebf4b63cf4a8a8691fe26df666f278baa82aebbcd480c753e60363e94736bd7fdc9963afe30c60aec03e198ef7c7a6a8d2125db178abf772810403aa86d5f86a3b6a65800ae7568d39b69591ce855a435d7f5e9b1d3975664e605c70e4289d3a9ba7479c190b4ceb924b35be672d0ec6bb806b6faa7fc658dd9101da26bdfd5c19b95377b3edf62e24164650b32bb76db9173c21e59264d55b561cd89daa2083d4591205a250b742bc116bb555fe2d0837df998c6813f022dbfd048104062caa84a96615f0e1c11b37fe9919b76fb2ce108b88d37493df0f305cb52055105021231af48f935132252158203f85c075dbc4e1009356263bc6d08318a08f07bc4eb2da837ee89ddf91ff0fac086b7b0338cf8a7b1f00bf138fd66da5f21317b506a152f9aaba519e2a3037318bdbaab41121508b83b22b1de0f6e4db7d4e04035247d98e1fd1b330d4ebb5ccac8f7743b4a1d5e4bfbe95cb5e62b4ee2268cc1f2ed510489a9163ec76206f73a990505fe1d2dc9e1368f7259e2e764477d76a07cbec4d14ce7c0d727484389e25545a29790ccbc57a3c77ad173587c3bec6e2ef09022bc375ec60d929e7358c8ac33394f22d4318bb9b0065eef3a76bfaef830408c4f122a13aa0f10a0831fcabffbf63de2d9209e209723ea26a32e671fabfdd1f35bac4fcdf7a47780fe687f17cb3563df2448bb1f00073b223ea19a3eb9b5901358222c0244e10dee16ff87fe3ad405b740e9e3580235bffccd2f25fed9894073c5d06f0e057fe47a4c44368c620021c9f36e338125b188ed5938c129f31e60401a2ea8625fc205c2f17ca9bd1473c95474d39d9e96107a0f61a1bb79ae9e517413b62f769e8e1afd66b6913f0307060400cfd82a1661c963810f2db3bebeb4c030a79b4531197b8663027e07348b04193419305093927b9cb3fff49be638a4b35a4424b74614e5fdb3ac2dd87654c60fb73e9e1ec6e2bdf5192ec4aa1581d4b04810406e45ea5e454d881c7ee2a343c880e1aa384c3889500d8339de474c8f704e844e48cad63823b3138a37c8395e1899b36697035ad8adb032eec90e1a6d131063e02c429ebe47f8578cc0a60bc7d59dffc3ce54c1d7fdae7f47838a32baee6951b6ca7ddb6ebcff21cbae42ab915eeccb86ebd720a91f8f9d304fd9ebc5f03fd65040427f35fe2449f1df1895bcb85fd5b48966edcf410be958ed3208503c593ae24b122a2db2fe8f6cbeca6fe760d8fece344e2c59ea21f5f8378ee426255fed66f03742c6af1e7ef82c2d980feee6ba262673a02d5eda8fb5d5d64c7396e9cdd108dbf3bf23bca77975a8dbfbfa7f50021f5a952bf294b45ad50ae079161fecb1a04034520aa85afbfab4be559a163a6c4b9b6fb0f9eb8eeac0915a5511d5715f24f9c5791041a9f9ac9de9a0e2bb43c957d3024bb2e894e6c838a0c3562c4cbe09f06962003038e15d2063779cdd70e3bd110eb1880c0ac97039855a9daa12d5f1ddcad1824c0b592c16b5fed522f445ad45ce7de8fc86e0c9b94189b4b51bea5b004025b30cada48b99cb79891bbe84c352d562319ef5712d66a84e08f44ff785131339d98c0a069e598d192d10a21c457edacedea23f618ddb37e63319674ed3f5400fc4ccdd8724d047bfd9565d5bd0dd8803fb3049a3541a427f760690f4feac4259b2a4a69e1f5471af6b7223a6ab407ca697fb9b5cae47e6e28bae6f412cf7a";
ChannelClosure c;
assert(c.fromHex(channel_closure));
std::cout << c.toJson() << std::endl;
ChannelClosure c_from_json;
assert(c_from_json.fromJson(c.toJson()));
std::cout << c_from_json.toJson() << std::endl;
return 0;
}