mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
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
This commit is contained in:
parent
818f152618
commit
41ebf71e26
8 changed files with 60 additions and 13 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <ccan/ccan/endian/endian.h>
|
#include <ccan/ccan/endian/endian.h>
|
||||||
#include <ccan/ccan/mem/mem.h>
|
#include <ccan/ccan/mem/mem.h>
|
||||||
#include <common/channel_id.h>
|
#include <common/channel_id.h>
|
||||||
|
#include <common/pseudorand.h>
|
||||||
#include <common/utils.h>
|
#include <common/utils.h>
|
||||||
#include <wire/peer_wire.h>
|
#include <wire/peer_wire.h>
|
||||||
|
|
||||||
|
@ -441,6 +442,35 @@ int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id)
|
||||||
return -1;
|
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)
|
bool psbt_has_required_fields(struct wally_psbt *psbt)
|
||||||
{
|
{
|
||||||
u16 serial_id;
|
u16 serial_id;
|
||||||
|
|
|
@ -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);
|
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
|
/* psbt_has_required_fields - Validates psbt field completion
|
||||||
*
|
*
|
||||||
* Required fields are:
|
* Required fields are:
|
||||||
|
|
|
@ -42,6 +42,9 @@ u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
||||||
/* Generated stub for fromwire_u8 */
|
/* Generated stub for fromwire_u8 */
|
||||||
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
||||||
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
|
{ 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 */
|
/* Generated stub for towire */
|
||||||
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
|
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
|
||||||
{ fprintf(stderr, "towire called!\n"); abort(); }
|
{ fprintf(stderr, "towire called!\n"); abort(); }
|
||||||
|
|
|
@ -41,7 +41,8 @@ HSMD_COMMON_OBJS := \
|
||||||
common/version.o
|
common/version.o
|
||||||
|
|
||||||
ifeq ($(EXPERIMENTAL_FEATURES),1)
|
ifeq ($(EXPERIMENTAL_FEATURES),1)
|
||||||
HSMD_COMMON_OBJS += common/psbt_open.o
|
HSMD_COMMON_OBJS += common/psbt_open.o \
|
||||||
|
common/pseudorand.o
|
||||||
endif
|
endif
|
||||||
|
|
||||||
lightningd/lightning_hsmd: $(HSMD_OBJS) $(HSMD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS)
|
lightningd/lightning_hsmd: $(HSMD_OBJS) $(HSMD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS)
|
||||||
|
|
|
@ -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)
|
static void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role)
|
||||||
{
|
{
|
||||||
u16 serial_id;
|
u16 serial_id;
|
||||||
const u64 serial_space = 100000;
|
|
||||||
for (size_t i = 0; i < psbt->num_inputs; i++) {
|
for (size_t i = 0; i < psbt->num_inputs; i++) {
|
||||||
/* Skip ones that already have a serial id */
|
/* Skip ones that already have a serial id */
|
||||||
if (psbt_get_serial_id(&psbt->inputs[i].unknowns, &serial_id))
|
if (psbt_get_serial_id(&psbt->inputs[i].unknowns, &serial_id))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
while ((serial_id = pseudorand(serial_space)) % 2 != role ||
|
serial_id = psbt_new_input_serial(psbt, role);
|
||||||
psbt_find_serial_input(psbt, serial_id) != -1) {
|
|
||||||
/* keep going; */
|
|
||||||
}
|
|
||||||
psbt_input_add_serial_id(psbt, &psbt->inputs[i], serial_id);
|
psbt_input_add_serial_id(psbt, &psbt->inputs[i], serial_id);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < psbt->num_outputs; i++) {
|
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))
|
if (psbt_get_serial_id(&psbt->outputs[i].unknowns, &serial_id))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
while ((serial_id = pseudorand(serial_space)) % 2 != role ||
|
serial_id = psbt_new_output_serial(psbt, role);
|
||||||
psbt_find_serial_output(psbt, serial_id) != -1) {
|
|
||||||
/* keep going; */
|
|
||||||
}
|
|
||||||
psbt_output_add_serial_id(psbt, &psbt->outputs[i], serial_id);
|
psbt_output_add_serial_id(psbt, &psbt->outputs[i], serial_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,8 @@ ONCHAIND_COMMON_OBJS := \
|
||||||
common/wallet.o
|
common/wallet.o
|
||||||
|
|
||||||
ifeq ($(EXPERIMENTAL_FEATURES),1)
|
ifeq ($(EXPERIMENTAL_FEATURES),1)
|
||||||
ONCHAIND_COMMON_OBJS += common/psbt_open.o
|
ONCHAIND_COMMON_OBJS += common/psbt_open.o \
|
||||||
|
common/pseudorand.o
|
||||||
endif
|
endif
|
||||||
|
|
||||||
lightningd/lightning_onchaind: $(ONCHAIND_OBJS) $(WIRE_ONION_OBJS) $(ONCHAIND_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(HSMD_CLIENT_OBJS)
|
lightningd/lightning_onchaind: $(ONCHAIND_OBJS) $(WIRE_ONION_OBJS) $(ONCHAIND_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(HSMD_CLIENT_OBJS)
|
||||||
|
|
|
@ -86,7 +86,8 @@ OPENINGD_COMMON_OBJS := \
|
||||||
lightningd/gossip_msg.o
|
lightningd/gossip_msg.o
|
||||||
|
|
||||||
ifeq ($(EXPERIMENTAL_FEATURES),1)
|
ifeq ($(EXPERIMENTAL_FEATURES),1)
|
||||||
OPENINGD_COMMON_OBJS += common/psbt_open.o
|
OPENINGD_COMMON_OBJS += common/psbt_open.o \
|
||||||
|
common/pseudorand.o
|
||||||
endif
|
endif
|
||||||
|
|
||||||
lightningd/lightning_openingd: $(OPENINGD_OBJS) $(OPENINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(HSMD_CLIENT_OBJS)
|
lightningd/lightning_openingd: $(OPENINGD_OBJS) $(OPENINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(HSMD_CLIENT_OBJS)
|
||||||
|
|
|
@ -1391,7 +1391,7 @@ static u8 *opener_start(struct state *state, u8 *msg)
|
||||||
scriptpubkey_p2wsh(tmpctx, wscript),
|
scriptpubkey_p2wsh(tmpctx, wscript),
|
||||||
total);
|
total);
|
||||||
/* Add a serial_id for this output */
|
/* 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);
|
psbt_output_add_serial_id(psbt, funding_out, serial_id);
|
||||||
|
|
||||||
/* Add all of our inputs/outputs to the changeset */
|
/* Add all of our inputs/outputs to the changeset */
|
||||||
|
|
Loading…
Add table
Reference in a new issue