Skip to content

Commit 5e7ca54

Browse files
committed
lnwire: let ChannelID implement RecordProducer
So that we can use it as a TLV record type.
1 parent 4addfd1 commit 5e7ca54

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lnwire/channel_id.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package lnwire
33
import (
44
"encoding/binary"
55
"encoding/hex"
6+
"io"
67
"math"
78

89
"github.com/btcsuite/btcd/chaincfg/chainhash"
910
"github.com/btcsuite/btcd/wire"
11+
"github.com/lightningnetwork/lnd/tlv"
1012
)
1113

1214
const (
@@ -36,6 +38,40 @@ func (c ChannelID) String() string {
3638
return hex.EncodeToString(c[:])
3739
}
3840

41+
// Record returns a TLV record that can be used to encode/decode a ChannelID
42+
// to/from a TLV stream.
43+
func (c *ChannelID) Record() tlv.Record {
44+
return tlv.MakeStaticRecord(0, c, 32, encodeChannelID, decodeChannelID)
45+
}
46+
47+
func encodeChannelID(w io.Writer, val interface{}, buf *[8]byte) error {
48+
if v, ok := val.(*ChannelID); ok {
49+
bigSize := [32]byte(*v)
50+
51+
return tlv.EBytes32(w, &bigSize, buf)
52+
}
53+
54+
return tlv.NewTypeForEncodingErr(val, "lnwire.ChannelID")
55+
}
56+
57+
func decodeChannelID(r io.Reader, val interface{}, buf *[8]byte,
58+
l uint64) error {
59+
60+
if v, ok := val.(*ChannelID); ok {
61+
var id [32]byte
62+
err := tlv.DBytes32(r, &id, buf, l)
63+
if err != nil {
64+
return err
65+
}
66+
67+
*v = id
68+
69+
return nil
70+
}
71+
72+
return tlv.NewTypeForDecodingErr(val, "lnwire.ChannelID", l, l)
73+
}
74+
3975
// NewChanIDFromOutPoint converts a target OutPoint into a ChannelID that is
4076
// usable within the network. In order to convert the OutPoint into a ChannelID,
4177
// we XOR the lower 2-bytes of the txid within the OutPoint with the big-endian

0 commit comments

Comments
 (0)