github.com/decred/dcrlnd@v0.7.6/lnwire/channel_type.go (about) 1 package lnwire 2 3 import ( 4 "io" 5 6 "github.com/decred/dcrlnd/tlv" 7 ) 8 9 const ( 10 // ChannelTypeRecordType is the type of the experimental record used 11 // to denote which channel type is being negotiated. 12 ChannelTypeRecordType tlv.Type = 1 13 ) 14 15 // ChannelType represents a specific channel type as a set of feature bits that 16 // comprise it. 17 type ChannelType RawFeatureVector 18 19 // featureBitLen returns the length in bytes of the encoded feature bits. 20 func (c ChannelType) featureBitLen() uint64 { 21 fv := RawFeatureVector(c) 22 return uint64(fv.SerializeSize()) 23 } 24 25 // Record returns a TLV record that can be used to encode/decode the channel 26 // type from a given TLV stream. 27 func (c *ChannelType) Record() tlv.Record { 28 return tlv.MakeDynamicRecord( 29 ChannelTypeRecordType, c, c.featureBitLen, channelTypeEncoder, 30 channelTypeDecoder, 31 ) 32 } 33 34 // channelTypeEncoder is a custom TLV encoder for the ChannelType record. 35 func channelTypeEncoder(w io.Writer, val interface{}, buf *[8]byte) error { 36 if v, ok := val.(*ChannelType); ok { 37 // Encode the feature bits as a byte slice without its length 38 // prepended, as that's already taken care of by the TLV record. 39 fv := RawFeatureVector(*v) 40 return fv.encode(w, fv.SerializeSize(), 8) 41 } 42 43 return tlv.NewTypeForEncodingErr(val, "lnwire.ChannelType") 44 } 45 46 // channelTypeDecoder is a custom TLV decoder for the ChannelType record. 47 func channelTypeDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error { 48 if v, ok := val.(*ChannelType); ok { 49 fv := NewRawFeatureVector() 50 if err := fv.decode(r, int(l), 8); err != nil { 51 return err 52 } 53 *v = ChannelType(*fv) 54 return nil 55 } 56 57 return tlv.NewTypeForEncodingErr(val, "lnwire.ChannelType") 58 }