diff options
Diffstat (limited to 'src/mint/mint_db.h')
-rw-r--r-- | src/mint/mint_db.h | 344 |
1 files changed, 344 insertions, 0 deletions
diff --git a/src/mint/mint_db.h b/src/mint/mint_db.h new file mode 100644 index 000000000..4f47aac1c --- /dev/null +++ b/src/mint/mint_db.h | |||
@@ -0,0 +1,344 @@ | |||
1 | /* | ||
2 | This file is part of TALER | ||
3 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | TALER is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file mint/mint_db.h | ||
19 | * @brief Mint-specific database access | ||
20 | * @author Florian Dold | ||
21 | */ | ||
22 | |||
23 | #ifndef _NEURO_MINT_DB_H | ||
24 | #define _NEURO_MINT_DB_H | ||
25 | |||
26 | #include <libpq-fe.h> | ||
27 | #include <gnunet/gnunet_util_lib.h> | ||
28 | #include "taler_util.h" | ||
29 | #include "taler_types.h" | ||
30 | #include "taler_rsa.h" | ||
31 | |||
32 | |||
33 | /** | ||
34 | * Reserve row. Corresponds to table 'reserves' in | ||
35 | * the mint's database. | ||
36 | */ | ||
37 | struct Reserve | ||
38 | { | ||
39 | /** | ||
40 | * Signature over the purse. | ||
41 | * Only valid if (blind_session_missing==GNUNET_YES). | ||
42 | */ | ||
43 | struct GNUNET_CRYPTO_EddsaSignature status_sig; | ||
44 | /** | ||
45 | * Signature with purpose TALER_SIGNATURE_PURSE. | ||
46 | * Only valid if (blind_session_missing==GNUNET_YES). | ||
47 | */ | ||
48 | struct GNUNET_CRYPTO_EccSignaturePurpose status_sig_purpose; | ||
49 | /** | ||
50 | * Signing key used to sign the purse. | ||
51 | * Only valid if (blind_session_missing==GNUNET_YES). | ||
52 | */ | ||
53 | struct GNUNET_CRYPTO_EddsaPublicKey status_sign_pub; | ||
54 | /** | ||
55 | * Withdraw public key, identifies the purse. | ||
56 | * Only the customer knows the corresponding private key. | ||
57 | */ | ||
58 | struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub; | ||
59 | /** | ||
60 | * Remaining balance in the purse. | ||
61 | */ | ||
62 | struct TALER_AmountNBO balance; | ||
63 | |||
64 | /** | ||
65 | * Expiration date for the purse. | ||
66 | */ | ||
67 | struct GNUNET_TIME_AbsoluteNBO expiration; | ||
68 | }; | ||
69 | |||
70 | |||
71 | struct CollectableBlindcoin | ||
72 | { | ||
73 | struct TALER_RSA_BlindedSignaturePurpose ev; | ||
74 | struct TALER_RSA_Signature ev_sig; | ||
75 | struct TALER_RSA_PublicKeyBinaryEncoded denom_pub; | ||
76 | struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub; | ||
77 | struct GNUNET_CRYPTO_EddsaSignature reserve_sig; | ||
78 | }; | ||
79 | |||
80 | |||
81 | struct RefreshSession | ||
82 | { | ||
83 | int has_commit_sig; | ||
84 | struct GNUNET_CRYPTO_EddsaSignature commit_sig; | ||
85 | struct GNUNET_CRYPTO_EddsaPublicKey session_pub; | ||
86 | uint16_t num_oldcoins; | ||
87 | uint16_t num_newcoins; | ||
88 | uint16_t kappa; | ||
89 | uint16_t noreveal_index; | ||
90 | uint8_t reveal_ok; | ||
91 | }; | ||
92 | |||
93 | |||
94 | #define TALER_REFRESH_SHARED_SECRET_LENGTH (sizeof (struct GNUNET_HashCode)) | ||
95 | #define TALER_REFRESH_LINK_LENGTH (sizeof (struct LinkData)) | ||
96 | |||
97 | struct RefreshCommitLink | ||
98 | { | ||
99 | struct GNUNET_CRYPTO_EddsaPublicKey session_pub; | ||
100 | struct GNUNET_CRYPTO_EcdsaPublicKey transfer_pub; | ||
101 | uint16_t cnc_index; | ||
102 | uint16_t oldcoin_index; | ||
103 | char shared_secret_enc[sizeof (struct GNUNET_HashCode)]; | ||
104 | }; | ||
105 | |||
106 | struct LinkData | ||
107 | { | ||
108 | struct GNUNET_CRYPTO_EcdsaPrivateKey coin_priv; | ||
109 | struct TALER_RSA_BlindingKeyBinaryEncoded bkey_enc; | ||
110 | }; | ||
111 | |||
112 | |||
113 | GNUNET_NETWORK_STRUCT_BEGIN | ||
114 | |||
115 | struct SharedSecretEnc | ||
116 | { | ||
117 | char data[TALER_REFRESH_SHARED_SECRET_LENGTH]; | ||
118 | }; | ||
119 | |||
120 | |||
121 | struct LinkDataEnc | ||
122 | { | ||
123 | char data[sizeof (struct LinkData)]; | ||
124 | }; | ||
125 | |||
126 | GNUNET_NETWORK_STRUCT_END | ||
127 | |||
128 | struct RefreshCommitCoin | ||
129 | { | ||
130 | struct GNUNET_CRYPTO_EddsaPublicKey session_pub; | ||
131 | struct TALER_RSA_BlindedSignaturePurpose coin_ev; | ||
132 | uint16_t cnc_index; | ||
133 | uint16_t newcoin_index; | ||
134 | char link_enc[sizeof (struct LinkData)]; | ||
135 | }; | ||
136 | |||
137 | |||
138 | struct KnownCoin | ||
139 | { | ||
140 | struct TALER_CoinPublicInfo public_info; | ||
141 | struct TALER_Amount expended_balance; | ||
142 | int is_refreshed; | ||
143 | /** | ||
144 | * Refreshing session, only valid if | ||
145 | * is_refreshed==1. | ||
146 | */ | ||
147 | struct GNUNET_CRYPTO_EddsaPublicKey refresh_session_pub; | ||
148 | }; | ||
149 | |||
150 | GNUNET_NETWORK_STRUCT_BEGIN | ||
151 | |||
152 | struct Deposit | ||
153 | { | ||
154 | /* FIXME: should be TALER_CoinPublicInfo */ | ||
155 | struct GNUNET_CRYPTO_EddsaPublicKey coin_pub; | ||
156 | struct TALER_RSA_PublicKeyBinaryEncoded denom_pub; | ||
157 | struct TALER_RSA_Signature coin_sig; | ||
158 | struct TALER_RSA_SignaturePurpose purpose; | ||
159 | uint64_t transaction_id; | ||
160 | struct TALER_AmountNBO amount; | ||
161 | struct GNUNET_CRYPTO_EddsaPublicKey merchant_pub; | ||
162 | struct GNUNET_HashCode h_contract; | ||
163 | struct GNUNET_HashCode h_wire; | ||
164 | /* TODO: uint16_t wire_size */ | ||
165 | char wire[]; /* string encoded wire JSON object */ | ||
166 | }; | ||
167 | |||
168 | GNUNET_NETWORK_STRUCT_END | ||
169 | |||
170 | int | ||
171 | TALER_MINT_DB_prepare (PGconn *db_conn); | ||
172 | |||
173 | int | ||
174 | TALER_MINT_DB_get_collectable_blindcoin (PGconn *db_conn, | ||
175 | struct TALER_RSA_BlindedSignaturePurpose *blind_ev, | ||
176 | struct CollectableBlindcoin *collectable); | ||
177 | |||
178 | int | ||
179 | TALER_MINT_DB_insert_collectable_blindcoin (PGconn *db_conn, | ||
180 | const struct CollectableBlindcoin *collectable); | ||
181 | |||
182 | |||
183 | int | ||
184 | TALER_MINT_DB_rollback (PGconn *db_conn); | ||
185 | |||
186 | |||
187 | int | ||
188 | TALER_MINT_DB_transaction (PGconn *db_conn); | ||
189 | |||
190 | |||
191 | int | ||
192 | TALER_MINT_DB_commit (PGconn *db_conn); | ||
193 | |||
194 | |||
195 | int | ||
196 | TALER_MINT_DB_get_reserve (PGconn *db_conn, | ||
197 | const struct GNUNET_CRYPTO_EddsaPublicKey *reserve_pub, | ||
198 | struct Reserve *reserve_res); | ||
199 | |||
200 | int | ||
201 | TALER_MINT_DB_update_reserve (PGconn *db_conn, | ||
202 | const struct Reserve *reserve, | ||
203 | int fresh); | ||
204 | |||
205 | |||
206 | int | ||
207 | TALER_MINT_DB_insert_refresh_order (PGconn *db_conn, | ||
208 | uint16_t newcoin_index, | ||
209 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub, | ||
210 | const struct TALER_RSA_PublicKeyBinaryEncoded *denom_pub); | ||
211 | |||
212 | int | ||
213 | TALER_MINT_DB_get_refresh_session (PGconn *db_conn, | ||
214 | const struct GNUNET_CRYPTO_EddsaPublicKey *refresh_session_pub, | ||
215 | struct RefreshSession *r_session); | ||
216 | |||
217 | |||
218 | int | ||
219 | TALER_MINT_DB_get_known_coin (PGconn *db_conn, struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub, | ||
220 | struct KnownCoin *known_coin); | ||
221 | |||
222 | |||
223 | int | ||
224 | TALER_MINT_DB_upsert_known_coin (PGconn *db_conn, struct KnownCoin *known_coin); | ||
225 | |||
226 | |||
227 | int | ||
228 | TALER_MINT_DB_insert_refresh_commit_link (PGconn *db_conn, struct RefreshCommitLink *commit_link); | ||
229 | |||
230 | int | ||
231 | TALER_MINT_DB_insert_refresh_commit_coin (PGconn *db_conn, struct RefreshCommitCoin *commit_coin); | ||
232 | |||
233 | |||
234 | int | ||
235 | TALER_MINT_DB_get_refresh_commit_link (PGconn *db_conn, | ||
236 | const struct GNUNET_CRYPTO_EddsaPublicKey *refresh_session_pub, | ||
237 | int i, int j, | ||
238 | struct RefreshCommitLink *commit_link); | ||
239 | |||
240 | |||
241 | int | ||
242 | TALER_MINT_DB_get_refresh_commit_coin (PGconn *db_conn, | ||
243 | const struct GNUNET_CRYPTO_EddsaPublicKey *refresh_session_pub, | ||
244 | int i, int j, | ||
245 | struct RefreshCommitCoin *commit_coin); | ||
246 | |||
247 | |||
248 | int | ||
249 | TALER_MINT_DB_create_refresh_session (PGconn *db_conn, | ||
250 | const struct GNUNET_CRYPTO_EddsaPublicKey | ||
251 | *session_pub); | ||
252 | |||
253 | |||
254 | int | ||
255 | TALER_MINT_DB_get_refresh_order (PGconn *db_conn, | ||
256 | uint16_t newcoin_index, | ||
257 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub, | ||
258 | struct TALER_RSA_PublicKeyBinaryEncoded *denom_pub); | ||
259 | |||
260 | |||
261 | int | ||
262 | TALER_MINT_DB_insert_refresh_collectable (PGconn *db_conn, | ||
263 | uint16_t newcoin_index, | ||
264 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub, | ||
265 | const struct TALER_RSA_Signature *ev_sig); | ||
266 | int | ||
267 | TALER_MINT_DB_get_refresh_collectable (PGconn *db_conn, | ||
268 | uint16_t newcoin_index, | ||
269 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub, | ||
270 | struct TALER_RSA_Signature *ev_sig); | ||
271 | int | ||
272 | TALER_MINT_DB_set_reveal_ok (PGconn *db_conn, | ||
273 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub); | ||
274 | |||
275 | int | ||
276 | TALER_MINT_DB_insert_refresh_melt (PGconn *db_conn, | ||
277 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub, | ||
278 | uint16_t oldcoin_index, | ||
279 | const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub, | ||
280 | const struct TALER_RSA_PublicKeyBinaryEncoded *denom_pub); | ||
281 | |||
282 | |||
283 | int | ||
284 | TALER_MINT_DB_get_refresh_melt (PGconn *db_conn, | ||
285 | const struct GNUNET_CRYPTO_EddsaPublicKey *session_pub, | ||
286 | uint16_t oldcoin_index, | ||
287 | struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub); | ||
288 | |||
289 | |||
290 | typedef | ||
291 | int (*LinkIterator) (void *cls, | ||
292 | const struct LinkDataEnc *link_data_enc, | ||
293 | const struct TALER_RSA_PublicKeyBinaryEncoded *denom_pub, | ||
294 | const struct TALER_RSA_Signature *ev_sig); | ||
295 | |||
296 | int | ||
297 | TALER_db_get_link (PGconn *db_conn, | ||
298 | const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub, | ||
299 | LinkIterator link_iter, | ||
300 | void *cls); | ||
301 | |||
302 | |||
303 | int | ||
304 | TALER_db_get_transfer (PGconn *db_conn, | ||
305 | const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub, | ||
306 | struct GNUNET_CRYPTO_EcdsaPublicKey *transfer_pub, | ||
307 | struct SharedSecretEnc *shared_secret_enc); | ||
308 | |||
309 | int | ||
310 | TALER_MINT_DB_init_deposits (PGconn *db_conn, int temporary); | ||
311 | |||
312 | int | ||
313 | TALER_MINT_DB_prepare_deposits (PGconn *db_conn); | ||
314 | |||
315 | int | ||
316 | TALER_MINT_DB_insert_deposit (PGconn *db_conn, | ||
317 | const struct Deposit *deposit); | ||
318 | |||
319 | int | ||
320 | TALER_MINT_DB_get_deposit (PGconn *db_conn, | ||
321 | const struct GNUNET_CRYPTO_EddsaPublicKey *coin_pub, | ||
322 | struct Deposit **r_deposit); | ||
323 | int | ||
324 | TALER_MINT_DB_insert_known_coin (PGconn *db_conn, | ||
325 | const struct KnownCoin *known_coin); | ||
326 | |||
327 | |||
328 | |||
329 | /** | ||
330 | * Get the thread-local database-handle. | ||
331 | * Connect to the db if the connection does not exist yet. | ||
332 | * | ||
333 | * @param the database connection, or NULL on error | ||
334 | */ | ||
335 | PGconn * | ||
336 | TALER_MINT_DB_get_connection (void); | ||
337 | |||
338 | |||
339 | int | ||
340 | TALER_MINT_DB_init (const char *connection_cfg); | ||
341 | |||
342 | |||
343 | |||
344 | #endif /* _NEURO_MINT_DB_H */ | ||