Browse Source

psbt: new methods for generating serial_ids for an input/output

we need to do this elsewhere later, pull it out so we can use it
travis-experimental
niftynei 5 years ago
committed by Rusty Russell
parent
commit
41ebf71e26
  1. 30
      common/psbt_open.c
  2. 18
      common/psbt_open.h
  3. 3
      common/test/exp-run-psbt_diff.c
  4. 3
      hsmd/Makefile
  5. 11
      lightningd/dual_open_control.c
  6. 3
      onchaind/Makefile
  7. 3
      openingd/Makefile
  8. 2
      openingd/dualopend.c

30
common/psbt_open.c

@ -7,6 +7,7 @@
#include <ccan/ccan/endian/endian.h>
#include <ccan/ccan/mem/mem.h>
#include <common/channel_id.h>
#include <common/pseudorand.h>
#include <common/utils.h>
#include <wire/peer_wire.h>
@ -441,6 +442,35 @@ int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id)
return -1;
}
static u16 get_random_serial(enum tx_role role)
{
return pseudorand(1 << 15) << 1 | role;
}
u16 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
while ((serial_id = get_random_serial(role)) &&
psbt_find_serial_input(psbt, serial_id) != -1) {
/* keep going; */
}
return serial_id;
}
u16 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
while ((serial_id = get_random_serial(role)) &&
psbt_find_serial_output(psbt, serial_id) != -1) {
/* keep going; */
}
return serial_id;
}
bool psbt_has_required_fields(struct wally_psbt *psbt)
{
u16 serial_id;

18
common/psbt_open.h

@ -130,6 +130,24 @@ int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id);
*/
int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id);
/* psbt_new_input_serial - Generate a new serial for an input for {role}
*
* @psbt - psbt to get a new serial for
* @role - which tx role to generate the serial for
*
* Returns a new, unique serial of the correct parity for the specified {role}
*/
u16 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role);
/* psbt_new_output_serial - Generate a new serial for an output for {role}
*
* @psbt - psbt to get a new serial for
* @role - which tx role to generate the serial for
*
* Returns a new, unique serial of the correct parity for the specified {role}
*/
u16 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role);
/* psbt_has_required_fields - Validates psbt field completion
*
* Required fields are:

3
common/test/exp-run-psbt_diff.c

@ -42,6 +42,9 @@ u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for pseudorand */
uint64_t pseudorand(uint64_t max UNNEEDED)
{ fprintf(stderr, "pseudorand called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }

3
hsmd/Makefile

@ -41,7 +41,8 @@ HSMD_COMMON_OBJS := \
common/version.o
ifeq ($(EXPERIMENTAL_FEATURES),1)
HSMD_COMMON_OBJS += common/psbt_open.o
HSMD_COMMON_OBJS += common/psbt_open.o \
common/pseudorand.o
endif
lightningd/lightning_hsmd: $(HSMD_OBJS) $(HSMD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS)

11
lightningd/dual_open_control.c

@ -359,16 +359,12 @@ static bool psbt_side_contribs_changed(struct wally_psbt *orig,
static void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role)
{
u16 serial_id;
const u64 serial_space = 100000;
for (size_t i = 0; i < psbt->num_inputs; i++) {
/* Skip ones that already have a serial id */
if (psbt_get_serial_id(&psbt->inputs[i].unknowns, &serial_id))
continue;
while ((serial_id = pseudorand(serial_space)) % 2 != role ||
psbt_find_serial_input(psbt, serial_id) != -1) {
/* keep going; */
}
serial_id = psbt_new_input_serial(psbt, role);
psbt_input_add_serial_id(psbt, &psbt->inputs[i], serial_id);
}
for (size_t i = 0; i < psbt->num_outputs; i++) {
@ -376,10 +372,7 @@ static void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role)
if (psbt_get_serial_id(&psbt->outputs[i].unknowns, &serial_id))
continue;
while ((serial_id = pseudorand(serial_space)) % 2 != role ||
psbt_find_serial_output(psbt, serial_id) != -1) {
/* keep going; */
}
serial_id = psbt_new_output_serial(psbt, role);
psbt_output_add_serial_id(psbt, &psbt->outputs[i], serial_id);
}
}

3
onchaind/Makefile

@ -64,7 +64,8 @@ ONCHAIND_COMMON_OBJS := \
common/wallet.o
ifeq ($(EXPERIMENTAL_FEATURES),1)
ONCHAIND_COMMON_OBJS += common/psbt_open.o
ONCHAIND_COMMON_OBJS += common/psbt_open.o \
common/pseudorand.o
endif
lightningd/lightning_onchaind: $(ONCHAIND_OBJS) $(WIRE_ONION_OBJS) $(ONCHAIND_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(HSMD_CLIENT_OBJS)

3
openingd/Makefile

@ -86,7 +86,8 @@ OPENINGD_COMMON_OBJS := \
lightningd/gossip_msg.o
ifeq ($(EXPERIMENTAL_FEATURES),1)
OPENINGD_COMMON_OBJS += common/psbt_open.o
OPENINGD_COMMON_OBJS += common/psbt_open.o \
common/pseudorand.o
endif
lightningd/lightning_openingd: $(OPENINGD_OBJS) $(OPENINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(HSMD_CLIENT_OBJS)

2
openingd/dualopend.c

@ -1391,7 +1391,7 @@ static u8 *opener_start(struct state *state, u8 *msg)
scriptpubkey_p2wsh(tmpctx, wscript),
total);
/* Add a serial_id for this output */
serial_id = 0; /* FIXME: generate new serial */
serial_id = psbt_new_input_serial(psbt, TX_INITIATOR);
psbt_output_add_serial_id(psbt, funding_out, serial_id);
/* Add all of our inputs/outputs to the changeset */

Loading…
Cancel
Save