2015-12-28 12:24:16 +01:00
|
|
|
package lnwire
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// code derived from https://github .com/btcsuite/btcd/blob/master/wire/message.go
|
|
|
|
|
2015-12-28 12:24:16 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-20 00:57:43 +02:00
|
|
|
"encoding/binary"
|
2015-12-28 12:24:16 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-12-31 17:38:33 +01:00
|
|
|
)
|
|
|
|
|
2016-05-23 22:49:10 +02:00
|
|
|
// MaxMessagePayload is the maximum bytes a message can be regardless of other
|
|
|
|
// individual limits imposed by messages themselves.
|
2017-04-20 00:09:12 +02:00
|
|
|
const MaxMessagePayload = 65535 // 65KB
|
2016-05-23 22:49:10 +02:00
|
|
|
|
2017-04-20 00:57:43 +02: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 10:19:54 +01:00
|
|
|
|
2017-04-21 00:50:06 +02:00
|
|
|
// The currently defined message types within this current version of the
|
|
|
|
// Lightning protocol.
|
2017-04-20 00:57:43 +02:00
|
|
|
const (
|
2017-07-29 01:30:39 +02: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
|
|
|
|
MsgUpdateFee = 137
|
|
|
|
MsgChannelAnnouncement = 256
|
|
|
|
MsgNodeAnnouncement = 257
|
|
|
|
MsgChannelUpdate = 258
|
|
|
|
MsgAnnounceSignatures = 259
|
2015-12-28 12:24:16 +01:00
|
|
|
)
|
|
|
|
|
2017-05-02 01:29:15 +02:00
|
|
|
// String return the string representation of message type.
|
|
|
|
func (t MessageType) String() string {
|
|
|
|
switch t {
|
|
|
|
case MsgInit:
|
|
|
|
return "Init"
|
2017-07-29 01:30:39 +02:00
|
|
|
case MsgOpenChannel:
|
|
|
|
return "MsgOpenChannel"
|
|
|
|
case MsgAcceptChannel:
|
|
|
|
return "MsgAcceptChannel"
|
|
|
|
case MsgFundingCreated:
|
|
|
|
return "MsgFundingCreated"
|
|
|
|
case MsgFundingSigned:
|
|
|
|
return "MsgFundingSigned"
|
2017-05-02 01:29:15 +02: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 17:22:23 +02:00
|
|
|
case MsgUpdateFailMalformedHTLC:
|
|
|
|
return "UpdateFailMalformedHTLC"
|
2017-05-02 01:29:15 +02: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 07:25:32 +02:00
|
|
|
case MsgUpdateFee:
|
|
|
|
return "UpdateFee"
|
2017-05-02 01:29:15 +02:00
|
|
|
default:
|
|
|
|
return "<unknown>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 03:03:34 +01: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 00:57:43 +02:00
|
|
|
messageType MessageType
|
2017-01-17 03:03:34 +01: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 19:55:41 +02: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 01:30:39 +02:00
|
|
|
// Encode converts object to the bytes stream and write it into the
|
|
|
|
// writer.
|
2017-06-08 19:55:41 +02:00
|
|
|
Encode(io.Writer, uint32) error
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:49:10 +02: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 12:24:16 +01:00
|
|
|
type Message interface {
|
2017-06-08 19:55:41 +02:00
|
|
|
Serializable
|
2017-04-20 00:57:43 +02:00
|
|
|
MsgType() MessageType
|
2016-05-23 22:49:10 +02:00
|
|
|
MaxPayloadLength(uint32) uint32
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:49:10 +02:00
|
|
|
// makeEmptyMessage creates a new empty message of the proper concrete type
|
2017-04-20 00:57:43 +02:00
|
|
|
// based on the passed message type.
|
|
|
|
func makeEmptyMessage(msgType MessageType) (Message, error) {
|
2015-12-28 12:24:16 +01:00
|
|
|
var msg Message
|
|
|
|
|
2017-04-20 00:57:43 +02:00
|
|
|
switch msgType {
|
|
|
|
case MsgInit:
|
2017-02-16 13:31:19 +01:00
|
|
|
msg = &Init{}
|
2017-07-29 01:30:39 +02:00
|
|
|
case MsgOpenChannel:
|
|
|
|
msg = &OpenChannel{}
|
|
|
|
case MsgAcceptChannel:
|
|
|
|
msg = &AcceptChannel{}
|
|
|
|
case MsgFundingCreated:
|
|
|
|
msg = &FundingCreated{}
|
|
|
|
case MsgFundingSigned:
|
|
|
|
msg = &FundingSigned{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgFundingLocked:
|
2017-01-31 03:45:28 +01:00
|
|
|
msg = &FundingLocked{}
|
2017-03-09 00:32:11 +01:00
|
|
|
case MsgShutdown:
|
|
|
|
msg = &Shutdown{}
|
|
|
|
case MsgClosingSigned:
|
|
|
|
msg = &ClosingSigned{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgUpdateAddHTLC:
|
2017-02-16 13:08:34 +01:00
|
|
|
msg = &UpdateAddHTLC{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgUpdateFailHTLC:
|
2017-02-16 13:25:36 +01:00
|
|
|
msg = &UpdateFailHTLC{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgUpdateFufillHTLC:
|
2017-02-16 13:34:09 +01:00
|
|
|
msg = &UpdateFufillHTLC{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgCommitSig:
|
2017-02-16 13:04:58 +01:00
|
|
|
msg = &CommitSig{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgRevokeAndAck:
|
2017-02-10 00:28:32 +01:00
|
|
|
msg = &RevokeAndAck{}
|
2017-07-14 20:28:40 +02:00
|
|
|
case MsgUpdateFee:
|
|
|
|
msg = &UpdateFee{}
|
2017-06-28 17:22:23 +02:00
|
|
|
case MsgUpdateFailMalformedHTLC:
|
|
|
|
msg = &UpdateFailMalformedHTLC{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgError:
|
2017-04-17 00:23:40 +02:00
|
|
|
msg = &Error{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgChannelAnnouncement:
|
2016-12-07 16:46:22 +01:00
|
|
|
msg = &ChannelAnnouncement{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgChannelUpdate:
|
|
|
|
msg = &ChannelUpdate{}
|
|
|
|
case MsgNodeAnnouncement:
|
2016-12-07 16:46:22 +01:00
|
|
|
msg = &NodeAnnouncement{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgPing:
|
2016-11-11 02:09:14 +01:00
|
|
|
msg = &Ping{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgAnnounceSignatures:
|
2017-03-27 17:07:18 +02:00
|
|
|
msg = &AnnounceSignatures{}
|
2017-04-20 00:57:43 +02:00
|
|
|
case MsgPong:
|
2016-11-11 02:09:14 +01:00
|
|
|
msg = &Pong{}
|
2015-12-28 12:24:16 +01:00
|
|
|
default:
|
2017-04-20 00:57:43 +02:00
|
|
|
return nil, fmt.Errorf("unknown message type [%d]", msgType)
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:49:10 +02:00
|
|
|
// WriteMessage writes a lightning Message to w including the necessary header
|
|
|
|
// information and returns the number of bytes written.
|
2017-04-20 01:13:08 +02:00
|
|
|
func WriteMessage(w io.Writer, msg Message, pver uint32) (int, error) {
|
2015-12-28 12:24:16 +01:00
|
|
|
totalBytes := 0
|
|
|
|
|
2017-04-20 01:13:08 +02:00
|
|
|
// Encode the message payload itself into a temporary buffer.
|
|
|
|
// TODO(roasbeef): create buffer pool
|
2015-12-28 12:24:16 +01:00
|
|
|
var bw bytes.Buffer
|
2017-04-20 01:13:08 +02:00
|
|
|
if err := msg.Encode(&bw, pver); err != nil {
|
2015-12-28 12:24:16 +01:00
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
payload := bw.Bytes()
|
|
|
|
lenp := len(payload)
|
|
|
|
|
2017-04-20 01:13:08 +02:00
|
|
|
// Enforce maximum overall message payload.
|
2015-12-28 12:24:16 +01:00
|
|
|
if lenp > MaxMessagePayload {
|
2016-05-23 22:49:10 +02: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 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 01:13:08 +02:00
|
|
|
// Enforce maximum message payload on the message type.
|
2015-12-28 12:24:16 +01:00
|
|
|
mpl := msg.MaxPayloadLength(pver)
|
|
|
|
if uint32(lenp) > mpl {
|
2016-05-23 22:49:10 +02:00
|
|
|
return totalBytes, fmt.Errorf("message payload is too large - "+
|
|
|
|
"encoded %d bytes, but maximum message payload of "+
|
2017-07-29 01:32:12 +02:00
|
|
|
"type %v is %d bytes", lenp, msg.MsgType(), mpl)
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 01:13:08 +02: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 12:24:16 +01:00
|
|
|
totalBytes += n
|
|
|
|
if err != nil {
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:13:08 +02:00
|
|
|
// With the message type written, we'll now write out the raw payload
|
|
|
|
// itself.
|
2015-12-28 12:24:16 +01:00
|
|
|
n, err = w.Write(payload)
|
|
|
|
totalBytes += n
|
2017-04-20 01:13:08 +02:00
|
|
|
|
2017-02-23 19:59:50 +01:00
|
|
|
return totalBytes, err
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:41:43 +02: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 01:13:08 +02: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 00:41:43 +02:00
|
|
|
if _, err := io.ReadFull(r, mType[:]); err != nil {
|
|
|
|
return nil, err
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 01:13:08 +02:00
|
|
|
msgType := MessageType(binary.BigEndian.Uint16(mType[:]))
|
2015-12-28 12:24:16 +01:00
|
|
|
|
2017-04-20 01:13:08 +02: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 12:24:16 +01:00
|
|
|
if err != nil {
|
2017-04-21 00:41:43 +02:00
|
|
|
return nil, err
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
2017-04-20 01:13:08 +02:00
|
|
|
if err := msg.Decode(r, pver); err != nil {
|
2017-04-21 00:41:43 +02:00
|
|
|
return nil, err
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:41:43 +02:00
|
|
|
return msg, nil
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|