2019-03-28 19:11:52 -07:00
|
|
|
#ifndef __CHANNELTOKEN_H__
|
|
|
|
#define __CHANNELTOKEN_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 VECTOR_LENGTH 810
|
|
|
|
#define HEX_STRING_LENGTH VECTOR_LENGTH*2
|
|
|
|
|
2019-03-28 22:21:46 -07:00
|
|
|
#define CHANNEL_TOKEN_NUM_BASE_POINTS 5
|
|
|
|
#define CHANNEL_TOKEN_LENGTH_POINT 129
|
|
|
|
#define CHANNEL_TOKEN_LENGTH_R 32
|
2019-03-28 19:11:52 -07:00
|
|
|
|
|
|
|
using namespace rapidjson;
|
|
|
|
|
|
|
|
class ChannelToken : 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();
|
|
|
|
|
|
|
|
// 810 bytes => [g-count] [g-len] [g-bytes_i] [g-bytes_count] [c-len] [c-bytes] [r-len] [r-bytes]
|
|
|
|
if( 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 g-count
|
2019-03-28 22:21:46 -07:00
|
|
|
if( CHANNEL_TOKEN_NUM_BASE_POINTS != *it )
|
2019-03-28 19:11:52 -07:00
|
|
|
return("{}");
|
|
|
|
it++;
|
|
|
|
|
|
|
|
// Double check the g-len
|
2019-03-28 22:21:46 -07:00
|
|
|
if( CHANNEL_TOKEN_LENGTH_POINT != *it )
|
2019-03-28 19:11:52 -07:00
|
|
|
return("{}");
|
|
|
|
it++;
|
|
|
|
|
|
|
|
Value params(kObjectType);
|
|
|
|
Value pub_bases(kArrayType);
|
|
|
|
|
2019-03-28 22:21:46 -07:00
|
|
|
for( int i = 0; i< CHANNEL_TOKEN_NUM_BASE_POINTS; i++)
|
2019-03-28 19:11:52 -07:00
|
|
|
{
|
|
|
|
Value base(kArrayType);
|
2019-03-28 22:21:46 -07:00
|
|
|
for( int j = 0; j< CHANNEL_TOKEN_LENGTH_POINT; j++)
|
2019-03-28 19:11:52 -07:00
|
|
|
{
|
|
|
|
base.PushBack(*it, allocator);
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
pub_bases.PushBack(base, allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
params.AddMember("pub_bases", pub_bases, allocator);
|
|
|
|
|
|
|
|
Value com(kObjectType);
|
|
|
|
|
|
|
|
// double check c-len
|
2019-03-28 22:21:46 -07:00
|
|
|
if( CHANNEL_TOKEN_LENGTH_POINT != *it )
|
2019-03-28 19:11:52 -07:00
|
|
|
return("{}");
|
|
|
|
it++;
|
|
|
|
|
|
|
|
Value c(kArrayType);
|
2019-03-28 22:21:46 -07:00
|
|
|
for( int j = 0; j< CHANNEL_TOKEN_LENGTH_POINT; j++)
|
2019-03-28 19:11:52 -07:00
|
|
|
{
|
|
|
|
c.PushBack(*it, allocator);
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
com.AddMember("c",c,allocator);
|
|
|
|
|
|
|
|
// double check r-len
|
2019-03-28 22:21:46 -07:00
|
|
|
if( CHANNEL_TOKEN_LENGTH_R != *it )
|
2019-03-28 19:11:52 -07:00
|
|
|
return("{}");
|
|
|
|
it++;
|
|
|
|
|
|
|
|
Value r(kArrayType);
|
2019-03-28 22:21:46 -07:00
|
|
|
for( int j = 0; j< CHANNEL_TOKEN_LENGTH_R; j++)
|
2019-03-28 19:11:52 -07:00
|
|
|
{
|
|
|
|
r.PushBack(*it, allocator);
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
com.AddMember("r",r,allocator);
|
|
|
|
|
|
|
|
// build final json string
|
|
|
|
json.AddMember("com", com, allocator);
|
|
|
|
json.AddMember("params", params, 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("params"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Value& params = json["params"];
|
|
|
|
|
|
|
|
if(!params.IsObject())
|
|
|
|
return false;
|
|
|
|
if(!params.HasMember("pub_bases"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
const Value& pub_bases = params["pub_bases"];
|
|
|
|
|
|
|
|
if(!pub_bases.IsArray())
|
|
|
|
return false;
|
2019-03-28 22:21:46 -07:00
|
|
|
if(!(pub_bases.Size() == SizeType(CHANNEL_TOKEN_NUM_BASE_POINTS)))
|
2019-03-28 19:11:52 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
//Checking the formatting in com ahead of time before we edit the internal vector
|
|
|
|
if(!json.HasMember("com"))
|
|
|
|
return false;
|
|
|
|
const Value& com = json["com"];
|
|
|
|
|
|
|
|
if(!com.IsObject())
|
|
|
|
return false;
|
|
|
|
if(!com.HasMember("c"))
|
|
|
|
return false;
|
|
|
|
if(!com.HasMember("r"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Value& c = com["c"];
|
|
|
|
const Value& r = com["r"];
|
|
|
|
|
|
|
|
if(!c.IsArray())
|
|
|
|
return false;
|
|
|
|
if(!r.IsArray())
|
|
|
|
return false;
|
|
|
|
|
2019-03-28 22:21:46 -07:00
|
|
|
if(!(c.Size() == SizeType(CHANNEL_TOKEN_LENGTH_POINT)))
|
2019-03-28 19:11:52 -07:00
|
|
|
return false;
|
2019-03-28 22:21:46 -07:00
|
|
|
if(!(r.Size() == SizeType(CHANNEL_TOKEN_LENGTH_R)))
|
2019-03-28 19:11:52 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Add the header information
|
|
|
|
// From here on out, make sure to call cleanupAndFalse() instead of false
|
2019-03-28 22:21:46 -07:00
|
|
|
this->push_back(CHANNEL_TOKEN_NUM_BASE_POINTS);
|
|
|
|
this->push_back(CHANNEL_TOKEN_LENGTH_POINT);
|
2019-03-28 19:11:52 -07:00
|
|
|
|
|
|
|
for (SizeType i = 0; i < pub_bases.Size(); i++)
|
|
|
|
{
|
|
|
|
const Value& basepoint = pub_bases[i];
|
|
|
|
if(!basepoint.IsArray())
|
|
|
|
return cleanupAndFalse();
|
2019-03-28 22:21:46 -07:00
|
|
|
if(!(basepoint.Size() == SizeType(CHANNEL_TOKEN_LENGTH_POINT)))
|
2019-03-28 19:11:52 -07:00
|
|
|
return cleanupAndFalse();
|
|
|
|
|
|
|
|
for(SizeType j = 0; j < basepoint.Size(); j++)
|
|
|
|
{
|
|
|
|
this->push_back(basepoint[j].GetUint64());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:21:46 -07:00
|
|
|
this->push_back(CHANNEL_TOKEN_LENGTH_POINT);
|
2019-03-28 19:11:52 -07:00
|
|
|
|
|
|
|
for(SizeType j = 0; j < c.Size(); j++)
|
|
|
|
this->push_back(c[j].GetUint64());
|
|
|
|
|
2019-03-28 22:21:46 -07:00
|
|
|
this->push_back(CHANNEL_TOKEN_LENGTH_R);
|
2019-03-28 19:11:52 -07:00
|
|
|
|
|
|
|
for(SizeType j = 0; j < r.Size(); j++)
|
|
|
|
this->push_back(r[j].GetUint64());
|
|
|
|
|
|
|
|
// Make sure the final length is good
|
|
|
|
if(!(this->size() == 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() != 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
|