github.com/decred/dcrlnd@v0.7.6/lnwire/custom.go (about) 1 package lnwire 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 ) 8 9 // CustomTypeStart is the start of the custom type range for peer messages as 10 // defined in BOLT 01. 11 var CustomTypeStart MessageType = 32768 12 13 // Custom represents an application-defined wire message. 14 type Custom struct { 15 Type MessageType 16 Data []byte 17 } 18 19 // A compile time check to ensure FundingCreated implements the lnwire.Message 20 // interface. 21 var _ Message = (*Custom)(nil) 22 23 // NewCustom instanties a new custom message. 24 func NewCustom(msgType MessageType, data []byte) (*Custom, error) { 25 if msgType < CustomTypeStart { 26 return nil, errors.New("msg type not in custom range") 27 } 28 29 return &Custom{ 30 Type: msgType, 31 Data: data, 32 }, nil 33 } 34 35 // Encode serializes the target Custom message into the passed io.Writer 36 // implementation. 37 // 38 // This is part of the lnwire.Message interface. 39 func (c *Custom) Encode(b *bytes.Buffer, pver uint32) error { 40 _, err := b.Write(c.Data) 41 return err 42 } 43 44 // Decode deserializes the serialized Custom message stored in the passed 45 // io.Reader into the target Custom message. 46 // 47 // This is part of the lnwire.Message interface. 48 func (c *Custom) Decode(r io.Reader, pver uint32) error { 49 var b bytes.Buffer 50 if _, err := io.Copy(&b, r); err != nil { 51 return err 52 } 53 54 c.Data = b.Bytes() 55 56 return nil 57 } 58 59 // MsgType returns the uint32 code which uniquely identifies this message as a 60 // Custom message on the wire. 61 // 62 // This is part of the lnwire.Message interface. 63 func (c *Custom) MsgType() MessageType { 64 return c.Type 65 }