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 (
|
|
|
|
MsgInit MessageType = 16
|
|
|
|
MsgError = 17
|
|
|
|
MsgPing = 18
|
|
|
|
MsgPong = 19
|
|
|
|
MsgSingleFundingRequest = 32
|
|
|
|
MsgSingleFundingResponse = 33
|
|
|
|
MsgSingleFundingComplete = 34
|
|
|
|
MsgSingleFundingSignComplete = 35
|
|
|
|
MsgFundingLocked = 36
|
|
|
|
MsgCloseRequest = 39
|
|
|
|
MsgCloseComplete = 40
|
|
|
|
MsgUpdateAddHTLC = 128
|
|
|
|
MsgUpdateFufillHTLC = 130
|
|
|
|
MsgUpdateFailHTLC = 131
|
|
|
|
MsgCommitSig = 132
|
|
|
|
MsgRevokeAndAck = 133
|
|
|
|
MsgChannelAnnouncement = 256
|
|
|
|
MsgNodeAnnouncement = 257
|
|
|
|
MsgChannelUpdate = 258
|
|
|
|
MsgAnnounceSignatures = 259
|
2015-12-28 14:24:16 +03:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-05-23 23:49:10 +03:00
|
|
|
Decode(io.Reader, uint32) error
|
|
|
|
Encode(io.Writer, uint32) error
|
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-04-20 01:57:43 +03:00
|
|
|
case MsgSingleFundingRequest:
|
2016-05-23 23:49:10 +03:00
|
|
|
msg = &SingleFundingRequest{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgSingleFundingResponse:
|
2016-05-23 23:49:10 +03:00
|
|
|
msg = &SingleFundingResponse{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgSingleFundingComplete:
|
2016-05-23 23:49:10 +03:00
|
|
|
msg = &SingleFundingComplete{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgSingleFundingSignComplete:
|
2016-05-23 23:49:10 +03:00
|
|
|
msg = &SingleFundingSignComplete{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgFundingLocked:
|
2017-01-31 05:45:28 +03:00
|
|
|
msg = &FundingLocked{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgCloseRequest:
|
2015-12-31 13:42:25 +03:00
|
|
|
msg = &CloseRequest{}
|
2017-04-20 01:57:43 +03:00
|
|
|
case MsgCloseComplete:
|
2015-12-31 13:42:25 +03:00
|
|
|
msg = &CloseComplete{}
|
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-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-04-20 02:13:08 +03:00
|
|
|
"type %x 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
|
|
|
}
|