commit_sig.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package lnwire
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/lightningnetwork/lnd/tlv"
  6. )
  7. // CommitSig is sent by either side to stage any pending HTLC's in the
  8. // receiver's pending set into a new commitment state. Implicitly, the new
  9. // commitment transaction constructed which has been signed by CommitSig
  10. // includes all HTLC's in the remote node's pending set. A CommitSig message
  11. // may be sent after a series of UpdateAddHTLC/UpdateFulfillHTLC messages in
  12. // order to batch add several HTLC's with a single signature covering all
  13. // implicitly accepted HTLC's.
  14. type CommitSig struct {
  15. // ChanID uniquely identifies to which currently active channel this
  16. // CommitSig applies to.
  17. ChanID ChannelID
  18. // CommitSig is Alice's signature for Bob's new commitment transaction.
  19. // Alice is able to send this signature without requesting any
  20. // additional data due to the piggybacking of Bob's next revocation
  21. // hash in his prior RevokeAndAck message, as well as the canonical
  22. // ordering used for all inputs/outputs within commitment transactions.
  23. // If initiating a new commitment state, this signature should ONLY
  24. // cover all of the sending party's pending log updates, and the log
  25. // updates of the remote party that have been ACK'd.
  26. CommitSig Sig
  27. // HtlcSigs is a signature for each relevant HTLC output within the
  28. // created commitment. The order of the signatures is expected to be
  29. // identical to the placement of the HTLC's within the BIP 69 sorted
  30. // commitment transaction. For each outgoing HTLC (from the PoV of the
  31. // sender of this message), a signature for an HTLC timeout transaction
  32. // should be signed, for each incoming HTLC the HTLC timeout
  33. // transaction should be signed.
  34. HtlcSigs []Sig
  35. // PartialSig is used to transmit a musig2 extended partial signature
  36. // that also carries along the public nonce of the signer.
  37. //
  38. // NOTE: This field is only populated if a musig2 taproot channel is
  39. // being signed for. In this case, the above Sig type MUST be blank.
  40. PartialSig OptPartialSigWithNonceTLV
  41. // ExtraData is the set of data that was appended to this message to
  42. // fill out the full maximum transport message size. These fields can
  43. // be used to specify optional data such as custom TLV fields.
  44. ExtraData ExtraOpaqueData
  45. }
  46. // NewCommitSig creates a new empty CommitSig message.
  47. func NewCommitSig() *CommitSig {
  48. return &CommitSig{
  49. ExtraData: make([]byte, 0),
  50. }
  51. }
  52. // A compile time check to ensure CommitSig implements the lnwire.Message
  53. // interface.
  54. var _ Message = (*CommitSig)(nil)
  55. // Decode deserializes a serialized CommitSig message stored in the
  56. // passed io.Reader observing the specified protocol version.
  57. //
  58. // This is part of the lnwire.Message interface.
  59. func (c *CommitSig) Decode(r io.Reader, pver uint32) error {
  60. err := ReadElements(r,
  61. &c.ChanID,
  62. &c.CommitSig,
  63. &c.HtlcSigs,
  64. )
  65. if err != nil {
  66. return err
  67. }
  68. var tlvRecords ExtraOpaqueData
  69. if err := ReadElements(r, &tlvRecords); err != nil {
  70. return err
  71. }
  72. partialSig := c.PartialSig.Zero()
  73. typeMap, err := tlvRecords.ExtractRecords(&partialSig)
  74. if err != nil {
  75. return err
  76. }
  77. // Set the corresponding TLV types if they were included in the stream.
  78. if val, ok := typeMap[c.PartialSig.TlvType()]; ok && val == nil {
  79. c.PartialSig = tlv.SomeRecordT(partialSig)
  80. }
  81. if len(tlvRecords) != 0 {
  82. c.ExtraData = tlvRecords
  83. }
  84. return nil
  85. }
  86. // Encode serializes the target CommitSig into the passed io.Writer
  87. // observing the protocol version specified.
  88. //
  89. // This is part of the lnwire.Message interface.
  90. func (c *CommitSig) Encode(w *bytes.Buffer, pver uint32) error {
  91. recordProducers := make([]tlv.RecordProducer, 0, 1)
  92. c.PartialSig.WhenSome(func(sig PartialSigWithNonceTLV) {
  93. recordProducers = append(recordProducers, &sig)
  94. })
  95. err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
  96. if err != nil {
  97. return err
  98. }
  99. if err := WriteChannelID(w, c.ChanID); err != nil {
  100. return err
  101. }
  102. if err := WriteSig(w, c.CommitSig); err != nil {
  103. return err
  104. }
  105. if err := WriteSigs(w, c.HtlcSigs); err != nil {
  106. return err
  107. }
  108. return WriteBytes(w, c.ExtraData)
  109. }
  110. // MsgType returns the integer uniquely identifying this message type on the
  111. // wire.
  112. //
  113. // This is part of the lnwire.Message interface.
  114. func (c *CommitSig) MsgType() MessageType {
  115. return MsgCommitSig
  116. }
  117. // TargetChanID returns the channel id of the link for which this message is
  118. // intended.
  119. //
  120. // NOTE: Part of peer.LinkUpdater interface.
  121. func (c *CommitSig) TargetChanID() ChannelID {
  122. return c.ChanID
  123. }