/
joiner.c
272 lines (237 loc) · 7.04 KB
/
joiner.c
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
/* Coinjoin Client
* Written in 2014 by
* Andrew Poelstra <apoelstra@wpsoftware.net>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to
* the public domain worldwide. This software is distributed without
* any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software.
* If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include "buffer.h"
#include "joiner.h"
#include "global.h"
struct joiner
{
enum joiner_status status;
char *session_id;
char *tx_to_sign;
char *update_url;
char donation_address[41];
int time_to_switch;
u64_t per_input_fee;
u64_t mpo;
unsigned n_transactions;
};
/*! \brief Create a new joiner object */
joiner_t *joiner_new (const char *update_url, const char *session_id)
{
joiner_t *rv = malloc (sizeof *rv);
if (rv)
{
rv->status = JS_UNKNOWN;
rv->update_url = malloc (1 + strlen (update_url));
strcpy (rv->update_url, update_url);
rv->tx_to_sign = NULL;
rv->session_id = NULL;
rv->per_input_fee = 0;
rv->mpo = 0;
if (session_id != NULL)
{
rv->session_id = malloc (1 + strlen (session_id));
strcpy (rv->session_id, session_id);
}
}
return rv;
}
/*! \brief Destroys a joiner object */
void joiner_destroy (joiner_t *jn)
{
if (jn) free (jn->update_url);
if (jn) free (jn->session_id);
free (jn);
}
static buffer_t *read_to_buffer (CURL *curl)
{
char curl_error_buf[CURL_ERROR_SIZE];
buffer_t *response = buffer_new ();
if (response == NULL)
{
fputs ("OOM creating buffer", stderr);
return NULL;
}
curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_error_buf);
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, buffer_write_data);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, response);
if (curl_easy_perform (curl) != 0)
{
fprintf (stderr, "Join server: %s\n", curl_error_buf);
buffer_destroy (response);
return NULL;
}
/* NULL-terminate the curl data */
buffer_null_terminate (response);
return response;
}
/*! \brief Resync joiner object with the server */
void joiner_update (joiner_t *jn)
{
CURL *curl = curl_easy_init ();
assert (jn != NULL);
if (jn->session_id != NULL)
{
char *url = malloc (30 + strlen (jn->update_url) + strlen (jn->session_id));
if (url != NULL)
{
sprintf (url, "%s?session=%s", jn->update_url, jn->session_id);
curl_easy_setopt (curl, CURLOPT_URL, url);
} else fputs ("OOM setting URL, Bad Things will happen.\n", stderr);
free (url);
fprintf (stderr, "Syncing with joiner, session ID %s\n", jn->session_id);
}
else
{
curl_easy_setopt (curl, CURLOPT_URL, jn->update_url);
fprintf (stderr, "Syncing with joiner, session ID unknown\n");
}
buffer_t *response = read_to_buffer (curl);
if (response == NULL)
return;
/* Parse response */
free (jn->tx_to_sign);
jn->tx_to_sign = malloc (buffer_get_size (response));
/* session_status:time_to_switch:donation_addr:required_fee:most_popular_output:n_transactions:session_id:session_spookwords:tx_to_sign */
sscanf (buffer_get_data (response), "%d:%d:%40[1a-zA-Z]:%llu:%llu:%u:%*[^:]:%*[^:]:%s",
&jn->status, &jn->time_to_switch, jn->donation_address,
&jn->per_input_fee, &jn->mpo, &jn->n_transactions,
jn->tx_to_sign);
buffer_destroy (response);
curl_easy_cleanup (curl);
}
static char *real_joiner_submit (joiner_t *jn, const char *raw_tx, const char *post_var)
{
char *rv;
char *post_fields = malloc (30 + strlen (raw_tx));
CURL *curl = curl_easy_init ();
assert (jn != NULL);
if (post_fields == NULL)
return NULL;
if (jn->session_id != NULL)
{
char *url = malloc (30 + strlen (jn->update_url) + strlen (jn->session_id));
if (url != NULL)
{
sprintf (url, "%s?session=%s", jn->update_url, jn->session_id);
curl_easy_setopt (curl, CURLOPT_URL, url);
} else fputs ("OOM setting URL, Bad Things will happen.\n", stderr);
free (url);
fprintf (stderr, "Submitting %s to joiner, session ID %s\n", post_var, jn->session_id);
}
else
{
curl_easy_setopt (curl, CURLOPT_URL, jn->update_url);
fprintf (stderr, "Submitting %s to joiner, session ID unknown\n", post_var);
}
sprintf (post_fields, "%s=%s", post_var, raw_tx);
curl_easy_setopt (curl, CURLOPT_POSTFIELDS, post_fields);
buffer_t *response = read_to_buffer (curl);
if (response == NULL)
return NULL;
rv = malloc (1 + buffer_get_size (response));
strcpy (rv, buffer_get_data (response));
/* trim trailing whitespace/crap from sess id */
int i;
for (i = 0; isalnum (rv[i]); ++i);
rv[i] = 0;
buffer_destroy (response);
curl_easy_cleanup (curl);
free (post_fields);
return rv;
}
/*! \brief Submits an unsigned transaction to the server
* \return The joiner session ID
*/
const char *joiner_submit_unsigned (joiner_t *jn, const char *raw_tx)
{
char *sess_id = real_joiner_submit (jn, raw_tx, "unsigned_tx");
if (sess_id == NULL)
return NULL;
jn->session_id = sess_id;
return sess_id;
}
/*! \brief Submits an signed transaction to the server */
void joiner_submit_signed (joiner_t *jn, const char *raw_tx)
{
free (real_joiner_submit (jn, raw_tx, "signed_tx"));
}
/*! \brief Getter for joiner status */
enum joiner_status joiner_status (const joiner_t *jn)
{
assert (jn != NULL);
return jn->status;
}
/*! \brief Getter for joiner status */
const char *joiner_status_text (const joiner_t *jn)
{
assert (jn != NULL);
switch (jn->status)
{
case JS_NOTFOUND: return "session not found";
case JS_NOTYETOPEN: return "no open session (submit a transaction to start one)";
case JS_STILLOPEN: return "session active";
case JS_NEEDSIGS: return "session waiting on signatures";
case JS_COMPLETE: return "session complete :)";
case JS_INVALID: return "invalid session";
default: return "unknown";
}
}
/*! \brief Getter for donation address */
const char *joiner_donation_address (const joiner_t *jn)
{
assert (jn != NULL);
return jn->donation_address;
}
/*! \brief Getter for per-input fee */
u64_t joiner_per_input_fee (const joiner_t *jn)
{
assert (jn != NULL);
return jn->per_input_fee;
}
/*! \brief Getter for most popular output */
u64_t joiner_most_popular_output (const joiner_t *jn)
{
assert (jn != NULL);
return jn->mpo;
}
/*! \brief Getter for # of transactions */
unsigned joiner_n_transactions (const joiner_t *jn)
{
assert (jn != NULL);
return jn->n_transactions;
}
/*! \brief Getter for time remaining */
int joiner_minutes_remaining (const joiner_t *jn)
{
assert (jn != NULL);
return jn->time_to_switch;
}
/*! \brief Getter for tx to sign (if the joiner is waiting on sigs) */
const char *joiner_tx_to_sign (const joiner_t *jn)
{
assert (jn != NULL);
return jn->tx_to_sign;
}
/*! \brief Getter for txid (if the joiner status is complete) */
const char *joiner_txid (const joiner_t *jn)
{
assert (jn != NULL);
return jn->tx_to_sign;
}