aboutsummaryrefslogtreecommitdiff
path: root/src/exchange-tools/taler-exchange-kyc-trigger.c
blob: eab7328940fa4f379749080707795c4099c75312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
   This file is part of TALER
   Copyright (C) 2020-2024 Taler Systems SA

   TALER is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
   Foundation; either version 3, or (at your option) any later version.

   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file taler-exchange-kyc-trigger.c
 * @brief Support for manually triggering KYC/AML processes for testing
 * @author Christian Grothoff
 */
#include <platform.h>
#include <gnunet/gnunet_json_lib.h>
#include <gnunet/gnunet_util_lib.h>
#include "taler_json_lib.h"
#include "taler_exchange_service.h"


/**
 * Our private key.
 */
static struct TALER_ReservePrivateKeyP reserve_priv;

/**
 * Our public key.
 */
static struct TALER_ReservePublicKeyP reserve_pub;

/**
 * Our context for making HTTP requests.
 */
static struct GNUNET_CURL_Context *ctx;

/**
 * Reschedule context for #ctx.
 */
static struct GNUNET_CURL_RescheduleContext *rc;

/**
 * Handle to the exchange's configuration
 */
static const struct GNUNET_CONFIGURATION_Handle *kcfg;

/**
 * Handle for exchange interaction.
 */
static struct TALER_EXCHANGE_KycWalletHandle *kwh;

/**
 * Balance threshold to report to the exchange.
 */
static struct TALER_Amount balance;

/**
 * Return value from main().
 */
static int global_ret;

/**
 * Currency we have configured.
 */
static char *currency;

/**
 * URL of the exchange we are interacting with
 * as per our configuration.
 */
static char *CFG_exchange_url;


/**
 * Function called with the result for a wallet looking
 * up its KYC payment target.
 *
 * @param cls closure
 * @param ks the wallets KYC payment target details
 */
static void
kyc_wallet_cb (
  void *cls,
  const struct TALER_EXCHANGE_WalletKycResponse *ks)
{
  kwh = NULL;
  switch (ks->hr.http_status)
  {
  case MHD_HTTP_OK:
    fprintf (stdout,
             "OK, next threshold at %s\n",
             TALER_amount2s (&ks->details.ok.next_threshold));
    break;
  case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
    {
      const struct TALER_EXCHANGE_KycNeededRedirect *knr
        = &ks->details.unavailable_for_legal_reasons;
      char *ps;

      ps = GNUNET_STRINGS_data_to_string_alloc (&knr->h_payto,
                                                sizeof (knr->h_payto));
      fprintf (stderr,
               "KYC needed (%llu, %s) for %s\n",
               (unsigned long long) knr->requirement_row,
               knr->bad_kyc_auth
               ? "KYC auth needed"
               : "KYC auth OK",
               ps);
      GNUNET_free (ps);
    }
    break;
  default:
    fprintf (stdout,
             "Unexpected HTTP status %u\n",
             ks->hr.http_status);
    break;
  }
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Shutdown task. Invoked when the application is being terminated.
 *
 * @param cls NULL
 */
static void
do_shutdown (void *cls)
{
  (void) cls;
  if (NULL != kwh)
  {
    TALER_EXCHANGE_kyc_wallet_cancel (kwh);
    kwh = NULL;
  }
  if (NULL != ctx)
  {
    GNUNET_CURL_fini (ctx);
    ctx = NULL;
  }
  if (NULL != rc)
  {
    GNUNET_CURL_gnunet_rc_destroy (rc);
    rc = NULL;
  }
}


/**
 * Load the reserve key.
 *
 * @param do_create #GNUNET_YES if the key may be created
 * @return #GNUNET_OK on success
 */
static enum GNUNET_GenericReturnValue
load_reserve_key (int do_create)
{
  char *fn;

  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_filename (kcfg,
                                               "exchange-testing",
                                               "RESERVE_PRIV_FILE",
                                               &fn))
  {
    enum GNUNET_GenericReturnValue ret;

    if (GNUNET_YES !=
        GNUNET_DISK_file_test (fn))
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Account private key `%s' does not exist yet, creating it!\n",
                  fn);
    ret = GNUNET_CRYPTO_eddsa_key_from_file (fn,
                                             do_create,
                                             &reserve_priv.eddsa_priv);
    if (GNUNET_SYSERR == ret)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Failed to initialize master key from file `%s': %s\n",
                  fn,
                  "could not create file");
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
    GNUNET_free (fn);
  }
  else
  {
    GNUNET_CRYPTO_eddsa_key_create (&reserve_priv.eddsa_priv);
  }
  GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv.eddsa_priv,
                                      &reserve_pub.eddsa_pub);
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Using reserve public key %s\n",
              TALER_B2S (&reserve_pub));
  return GNUNET_OK;
}


/**
 * Main function that will be run.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  (void) cls;
  (void) cfgfile;
  kcfg = cfg;

  if (GNUNET_OK !=
      load_reserve_key (GNUNET_YES))
  {
    GNUNET_break (0);
    global_ret = EXIT_FAILURE;
    return;
  }
  if (GNUNET_OK !=
      TALER_config_get_currency (kcfg,
                                 &currency))
  {
    global_ret = EXIT_NOTCONFIGURED;
    return;
  }
  if ( (GNUNET_OK !=
        TALER_amount_is_valid (&balance)) ||
       (0 != strcmp (balance.currency,
                     currency)) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Invalid balance threshold `%s'\n",
                TALER_amount2s (&balance));
    global_ret = EXIT_FAILURE;
    return;
  }
  if ( (NULL == CFG_exchange_url) &&
       (GNUNET_OK !=
        GNUNET_CONFIGURATION_get_value_string (kcfg,
                                               "exchange",
                                               "BASE_URL",
                                               &CFG_exchange_url)) )
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "exchange",
                               "BASE_URL");
    global_ret = EXIT_NOTCONFIGURED;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
                          &rc);
  rc = GNUNET_CURL_gnunet_rc_create (ctx);
  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
                                 NULL);
  kwh = TALER_EXCHANGE_kyc_wallet (ctx,
                                   CFG_exchange_url,
                                   &reserve_priv,
                                   &balance,
                                   &kyc_wallet_cb,
                                   NULL);
  if (NULL == kwh)
  {
    GNUNET_break (0);
    GNUNET_SCHEDULER_shutdown ();
  }
}


/**
 * The main function of the taler-exchange-kyc-trigger tool.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc,
      char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    TALER_getopt_get_amount ('b',
                             "balance",
                             "AMOUNT",
                             "balance threshold to report to the exchange",
                             &balance),
    GNUNET_GETOPT_OPTION_END
  };
  enum GNUNET_GenericReturnValue ret;

  /* force linker to link against libtalerutil; if we do
     not do this, the linker may "optimize" libtalerutil
     away and skip #TALER_OS_init(), which we do need */
  (void) TALER_project_data_default ();
  TALER_OS_init ();
  ret = GNUNET_PROGRAM_run (
    argc, argv,
    "taler-exchange-kyc-trigger",
    gettext_noop (
      "Trigger KYC/AML measures based on high wallet balance for testing"),
    options,
    &run, NULL);
  if (GNUNET_SYSERR == ret)
    return EXIT_INVALIDARGUMENT;
  if (GNUNET_NO == ret)
    return EXIT_SUCCESS;
  return global_ret;
}


/* end of taler-exchange-kyc-trigger.c */