2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2017-07-20 20:49:45 +02:00
|
|
|
#include <bitcoin/short_channel_id.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <stdio.h>
|
2020-05-15 12:32:43 +02:00
|
|
|
#include <wire/wire.h>
|
2017-07-20 20:49:45 +02:00
|
|
|
|
2019-01-12 21:11:07 +01:00
|
|
|
/* BOLT#07:
|
|
|
|
*
|
|
|
|
* The `short_channel_id` is the unique description of the funding
|
|
|
|
* transaction. It is constructed as follows:
|
|
|
|
|
|
|
|
* 1. the most significant 3 bytes: indicating the block height
|
|
|
|
* 2. the next 3 bytes: indicating the transaction index within the block
|
|
|
|
* 3. the least significant 2 bytes: indicating the output index that pays to the channel.
|
|
|
|
*
|
|
|
|
* The standard human readable format for `short_channel_id` is created
|
|
|
|
* by printing the above components, in the order: block height,
|
|
|
|
* transaction index, and output index. Each component is printed as a
|
|
|
|
* decimal number, and separated from each other by the small letter
|
|
|
|
* `x`. For example, a `short_channel_id` might be written as
|
|
|
|
* `539268x845x1`, indicating a channel on the output 1 of the
|
|
|
|
* transaction at index 845 of the block at height 539268.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2019-01-21 01:57:43 +01:00
|
|
|
bool mk_short_channel_id(struct short_channel_id *scid,
|
|
|
|
u64 blocknum, u64 txnum, u64 outnum)
|
2018-03-01 10:22:24 +01:00
|
|
|
{
|
2019-01-21 01:57:43 +01:00
|
|
|
if ((blocknum & 0xFFFFFF) != blocknum
|
|
|
|
|| (txnum & 0xFFFFFF) != txnum
|
|
|
|
|| (outnum & 0xFFFF) != outnum)
|
|
|
|
return false;
|
2018-03-01 10:22:24 +01:00
|
|
|
scid->u64 = (((u64)blocknum & 0xFFFFFF) << 40 |
|
|
|
|
((u64)txnum & 0xFFFFFF) << 16 |
|
|
|
|
(outnum & 0xFFFF));
|
2019-01-21 01:57:43 +01:00
|
|
|
return true;
|
2018-03-01 10:22:24 +01:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:49:45 +02:00
|
|
|
bool short_channel_id_from_str(const char *str, size_t strlen,
|
2019-09-06 08:41:41 +02:00
|
|
|
struct short_channel_id *dst)
|
2017-07-20 20:49:45 +02:00
|
|
|
{
|
|
|
|
u32 blocknum, txnum;
|
|
|
|
u16 outnum;
|
|
|
|
int matches;
|
|
|
|
|
|
|
|
char buf[strlen + 1];
|
|
|
|
memcpy(buf, str, strlen);
|
|
|
|
buf[strlen] = 0;
|
|
|
|
|
2019-01-10 22:42:18 +01:00
|
|
|
matches = sscanf(buf, "%ux%ux%hu", &blocknum, &txnum, &outnum);
|
2019-01-21 01:57:43 +01:00
|
|
|
return matches == 3
|
|
|
|
&& mk_short_channel_id(dst, blocknum, txnum, outnum);
|
2017-07-20 20:49:45 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 01:40:16 +01:00
|
|
|
char *fmt_short_channel_id(const tal_t *ctx, struct short_channel_id scid)
|
2017-07-20 20:49:45 +02:00
|
|
|
{
|
2019-01-10 22:42:18 +01:00
|
|
|
return tal_fmt(ctx, "%dx%dx%d",
|
2024-03-20 02:59:51 +01:00
|
|
|
short_channel_id_blocknum(scid),
|
|
|
|
short_channel_id_txnum(scid),
|
|
|
|
short_channel_id_outnum(scid));
|
2018-01-30 20:11:27 +01:00
|
|
|
}
|
2019-01-15 05:11:27 +01:00
|
|
|
|
|
|
|
bool short_channel_id_dir_from_str(const char *str, size_t strlen,
|
2019-09-06 08:41:41 +02:00
|
|
|
struct short_channel_id_dir *scidd)
|
2019-01-15 05:11:27 +01:00
|
|
|
{
|
|
|
|
const char *slash = memchr(str, '/', strlen);
|
|
|
|
if (!slash || slash + 2 != str + strlen)
|
|
|
|
return false;
|
2019-09-06 08:41:41 +02:00
|
|
|
if (!short_channel_id_from_str(str, slash - str, &scidd->scid))
|
2019-01-15 05:11:27 +01:00
|
|
|
return false;
|
|
|
|
if (slash[1] == '0')
|
|
|
|
scidd->dir = 0;
|
|
|
|
else if (slash[1] == '1')
|
|
|
|
scidd->dir = 1;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-20 01:40:16 +01:00
|
|
|
char *fmt_short_channel_id_dir(const tal_t *ctx,
|
|
|
|
const struct short_channel_id_dir *scidd)
|
2019-01-15 05:11:27 +01:00
|
|
|
{
|
2024-03-20 01:40:16 +01:00
|
|
|
char *str, *scidstr = fmt_short_channel_id(NULL, scidd->scid);
|
2019-01-15 05:11:27 +01:00
|
|
|
str = tal_fmt(ctx, "%s/%u", scidstr, scidd->dir);
|
|
|
|
tal_free(scidstr);
|
|
|
|
return str;
|
|
|
|
}
|
2019-02-20 12:31:48 +01:00
|
|
|
|
2020-05-15 12:32:43 +02:00
|
|
|
void towire_short_channel_id(u8 **pptr,
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id short_channel_id)
|
2020-05-15 12:32:43 +02:00
|
|
|
{
|
2024-03-20 02:59:51 +01:00
|
|
|
towire_u64(pptr, short_channel_id.u64);
|
2020-05-15 12:32:43 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max)
|
2020-05-15 12:32:43 +02:00
|
|
|
{
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id scid;
|
|
|
|
|
|
|
|
scid.u64 = fromwire_u64(cursor, max);
|
|
|
|
return scid;
|
2020-05-15 12:32:43 +02:00
|
|
|
}
|