2015-12-28 14:24:16 +03:00
|
|
|
package lnwire
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// code derived from https://github .com/btcsuite/btcd/blob/master/wire/message.go
|
|
|
|
|
2015-12-28 14:24:16 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-20 01:57:43 +03:00
|
|
|
"encoding/binary"
|
2015-12-28 14:24:16 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-12-31 19:38:33 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:49:10 +03:00
|
|
|
// MaxMessagePayload is the maximum bytes a message can be regardless of other
|
|
|
|
// individual limits imposed by messages themselves.
|
2017-04-20 01:09:12 +03:00
|
|
|
const MaxMessagePayload = 65535 // 65KB
|
2016-05-23 23:49:10 +03:00
|
|
|
|
2017-04-20 01:57:43 +03:00
|
|
|
// MessageType is the unique 2 byte big-endian integer that indicates the type
|
|
|
|
// of message on the wire. All messages have a very simple header which
|
|
|
|
// consists simply of 2-byte message type. We omit a length field, and checksum
|
|
|
|
// as the Lighting Protocol is intended to be encapsulated within a
|
|
|
|
// confidential+authenticated cryptographic messaging protocol.
|
|
|
|
type MessageType uint16
|
2015-12-31 12:19:54 +03:00
|
|
|
|
2017-04-21 01:50:06 +03:00
|
|
|
// The currently defined message types within this current version of the
|
|
|
|
// Lightning protocol.
|
2017-04-20 01:57:43 +03:00
|
|
|
const (
|
2017-07-29 02:30:39 +03:00
|
|
|
MsgInit MessageType = 16
|
|
|
|
MsgError = 17
|
|
|
|
MsgPing = 18
|
|
|
|
MsgPong = 19
|
|
|
|
MsgOpenChannel = 32
|
|
|
|
MsgAcceptChannel = 33
|
|
|
|
MsgFundingCreated = 34
|
|
|
|
MsgFundingSigned = 35
|
|
|
|
MsgFundingLocked = 36
|
|
|
|
MsgShutdown = 39
|
|
|
|
MsgClosingSigned = 40
|
|
|
|
MsgUpdateAddHTLC = 128
|
|
|
|
MsgUpdateFufillHTLC = 130
|
|
|
|
MsgUpdateFailHTLC = 131
|
|
|
|
MsgCommitSig = 132
|
|
|
|
MsgRevokeAndAck = 133
|
|
|
|
MsgUpdateFailMalformedHTLC = 135
|
2017-07-09 01:12:36 +03:00
|
|
|
MsgChannelReestablish = 136
|
2017-07-29 02:30:39 +03:00
|
|
|
MsgUpdateFee = 137
|
|
|
|
MsgChannelAnnouncement = 256
|
|
|
|
MsgNodeAnnouncement = 257
|
|
|
|
MsgChannelUpdate = 258
|
|
|
|
MsgAnnounceSignatures = 259
|
2015-12-28 14:24:16 +03:00
|
|
|
)
|
|
|
|
|
2017-05-02 02:29:15 +03:00
|
|
|
// String return the string representation of message type.
|
|
|
|
func (t MessageType) String() string {
|
|
|
|
switch t {
|
|
|
|
case MsgInit:
|
|
|
|
return "Init"
|
2017-07-29 02:30:39 +03:00
|
|
|
case MsgOpenChannel:
|
|
|
|
return "MsgOpenChannel"
|
|
|
|
case MsgAcceptChannel:
|
|
|
|
return "MsgAcceptChannel"
|
|
|
|
case MsgFundingCreated:
|
|
|
|
return "MsgFundingCreated"
|
|
|
|
case MsgFundingSigned:
|
|
|
|
return "MsgFundingSigned"
|
2017-05-02 02:29:15 +03:00
|
|
|
case MsgFundingLocked:
|
|
|
|
return "FundingLocked"
|
|
|
|
case MsgShutdown:
|
|
|
|
return "Shutdown"
|
|
|
|
case MsgClosingSigned:
|
|
|
|
return "ClosingSigned"
|
|
|
|
case MsgUpdateAddHTLC:
|
|
|
|
return "UpdateAddHTLC"
|
|
|
|
case MsgUpdateFailHTLC:
|
|
|
|
return "UpdateFailHTLC"
|
|
|
|
case MsgUpdateFufillHTLC:
|
|
|
|
return "UpdateFufillHTLC"
|
|
|
|
case MsgCommitSig:
|
|
|
|
return "CommitSig"
|
|
|
|
case MsgRevokeAndAck:
|
|
|
|
return "RevokeAndAck"
|
2017-06-28 18:22:23 +03:00
|
|
|
case MsgUpdateFailMalformedHTLC:
|
|
|
|
return "UpdateFailMalformedHTLC"
|
2017-07-09 01:12:36 +03:00
|
|
|
case MsgChannelReestablish:
|
|
|
|
return "ChannelReestablish"
|
2017-05-02 02:29:15 +03:00
|
|
|
case MsgError:
|
|
|
|
return "Error"
|
|
|
|
case MsgChannelAnnouncement:
|
|
|
|
return "ChannelAnnouncement"
|
|
|
|
case MsgChannelUpdate:
|
|
|
|
return "ChannelUpdate"
|
|
|
|
case MsgNodeAnnouncement:
|
|
|
|
return "NodeAnnouncement"
|
|
|
|
case MsgPing:
|
|
|
|
return "Ping"
|
|
|
|
case MsgAnnounceSignatures:
|
|
|
|
return "AnnounceSignatures"
|
|
|
|
case MsgPong:
|
|
|
|
return "Pong"
|
2017-08-22 08:25:32 +03:00
|
|
|
case MsgUpdateFee:
|
|
|
|
return "UpdateFee"
|
2017-05-02 02:29:15 +03:00
|
|
|
default:
|
|
|
|
return "<unknown>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 05:03:34 +03:00
|
|
|
// UnknownMessage is an implementation of the error interface that allows the
|
|
|
|
// creation of an error in response to an unknown message.
|
|
|
|
type UnknownMessage struct {
|
2017-04-20 01:57:43 +03:00
|
|
|
messageType MessageType
|
2017-01-17 05:03:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a human readable string describing the error.
|
|
|
|
//
|
|
|
|
// This is part of the error interface.
|
|
|
|
func (u *UnknownMessage) Error() string {
|
|
|
|
return fmt.Sprintf("unable to parse message of unknown type: %v",
|
|
|
|
u.messageType)
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:55:41 +03:00
|
|
|
// Serializable is an interface which defines a lightning wire serializable
|
|
|
|
// object.
|
|
|
|
type Serializable interface {
|
|
|
|
// Decode reads the bytes stream and converts it to the object.
|
|
|
|
Decode(io.Reader, uint32) error
|
|
|
|
|
2017-07-29 02:30:39 +03:00
|
|
|
// Encode converts object to the bytes stream and write it into the
|
|
|
|
// writer.
|
2017-06-08 20:55:41 +03:00
|
|
|
Encode(io.Writer, uint32) error
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:49:10 +03:00
|
|
|
// Message is an interface that defines a lightning wire protocol message. The
|
|
|
|
// interface is general in order to allow implementing types full control over
|
|
|
|
// the representation of its data.
|
2015-12-28 14:24:16 +03:00
|
|
|
type Message interface {
|
2017-06-08 20:55:41 +03:00
|
|
|
Serializable
|
2017-04-20 01:57:43 +03:00
|
|
|
MsgType() MessageType
|
2016-05-23 23:49:10 +03:00
|
|
|
MaxPayloadLength(uint32) uint32
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:49:10 +03:00
|
|
|
// makeEmptyMessage creates a new empty message of the proper concrete type
|
2017-04-20 01:57:43 +03:00
|
|
|
// based on the passed message type.
|
|
|
|
func makeEmptyMessage(msgType MessageType) (Message, error) {
|
2015-12-28 14:24:16 +03:00
|
|
|
var msg Message
|
|
|
|
|
2017-04-20 01:57:43 +03:00
|
|
|
switch msgType {
|
|
|
|
case MsgInit:
|
2017-02-16 15:31:19 +03:00
|
|
|
msg = &Init{}
|
2017-07-29 02:30:39 +03:00
|
|
|
case MsgOpenChannel:
|
|
|
|
msg = &OpenChannel{}
|
|
|
|
case MsgAcceptChannel:
|
|
|
|
msg = &AcceptChannel{}
|
|
|
|
case MsgFundingCreated:
|
|
|
|
msg = &FundingCreated{}
|
|
|
|
case MsgFundingSigned:
|
|
|
|
msg = &FundingSigned{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgFundingLocked:
|
2017-01-31 05:45:28 +03:00
|
|
|
msg = &FundingLocked{}
|
2017-03-09 02:32:11 +03:00
|
|
|
case MsgShutdown:
|
|
|
|
msg = &Shutdown{}
|
|
|
|
case MsgClosingSigned:
|
|
|
|
msg = &ClosingSigned{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgUpdateAddHTLC:
|
2017-02-16 15:08:34 +03:00
|
|
|
msg = &UpdateAddHTLC{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgUpdateFailHTLC:
|
2017-02-16 15:25:36 +03:00
|
|
|
msg = &UpdateFailHTLC{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgUpdateFufillHTLC:
|
2017-02-16 15:34:09 +03:00
|
|
|
msg = &UpdateFufillHTLC{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgCommitSig:
|
2017-02-16 15:04:58 +03:00
|
|
|
msg = &CommitSig{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgRevokeAndAck:
|
2017-02-10 02:28:32 +03:00
|
|
|
msg = &RevokeAndAck{}
|
2017-07-14 21:28:40 +03:00
|
|
|
case MsgUpdateFee:
|
|
|
|
msg = &UpdateFee{}
|
2017-06-28 18:22:23 +03:00
|
|
|
case MsgUpdateFailMalformedHTLC:
|
|
|
|
msg = &UpdateFailMalformedHTLC{}
|
2017-07-09 01:12:36 +03:00
|
|
|
case MsgChannelReestablish:
|
|
|
|
msg = &ChannelReestablish{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgError:
|
2017-04-17 01:23:40 +03:00
|
|
|
msg = &Error{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgChannelAnnouncement:
|
2016-12-07 18:46:22 +03:00
|
|
|
msg = &ChannelAnnouncement{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgChannelUpdate:
|
|
|
|
msg = &ChannelUpdate{}
|
|
|
|
case MsgNodeAnnouncement:
|
2016-12-07 18:46:22 +03:00
|
|
|
msg = &NodeAnnouncement{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgPing:
|
2016-11-11 04:09:14 +03:00
|
|
|
msg = &Ping{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgAnnounceSignatures:
|
2017-03-27 18:07:18 +03:00
|
|
|
msg = &AnnounceSignatures{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgPong:
|
2016-11-11 04:09:14 +03:00
|
|
|
msg = &Pong{}
|
2015-12-28 14:24:16 +03:00
|
|
|
default:
|
2017-04-20 01:57:43 +03:00
|
|
|
return nil, fmt.Errorf("unknown message type [%d]", msgType)
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:49:10 +03:00
|
|
|
// WriteMessage writes a lightning Message to w including the necessary header
|
|
|
|
// information and returns the number of bytes written.
|
2017-04-20 02:13:08 +03:00
|
|
|
func WriteMessage(w io.Writer, msg Message, pver uint32) (int, error) {
|
2015-12-28 14:24:16 +03:00
|
|
|
totalBytes := 0
|
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
// Encode the message payload itself into a temporary buffer.
|
|
|
|
// TODO(roasbeef): create buffer pool
|
2015-12-28 14:24:16 +03:00
|
|
|
var bw bytes.Buffer
|
2017-04-20 02:13:08 +03:00
|
|
|
if err := msg.Encode(&bw, pver); err != nil {
|
2015-12-28 14:24:16 +03:00
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
payload := bw.Bytes()
|
|
|
|
lenp := len(payload)
|
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
// Enforce maximum overall message payload.
|
2015-12-28 14:24:16 +03:00
|
|
|
if lenp > MaxMessagePayload {
|
2016-05-23 23:49:10 +03:00
|
|
|
return totalBytes, fmt.Errorf("message payload is too large - "+
|
|
|
|
"encoded %d bytes, but maximum message payload is %d bytes",
|
|
|
|
lenp, MaxMessagePayload)
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
// Enforce maximum message payload on the message type.
|
2015-12-28 14:24:16 +03:00
|
|
|
mpl := msg.MaxPayloadLength(pver)
|
|
|
|
if uint32(lenp) > mpl {
|
2016-05-23 23:49:10 +03:00
|
|
|
return totalBytes, fmt.Errorf("message payload is too large - "+
|
|
|
|
"encoded %d bytes, but maximum message payload of "+
|
2017-07-29 02:32:12 +03:00
|
|
|
"type %v is %d bytes", lenp, msg.MsgType(), mpl)
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
// With the initial sanity checks complete, we'll now write out the
|
|
|
|
// message type itself.
|
|
|
|
var mType [2]byte
|
|
|
|
binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
|
|
|
|
n, err := w.Write(mType[:])
|
2015-12-28 14:24:16 +03:00
|
|
|
totalBytes += n
|
|
|
|
if err != nil {
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
// With the message type written, we'll now write out the raw payload
|
|
|
|
// itself.
|
2015-12-28 14:24:16 +03:00
|
|
|
n, err = w.Write(payload)
|
|
|
|
totalBytes += n
|
2017-04-20 02:13:08 +03:00
|
|
|
|
2017-02-23 21:59:50 +03:00
|
|
|
return totalBytes, err
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-21 01:41:43 +03:00
|
|
|
// ReadMessage reads, validates, and parses the next Lightning message from r
|
|
|
|
// for the provided protocol version.
|
|
|
|
func ReadMessage(r io.Reader, pver uint32) (Message, error) {
|
2017-04-20 02:13:08 +03:00
|
|
|
// First, we'll read out the first two bytes of the message so we can
|
|
|
|
// create the proper empty message.
|
|
|
|
var mType [2]byte
|
2017-04-21 01:41:43 +03:00
|
|
|
if _, err := io.ReadFull(r, mType[:]); err != nil {
|
|
|
|
return nil, err
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
msgType := MessageType(binary.BigEndian.Uint16(mType[:]))
|
2015-12-28 14:24:16 +03:00
|
|
|
|
2017-04-20 02:13:08 +03:00
|
|
|
// Now that we know the target message type, we can create the proper
|
|
|
|
// empty message type and decode the message into it.
|
|
|
|
msg, err := makeEmptyMessage(msgType)
|
2015-12-28 14:24:16 +03:00
|
|
|
if err != nil {
|
2017-04-21 01:41:43 +03:00
|
|
|
return nil, err
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
2017-04-20 02:13:08 +03:00
|
|
|
if err := msg.Decode(r, pver); err != nil {
|
2017-04-21 01:41:43 +03:00
|
|
|
return nil, err
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|
|
|
|
|
2017-04-21 01:41:43 +03:00
|
|
|
return msg, nil
|
2015-12-28 14:24:16 +03:00
|
|
|
}
|