From 6517104da6e347a86a41056d9b44b2c881da58eb Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 16 Apr 2025 14:47:18 -0700 Subject: [PATCH] lnutil: add LogPubKey helper function This captures a common pattern where we want to log a peer's public key along side each logging statement. --- lnutils/log.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lnutils/log.go b/lnutils/log.go index a588bf3ce..87057290b 100644 --- a/lnutils/log.go +++ b/lnutils/log.go @@ -1,8 +1,11 @@ package lnutils import ( + "log/slog" "strings" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btclog/v2" "github.com/davecgh/go-spew/spew" ) @@ -36,3 +39,14 @@ func NewSeparatorClosure() LogClosure { return strings.Repeat("=", 80) } } + +// LogPubKey returns a slog attribute for logging a public key in hex format. +func LogPubKey(key string, pubKey *btcec.PublicKey) slog.Attr { + // Handle nil pubkey gracefully, although callers should ideally prevent + // this. + if pubKey == nil { + return btclog.Fmt(key, "") + } + + return btclog.Hex6(key, pubKey.SerializeCompressed()) +}