common: add helpers for bitcoin blockids.
What changed, and why it matters
This commit adds helper functions for converting Bitcoin block IDs between hex strings and internal binary structures, and exposes them through JSON parsing/serialization helpers. It is a straightforward code refactoring and feature addition with no apparent security relevance.
No security action required. Treat as normal code maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces bitcoin_blkid_from_hex() and bitcoin_blkid_to_hex() in bitcoin/block.c, replacing an earlier static helper and a test-local workaround that previously piggybacked on bitcoin_txid_from_hex() by casting a bitcoin_txid as a fake carrier for the block ID’s double-SHA256 value. It also adds json_to_bitcoin_blkid() and json_add_bitcoin_blkid() in common/json_parse.c and common/json_stream.c. The new helpers correctly reverse the byte order to match Bitcoin’s display convention. No callers are changed beyond the test file removing its local duplicate.
Changed components
bitcoin/block.cbitcoin/block.hbitcoin/test/run-bitcoin_block_from_hex.ccommon/json_parse.ccommon/json_parse.hcommon/json_stream.ccommon/json_stream.hInspect captured patch +45 / −16
diff --git a/bitcoin/block.c b/bitcoin/block.c
index 5a36a7df..6838f2c3 100644
--- a/bitcoin/block.c
+++ b/bitcoin/block.c
@@ -228,16 +228,24 @@ void bitcoin_block_blkid(const struct bitcoin_block *b,
*out = b->hdr.hash;
}
-static bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blockid,
- char *hexstr, size_t hexstr_len)
+bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
+ struct bitcoin_blkid *blkid)
{
- struct bitcoin_txid fake_txid;
- fake_txid.shad = blockid->shad;
- return bitcoin_txid_to_hex(&fake_txid, hexstr, hexstr_len);
+ if (!hex_decode(hexstr, hexstr_len, blkid, sizeof(*blkid)))
+ return false;
+ reverse_bytes(blkid->shad.sha.u.u8, sizeof(blkid->shad.sha.u.u8));
+ return true;
}
-char *fmt_bitcoin_blkid(const tal_t *ctx,
- const struct bitcoin_blkid *blkid)
+bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blkid,
+ char *hexstr, size_t hexstr_len)
+{
+ struct sha256_double rev = blkid->shad;
+ reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8));
+ return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len);
+}
+
+char *fmt_bitcoin_blkid(const tal_t *ctx, const struct bitcoin_blkid *blkid)
{
char *hexstr = tal_arr(ctx, char, hex_str_size(sizeof(*blkid)));
diff --git a/bitcoin/block.h b/bitcoin/block.h
index a3289ee7..8a945ca4 100644
--- a/bitcoin/block.h
+++ b/bitcoin/block.h
@@ -52,6 +52,11 @@ void fromwire_chainparams(const u8 **cursor, size_t *max,
const struct chainparams **chainparams);
void towire_chainparams(u8 **cursor, const struct chainparams *chainparams);
+bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
+ struct bitcoin_blkid *blkid);
+bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blkid,
+ char *hexstr, size_t hexstr_len);
+
char *fmt_bitcoin_blkid(const tal_t *ctx,
const struct bitcoin_blkid *blkid);
diff --git a/bitcoin/test/run-bitcoin_block_from_hex.c b/bitcoin/test/run-bitcoin_block_from_hex.c
index 0dc0bd64..94b26ad5 100644
--- a/bitcoin/test/run-bitcoin_block_from_hex.c
+++ b/bitcoin/test/run-bitcoin_block_from_hex.c
@@ -62,15 +62,6 @@ static const char block[] =
STRUCTEQ_DEF(sha256_double, 0, sha);
-static bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
- struct bitcoin_blkid *blockid)
-{
- struct bitcoin_txid fake_txid;
- if (!bitcoin_txid_from_hex(hexstr, hexstr_len, &fake_txid))
- return false;
- blockid->shad = fake_txid.shad;
- return true;
-}
int main(int argc, const char *argv[])
{
struct bitcoin_blkid prev;
diff --git a/common/json_parse.c b/common/json_parse.c
index 7841e082..5c1c6831 100644
--- a/common/json_parse.c
+++ b/common/json_parse.c
@@ -601,6 +601,13 @@ bool json_to_txid(const char *buffer, const jsmntok_t *tok,
tok->end - tok->start, txid);
}
+bool json_to_bitcoin_blkid(const char *buffer, const jsmntok_t *tok,
+ struct bitcoin_blkid *blkid)
+{
+ return bitcoin_blkid_from_hex(buffer + tok->start,
+ tok->end - tok->start, blkid);
+}
+
bool json_to_outpoint(const char *buffer, const jsmntok_t *tok,
struct bitcoin_outpoint *op)
{
diff --git a/common/json_parse.h b/common/json_parse.h
index 4706c377..4c739154 100644
--- a/common/json_parse.h
+++ b/common/json_parse.h
@@ -108,6 +108,10 @@ bool json_to_msat(const char *buffer, const jsmntok_t *tok,
bool json_to_txid(const char *buffer, const jsmntok_t *tok,
struct bitcoin_txid *txid);
+/* Extract a bitcoin blkid from this */
+bool json_to_bitcoin_blkid(const char *buffer, const jsmntok_t *tok,
+ struct bitcoin_blkid *blkid);
+
/* Extract a bitcoin outpoint from this */
bool json_to_outpoint(const char *buffer, const jsmntok_t *tok,
struct bitcoin_outpoint *op);
diff --git a/common/json_stream.c b/common/json_stream.c
index 6a074607..3daefe46 100644
--- a/common/json_stream.c
+++ b/common/json_stream.c
@@ -455,6 +455,15 @@ void json_add_txid(struct json_stream *result, const char *fieldname,
json_add_string(result, fieldname, hex);
}
+void json_add_bitcoin_blkid(struct json_stream *result, const char *fieldname,
+ const struct bitcoin_blkid *blkid)
+{
+ char hex[hex_str_size(sizeof(*blkid))];
+
+ bitcoin_blkid_to_hex(blkid, hex, sizeof(hex));
+ json_add_string(result, fieldname, hex);
+}
+
void json_add_outpoint(struct json_stream *result, const char *fieldname,
const struct bitcoin_outpoint *out)
{
diff --git a/common/json_stream.h b/common/json_stream.h
index 7756c013..3263dfd9 100644
--- a/common/json_stream.h
+++ b/common/json_stream.h
@@ -31,6 +31,7 @@ struct short_channel_id;
struct sha256;
struct preimage;
struct bitcoin_tx;
+struct bitcoin_blkid;
struct wally_psbt;
struct lease_rates;
struct wireaddr;
@@ -310,6 +311,10 @@ void json_add_channel_id(struct json_stream *response,
void json_add_txid(struct json_stream *result, const char *fieldname,
const struct bitcoin_txid *txid);
+/* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */
+void json_add_bitcoin_blkid(struct json_stream *result, const char *fieldname,
+ const struct bitcoin_blkid *blkid);
+
/* '"fieldname" : "txid:n" */
void json_add_outpoint(struct json_stream *result, const char *fieldname,
const struct bitcoin_outpoint *out);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.