Skip to content

Commit 51e52fa

Browse files
committed
bitcoin: have random_scid() function.
Signed-off-by: Rusty Russell <[email protected]>
1 parent c55720b commit 51e52fa

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

bitcoin/short_channel_id.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22
#include <bitcoin/short_channel_id.h>
33
#include <ccan/tal/str/str.h>
4+
#include <sodium/randombytes.h>
45
#include <stdio.h>
56
#include <wire/wire.h>
67

@@ -99,3 +100,10 @@ struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max
99100
scid.u64 = fromwire_u64(cursor, max);
100101
return scid;
101102
}
103+
104+
struct short_channel_id random_scid(void)
105+
{
106+
struct short_channel_id scid;
107+
randombytes_buf(&scid, sizeof(scid));
108+
return scid;
109+
}

bitcoin/short_channel_id.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ void towire_short_channel_id(u8 **pptr,
9898
struct short_channel_id short_channel_id);
9999
struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max);
100100

101+
/* Set to random bytes */
102+
struct short_channel_id random_scid(void);
101103
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */

lightningd/channel.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <lightningd/opening_common.h>
1818
#include <lightningd/peer_control.h>
1919
#include <lightningd/subd.h>
20-
#include <sodium/randombytes.h>
2120
#include <wallet/txfilter.h>
2221
#include <wire/peer_wire.h>
2322

@@ -273,7 +272,7 @@ static void channel_set_random_local_alias(struct channel *channel)
273272
{
274273
assert(channel->alias[LOCAL] == NULL);
275274
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
276-
randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id));
275+
*channel->alias[LOCAL] = random_scid();
277276
/* We don't check for uniqueness. We would crash on a clash, but your machine is
278277
* probably broken beyond repair if it gets two equal 64 bit numbers */
279278
chanmap_add(channel->peer->ld, channel, *channel->alias[LOCAL]);

wallet/db.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,13 +2020,11 @@ static void migrate_initialize_alias_local(struct lightningd *ld,
20202020
tal_free(stmt);
20212021

20222022
for (size_t i = 0; i < tal_count(ids); i++) {
2023-
struct short_channel_id alias;
20242023
stmt = db_prepare_v2(db, SQL("UPDATE channels"
20252024
" SET alias_local = ?"
20262025
" WHERE id = ?;"));
20272026
/* We don't even check for clashes! */
2028-
randombytes_buf(&alias, sizeof(alias));
2029-
db_bind_short_channel_id(stmt, alias);
2027+
db_bind_short_channel_id(stmt, random_scid());
20302028
db_bind_u64(stmt, ids[i]);
20312029
db_exec_prepared_v2(stmt);
20322030
tal_free(stmt);

0 commit comments

Comments
 (0)