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