-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.hpp
More file actions
118 lines (90 loc) · 2.81 KB
/
Copy pathTransaction.hpp
File metadata and controls
118 lines (90 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// RootCoin lib
/* List of ROOTcoin primitives:
* - Account
* - Address
* - Public Key
* - Private Key
* - Transaction amount
*/
// Building a cryptocurrency from first principles
// ROOTCoin
#pragma once
// transaction.hpp
#include<vector>
#include<cstring>
#include<cassert>
#include<memory>
#include<string>
// Cryptolib components
#include "Uint256.hpp"
#include "Sha256.hpp"
#include "CurvePoint.hpp"
#include "FieldInt.hpp"
#include "Ecdsa.hpp"
// Rootcoin tools
#include "Utils.hpp"
using std::uint8_t;
using std::vector;
using std::string;
using std::unique_ptr;
typedef vector<uint8_t> Bytes;
struct Signature {
Uint256 r; // output
Uint256 s; //
Signature(Uint256& r, Uint256& s);
Signature();
Bytes toBytes() const;
};
struct PrivateKey {
const Uint256 value;
PrivateKey(const Uint256 val);
PrivateKey(const char* key);
//PrivateKey();
Bytes toBytes() const;
const Uint256 get() const;
};
struct PublicKey {
CurvePoint curvePoint;
PublicKey(const Bytes &keyBytes);
PublicKey(const char* address);
PublicKey(const CurvePoint &cp);
Bytes toBytes() const;
const string toString() const;
bool operator ==(const PublicKey &key) const;
};
class Transaction {
/*--- Fields ---*/
public:
PublicKey fromKey; // sending address
PublicKey toKey; // recipient address
int amount; // amount to send
Signature signature; // @fix why is this a pointer
/*--- Constructors ---*/
Transaction(const Bytes &fromAddr, const Bytes &toAddr, int sendAmount);
Transaction(const char *fromAddr, const char *toAddr, int sendAmount); // UNTESTED
Transaction(const PublicKey &from, const PublicKey &to, int sendAmount);
Transaction(); // @fix should never be used
// Copy constructor
Transaction(const Transaction& transaction);
/*--- Public instance methods ---*/
// Calculate ECDSA signature
public: Signature sign(Uint256 privateKey);
// how to verify API isn't storing PK?
// Verify transaction origin
public: bool verify(const Signature &sig) const;
// Calculate hash of serialized tx data (message hash for transaction contents)
public: Sha256Hash getHash() const;
// Serialize transaction fields (for hash calculation)
public: vector<uint8_t> serialize() const;
/*--- Helper methods ---*/
public: static vector<uint8_t> serializeCurvePoint(CurvePoint curvePoint) ;
public: static unique_ptr<CurvePoint> deserializeCurvePoint(const vector<uint8_t> bytes) ;
public: static unique_ptr<CurvePoint> deserializeCurvePoint(const char* curvePointStr) ;
//public: static bool verify(char* from, char* to, Signature signature) ;
/*--- Public static constants ---*/
public: static const Uint256 DEFAULT_NONCE;
public: static const size_t KEY_SIZE;
public: static const size_t ADDRESS_SIZE;
// Static const factories --- (static initialization fiasco)
public: static size_t TX_SIZE();
};