Skip to content

Commit

Permalink
common: add sciddir_or_pubkey type.
Browse files Browse the repository at this point in the history
This is proposed to be added to bolt 12, so we need a type to
represent it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed May 13, 2024
1 parent 081a2ee commit e30c835
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 1 deletion.
3 changes: 3 additions & 0 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down
3 changes: 3 additions & 0 deletions cli/test/run-human-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions cli/test/run-large-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions cli/test/run-remove-hint.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
1 change: 1 addition & 0 deletions common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
71 changes: 71 additions & 0 deletions common/sciddir_or_pubkey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "config.h"
#include <assert.h>
#include <common/node_id.h>
#include <common/sciddir_or_pubkey.h>
#include <wire/wire.h>

/* 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);
}
30 changes: 30 additions & 0 deletions common/sciddir_or_pubkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef LIGHTNING_COMMON_SCIDDIR_OR_PUBKEY_H
#define LIGHTNING_COMMON_SCIDDIR_OR_PUBKEY_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <bitcoin/short_channel_id.h>

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 */
1 change: 1 addition & 0 deletions devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 2 additions & 0 deletions devtools/print_wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ccan/mem/mem.h>
#include <ccan/utf8/utf8.h>
#include <common/decode_array.h>
#include <common/sciddir_or_pubkey.h>
#include <devtools/print_wire.h>
#include <errno.h>
#include <stdio.h>
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions devtools/print_wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-check_node_announcement.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-find_my_abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-log-pruning.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-shuffle_fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions plugins/test/run-funder_policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
2 changes: 1 addition & 1 deletion wire/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e30c835

Please sign in to comment.