diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 69aefb862..ab14e222c 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -31,6 +31,9 @@ bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, /* Generated stub for fromwire_node_id */ void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED) { fprintf(stderr, "fromwire_node_id called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for status_fmt */ void status_fmt(enum log_level level UNNEEDED, const struct node_id *peer UNNEEDED, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 55679160f..d73d80011 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -22,6 +22,9 @@ void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memt /* Generated stub for memleak_scan_htable */ void memleak_scan_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED) { fprintf(stderr, "memleak_scan_htable called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_failreason code UNNEEDED, const char *fmt UNNEEDED, ...) diff --git a/cli/test/run-human-mode.c b/cli/test/run-human-mode.c index 29997c6db..6c3d3177a 100644 --- a/cli/test/run-human-mode.c +++ b/cli/test/run-human-mode.c @@ -89,6 +89,9 @@ const char *log_level_name(enum log_level level UNNEEDED) bool log_level_parse(const char *levelstr UNNEEDED, size_t len UNNEEDED, enum log_level *level UNNEEDED) { fprintf(stderr, "log_level_parse called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_amount_msat */ void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED) { fprintf(stderr, "towire_amount_msat called!\n"); abort(); } diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index 502b31861..51a9b3f6b 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -89,6 +89,9 @@ const char *log_level_name(enum log_level level UNNEEDED) bool log_level_parse(const char *levelstr UNNEEDED, size_t len UNNEEDED, enum log_level *level UNNEEDED) { fprintf(stderr, "log_level_parse called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_amount_msat */ void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED) { fprintf(stderr, "towire_amount_msat called!\n"); abort(); } diff --git a/cli/test/run-remove-hint.c b/cli/test/run-remove-hint.c index 876b3e88d..e31d8b2f6 100644 --- a/cli/test/run-remove-hint.c +++ b/cli/test/run-remove-hint.c @@ -92,6 +92,9 @@ const char *log_level_name(enum log_level level UNNEEDED) bool log_level_parse(const char *levelstr UNNEEDED, size_t len UNNEEDED, enum log_level *level UNNEEDED) { fprintf(stderr, "log_level_parse called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_amount_msat */ void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED) { fprintf(stderr, "towire_amount_msat called!\n"); abort(); } diff --git a/common/Makefile b/common/Makefile index 4c8c946f6..c850ac597 100644 --- a/common/Makefile +++ b/common/Makefile @@ -84,6 +84,7 @@ COMMON_SRC_NOGEN := \ common/random_select.c \ common/read_peer_msg.c \ common/route.c \ + common/sciddir_or_pubkey.c \ common/setup.c \ common/shutdown_scriptpubkey.c \ common/sphinx.c \ diff --git a/common/sciddir_or_pubkey.c b/common/sciddir_or_pubkey.c new file mode 100644 index 000000000..3d47b3a86 --- /dev/null +++ b/common/sciddir_or_pubkey.c @@ -0,0 +1,71 @@ +#include "config.h" +#include +#include +#include +#include + +/* BOLT-sciddir_or_pubkey #1: + * * `sciddir_or_pubkey`: either 9 or 33 bytes referencing or identifying a node, respectively + * * if the first byte is 0 or 1, then an 8-byte `short_channel_id` follows for a total of 9 bytes + * * 0 for the first byte indicates this refers to `node_id_1` in the `channel_announcement` for `short_channel_id` (see [BOLT #7](07-routing-gossip.md#the-channel_announcement-message)) + * * 1 for the first byte indicates this refers to `node_id_2` in the `channel_announcement` for `short_channel_id` (see [BOLT #7](07-routing-gossip.md#the-channel_announcement-message)) + * * if the first byte is 2 or 3, then the value is a 33-byte `point` + */ +void towire_sciddir_or_pubkey(u8 **pptr, + const struct sciddir_or_pubkey *sciddpk) +{ + if (sciddpk->is_pubkey) + towire_pubkey(pptr, &sciddpk->pubkey); + else { + assert(sciddpk->scidd.dir == 0 || sciddpk->scidd.dir == 1); + towire_u8(pptr, sciddpk->scidd.dir); + towire_short_channel_id(pptr, sciddpk->scidd.scid); + } +} + +void fromwire_sciddir_or_pubkey(const u8 **cursor, size_t *max, + struct sciddir_or_pubkey *sciddpk) +{ + const u8 *peek = *cursor; + size_t peek_max = *max; + u8 firstbyte = fromwire_u8(&peek, &peek_max); + + if (firstbyte == 0 || firstbyte == 1) { + sciddpk->is_pubkey = false; + sciddpk->scidd.dir = fromwire_u8(cursor, max); + sciddpk->scidd.scid = fromwire_short_channel_id(cursor, max); + } else { + sciddpk->is_pubkey = true; + fromwire_pubkey(cursor, max, &sciddpk->pubkey); + } +} + +void sciddir_or_pubkey_from_pubkey(struct sciddir_or_pubkey *sciddpk, + const struct pubkey *pubkey) +{ + sciddpk->is_pubkey = true; + sciddpk->pubkey = *pubkey; +} + +bool sciddir_or_pubkey_from_node_id(struct sciddir_or_pubkey *sciddpk, + const struct node_id *node_id) +{ + sciddpk->is_pubkey = true; + return pubkey_from_node_id(&sciddpk->pubkey, node_id); +} + +void sciddir_or_pubkey_from_scidd(struct sciddir_or_pubkey *sciddpk, + const struct short_channel_id_dir *scidd) +{ + sciddpk->is_pubkey = false; + sciddpk->scidd = *scidd; + assert(sciddpk->scidd.dir == 0 || sciddpk->scidd.dir == 1); +} + +const char *fmt_sciddir_or_pubkey(const tal_t *ctx, + const struct sciddir_or_pubkey *sciddpk) +{ + if (sciddpk->is_pubkey) + return fmt_pubkey(ctx, &sciddpk->pubkey); + return fmt_short_channel_id_dir(ctx, &sciddpk->scidd); +} diff --git a/common/sciddir_or_pubkey.h b/common/sciddir_or_pubkey.h new file mode 100644 index 000000000..f86e64e8b --- /dev/null +++ b/common/sciddir_or_pubkey.h @@ -0,0 +1,30 @@ +#ifndef LIGHTNING_COMMON_SCIDDIR_OR_PUBKEY_H +#define LIGHTNING_COMMON_SCIDDIR_OR_PUBKEY_H +#include "config.h" +#include +#include + +struct node_id; + +struct sciddir_or_pubkey { + bool is_pubkey; + /* Only valid if is_pubkey is true */ + struct pubkey pubkey; + /* Only valid if is_pubkey is false */ + struct short_channel_id_dir scidd; +}; +void towire_sciddir_or_pubkey(u8 **pptr, + const struct sciddir_or_pubkey *sciddpk); +void fromwire_sciddir_or_pubkey(const u8 **cursor, size_t *max, + struct sciddir_or_pubkey *sciddpk); + +void sciddir_or_pubkey_from_pubkey(struct sciddir_or_pubkey *sciddpk, + const struct pubkey *pubkey); +WARN_UNUSED_RESULT +bool sciddir_or_pubkey_from_node_id(struct sciddir_or_pubkey *sciddpk, + const struct node_id *node_id); +void sciddir_or_pubkey_from_scidd(struct sciddir_or_pubkey *sciddpk, + const struct short_channel_id_dir *scidd); +const char *fmt_sciddir_or_pubkey(const tal_t *ctx, + const struct sciddir_or_pubkey *sciddpk); +#endif /* LIGHTNING_COMMON_SCIDDIR_OR_PUBKEY_H */ diff --git a/devtools/Makefile b/devtools/Makefile index d9aa1d04f..3632b5476 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -37,6 +37,7 @@ DEVTOOLS_COMMON_OBJS := \ common/per_peer_state.o \ common/psbt_open.o \ common/pseudorand.o \ + common/sciddir_or_pubkey.o \ common/setup.o \ common/utils.o \ common/version.o \ diff --git a/devtools/print_wire.c b/devtools/print_wire.c index ed28805ae..15669899d 100644 --- a/devtools/print_wire.c +++ b/devtools/print_wire.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -366,6 +367,7 @@ PRINTWIRE_STRUCT_TYPE_TO_STRING(channel_id) PRINTWIRE_STRUCT_TYPE_TO_STRING(node_id) PRINTWIRE_STRUCT_TYPE_TO_STRING(preimage) PRINTWIRE_STRUCT_TYPE_TO_STRING(pubkey) +PRINTWIRE_STRUCT_TYPE_TO_STRING(sciddir_or_pubkey) PRINTWIRE_STRUCT_TYPE_TO_STRING(sha256) PRINTWIRE_STRUCT_TYPE_TO_STRING(secret) PRINTWIRE_ASSIGNABLE_STRUCT_TO_STRING(short_channel_id) diff --git a/devtools/print_wire.h b/devtools/print_wire.h index 96ae674f7..40c86d235 100644 --- a/devtools/print_wire.h +++ b/devtools/print_wire.h @@ -41,6 +41,7 @@ bool printwire_amount_sat(const char *fieldname, const u8 **cursor, size_t *plen bool printwire_amount_msat(const char *fieldname, const u8 **cursor, size_t *plen); bool printwire_preimage(const char *fieldname, const u8 **cursor, size_t *plen); bool printwire_pubkey(const char *fieldname, const u8 **cursor, size_t *plen); +bool printwire_sciddir_or_pubkey(const char *fieldname, const u8 **cursor, size_t *plen); bool printwire_node_id(const char *fieldname, const u8 **cursor, size_t *plen); bool printwire_secp256k1_ecdsa_signature(const char *fieldname, const u8 **cursor, size_t *plen); bool printwire_sha256(const char *fieldname, const u8 **cursor, size_t *plen); diff --git a/lightningd/test/run-check_node_announcement.c b/lightningd/test/run-check_node_announcement.c index 76935e22d..26c801708 100644 --- a/lightningd/test/run-check_node_announcement.c +++ b/lightningd/test/run-check_node_announcement.c @@ -32,6 +32,9 @@ u8 *get_agreed_channelfeatures(const tal_t *ctx UNNEEDED, /* Generated stub for node_id_cmp */ int node_id_cmp(const struct node_id *a UNNEEDED, const struct node_id *b UNNEEDED) { fprintf(stderr, "node_id_cmp called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_bigsize */ void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) { fprintf(stderr, "towire_bigsize called!\n"); abort(); } diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 42fc8f3aa..216d1e4d5 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -218,6 +218,9 @@ struct plugins *plugins_new(const tal_t *ctx UNNEEDED, struct log_book *log_book void plugins_set_builtin_plugins_dir(struct plugins *plugins UNNEEDED, const char *dir UNNEEDED) { fprintf(stderr, "plugins_set_builtin_plugins_dir called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for resend_closing_transactions */ void resend_closing_transactions(struct lightningd *ld UNNEEDED) { fprintf(stderr, "resend_closing_transactions called!\n"); abort(); } diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 9cc48b9b8..3b2772e53 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -150,6 +150,9 @@ bool plugin_hook_call_(struct lightningd *ld UNNEEDED, const char *cmd_id TAKES UNNEEDED, tal_t *cb_arg STEALS UNNEEDED) { fprintf(stderr, "plugin_hook_call_ called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_bigsize */ void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) { fprintf(stderr, "towire_bigsize called!\n"); abort(); } diff --git a/lightningd/test/run-log-pruning.c b/lightningd/test/run-log-pruning.c index e6dfd00c1..d5e5d8a0f 100644 --- a/lightningd/test/run-log-pruning.c +++ b/lightningd/test/run-log-pruning.c @@ -91,6 +91,9 @@ void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED bool param(struct command *cmd UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t params[] UNNEEDED, ...) { fprintf(stderr, "param called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_bigsize */ void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) { fprintf(stderr, "towire_bigsize called!\n"); abort(); } diff --git a/lightningd/test/run-shuffle_fds.c b/lightningd/test/run-shuffle_fds.c index eb4e54c6f..e56927c67 100644 --- a/lightningd/test/run-shuffle_fds.c +++ b/lightningd/test/run-shuffle_fds.c @@ -101,6 +101,9 @@ struct logger *new_logger(const tal_t *ctx UNNEEDED, struct log_book *record UNN /* Generated stub for new_peer_fd_arr */ struct peer_fd *new_peer_fd_arr(const tal_t *ctx UNNEEDED, const int *fd UNNEEDED) { fprintf(stderr, "new_peer_fd_arr called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for subdaemon_path */ const char *subdaemon_path(const tal_t *ctx UNNEEDED, const struct lightningd *ld UNNEEDED, const char *name UNNEEDED) { fprintf(stderr, "subdaemon_path called!\n"); abort(); } diff --git a/plugins/test/run-funder_policy.c b/plugins/test/run-funder_policy.c index 8db20983f..d2e00fbf0 100644 --- a/plugins/test/run-funder_policy.c +++ b/plugins/test/run-funder_policy.c @@ -15,6 +15,9 @@ bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, /* Generated stub for fromwire_node_id */ void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED) { fprintf(stderr, "fromwire_node_id called!\n"); abort(); } +/* Generated stub for pubkey_from_node_id */ +bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) +{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } /* Generated stub for towire_bigsize */ void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) { fprintf(stderr, "towire_bigsize called!\n"); abort(); } diff --git a/wire/Makefile b/wire/Makefile index 1bc528cee..35616d9ae 100644 --- a/wire/Makefile +++ b/wire/Makefile @@ -36,7 +36,7 @@ WIRE_PRINT_SRC := \ WIRE_PRINT_HEADERS := $(WIRE_PRINT_SRC:.c=.h) -WIRE_OBJS := $(WIRE_SRC:.c=.o) +WIRE_OBJS := $(WIRE_SRC:.c=.o) common/sciddir_or_pubkey.o WIRE_PRINT_OBJS := $(WIRE_PRINT_SRC:.c=.o) WIRE_BOLT12_OBJS := $(WIRE_BOLT12_SRC:.c=.o) $(WIRE_OBJS) $(WIRE_PRINT_OBJS) $(WIRE_BOLT12_OBJS): $(WIRE_HEADERS) $(WIRE_PRINT_HEADERS)