update_fulfill_htlc.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package lnwire
  2. import (
  3. "bytes"
  4. "io"
  5. )
  6. // UpdateFulfillHTLC is sent by Alice to Bob when she wishes to settle a
  7. // particular HTLC referenced by its HTLCKey within a specific active channel
  8. // referenced by ChannelPoint. A subsequent CommitSig message will be sent by
  9. // Alice to "lock-in" the removal of the specified HTLC, possible containing a
  10. // batch signature covering several settled HTLC's.
  11. type UpdateFulfillHTLC struct {
  12. // ChanID references an active channel which holds the HTLC to be
  13. // settled.
  14. ChanID ChannelID
  15. // ID denotes the exact HTLC stage within the receiving node's
  16. // commitment transaction to be removed.
  17. ID uint64
  18. // PaymentPreimage is the R-value preimage required to fully settle an
  19. // HTLC.
  20. PaymentPreimage [32]byte
  21. // CustomRecords maps TLV types to byte slices, storing arbitrary data
  22. // intended for inclusion in the ExtraData field.
  23. CustomRecords CustomRecords
  24. // ExtraData is the set of data that was appended to this message to
  25. // fill out the full maximum transport message size. These fields can
  26. // be used to specify optional data such as custom TLV fields.
  27. ExtraData ExtraOpaqueData
  28. }
  29. // NewUpdateFulfillHTLC returns a new empty UpdateFulfillHTLC.
  30. func NewUpdateFulfillHTLC(chanID ChannelID, id uint64,
  31. preimage [32]byte) *UpdateFulfillHTLC {
  32. return &UpdateFulfillHTLC{
  33. ChanID: chanID,
  34. ID: id,
  35. PaymentPreimage: preimage,
  36. }
  37. }
  38. // A compile time check to ensure UpdateFulfillHTLC implements the lnwire.Message
  39. // interface.
  40. var _ Message = (*UpdateFulfillHTLC)(nil)
  41. // Decode deserializes a serialized UpdateFulfillHTLC message stored in the passed
  42. // io.Reader observing the specified protocol version.
  43. //
  44. // This is part of the lnwire.Message interface.
  45. func (c *UpdateFulfillHTLC) Decode(r io.Reader, pver uint32) error {
  46. // msgExtraData is a temporary variable used to read the message extra
  47. // data field from the reader.
  48. var msgExtraData ExtraOpaqueData
  49. if err := ReadElements(r,
  50. &c.ChanID,
  51. &c.ID,
  52. c.PaymentPreimage[:],
  53. &msgExtraData,
  54. ); err != nil {
  55. return err
  56. }
  57. // Extract custom records from the extra data field.
  58. customRecords, _, extraData, err := ParseAndExtractCustomRecords(
  59. msgExtraData,
  60. )
  61. if err != nil {
  62. return err
  63. }
  64. c.CustomRecords = customRecords
  65. c.ExtraData = extraData
  66. return nil
  67. }
  68. // Encode serializes the target UpdateFulfillHTLC into the passed io.Writer
  69. // observing the protocol version specified.
  70. //
  71. // This is part of the lnwire.Message interface.
  72. func (c *UpdateFulfillHTLC) Encode(w *bytes.Buffer, pver uint32) error {
  73. if err := WriteChannelID(w, c.ChanID); err != nil {
  74. return err
  75. }
  76. if err := WriteUint64(w, c.ID); err != nil {
  77. return err
  78. }
  79. if err := WriteBytes(w, c.PaymentPreimage[:]); err != nil {
  80. return err
  81. }
  82. // Combine the custom records and the extra data, then encode the
  83. // result as a byte slice.
  84. extraData, err := MergeAndEncode(nil, c.ExtraData, c.CustomRecords)
  85. if err != nil {
  86. return err
  87. }
  88. return WriteBytes(w, extraData)
  89. }
  90. // MsgType returns the integer uniquely identifying this message type on the
  91. // wire.
  92. //
  93. // This is part of the lnwire.Message interface.
  94. func (c *UpdateFulfillHTLC) MsgType() MessageType {
  95. return MsgUpdateFulfillHTLC
  96. }
  97. // TargetChanID returns the channel id of the link for which this message is
  98. // intended.
  99. //
  100. // NOTE: Part of peer.LinkUpdater interface.
  101. func (c *UpdateFulfillHTLC) TargetChanID() ChannelID {
  102. return c.ChanID
  103. }