mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 14:17:56 +01:00
lnwire: let ChannelID implement RecordProducer
So that we can use it as a TLV record type.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user