oc-mint  0.0.1
model.hpp
1 #ifndef MODEL_HPP
2 #define MODEL_HPP
3 
4 #include <chrono>
5 #include <memory>
6 #include <optional>
7 #include <string>
8 
9 #include "crow/json.h"
10 
11 #include "tl/expected.hpp"
12 #include "big_int.hpp"
13 
14 struct PublicKey {
15  BigInt modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
16  BigInt public_exponent;//: 65537,
17 
18  crow::json::wvalue to_json() const;
19 };
20 
21 struct WeightedUrl {
22  uint32_t weight;
23  std::string url;
24 
25  crow::json::wvalue to_json() const;
26 };
27 
29 struct CDD {
30 
31  std::string additional_info;
32  time_t cdd_expiry_date;//: 2023-07-22T15:45:53.164685
33  std::string cdd_location;//: https://opencent.org,
34  size_t cdd_serial;//: 1,
35  time_t cdd_signing_date;//: 2022-07-22T15:45:53.164685,
36  size_t currency_divisor;//: 100,
37  std::string currency_name;//: OpenCent,
38  std::vector<unsigned> denominations;//: [1, 2, 5],
39  BigInt id;//: 23ed956e629ba35f0002eaf833ea436aea7db5c2,
40 
41 std::vector<WeightedUrl> info_service;
42  /* eCipherSuite*/ std::string issuer_cipher_suite; //: RSA-SHA256-PSS-CHAUM82,
43  PublicKey
44  issuer_public_master_key; //: {
45  // modulus:
46  // daaa63ddda38c189b8c49020c8276adbe0a695685a...,
47  // public_exponent: 65537,
48  // type: rsa public key
49  //},
50  std::vector<WeightedUrl> mint_service;
51  std::string protocol_version; //: https://opencoin.org/1.0,
52  std::vector<WeightedUrl> redeem_service;
53  std::vector<WeightedUrl> renew_service;
54 
55  crow::json::wvalue to_json() const;
56 };
57 
58 struct CDDC {
59  CDD cdd;
60  std::string signature;
61 
62  crow::json::wvalue to_json() const;
63 };
64 
65 struct MintKey {
66  unsigned int cdd_serial;
67  std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685",
68  unsigned int denomination; //": 1,
69  BigInt id; // "1ceb977bb531c65f133ab8b0d60862b17369d96",
70  BigInt issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2",
71  PublicKey public_mint_key;
72 
73  std::string sign_coins_not_after;
74  std::string sign_coins_not_before;
75  // "type": "mint key"
76  crow::json::wvalue to_json() const;
77 };
78 
79 struct MintKeyCert {
80  MintKey mint_key;
81  std::string signature;
82 
83  crow::json::wvalue to_json() const;
84 };
85 
86 enum class eError {
87  JSON_PARSE_ERROR,
88  JSON_MISSING_KEY,
89  JSON_WRONG_REQUEST_TYPE,
90  JSON_WRONG_VALUE_TYPE,
91  JSON_ERROR,
92  NOT_IMPLEMENTED
93 };
94 
95 struct Response {
96  unsigned int message_reference;
97  unsigned int status_code;
98  std::string status_description;
99 
100  virtual crow::json::wvalue to_json() const;
101 };
102 
104  unsigned int message_reference;
106  static tl::expected<RequestCDDSerial, eError>
107  from_string(const std::string &str);
108 };
109 
111  unsigned int cdd_serial;
112 
113  crow::json::wvalue to_json() const override;
114 };
115 
116 struct RequestCDDC {
117  unsigned int cdd_serial;
118  unsigned int message_reference;
120  static tl::expected<RequestCDDC, eError> from_string(const std::string &str);
121 };
122 
124  CDDC cddc;
125 
126  crow::json::wvalue to_json() const override;
127 };
128 
129 struct RequestMKCs {
130  std::vector<unsigned int> denominations;
131  unsigned int message_reference;
133  std::vector<BigInt> mint_key_ids;
134  // "type": "request mint key certificates"
135  static tl::expected<RequestMKCs, eError> from_string(const std::string &str);
136 };
137 
139  std::vector<MintKeyCert> keys;
140 
141  crow::json::wvalue to_json() const override;
142 };
143 
144 struct Blind {
145  BigInt blinded_payload_hash; //bigint
146  BigInt mint_key_id; //bigint
147  std::string reference;
148  crow::json::wvalue to_json() const;
149  static tl::expected<Blind, eError> from_json(const crow::json::rvalue &json);
150 };
151 
153  BigInt blind_signature;
154  std::string reference;
155  crow::json::wvalue to_json() const;
156 };
157 
158 struct RequestMint {
159  unsigned int message_reference;
162  std::vector<Blind> blinds;
163  // "type": "request mint"
164  static tl::expected<RequestMint, eError> from_string(const std::string &str);
165 };
166 
168  std::vector<BlindSignature> blind_signatures;
169 
170  crow::json::wvalue to_json() const override;
171 };
172 
173 struct Coin {
174  struct Payload {
175  std::string cdd_location;
176  unsigned int denomination;
177  BigInt issuer_id;
178  BigInt mint_key_id;
179  std::string protocol_version;
180  BigInt serial;
181 
182  crow::json::wvalue to_json() const;
183  static tl::expected<Payload,eError> from_json(const crow::json::rvalue& json);
184  };
185 
186  Payload payload;
187  std::string signature;
188 
189  crow::json::wvalue to_json() const;
190  static tl::expected<Coin, eError> from_json(const crow::json::rvalue &json);
191 };
192 
193 struct CoinStack {
194  std::vector<Coin> coins;
195  std::string subject;
196  // "type": "coinstack"
197  crow::json::wvalue to_json() const;
198 };
199 
200 struct RequestRenew {
201  std::vector<Blind> blinds;
202  std::vector<Coin> coins;
203  unsigned int message_reference;
206  // "type": "request renew"
207  static tl::expected<RequestRenew, eError> from_string(const std::string &str);
208 };
209 
211  crow::json::wvalue to_json() const override;
212 };
213 
215  unsigned int message_reference;
218  // "type": "request resume"
219  static tl::expected<RequestResume, eError>
220  from_string(const std::string &str);
221 };
222 
224  std::vector<Coin> coins;
225  unsigned int message_reference;
227  // "type": "request redeem"
228  static tl::expected<RequestRedeem, eError>
229  from_string(const std::string &str);
230 };
231 
233  crow::json::wvalue to_json() const override;
234 };
235 
236 class Model {
237 public:
238  virtual ~Model(){};
239 
240  virtual tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) = 0;
241  virtual tl::expected<CDDC *, bool> getCurrentCDDC() = 0;
242 
243  virtual const std::vector<MintKeyCert>
244  getMKCs(const std::vector<unsigned int> &denominations,
245  const std::vector<BigInt> &mint_key_ids) = 0;
246 
247  virtual std::vector<BlindSignature>
248  mint(std::string const& transaction_reference,
249  const std::vector<Blind> &blinds) = 0;
250  virtual bool redeem(const std::vector<Coin> &coins) = 0;
251 
252  static std::unique_ptr<Model> getModel(const std::string &backend_name);
253 
254 private:
255 };
256 
257 #endif // #ifndef MODEL_HPP
Definition: model.hpp:236
Definition: big_int.hpp:9
Definition: model.hpp:152
Definition: model.hpp:144
Definition: model.hpp:58
Definition: model.hpp:29
Definition: model.hpp:193
Definition: model.hpp:174
Definition: model.hpp:173
Definition: model.hpp:79
Definition: model.hpp:65
Definition: model.hpp:14
Definition: model.hpp:116
static tl::expected< RequestCDDC, eError > from_string(const std::string &str)
Definition: model.cpp:139
unsigned int message_reference
The version of the CDD. (Int)
Definition: model.hpp:118
Definition: model.hpp:103
static tl::expected< RequestCDDSerial, eError > from_string(const std::string &str)
Definition: model.cpp:116
Definition: model.hpp:129
std::vector< BigInt > mint_key_ids
Definition: model.hpp:133
Definition: model.hpp:158
BigInt transaction_reference
Definition: model.hpp:161
Definition: model.hpp:223
static tl::expected< RequestRedeem, eError > from_string(const std::string &str)
Definition: model.cpp:439
Definition: model.hpp:200
std::string transaction_reference
Definition: model.hpp:205
Definition: model.hpp:214
BigInt transaction_reference
Definition: model.hpp:217
Definition: model.hpp:123
Definition: model.hpp:110
Definition: model.hpp:210
Definition: model.hpp:138
Definition: model.hpp:167
Definition: model.hpp:232
Definition: model.hpp:95
Definition: model.hpp:21