2015-12-23 09:08:34 +01:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2015-12-30 14:38:57 +01:00
|
|
|
"bytes"
|
2015-12-23 09:08:34 +01:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2016-05-23 22:50:36 +02:00
|
|
|
"io"
|
|
|
|
|
2016-12-24 23:55:31 +01:00
|
|
|
"net"
|
|
|
|
|
2016-12-07 16:46:22 +01:00
|
|
|
"github.com/go-errors/errors"
|
2016-05-15 16:17:44 +02:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-01-05 22:56:27 +01:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-05-23 22:50:36 +02:00
|
|
|
"github.com/roasbeef/btcd/txscript"
|
2016-05-15 16:17:44 +02:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2015-12-23 09:08:34 +01:00
|
|
|
)
|
|
|
|
|
2016-05-23 22:50:36 +02:00
|
|
|
// MaxSliceLength is the maximum allowed lenth for any opaque byte slices in
|
|
|
|
// the wire protocol.
|
|
|
|
const MaxSliceLength = 65535
|
2016-01-05 17:19:22 +01:00
|
|
|
|
2016-05-23 22:50:36 +02:00
|
|
|
// PkScript is simple type definition which represents a raw serialized public
|
|
|
|
// key script.
|
2015-12-23 09:08:34 +01:00
|
|
|
type PkScript []byte
|
|
|
|
|
2016-05-23 22:50:36 +02:00
|
|
|
// CreditsAmount are the native currency unit used within the Lightning Network.
|
|
|
|
// Credits are denominated in sub-satoshi amounts, so micro-satoshis (1/1000).
|
|
|
|
// This value is purposefully signed in order to allow the expression of negative
|
|
|
|
// fees.
|
2016-02-18 13:29:08 +01:00
|
|
|
//
|
2016-01-17 02:14:35 +01:00
|
|
|
// "In any science-fiction movie, anywhere in the galaxy, currency is referred
|
|
|
|
// to as 'credits.'"
|
|
|
|
// --Sam Humphries. Ebert, Roger (1999). Ebert's bigger little movie
|
|
|
|
// glossary. Andrews McMeel. p. 172.
|
2016-02-18 13:29:08 +01:00
|
|
|
//
|
2016-05-23 22:50:36 +02:00
|
|
|
// https://en.wikipedia.org/wiki/List_of_fictional_currencies
|
|
|
|
// https://en.wikipedia.org/wiki/Fictional_currency#Trends_in_the_use_of_fictional_currencies
|
|
|
|
// http://tvtropes.org/pmwiki/pmwiki.php/Main/WeWillSpendCreditsInTheFuture
|
2016-01-17 02:14:35 +01:00
|
|
|
// US Display format: 1 BTC = 100,000,000'000 XCB
|
|
|
|
// Or in BTC = 1.00000000'000
|
2016-05-23 22:50:36 +02:00
|
|
|
// Credits (XCB, accountants should use XCB :^)
|
|
|
|
type CreditsAmount int64
|
2015-12-27 03:20:25 +01:00
|
|
|
|
2016-05-23 22:50:36 +02:00
|
|
|
// ToSatoshi converts an amount in Credits to the coresponding amount
|
|
|
|
// expressed in Satoshis.
|
|
|
|
//
|
|
|
|
// NOTE: This function rounds down by default (floor).
|
2016-02-29 09:45:38 +01:00
|
|
|
func (c CreditsAmount) ToSatoshi() int64 {
|
|
|
|
return int64(c / 1000)
|
|
|
|
}
|
|
|
|
|
2017-02-17 10:29:23 +01:00
|
|
|
// addressType specifies the network protocol and version that should be used
|
|
|
|
// when connecting to a node at a particular address.
|
|
|
|
type addressType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
tcp4Addr addressType = 1
|
|
|
|
tcp6Addr addressType = 2
|
|
|
|
onionAddr addressType = 3
|
|
|
|
)
|
|
|
|
|
2016-05-23 22:51:21 +02:00
|
|
|
// writeElement is a one-stop shop to write the big endian representation of
|
|
|
|
// any element which is to be serialized for the wire protocol. The passed
|
2017-04-20 00:58:44 +02:00
|
|
|
// io.Writer should be backed by an appropriately sized byte slice, or be able
|
|
|
|
// to dynamically expand to accommodate additional data.
|
2016-05-23 22:51:21 +02:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): this should eventually draw from a buffer pool for
|
|
|
|
// serialization.
|
|
|
|
// TODO(roasbeef): switch to var-ints for all?
|
2015-12-31 08:34:40 +01:00
|
|
|
func writeElement(w io.Writer, element interface{}) error {
|
2015-12-23 09:08:34 +01:00
|
|
|
switch e := element.(type) {
|
|
|
|
case uint8:
|
|
|
|
var b [1]byte
|
2017-02-23 20:07:01 +01:00
|
|
|
b[0] = e
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
2015-12-23 09:08:34 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-01-05 17:19:22 +01:00
|
|
|
case uint16:
|
|
|
|
var b [2]byte
|
2017-02-23 20:07:01 +01:00
|
|
|
binary.BigEndian.PutUint16(b[:], e)
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
2016-01-05 17:19:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-10-23 22:39:54 +02:00
|
|
|
case ErrorCode:
|
|
|
|
var b [2]byte
|
|
|
|
binary.BigEndian.PutUint16(b[:], uint16(e))
|
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-16 13:25:36 +01:00
|
|
|
case btcutil.Amount:
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], uint64(e))
|
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
2016-01-05 17:19:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-23 09:08:34 +01:00
|
|
|
case uint32:
|
|
|
|
var b [4]byte
|
2017-02-23 20:07:01 +01:00
|
|
|
binary.BigEndian.PutUint32(b[:], e)
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
2015-12-23 09:08:34 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-30 14:38:57 +01:00
|
|
|
case uint64:
|
|
|
|
var b [8]byte
|
2017-02-23 20:07:01 +01:00
|
|
|
binary.BigEndian.PutUint64(b[:], e)
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
2015-12-30 14:38:57 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-23 09:08:34 +01:00
|
|
|
case *btcec.PublicKey:
|
|
|
|
var b [33]byte
|
|
|
|
serializedPubkey := e.SerializeCompressed()
|
|
|
|
copy(b[:], serializedPubkey)
|
2016-05-23 22:51:21 +02:00
|
|
|
// TODO(roasbeef): use WriteVarBytes here?
|
|
|
|
if _, err := w.Write(b[:]); err != nil {
|
2015-12-23 09:08:34 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-30 14:38:57 +01:00
|
|
|
case *btcec.Signature:
|
2016-12-08 21:56:37 +01:00
|
|
|
var b [64]byte
|
|
|
|
err := serializeSigToWire(&b, e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-30 14:38:57 +01:00
|
|
|
}
|
2016-12-08 21:56:37 +01:00
|
|
|
// Write buffer
|
|
|
|
if _, err = w.Write(b[:]); err != nil {
|
2015-12-30 14:38:57 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-04-17 03:11:39 +02:00
|
|
|
case PingPayload:
|
|
|
|
var l [2]byte
|
|
|
|
binary.BigEndian.PutUint16(l[:], uint16(len(e)))
|
|
|
|
if _, err := w.Write(l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case PongPayload:
|
|
|
|
var l [2]byte
|
|
|
|
binary.BigEndian.PutUint16(l[:], uint16(len(e)))
|
|
|
|
if _, err := w.Write(l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 00:23:40 +02:00
|
|
|
case ErrorData:
|
|
|
|
var l [2]byte
|
|
|
|
binary.BigEndian.PutUint16(l[:], uint16(len(e)))
|
|
|
|
if _, err := w.Write(l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-21 07:02:42 +01:00
|
|
|
case OpaqueReason:
|
|
|
|
var l [2]byte
|
|
|
|
binary.BigEndian.PutUint16(l[:], uint16(len(e)))
|
|
|
|
if _, err := w.Write(l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-05 17:19:22 +01:00
|
|
|
case []byte:
|
2017-02-16 13:25:36 +01:00
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
2016-05-31 00:42:53 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-12-23 09:08:34 +01:00
|
|
|
case PkScript:
|
2017-04-20 01:04:38 +02:00
|
|
|
// The largest script we'll accept is a p2wsh which is exactly
|
|
|
|
// 34 bytes long.
|
2015-12-23 09:08:34 +01:00
|
|
|
scriptLength := len(e)
|
2017-04-20 01:04:38 +02:00
|
|
|
if scriptLength > 34 {
|
2017-02-23 20:56:47 +01:00
|
|
|
return fmt.Errorf("'PkScript' too long")
|
2015-12-23 09:08:34 +01:00
|
|
|
}
|
2016-05-23 22:51:21 +02:00
|
|
|
|
|
|
|
if err := wire.WriteVarBytes(w, 0, e); err != nil {
|
2015-12-23 09:08:34 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-02-16 13:31:19 +01:00
|
|
|
case *FeatureVector:
|
|
|
|
if err := e.Encode(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-16 13:34:44 +01:00
|
|
|
|
|
|
|
case wire.OutPoint:
|
2016-05-31 00:42:53 +02:00
|
|
|
var h [32]byte
|
|
|
|
copy(h[:], e.Hash[:])
|
|
|
|
if _, err := w.Write(h[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:18:31 +02:00
|
|
|
if e.Index > math.MaxUint16 {
|
|
|
|
return fmt.Errorf("index for outpoint (%v) is "+
|
|
|
|
"greater than max index of %v", e.Index,
|
|
|
|
math.MaxUint16)
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:22:20 +02:00
|
|
|
var idx [2]byte
|
|
|
|
binary.BigEndian.PutUint16(idx[:], uint16(e.Index))
|
2016-05-31 00:42:53 +02:00
|
|
|
if _, err := w.Write(idx[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 00:22:20 +02:00
|
|
|
|
|
|
|
case ChannelID:
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:22:37 +02:00
|
|
|
case ShortChannelID:
|
2016-12-07 16:46:22 +01:00
|
|
|
// Check that field fit in 3 bytes and write the blockHeight
|
|
|
|
if e.BlockHeight > ((1 << 24) - 1) {
|
|
|
|
return errors.New("block height should fit in 3 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockHeight [4]byte
|
|
|
|
binary.BigEndian.PutUint32(blockHeight[:], e.BlockHeight)
|
|
|
|
|
|
|
|
if _, err := w.Write(blockHeight[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that field fit in 3 bytes and write the txIndex
|
|
|
|
if e.TxIndex > ((1 << 24) - 1) {
|
|
|
|
return errors.New("tx index should fit in 3 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var txIndex [4]byte
|
|
|
|
binary.BigEndian.PutUint32(txIndex[:], e.TxIndex)
|
|
|
|
if _, err := w.Write(txIndex[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the txPosition
|
|
|
|
var txPosition [2]byte
|
|
|
|
binary.BigEndian.PutUint16(txPosition[:], e.TxPosition)
|
|
|
|
if _, err := w.Write(txPosition[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *net.TCPAddr:
|
2017-02-17 10:29:23 +01:00
|
|
|
if e.IP.To4() != nil {
|
|
|
|
var descriptor [1]byte
|
|
|
|
descriptor[0] = uint8(tcp4Addr)
|
|
|
|
if _, err := w.Write(descriptor[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-07 16:46:22 +01:00
|
|
|
|
2017-02-17 10:29:23 +01:00
|
|
|
var ip [4]byte
|
|
|
|
copy(ip[:], e.IP.To4())
|
|
|
|
if _, err := w.Write(ip[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var descriptor [1]byte
|
|
|
|
descriptor[0] = uint8(tcp6Addr)
|
|
|
|
if _, err := w.Write(descriptor[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var ip [16]byte
|
|
|
|
copy(ip[:], e.IP.To16())
|
|
|
|
if _, err := w.Write(ip[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var port [2]byte
|
|
|
|
binary.BigEndian.PutUint16(port[:], uint16(e.Port))
|
2016-12-07 16:46:22 +01:00
|
|
|
if _, err := w.Write(port[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 10:29:23 +01:00
|
|
|
case []net.Addr:
|
|
|
|
// Write out the number of addresses.
|
|
|
|
if err := writeElement(w, uint16(len(e))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the actual addresses.
|
|
|
|
for _, address := range e {
|
|
|
|
if err := writeElement(w, address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 16:46:22 +01:00
|
|
|
case RGB:
|
|
|
|
err := writeElements(w,
|
|
|
|
e.red,
|
|
|
|
e.green,
|
|
|
|
e.blue,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Alias:
|
2017-02-16 13:25:36 +01:00
|
|
|
if err := writeElements(w, e.data[:]); err != nil {
|
2016-12-07 16:46:22 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-23 09:08:34 +01:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type in writeElement: %T", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:51:21 +02:00
|
|
|
// writeElements is writes each element in the elements slice to the passed
|
|
|
|
// io.Writer using writeElement.
|
2015-12-31 08:34:40 +01:00
|
|
|
func writeElements(w io.Writer, elements ...interface{}) error {
|
2015-12-28 12:24:16 +01:00
|
|
|
for _, element := range elements {
|
2015-12-31 08:34:40 +01:00
|
|
|
err := writeElement(w, element)
|
2015-12-28 12:24:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:51:21 +02:00
|
|
|
// readElement is a one-stop utility function to deserialize any datastructure
|
|
|
|
// encoded using the serialization format of lnwire.
|
2015-12-31 08:34:40 +01:00
|
|
|
func readElement(r io.Reader, element interface{}) error {
|
2015-12-23 09:08:34 +01:00
|
|
|
var err error
|
|
|
|
switch e := element.(type) {
|
2015-12-27 03:20:25 +01:00
|
|
|
case *uint8:
|
2015-12-27 08:52:20 +01:00
|
|
|
var b [1]uint8
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := r.Read(b[:]); err != nil {
|
2015-12-27 03:20:25 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-27 08:52:20 +01:00
|
|
|
*e = b[0]
|
2016-01-05 17:19:22 +01:00
|
|
|
case *uint16:
|
|
|
|
var b [2]byte
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
2016-01-05 17:19:22 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.BigEndian.Uint16(b[:])
|
2016-10-23 22:39:54 +02:00
|
|
|
case *ErrorCode:
|
|
|
|
var b [2]byte
|
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = ErrorCode(binary.BigEndian.Uint16(b[:]))
|
2015-12-23 09:08:34 +01:00
|
|
|
case *uint32:
|
|
|
|
var b [4]byte
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
2015-12-23 09:08:34 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.BigEndian.Uint32(b[:])
|
2015-12-30 14:38:57 +01:00
|
|
|
case *uint64:
|
|
|
|
var b [8]byte
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
2015-12-30 14:38:57 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.BigEndian.Uint64(b[:])
|
2015-12-27 03:20:25 +01:00
|
|
|
case *btcutil.Amount:
|
|
|
|
var b [8]byte
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
2015-12-27 03:20:25 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-27 08:52:20 +01:00
|
|
|
*e = btcutil.Amount(int64(binary.BigEndian.Uint64(b[:])))
|
|
|
|
case **btcec.PublicKey:
|
2016-12-07 16:46:22 +01:00
|
|
|
var b [btcec.PubKeyBytesLenCompressed]byte
|
2016-05-23 22:51:21 +02:00
|
|
|
if _, err = io.ReadFull(r, b[:]); err != nil {
|
2015-12-27 03:20:25 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-05-23 22:51:21 +02:00
|
|
|
|
|
|
|
pubKey, err := btcec.ParsePubKey(b[:], btcec.S256())
|
2015-12-30 14:38:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-23 22:51:21 +02:00
|
|
|
*e = pubKey
|
2017-02-16 13:31:19 +01:00
|
|
|
case **FeatureVector:
|
|
|
|
f, err := NewFeatureVectorFromReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = f
|
|
|
|
|
2015-12-30 14:38:57 +01:00
|
|
|
case **btcec.Signature:
|
2016-12-08 21:56:37 +01:00
|
|
|
var b [64]byte
|
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
2015-12-30 14:38:57 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-12-08 21:56:37 +01:00
|
|
|
err = deserializeSigFromWire(e, b)
|
2015-12-30 14:38:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-21 07:02:42 +01:00
|
|
|
case *OpaqueReason:
|
|
|
|
var l [2]byte
|
|
|
|
if _, err := io.ReadFull(r, l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reasonLen := binary.BigEndian.Uint16(l[:])
|
|
|
|
|
|
|
|
*e = OpaqueReason(make([]byte, reasonLen))
|
|
|
|
if _, err := io.ReadFull(r, *e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 00:23:40 +02:00
|
|
|
case *ErrorData:
|
|
|
|
var l [2]byte
|
|
|
|
if _, err := io.ReadFull(r, l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errorLen := binary.BigEndian.Uint16(l[:])
|
|
|
|
|
|
|
|
*e = ErrorData(make([]byte, errorLen))
|
|
|
|
if _, err := io.ReadFull(r, *e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 03:11:39 +02:00
|
|
|
case *PingPayload:
|
|
|
|
var l [2]byte
|
|
|
|
if _, err := io.ReadFull(r, l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pingLen := binary.BigEndian.Uint16(l[:])
|
|
|
|
|
|
|
|
*e = PingPayload(make([]byte, pingLen))
|
|
|
|
if _, err := io.ReadFull(r, *e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *PongPayload:
|
|
|
|
var l [2]byte
|
|
|
|
if _, err := io.ReadFull(r, l[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pongLen := binary.BigEndian.Uint16(l[:])
|
|
|
|
|
|
|
|
*e = PongPayload(make([]byte, pongLen))
|
|
|
|
if _, err := io.ReadFull(r, *e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-16 13:25:36 +01:00
|
|
|
case []byte:
|
|
|
|
if _, err := io.ReadFull(r, e); err != nil {
|
2016-01-05 17:19:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-12-27 03:20:25 +01:00
|
|
|
case *PkScript:
|
2017-04-20 01:04:38 +02:00
|
|
|
pkScript, err := wire.ReadVarBytes(r, 0, 34, "pkscript")
|
2015-12-27 03:20:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-23 22:51:21 +02:00
|
|
|
*e = pkScript
|
2017-02-16 13:34:44 +01:00
|
|
|
case *wire.OutPoint:
|
2016-05-31 00:42:53 +02:00
|
|
|
var h [32]byte
|
|
|
|
if _, err = io.ReadFull(r, h[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-05 22:56:27 +01:00
|
|
|
hash, err := chainhash.NewHash(h[:])
|
2016-05-31 00:42:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 00:22:20 +02:00
|
|
|
|
|
|
|
var idxBytes [2]byte
|
2016-05-31 00:42:53 +02:00
|
|
|
_, err = io.ReadFull(r, idxBytes[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 00:22:20 +02:00
|
|
|
index := binary.BigEndian.Uint16(idxBytes[:])
|
2016-06-21 06:55:07 +02:00
|
|
|
|
2017-04-17 00:22:20 +02:00
|
|
|
*e = wire.OutPoint{
|
|
|
|
Hash: *hash,
|
|
|
|
Index: uint32(index),
|
|
|
|
}
|
|
|
|
|
|
|
|
case *ChannelID:
|
|
|
|
if _, err := io.ReadFull(r, e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:22:37 +02:00
|
|
|
case *ShortChannelID:
|
2016-12-07 16:46:22 +01:00
|
|
|
var blockHeight [4]byte
|
|
|
|
if _, err = io.ReadFull(r, blockHeight[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txIndex [4]byte
|
|
|
|
if _, err = io.ReadFull(r, txIndex[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txPosition [2]byte
|
|
|
|
if _, err = io.ReadFull(r, txPosition[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:22:37 +02:00
|
|
|
*e = ShortChannelID{
|
2016-12-07 16:46:22 +01:00
|
|
|
BlockHeight: binary.BigEndian.Uint32(blockHeight[:]),
|
|
|
|
TxIndex: binary.BigEndian.Uint32(txIndex[:]),
|
|
|
|
TxPosition: binary.BigEndian.Uint16(txPosition[:]),
|
|
|
|
}
|
|
|
|
|
2017-02-17 10:29:23 +01:00
|
|
|
case *[]net.Addr:
|
2017-03-30 01:55:28 +02:00
|
|
|
var numAddrsBytes [2]byte
|
|
|
|
if _, err = io.ReadFull(r, numAddrsBytes[:]); err != nil {
|
2016-12-07 16:46:22 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-30 01:55:28 +02:00
|
|
|
numAddrs := binary.BigEndian.Uint16(numAddrsBytes[:])
|
|
|
|
addresses := make([]net.Addr, 0, numAddrs)
|
|
|
|
|
|
|
|
for i := 0; i < int(numAddrs); i++ {
|
2017-02-17 10:29:23 +01:00
|
|
|
var descriptor [1]byte
|
|
|
|
if _, err = io.ReadFull(r, descriptor[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
address := &net.TCPAddr{}
|
|
|
|
switch descriptor[0] {
|
|
|
|
case 1:
|
|
|
|
var ip [4]byte
|
|
|
|
if _, err = io.ReadFull(r, ip[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
address.IP = (net.IP)(ip[:])
|
|
|
|
case 2:
|
|
|
|
var ip [16]byte
|
|
|
|
if _, err = io.ReadFull(r, ip[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
address.IP = (net.IP)(ip[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
var port [2]byte
|
|
|
|
if _, err = io.ReadFull(r, port[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-07 16:46:22 +01:00
|
|
|
|
2017-02-17 10:29:23 +01:00
|
|
|
address.Port = int(binary.BigEndian.Uint16(port[:]))
|
|
|
|
addresses = append(addresses, address)
|
2016-12-07 16:46:22 +01:00
|
|
|
}
|
2017-02-17 10:29:23 +01:00
|
|
|
*e = addresses
|
2016-12-07 16:46:22 +01:00
|
|
|
case *RGB:
|
|
|
|
err := readElements(r,
|
|
|
|
&e.red,
|
|
|
|
&e.green,
|
|
|
|
&e.blue,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *Alias:
|
|
|
|
var a [32]byte
|
2017-02-16 13:25:36 +01:00
|
|
|
if err := readElements(r, a[:]); err != nil {
|
2016-12-07 16:46:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-10-05 22:47:02 +02:00
|
|
|
|
2017-04-19 23:59:16 +02:00
|
|
|
*e = newAlias(a[:])
|
2015-12-27 03:20:25 +01:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type in readElement: %T", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:51:21 +02:00
|
|
|
// readElements deserializes a variable number of elements into the passed
|
|
|
|
// io.Reader, with each element being deserialized according to the readElement
|
|
|
|
// function.
|
2015-12-31 08:34:40 +01:00
|
|
|
func readElements(r io.Reader, elements ...interface{}) error {
|
2015-12-28 12:24:16 +01:00
|
|
|
for _, element := range elements {
|
2015-12-31 08:34:40 +01:00
|
|
|
err := readElement(r, element)
|
2015-12-28 12:24:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-23 09:08:34 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|