Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2018 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it
6 : under the terms of the GNU General Public License as published
7 : by the Free Software Foundation; either version 3, or (at your
8 : option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not,
17 : see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing/testing_api_cmd_insert_deposit.c
21 : * @brief deposit a coin directly into the database.
22 : * @author Marcello Stanisci
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include "taler_util.h"
27 : #include "taler_json_lib.h"
28 : #include <gnunet/gnunet_curl_lib.h>
29 : #include "taler_signatures.h"
30 : #include "taler_testing_lib.h"
31 : #include "taler_exchangedb_plugin.h"
32 :
33 :
34 : /**
35 : * State for a "insert-deposit" CMD.
36 : */
37 : struct InsertDepositState
38 : {
39 : /**
40 : * Configuration file used by the command.
41 : */
42 : const struct TALER_TESTING_DatabaseConnection *dbc;
43 :
44 : /**
45 : * Human-readable name of the shop.
46 : */
47 : const char *merchant_name;
48 :
49 : /**
50 : * Merchant account name (NOT a payto-URI).
51 : */
52 : const char *merchant_account;
53 :
54 : /**
55 : * Deadline before which the aggregator should
56 : * send the payment to the merchant.
57 : */
58 : struct GNUNET_TIME_Relative wire_deadline;
59 :
60 : /**
61 : * When did the exchange receive the deposit?
62 : */
63 : struct GNUNET_TIME_Timestamp exchange_timestamp;
64 :
65 : /**
66 : * Amount to deposit, inclusive of deposit fee.
67 : */
68 : const char *amount_with_fee;
69 :
70 : /**
71 : * Deposit fee.
72 : */
73 : const char *deposit_fee;
74 : };
75 :
76 : /**
77 : * Setup (fake) information about a coin used in deposit.
78 : *
79 : * @param[out] issue information to initialize with "valid" data
80 : */
81 : static void
82 0 : fake_issue (struct TALER_EXCHANGEDB_DenominationKeyInformation *issue)
83 : {
84 : struct GNUNET_TIME_Timestamp now;
85 :
86 0 : memset (issue,
87 : 0,
88 : sizeof (*issue));
89 0 : now = GNUNET_TIME_timestamp_get ();
90 : issue->start
91 0 : = now;
92 : issue->expire_withdraw
93 0 : = GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_MINUTES);
94 : issue->expire_deposit
95 0 : = GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_HOURS);
96 : issue->expire_legal
97 0 : = GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_DAYS);
98 0 : GNUNET_assert (GNUNET_OK ==
99 : TALER_string_to_amount ("EUR:1",
100 : &issue->value));
101 0 : GNUNET_assert (GNUNET_OK ==
102 : TALER_string_to_amount ("EUR:0.1",
103 : &issue->fees.withdraw));
104 0 : GNUNET_assert (GNUNET_OK ==
105 : TALER_string_to_amount ("EUR:0.1",
106 : &issue->fees.deposit));
107 0 : GNUNET_assert (GNUNET_OK ==
108 : TALER_string_to_amount ("EUR:0.1",
109 : &issue->fees.refresh));
110 0 : GNUNET_assert (GNUNET_OK ==
111 : TALER_string_to_amount ("EUR:0.1",
112 : &issue->fees.refund));
113 0 : }
114 :
115 :
116 : /**
117 : * Run the command.
118 : *
119 : * @param cls closure.
120 : * @param cmd the commaind being run.
121 : * @param is interpreter state.
122 : */
123 : static void
124 0 : insert_deposit_run (void *cls,
125 : const struct TALER_TESTING_Command *cmd,
126 : struct TALER_TESTING_Interpreter *is)
127 : {
128 0 : struct InsertDepositState *ids = cls;
129 : struct TALER_EXCHANGEDB_Deposit deposit;
130 : struct TALER_MerchantPrivateKeyP merchant_priv;
131 : struct TALER_EXCHANGEDB_DenominationKeyInformation issue;
132 : struct TALER_DenominationPublicKey dpk;
133 : struct TALER_DenominationPrivateKey denom_priv;
134 :
135 : (void) cmd;
136 : // prepare and store issue first.
137 0 : fake_issue (&issue);
138 0 : GNUNET_assert (GNUNET_OK ==
139 : TALER_denom_priv_create (&denom_priv,
140 : &dpk,
141 : TALER_DENOMINATION_RSA,
142 : 1024));
143 0 : TALER_denom_pub_hash (&dpk,
144 : &issue.denom_hash);
145 :
146 0 : if ( (GNUNET_OK !=
147 0 : ids->dbc->plugin->start (ids->dbc->plugin->cls,
148 0 : "talertestinglib: denomination insertion")) ||
149 : (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
150 0 : ids->dbc->plugin->insert_denomination_info (ids->dbc->plugin->cls,
151 : &dpk,
152 0 : &issue)) ||
153 : (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
154 0 : ids->dbc->plugin->commit (ids->dbc->plugin->cls)) )
155 : {
156 0 : TALER_TESTING_interpreter_fail (is);
157 0 : TALER_denom_pub_free (&dpk);
158 0 : TALER_denom_priv_free (&denom_priv);
159 0 : return;
160 : }
161 :
162 : /* prepare and store deposit now. */
163 0 : memset (&deposit,
164 : 0,
165 : sizeof (deposit));
166 :
167 0 : GNUNET_assert (
168 : GNUNET_YES ==
169 : GNUNET_CRYPTO_kdf (&merchant_priv,
170 : sizeof (struct TALER_MerchantPrivateKeyP),
171 : "merchant-priv",
172 : strlen ("merchant-priv"),
173 : ids->merchant_name,
174 : strlen (ids->merchant_name),
175 : NULL,
176 : 0));
177 0 : GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
178 : &deposit.merchant_pub.eddsa_pub);
179 0 : GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
180 : &deposit.h_contract_terms.hash);
181 0 : if ( (GNUNET_OK !=
182 0 : TALER_string_to_amount (ids->amount_with_fee,
183 0 : &deposit.amount_with_fee)) ||
184 : (GNUNET_OK !=
185 0 : TALER_string_to_amount (ids->deposit_fee,
186 : &deposit.deposit_fee)) )
187 : {
188 0 : TALER_TESTING_interpreter_fail (is);
189 0 : TALER_denom_pub_free (&dpk);
190 0 : TALER_denom_priv_free (&denom_priv);
191 0 : return;
192 : }
193 :
194 0 : TALER_denom_pub_hash (&dpk,
195 : &deposit.coin.denom_pub_hash);
196 0 : GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
197 : &deposit.coin.coin_pub,
198 : sizeof (deposit.coin.coin_pub));
199 : {
200 : struct TALER_CoinPubHashP c_hash;
201 : struct TALER_PlanchetDetail pd;
202 : struct TALER_BlindedDenominationSignature bds;
203 : struct TALER_PlanchetMasterSecretP ps;
204 : struct TALER_ExchangeWithdrawValues alg_values;
205 : union TALER_DenominationBlindingKeyP bks;
206 :
207 0 : alg_values.cipher = TALER_DENOMINATION_RSA;
208 0 : TALER_planchet_blinding_secret_create (&ps,
209 : &alg_values,
210 : &bks);
211 0 : GNUNET_assert (GNUNET_OK ==
212 : TALER_denom_blind (&dpk,
213 : &bks,
214 : NULL, /* no age restriction active */
215 : &deposit.coin.coin_pub,
216 : &alg_values,
217 : &c_hash,
218 : &pd.blinded_planchet));
219 0 : GNUNET_assert (GNUNET_OK ==
220 : TALER_denom_sign_blinded (&bds,
221 : &denom_priv,
222 : false,
223 : &pd.blinded_planchet));
224 0 : TALER_blinded_planchet_free (&pd.blinded_planchet);
225 0 : GNUNET_assert (GNUNET_OK ==
226 : TALER_denom_sig_unblind (&deposit.coin.denom_sig,
227 : &bds,
228 : &bks,
229 : &c_hash,
230 : &alg_values,
231 : &dpk));
232 0 : TALER_blinded_denom_sig_free (&bds);
233 : }
234 0 : GNUNET_asprintf (&deposit.receiver_wire_account,
235 : "payto://x-taler-bank/localhost/%s?receiver-name=%s",
236 : ids->merchant_account,
237 : ids->merchant_account);
238 0 : memset (&deposit.wire_salt,
239 : 46,
240 : sizeof (deposit.wire_salt));
241 0 : deposit.timestamp = GNUNET_TIME_timestamp_get ();
242 0 : deposit.wire_deadline = GNUNET_TIME_relative_to_timestamp (
243 : ids->wire_deadline);
244 : /* finally, actually perform the DB operation */
245 : {
246 : uint64_t known_coin_id;
247 : struct TALER_DenominationHashP dph;
248 : struct TALER_AgeCommitmentHash agh;
249 :
250 0 : if ( (GNUNET_OK !=
251 0 : ids->dbc->plugin->start (ids->dbc->plugin->cls,
252 0 : "libtalertesting: insert deposit")) ||
253 : (0 >
254 0 : ids->dbc->plugin->ensure_coin_known (ids->dbc->plugin->cls,
255 : &deposit.coin,
256 : &known_coin_id,
257 : &dph,
258 0 : &agh)) ||
259 : (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
260 0 : ids->dbc->plugin->insert_deposit (ids->dbc->plugin->cls,
261 : ids->exchange_timestamp,
262 0 : &deposit)) ||
263 : (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
264 0 : ids->dbc->plugin->commit (ids->dbc->plugin->cls)) )
265 : {
266 0 : GNUNET_break (0);
267 0 : ids->dbc->plugin->rollback (ids->dbc->plugin->cls);
268 0 : GNUNET_free (deposit.receiver_wire_account);
269 0 : TALER_denom_pub_free (&dpk);
270 0 : TALER_denom_priv_free (&denom_priv);
271 0 : TALER_TESTING_interpreter_fail (is);
272 0 : return;
273 : }
274 : }
275 :
276 0 : TALER_denom_sig_free (&deposit.coin.denom_sig);
277 0 : TALER_denom_pub_free (&dpk);
278 0 : TALER_denom_priv_free (&denom_priv);
279 0 : GNUNET_free (deposit.receiver_wire_account);
280 0 : TALER_TESTING_interpreter_next (is);
281 : }
282 :
283 :
284 : /**
285 : * Free the state of a "auditor-dbinit" CMD, and possibly kills its
286 : * process if it did not terminate correctly.
287 : *
288 : * @param cls closure.
289 : * @param cmd the command being freed.
290 : */
291 : static void
292 0 : insert_deposit_cleanup (void *cls,
293 : const struct TALER_TESTING_Command *cmd)
294 : {
295 0 : struct InsertDepositState *ids = cls;
296 :
297 : (void) cmd;
298 0 : GNUNET_free (ids);
299 0 : }
300 :
301 :
302 : struct TALER_TESTING_Command
303 0 : TALER_TESTING_cmd_insert_deposit (
304 : const char *label,
305 : const struct TALER_TESTING_DatabaseConnection *dbc,
306 : const char *merchant_name,
307 : const char *merchant_account,
308 : struct GNUNET_TIME_Timestamp exchange_timestamp,
309 : struct GNUNET_TIME_Relative wire_deadline,
310 : const char *amount_with_fee,
311 : const char *deposit_fee)
312 : {
313 : struct InsertDepositState *ids;
314 :
315 0 : ids = GNUNET_new (struct InsertDepositState);
316 0 : ids->dbc = dbc;
317 0 : ids->merchant_name = merchant_name;
318 0 : ids->merchant_account = merchant_account;
319 0 : ids->exchange_timestamp = exchange_timestamp;
320 0 : ids->wire_deadline = wire_deadline;
321 0 : ids->amount_with_fee = amount_with_fee;
322 0 : ids->deposit_fee = deposit_fee;
323 :
324 : {
325 0 : struct TALER_TESTING_Command cmd = {
326 : .cls = ids,
327 : .label = label,
328 : .run = &insert_deposit_run,
329 : .cleanup = &insert_deposit_cleanup
330 : };
331 :
332 0 : return cmd;
333 : }
334 : }
335 :
336 :
337 : /* end of testing_api_cmd_insert_deposit.c */
|