github.com/decred/dcrlnd@v0.7.6/lnwire/update_add_htlc.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  )
     7  
     8  // OnionPacketSize is the size of the serialized Sphinx onion packet included
     9  // in each UpdateAddHTLC message. The breakdown of the onion packet is as
    10  // follows: 1-byte version, 33-byte ephemeral public key (for ECDH), 1300-bytes
    11  // of per-hop data, and a 32-byte HMAC over the entire packet.
    12  const OnionPacketSize = 1366
    13  
    14  // UpdateAddHTLC is the message sent by Alice to Bob when she wishes to add an
    15  // HTLC to his remote commitment transaction. In addition to information
    16  // detailing the value, the ID, expiry, and the onion blob is also included
    17  // which allows Bob to derive the next hop in the route. The HTLC added by this
    18  // message is to be added to the remote node's "pending" HTLC's.  A subsequent
    19  // CommitSig message will move the pending HTLC to the newly created commitment
    20  // transaction, marking them as "staged".
    21  type UpdateAddHTLC struct {
    22  	// ChanID is the particular active channel that this UpdateAddHTLC is
    23  	// bound to.
    24  	ChanID ChannelID
    25  
    26  	// ID is the identification server for this HTLC. This value is
    27  	// explicitly included as it allows nodes to survive single-sided
    28  	// restarts. The ID value for this sides starts at zero, and increases
    29  	// with each offered HTLC.
    30  	ID uint64
    31  
    32  	// Amount is the amount of MilliAtoms this HTLC is worth.
    33  	Amount MilliAtom
    34  
    35  	// PaymentHash is the payment hash to be included in the HTLC this
    36  	// request creates. The pre-image to this HTLC must be revealed by the
    37  	// upstream peer in order to fully settle the HTLC.
    38  	PaymentHash [32]byte
    39  
    40  	// Expiry is the number of blocks after which this HTLC should expire.
    41  	// It is the receiver's duty to ensure that the outgoing HTLC has a
    42  	// sufficient expiry value to allow her to redeem the incoming HTLC.
    43  	Expiry uint32
    44  
    45  	// OnionBlob is the raw serialized mix header used to route an HTLC in
    46  	// a privacy-preserving manner. The mix header is defined currently to
    47  	// be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC,
    48  	// body).  First the receiving node should use the groupElement, and
    49  	// its current onion key to derive a shared secret with the source.
    50  	// Once the shared secret has been derived, the headerMAC should be
    51  	// checked FIRST. Note that the MAC only covers the routingInfo field.
    52  	// If the MAC matches, and the shared secret is fresh, then the node
    53  	// should strip off a layer of encryption, exposing the next hop to be
    54  	// used in the subsequent UpdateAddHTLC message.
    55  	OnionBlob [OnionPacketSize]byte
    56  
    57  	// ExtraData is the set of data that was appended to this message to
    58  	// fill out the full maximum transport message size. These fields can
    59  	// be used to specify optional data such as custom TLV fields.
    60  	ExtraData ExtraOpaqueData
    61  }
    62  
    63  // NewUpdateAddHTLC returns a new empty UpdateAddHTLC message.
    64  func NewUpdateAddHTLC() *UpdateAddHTLC {
    65  	return &UpdateAddHTLC{}
    66  }
    67  
    68  // A compile time check to ensure UpdateAddHTLC implements the lnwire.Message
    69  // interface.
    70  var _ Message = (*UpdateAddHTLC)(nil)
    71  
    72  // Decode deserializes a serialized UpdateAddHTLC message stored in the passed
    73  // io.Reader observing the specified protocol version.
    74  //
    75  // This is part of the lnwire.Message interface.
    76  func (c *UpdateAddHTLC) Decode(r io.Reader, pver uint32) error {
    77  	return ReadElements(r,
    78  		&c.ChanID,
    79  		&c.ID,
    80  		&c.Amount,
    81  		c.PaymentHash[:],
    82  		&c.Expiry,
    83  		c.OnionBlob[:],
    84  		&c.ExtraData,
    85  	)
    86  }
    87  
    88  // Encode serializes the target UpdateAddHTLC into the passed io.Writer
    89  // observing the protocol version specified.
    90  //
    91  // This is part of the lnwire.Message interface.
    92  func (c *UpdateAddHTLC) Encode(w *bytes.Buffer, pver uint32) error {
    93  	if err := WriteChannelID(w, c.ChanID); err != nil {
    94  		return err
    95  	}
    96  
    97  	if err := WriteUint64(w, c.ID); err != nil {
    98  		return err
    99  	}
   100  
   101  	if err := WriteMilliAtom(w, c.Amount); err != nil {
   102  		return err
   103  	}
   104  
   105  	if err := WriteBytes(w, c.PaymentHash[:]); err != nil {
   106  		return err
   107  	}
   108  
   109  	if err := WriteUint32(w, c.Expiry); err != nil {
   110  		return err
   111  	}
   112  
   113  	if err := WriteBytes(w, c.OnionBlob[:]); err != nil {
   114  		return err
   115  	}
   116  
   117  	return WriteBytes(w, c.ExtraData)
   118  }
   119  
   120  // MsgType returns the integer uniquely identifying this message type on the
   121  // wire.
   122  //
   123  // This is part of the lnwire.Message interface.
   124  func (c *UpdateAddHTLC) MsgType() MessageType {
   125  	return MsgUpdateAddHTLC
   126  }
   127  
   128  // TargetChanID returns the channel id of the link for which this message is
   129  // intended.
   130  //
   131  // NOTE: Part of peer.LinkUpdater interface.
   132  func (c *UpdateAddHTLC) TargetChanID() ChannelID {
   133  	return c.ChanID
   134  }