Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2016-2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or
6 : modify it under the terms of the GNU General Public License
7 : as published by the Free Software Foundation; either version 3,
8 : or (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful,
11 : but 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 bank-lib/fakebank_common_lookup.c
21 : * @brief common helper functions related to lookups
22 : * @author Christian Grothoff <christian@grothoff.org>
23 : */
24 : #include <pthread.h>
25 : #include "taler/taler_fakebank_lib.h"
26 : #include "taler/taler_bank_service.h"
27 : #include "taler/taler_mhd_lib.h"
28 : #include <gnunet/gnunet_mhd_compat.h>
29 : #include "fakebank.h"
30 : #include "fakebank_common_lookup.h"
31 :
32 : struct WithdrawalOperation *
33 0 : TALER_FAKEBANK_lookup_withdrawal_operation_ (struct TALER_FAKEBANK_Handle *h,
34 : const char *wopid)
35 : {
36 : struct GNUNET_ShortHashCode sh;
37 :
38 0 : if (NULL == h->wops)
39 0 : return NULL;
40 0 : if (GNUNET_OK !=
41 0 : GNUNET_STRINGS_string_to_data (wopid,
42 : strlen (wopid),
43 : &sh,
44 : sizeof (sh)))
45 : {
46 0 : GNUNET_break_op (0);
47 0 : return NULL;
48 : }
49 0 : return GNUNET_CONTAINER_multishortmap_get (h->wops,
50 : &sh);
51 : }
52 :
53 :
54 : struct Account *
55 655 : TALER_FAKEBANK_lookup_account_ (struct TALER_FAKEBANK_Handle *h,
56 : const char *name,
57 : const char *receiver_name)
58 : {
59 : struct GNUNET_HashCode hc;
60 : size_t slen;
61 : struct Account *account;
62 :
63 655 : memset (&hc,
64 : 0,
65 : sizeof (hc));
66 655 : slen = strlen (name);
67 655 : GNUNET_CRYPTO_hash (name,
68 : slen,
69 : &hc);
70 655 : GNUNET_assert (0 ==
71 : pthread_mutex_lock (&h->accounts_lock));
72 655 : account = GNUNET_CONTAINER_multihashmap_get (h->accounts,
73 : &hc);
74 655 : if (NULL == account)
75 : {
76 69 : if (NULL == receiver_name)
77 : {
78 16 : GNUNET_assert (0 ==
79 : pthread_mutex_unlock (&h->accounts_lock));
80 16 : return NULL;
81 : }
82 53 : account = GNUNET_new (struct Account);
83 53 : account->account_name = GNUNET_strdup (name);
84 53 : account->receiver_name = GNUNET_strdup (receiver_name);
85 53 : GNUNET_asprintf (&account->payto_uri,
86 : "payto://x-taler-bank/%s/%s?receiver-name=%s",
87 : h->hostname,
88 : account->account_name,
89 : account->receiver_name);
90 53 : GNUNET_assert (GNUNET_OK ==
91 : TALER_amount_set_zero (h->currency,
92 : &account->balance));
93 53 : GNUNET_assert (GNUNET_OK ==
94 : GNUNET_CONTAINER_multihashmap_put (h->accounts,
95 : &hc,
96 : account,
97 : GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
98 : }
99 639 : GNUNET_assert (0 ==
100 : pthread_mutex_unlock (&h->accounts_lock));
101 639 : return account;
102 : }
|