Browse Source

wire: serialize the amounts for a bitcoin tx over the wire

we'll need this for calculating fees etc in onchaind
nifty/pset-pre
lisa neigut 5 years ago
committed by Rusty Russell
parent
commit
15e4e922c9
  1. 19
      wire/fromwire.c
  2. 17
      wire/towire.c

19
wire/fromwire.c

@ -368,7 +368,24 @@ void derive_channel_id(struct channel_id *channel_id,
struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx,
const u8 **cursor, size_t *max)
{
return pull_bitcoin_tx(ctx, cursor, max);
struct bitcoin_tx *tx;
u16 input_amts_len;
size_t i;
tx = pull_bitcoin_tx(ctx, cursor, max);
input_amts_len = fromwire_u16(cursor, max);
/* We don't serialize the amounts if they're not *all* populated */
if (input_amts_len != tal_count(tx->input_amounts))
return tx;
for (i = 0; i < input_amts_len; i++) {
struct amount_sat sat;
sat = fromwire_amount_sat(cursor, max);
tx->input_amounts[i] =
tal_dup(tx, struct amount_sat, &sat);
}
return tx;
}
void fromwire_siphash_seed(const u8 **cursor, size_t *max,

17
wire/towire.c

@ -226,8 +226,25 @@ void towire_wirestring(u8 **pptr, const char *str)
void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx)
{
size_t i;
u8 *lin = linearize_tx(tmpctx, tx);
towire_u8_array(pptr, lin, tal_count(lin));
/* We only want to 'save' the amounts if every amount
* has been populated */
for (i = 0; i < tal_count(tx->input_amounts); i++) {
if (!tx->input_amounts[i]) {
towire_u16(pptr, 0);
return;
}
}
/* Otherwise, we include the input amount set */
towire_u16(pptr, tal_count(tx->input_amounts));
for (i = 0; i < tal_count(tx->input_amounts); i++) {
assert(tx->input_amounts[i]);
towire_amount_sat(pptr, *tx->input_amounts[i]);
}
}
void towire_siphash_seed(u8 **pptr, const struct siphash_seed *seed)

Loading…
Cancel
Save