lnwire: let ChannelID implement RecordProducer
What changed, and why it matters
This commit adds a small plumbing feature that lets a ChannelID be packaged as a TLV record. It is a straightforward, additive code change with no visible security relevance on its own.
No security action required; review as normal feature code.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch implements the tlv.RecordProducer interface for lnwire.ChannelID by adding a Record() method plus encodeChannelID/decodeChannelID helpers. It uses existing tlv.EBytes32/DBytes32 helpers and adds no parsing of untrusted input, no state mutation, no cryptographic operations, and no change to existing wire behavior. It is purely enabling infrastructure.
Changed components
lnwire/channel_id.goInspect captured patch +36 / −0
diff --git a/lnwire/channel_id.go b/lnwire/channel_id.go
index 1615eb7..5c9eca3 100644
--- a/lnwire/channel_id.go
+++ b/lnwire/channel_id.go
@@ -3,10 +3,12 @@ package lnwire
import (
"encoding/binary"
"encoding/hex"
+ "io"
"math"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
+ "github.com/lightningnetwork/lnd/tlv"
)
const (
@@ -36,6 +38,40 @@ func (c ChannelID) String() string {
return hex.EncodeToString(c[:])
}
+// Record returns a TLV record that can be used to encode/decode a ChannelID
+// to/from a TLV stream.
+func (c *ChannelID) Record() tlv.Record {
+ return tlv.MakeStaticRecord(0, c, 32, encodeChannelID, decodeChannelID)
+}
+
+func encodeChannelID(w io.Writer, val interface{}, buf *[8]byte) error {
+ if v, ok := val.(*ChannelID); ok {
+ bigSize := [32]byte(*v)
+
+ return tlv.EBytes32(w, &bigSize, buf)
+ }
+
+ return tlv.NewTypeForEncodingErr(val, "lnwire.ChannelID")
+}
+
+func decodeChannelID(r io.Reader, val interface{}, buf *[8]byte,
+ l uint64) error {
+
+ if v, ok := val.(*ChannelID); ok {
+ var id [32]byte
+ err := tlv.DBytes32(r, &id, buf, l)
+ if err != nil {
+ return err
+ }
+
+ *v = id
+
+ return nil
+ }
+
+ return tlv.NewTypeForDecodingErr(val, "lnwire.ChannelID", l, l)
+}
+
// NewChanIDFromOutPoint converts a target OutPoint into a ChannelID that is
// usable within the network. In order to convert the OutPoint into a ChannelID,
// we XOR the lower 2-bytes of the txid within the OutPoint with the big-endian
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.