Skip to content

Commit

Permalink
Resolves #714 (#1076)
Browse files Browse the repository at this point in the history
Resolves #1058
  • Loading branch information
brandonlehmann committed Jul 30, 2020
1 parent fdb139f commit 4312e83
Show file tree
Hide file tree
Showing 22 changed files with 1,617 additions and 19,934 deletions.
18,912 changes: 0 additions & 18,912 deletions external/nlohmann-json/json.hpp

This file was deleted.

108 changes: 63 additions & 45 deletions include/CryptoNote.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#pragma once

#include "CryptoTypes.h"
#include "json.hpp"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"

Expand All @@ -25,8 +24,49 @@ namespace CryptoNote
struct KeyInput
{
uint64_t amount;

std::vector<uint32_t> outputIndexes;

Crypto::KeyImage keyImage;

void toJSON(rapidjson::Writer<rapidjson::StringBuffer> &writer) const
{
writer.StartObject();
{
writer.Key("amount");
writer.Uint64(amount);

writer.Key("key_offsets");
writer.StartArray();
{
for (const auto &offset : outputIndexes)
{
writer.Uint(offset);
}
}
writer.EndArray();

writer.Key("k_image");
keyImage.toJSON(writer);
}
writer.EndObject();
}

void fromJSON(const JSONValue &j)
{
amount = getUint64FromJSON(j, "amount");

outputIndexes.clear();

for (const auto &offset : getArrayFromJSON(j, "key_offsets"))
{
uint32_t index = getUintFromJSON(offset);

outputIndexes.push_back(index);
}

keyImage.fromString(getStringFromJSON(j, "k_image"));
}
};

struct KeyOutput
Expand All @@ -41,15 +81,20 @@ namespace CryptoNote
struct TransactionOutput
{
uint64_t amount;

TransactionOutputTarget target;
};

struct TransactionPrefix
{
uint8_t version;

uint64_t unlockTime;

std::vector<TransactionInput> inputs;

std::vector<TransactionOutput> outputs;

std::vector<uint8_t> extra;
};

Expand All @@ -65,46 +110,62 @@ namespace CryptoNote
struct ParentBlock
{
uint8_t majorVersion;

uint8_t minorVersion;

Crypto::Hash previousBlockHash;

uint16_t transactionCount;

std::vector<Crypto::Hash> baseTransactionBranch;

BaseTransaction baseTransaction;

std::vector<Crypto::Hash> blockchainBranch;
};

struct BlockHeader
{
uint8_t majorVersion;

uint8_t minorVersion;

uint32_t nonce;

uint64_t timestamp;

Crypto::Hash previousBlockHash;
};

struct BlockTemplate : public BlockHeader
{
ParentBlock parentBlock;

Transaction baseTransaction;

std::vector<Crypto::Hash> transactionHashes;
};

struct AccountPublicAddress
{
Crypto::PublicKey spendPublicKey;

Crypto::PublicKey viewPublicKey;
};

struct AccountKeys
{
AccountPublicAddress address;

Crypto::SecretKey spendSecretKey;

Crypto::SecretKey viewSecretKey;
};

struct KeyPair
{
Crypto::PublicKey publicKey;

Crypto::SecretKey secretKey;
};

Expand All @@ -113,6 +174,7 @@ namespace CryptoNote
struct RawBlock
{
BinaryArray block; // BlockTemplate

std::vector<BinaryArray> transactions;

void toJSON(rapidjson::Writer<rapidjson::StringBuffer> &writer) const
Expand Down Expand Up @@ -145,48 +207,4 @@ namespace CryptoNote
}
}
};

inline void to_json(nlohmann::json &j, const CryptoNote::KeyInput &k)
{
j = {{"amount", k.amount}, {"key_offsets", k.outputIndexes}, {"k_image", k.keyImage}};
}

inline void from_json(const nlohmann::json &j, CryptoNote::KeyInput &k)
{
k.amount = j.at("amount").get<uint64_t>();
if (j.find("key_offsets") != j.end())
{
k.outputIndexes = j.at("key_offsets").get<std::vector<uint32_t>>();
}
k.keyImage = j.at("k_image").get<Crypto::KeyImage>();
}

inline void to_json(nlohmann::json &j, const CryptoNote::RawBlock &block)
{
std::vector<std::string> transactions;

for (const auto &transaction : block.transactions)
{
transactions.push_back(Common::toHex(transaction));
}

j = {{"block", Common::toHex(block.block)}, {"transactions", transactions}};
}

inline void from_json(const nlohmann::json &j, CryptoNote::RawBlock &block)
{
block.transactions.clear();

std::string blockString = j.at("block").get<std::string>();

block.block = Common::fromHex(blockString);

std::vector<std::string> transactions = j.at("transactions").get<std::vector<std::string>>();

for (const auto &transaction : transactions)
{
block.transactions.push_back(Common::fromHex(transaction));
}
}

} // namespace CryptoNote
Loading

0 comments on commit 4312e83

Please sign in to comment.