forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
081a2ee
commit e30c835
Showing
18 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters