github.com/decred/dcrlnd@v0.7.6/channeldb/migration/lnwire21/channel_announcement.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  
     8  	"github.com/decred/dcrd/chaincfg/chainhash"
     9  )
    10  
    11  // ChannelAnnouncement message is used to announce the existence of a channel
    12  // between two peers in the overlay, which is propagated by the discovery
    13  // service over broadcast handler.
    14  type ChannelAnnouncement struct {
    15  	// This signatures are used by nodes in order to create cross
    16  	// references between node's channel and node. Requiring both nodes
    17  	// to sign indicates they are both willing to route other payments via
    18  	// this node.
    19  	NodeSig1 Sig
    20  	NodeSig2 Sig
    21  
    22  	// This signatures are used by nodes in order to create cross
    23  	// references between node's channel and node. Requiring the decred
    24  	// signatures proves they control the channel.
    25  	DecredSig1 Sig
    26  	DecredSig2 Sig
    27  
    28  	// Features is the feature vector that encodes the features supported
    29  	// by the target node. This field can be used to signal the type of the
    30  	// channel, or modifications to the fields that would normally follow
    31  	// this vector.
    32  	Features *RawFeatureVector
    33  
    34  	// ChainHash denotes the target chain that this channel was opened
    35  	// within. This value should be the genesis hash of the target chain.
    36  	ChainHash chainhash.Hash
    37  
    38  	// ShortChannelID is the unique description of the funding transaction,
    39  	// or where exactly it's located within the target blockchain.
    40  	ShortChannelID ShortChannelID
    41  
    42  	// The public keys of the two nodes who are operating the channel, such
    43  	// that is NodeID1 the numerically-lesser than NodeID2 (ascending
    44  	// numerical order).
    45  	NodeID1 [33]byte
    46  	NodeID2 [33]byte
    47  
    48  	// Public keys which corresponds to the keys which was declared in
    49  	// multisig funding transaction output.
    50  	DecredKey1 [33]byte
    51  	DecredKey2 [33]byte
    52  
    53  	// ExtraOpaqueData is the set of data that was appended to this
    54  	// message, some of which we may not actually know how to iterate or
    55  	// parse. By holding onto this data, we ensure that we're able to
    56  	// properly validate the set of signatures that cover these new fields,
    57  	// and ensure we're able to make upgrades to the network in a forwards
    58  	// compatible manner.
    59  	ExtraOpaqueData []byte
    60  }
    61  
    62  // A compile time check to ensure ChannelAnnouncement implements the
    63  // lnwire.Message interface.
    64  var _ Message = (*ChannelAnnouncement)(nil)
    65  
    66  // Decode deserializes a serialized ChannelAnnouncement stored in the passed
    67  // io.Reader observing the specified protocol version.
    68  //
    69  // This is part of the lnwire.Message interface.
    70  func (a *ChannelAnnouncement) Decode(r io.Reader, pver uint32) error {
    71  	err := ReadElements(r,
    72  		&a.NodeSig1,
    73  		&a.NodeSig2,
    74  		&a.DecredSig1,
    75  		&a.DecredSig2,
    76  		&a.Features,
    77  		a.ChainHash[:],
    78  		&a.ShortChannelID,
    79  		&a.NodeID1,
    80  		&a.NodeID2,
    81  		&a.DecredKey1,
    82  		&a.DecredKey2,
    83  	)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	// Now that we've read out all the fields that we explicitly know of,
    89  	// we'll collect the remainder into the ExtraOpaqueData field. If there
    90  	// aren't any bytes, then we'll snip off the slice to avoid carrying
    91  	// around excess capacity.
    92  	a.ExtraOpaqueData, err = ioutil.ReadAll(r)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	if len(a.ExtraOpaqueData) == 0 {
    97  		a.ExtraOpaqueData = nil
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  // Encode serializes the target ChannelAnnouncement into the passed io.Writer
   104  // observing the protocol version specified.
   105  //
   106  // This is part of the lnwire.Message interface.
   107  func (a *ChannelAnnouncement) Encode(w io.Writer, pver uint32) error {
   108  	return WriteElements(w,
   109  		a.NodeSig1,
   110  		a.NodeSig2,
   111  		a.DecredSig1,
   112  		a.DecredSig2,
   113  		a.Features,
   114  		a.ChainHash[:],
   115  		a.ShortChannelID,
   116  		a.NodeID1,
   117  		a.NodeID2,
   118  		a.DecredKey1,
   119  		a.DecredKey2,
   120  		a.ExtraOpaqueData,
   121  	)
   122  }
   123  
   124  // MsgType returns the integer uniquely identifying this message type on the
   125  // wire.
   126  //
   127  // This is part of the lnwire.Message interface.
   128  func (a *ChannelAnnouncement) MsgType() MessageType {
   129  	return MsgChannelAnnouncement
   130  }
   131  
   132  // MaxPayloadLength returns the maximum allowed payload size for this message
   133  // observing the specified protocol version.
   134  //
   135  // This is part of the lnwire.Message interface.
   136  func (a *ChannelAnnouncement) MaxPayloadLength(pver uint32) uint32 {
   137  	return 65533
   138  }
   139  
   140  // DataToSign is used to retrieve part of the announcement message which should
   141  // be signed.
   142  func (a *ChannelAnnouncement) DataToSign() ([]byte, error) {
   143  	// We should not include the signatures itself.
   144  	var w bytes.Buffer
   145  	err := WriteElements(&w,
   146  		a.Features,
   147  		a.ChainHash[:],
   148  		a.ShortChannelID,
   149  		a.NodeID1,
   150  		a.NodeID2,
   151  		a.DecredKey1,
   152  		a.DecredKey2,
   153  		a.ExtraOpaqueData,
   154  	)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	return w.Bytes(), nil
   160  }