forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_parts.c
433 lines (388 loc) · 12 KB
/
tx_parts.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "config.h"
#include <assert.h>
#include <bitcoin/tx_parts.h>
#include <common/utils.h>
#include <wire/wire.h>
/* This destructor makes it behave like a native tal tree (a little!) */
static void destroy_wally_tx_input(struct wally_tx_input *in)
{
wally_tx_input_free(in);
}
static struct wally_tx_input *clone_input(const struct wally_tx_input *src)
{
struct wally_tx_input *in;
int ret;
if (is_elements(chainparams)) {
ret = wally_tx_elements_input_init_alloc
(src->txhash, sizeof(src->txhash),
src->index, src->sequence,
src->script, src->script_len,
src->witness,
src->blinding_nonce, sizeof(src->blinding_nonce),
src->entropy, sizeof(src->entropy),
src->issuance_amount, src->issuance_amount_len,
src->inflation_keys, src->inflation_keys_len,
src->issuance_amount_rangeproof,
src->issuance_amount_rangeproof_len,
src->inflation_keys_rangeproof,
src->inflation_keys_rangeproof_len,
src->pegin_witness,
&in);
} else {
ret = wally_tx_input_init_alloc(src->txhash, sizeof(src->txhash),
src->index, src->sequence,
src->script, src->script_len,
src->witness, &in);
}
assert(ret == WALLY_OK);
tal_add_destructor(in, destroy_wally_tx_input);
return in;
}
static void destroy_wally_tx_output(struct wally_tx_output *out)
{
wally_tx_output_free(out);
}
static struct wally_tx_output *clone_output(const struct wally_tx_output *src)
{
struct wally_tx_output *out;
int ret;
if (is_elements(chainparams)) {
ret = wally_tx_elements_output_init_alloc
(src->script, src->script_len,
src->asset, src->asset_len,
src->value, src->value_len,
src->nonce, src->nonce_len,
src->surjectionproof, src->surjectionproof_len,
src->rangeproof, src->rangeproof_len,
&out);
} else {
ret = wally_tx_output_init_alloc(src->satoshi,
src->script, src->script_len,
&out);
}
assert(ret == WALLY_OK);
tal_add_destructor(out, destroy_wally_tx_output);
return out;
}
struct tx_parts *tx_parts_from_wally_tx(const tal_t *ctx,
const struct wally_tx *wtx,
int input, int output)
{
struct tx_parts *txp = tal(ctx, struct tx_parts);
wally_txid(wtx, &txp->txid);
txp->inputs = tal_arrz(txp, struct wally_tx_input *, wtx->num_inputs);
txp->outputs = tal_arrz(txp, struct wally_tx_output *, wtx->num_outputs);
tal_wally_start();
for (size_t i = 0; i < wtx->num_inputs; i++) {
if (input != -1 && input != i)
continue;
txp->inputs[i] = clone_input(&wtx->inputs[i]);
}
for (size_t i = 0; i < wtx->num_outputs; i++) {
if (output != -1 && output != i)
continue;
txp->outputs[i] = clone_output(&wtx->outputs[i]);
/* Cheat a bit by also setting the numeric satoshi
* value, otherwise we end up converting a
* number of times */
if (chainparams->is_elements) {
struct amount_asset asset;
struct amount_sat sats;
asset = wally_tx_output_get_amount(txp->outputs[i]);
/* FIXME: non l-btc assets */
assert(amount_asset_is_main(&asset));
sats = amount_asset_to_sat(&asset);
txp->outputs[i]->satoshi = sats.satoshis; /* Raw: wally conversion */
}
}
tal_wally_end(txp);
return txp;
}
static void destroy_wally_tx_witness_stack(struct wally_tx_witness_stack *ws)
{
wally_tx_witness_stack_free(ws);
}
/* FIXME: If libwally exposed their linearization code, we could use it */
static struct wally_tx_witness_stack *
fromwire_wally_tx_witness_stack(const tal_t *ctx,
const u8 **cursor,
size_t *max)
{
struct wally_tx_witness_stack *ws;
size_t num;
int ret;
num = fromwire_u32(cursor, max);
if (num == 0)
return NULL;
tal_wally_start();
ret = wally_tx_witness_stack_init_alloc(num, &ws);
if (ret != WALLY_OK) {
fromwire_fail(cursor, max);
return NULL;
}
for (size_t i = 0; i < num; i++) {
u8 *w = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
ret = wally_tx_witness_stack_add(ws, w, tal_bytelen(w));
if (ret != WALLY_OK) {
wally_tx_witness_stack_free(ws);
fromwire_fail(cursor, max);
ws = NULL;
goto out;
}
}
tal_add_destructor(ws, destroy_wally_tx_witness_stack);
out:
tal_wally_end_onto(ctx, ws, struct wally_tx_witness_stack);
return ws;
}
static void towire_wally_tx_witness_stack(u8 **pptr,
const struct wally_tx_witness_stack *ws)
{
if (!ws) {
towire_u32(pptr, 0);
return;
}
towire_u32(pptr, ws->num_items);
for (size_t i = 0; i < ws->num_items; i++) {
towire_u32(pptr, ws->items[i].witness_len);
towire_u8_array(pptr,
ws->items[i].witness,
ws->items[i].witness_len);
}
}
static struct wally_tx_input *fromwire_wally_tx_input(const tal_t *ctx,
const u8 **cursor,
size_t *max)
{
struct wally_tx_input *in;
struct bitcoin_txid txid;
u32 index, sequence;
u8 *script;
struct wally_tx_witness_stack *ws;
int ret;
fromwire_bitcoin_txid(cursor, max, &txid);
index = fromwire_u32(cursor, max);
sequence = fromwire_u32(cursor, max);
script = fromwire_tal_arrn(tmpctx,
cursor, max, fromwire_u32(cursor, max));
/* libwally doesn't like non-NULL ptrs with zero lengths. */
if (tal_bytelen(script) == 0)
script = tal_free(script);
ws = fromwire_wally_tx_witness_stack(tmpctx, cursor, max);
tal_wally_start();
if (is_elements(chainparams)) {
u8 *blinding_nonce, *entropy, *issuance_amount,
*inflation_keys, *issuance_amount_rangeproof,
*inflation_keys_rangeproof;
struct wally_tx_witness_stack *pegin_witness;
blinding_nonce = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
entropy = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
issuance_amount = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
inflation_keys = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
issuance_amount_rangeproof = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
inflation_keys_rangeproof = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
pegin_witness = fromwire_wally_tx_witness_stack(tmpctx,
cursor, max);
ret = wally_tx_elements_input_init_alloc
(txid.shad.sha.u.u8, sizeof(txid.shad.sha.u.u8),
index, sequence,
script, tal_bytelen(script),
ws,
blinding_nonce, tal_bytelen(blinding_nonce),
entropy, tal_bytelen(entropy),
issuance_amount, tal_bytelen(issuance_amount),
inflation_keys, tal_bytelen(inflation_keys),
issuance_amount_rangeproof,
tal_bytelen(issuance_amount_rangeproof),
inflation_keys_rangeproof,
tal_bytelen(inflation_keys_rangeproof),
pegin_witness,
&in);
} else {
ret = wally_tx_input_init_alloc(txid.shad.sha.u.u8,
sizeof(txid.shad.sha.u.u8),
index, sequence,
script, tal_bytelen(script),
ws, &in);
}
if (ret != WALLY_OK) {
fromwire_fail(cursor, max);
in = NULL;
} else {
tal_add_destructor(in, destroy_wally_tx_input);
}
tal_wally_end_onto(ctx, in, struct wally_tx_input);
return in;
}
static struct wally_tx_output *fromwire_wally_tx_output(const tal_t *ctx,
const u8 **cursor,
size_t *max)
{
struct wally_tx_output *out;
unsigned char *script;
int ret;
script = fromwire_tal_arrn(tmpctx,
cursor, max, fromwire_u32(cursor, max));
tal_wally_start();
if (is_elements(chainparams)) {
u8 *asset, *value, *nonce, *surjectionproof, *rangeproof;
asset = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
value = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
nonce = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
surjectionproof = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
rangeproof = fromwire_tal_arrn(tmpctx,
cursor, max,
fromwire_u32(cursor, max));
ret = wally_tx_elements_output_init_alloc
(script, tal_bytelen(script),
asset, tal_bytelen(asset),
value, tal_bytelen(value),
nonce, tal_bytelen(nonce),
surjectionproof, tal_bytelen(surjectionproof),
rangeproof, tal_bytelen(rangeproof),
&out);
/* As a convenience, we sent the value over as satoshis */
out->satoshi = fromwire_u64(cursor, max);
} else {
u64 satoshi;
satoshi = fromwire_u64(cursor, max);
ret = wally_tx_output_init_alloc(satoshi,
script, tal_bytelen(script),
&out);
}
if (ret != WALLY_OK) {
fromwire_fail(cursor, max);
out = NULL;
} else {
tal_add_destructor(out, destroy_wally_tx_output);
}
tal_wally_end_onto(ctx, out, struct wally_tx_output);
return out;
}
static void towire_wally_tx_input(u8 **pptr, const struct wally_tx_input *in)
{
/* Just like a bitcoin_txid */
towire_u8_array(pptr, in->txhash, sizeof(in->txhash));
towire_u32(pptr, in->index);
towire_u32(pptr, in->sequence);
towire_u32(pptr, in->script_len);
towire_u8_array(pptr, in->script, in->script_len);
towire_wally_tx_witness_stack(pptr, in->witness);
if (is_elements(chainparams)) {
towire_u32(pptr, sizeof(in->blinding_nonce));
towire_u8_array(pptr, in->blinding_nonce,
sizeof(in->blinding_nonce));
towire_u32(pptr, sizeof(in->entropy));
towire_u8_array(pptr, in->entropy, sizeof(in->entropy));
towire_u32(pptr, in->issuance_amount_len);
towire_u8_array(pptr, in->issuance_amount,
in->issuance_amount_len);
towire_u32(pptr, in->inflation_keys_len);
towire_u8_array(pptr, in->inflation_keys,
in->inflation_keys_len);
towire_u32(pptr, in->issuance_amount_rangeproof_len);
towire_u8_array(pptr, in->issuance_amount_rangeproof,
in->issuance_amount_rangeproof_len);
towire_u32(pptr, in->inflation_keys_rangeproof_len);
towire_u8_array(pptr, in->inflation_keys_rangeproof,
in->inflation_keys_rangeproof_len);
towire_wally_tx_witness_stack(pptr, in->pegin_witness);
}
}
static void towire_wally_tx_output(u8 **pptr, const struct wally_tx_output *out)
{
towire_u32(pptr, out->script_len);
towire_u8_array(pptr, out->script, out->script_len);
if (is_elements(chainparams)) {
towire_u32(pptr, out->asset_len);
towire_u8_array(pptr, out->asset, out->asset_len);
towire_u32(pptr, out->value_len);
towire_u8_array(pptr, out->value, out->value_len);
towire_u32(pptr, out->nonce_len);
towire_u8_array(pptr, out->nonce, out->nonce_len);
towire_u32(pptr, out->surjectionproof_len);
towire_u8_array(pptr, out->surjectionproof,
out->surjectionproof_len);
towire_u32(pptr, out->rangeproof_len);
towire_u8_array(pptr, out->rangeproof, out->rangeproof_len);
/* Copy the value over, as a convenience */
towire_u64(pptr, out->satoshi);
} else {
towire_u64(pptr, out->satoshi);
}
}
/* Wire marshalling and unmarshalling */
struct tx_parts *fromwire_tx_parts(const tal_t *ctx,
const u8 **cursor, size_t *max)
{
struct tx_parts *txp = tal(ctx, struct tx_parts);
u32 num_inputs, num_outputs;
fromwire_bitcoin_txid(cursor, max, &txp->txid);
num_inputs = fromwire_u32(cursor, max);
txp->inputs = tal_arr(txp, struct wally_tx_input *, num_inputs);
for (size_t i = 0; i < num_inputs; i++) {
if (fromwire_bool(cursor, max)) {
txp->inputs[i] = fromwire_wally_tx_input(txp->inputs,
cursor, max);
} else {
txp->inputs[i] = NULL;
}
}
num_outputs = fromwire_u32(cursor, max);
txp->outputs = tal_arr(txp, struct wally_tx_output *, num_outputs);
for (size_t i = 0; i < num_outputs; i++) {
if (fromwire_bool(cursor, max)) {
txp->outputs[i] = fromwire_wally_tx_output(txp->outputs,
cursor, max);
} else {
txp->outputs[i] = NULL;
}
}
if (*cursor == NULL)
return tal_free(txp);
return txp;
}
void towire_tx_parts(u8 **pptr, const struct tx_parts *txp)
{
towire_bitcoin_txid(pptr, &txp->txid);
towire_u32(pptr, tal_count(txp->inputs));
for (size_t i = 0; i < tal_count(txp->inputs); i++) {
if (txp->inputs[i]) {
towire_bool(pptr, true);
towire_wally_tx_input(pptr, txp->inputs[i]);
} else {
towire_bool(pptr, false);
}
}
towire_u32(pptr, tal_count(txp->outputs));
for (size_t i = 0; i < tal_count(txp->outputs); i++) {
if (txp->outputs[i]) {
towire_bool(pptr, true);
towire_wally_tx_output(pptr, txp->outputs[i]);
} else {
towire_bool(pptr, false);
}
}
}