revoke_and_ack.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package lnwire
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/btcsuite/btcd/btcec/v2"
  6. "github.com/lightningnetwork/lnd/tlv"
  7. )
  8. // RevokeAndAck is sent by either side once a CommitSig message has been
  9. // received, and validated. This message serves to revoke the prior commitment
  10. // transaction, which was the most up to date version until a CommitSig message
  11. // referencing the specified ChannelPoint was received. Additionally, this
  12. // message also piggyback's the next revocation hash that Alice should use when
  13. // constructing the Bob's version of the next commitment transaction (which
  14. // would be done before sending a CommitSig message). This piggybacking allows
  15. // Alice to send the next CommitSig message modifying Bob's commitment
  16. // transaction without first asking for a revocation hash initially.
  17. type RevokeAndAck struct {
  18. // ChanID uniquely identifies to which currently active channel this
  19. // RevokeAndAck applies to.
  20. ChanID ChannelID
  21. // Revocation is the preimage to the revocation hash of the now prior
  22. // commitment transaction.
  23. Revocation [32]byte
  24. // NextRevocationKey is the next commitment point which should be used
  25. // for the next commitment transaction the remote peer creates for us.
  26. // This, in conjunction with revocation base point will be used to
  27. // create the proper revocation key used within the commitment
  28. // transaction.
  29. NextRevocationKey *btcec.PublicKey
  30. // LocalNonce is the next _local_ nonce for the sending party. This
  31. // allows the receiving party to propose a new commitment using their
  32. // remote nonce and the sender's local nonce.
  33. LocalNonce OptMusig2NonceTLV
  34. // ExtraData is the set of data that was appended to this message to
  35. // fill out the full maximum transport message size. These fields can
  36. // be used to specify optional data such as custom TLV fields.
  37. ExtraData ExtraOpaqueData
  38. }
  39. // NewRevokeAndAck creates a new RevokeAndAck message.
  40. func NewRevokeAndAck() *RevokeAndAck {
  41. return &RevokeAndAck{
  42. ExtraData: make([]byte, 0),
  43. }
  44. }
  45. // A compile time check to ensure RevokeAndAck implements the lnwire.Message
  46. // interface.
  47. var _ Message = (*RevokeAndAck)(nil)
  48. // Decode deserializes a serialized RevokeAndAck message stored in the
  49. // passed io.Reader observing the specified protocol version.
  50. //
  51. // This is part of the lnwire.Message interface.
  52. func (c *RevokeAndAck) Decode(r io.Reader, pver uint32) error {
  53. err := ReadElements(r,
  54. &c.ChanID,
  55. c.Revocation[:],
  56. &c.NextRevocationKey,
  57. )
  58. if err != nil {
  59. return err
  60. }
  61. var tlvRecords ExtraOpaqueData
  62. if err := ReadElements(r, &tlvRecords); err != nil {
  63. return err
  64. }
  65. localNonce := c.LocalNonce.Zero()
  66. typeMap, err := tlvRecords.ExtractRecords(&localNonce)
  67. if err != nil {
  68. return err
  69. }
  70. // Set the corresponding TLV types if they were included in the stream.
  71. if val, ok := typeMap[c.LocalNonce.TlvType()]; ok && val == nil {
  72. c.LocalNonce = tlv.SomeRecordT(localNonce)
  73. }
  74. if len(tlvRecords) != 0 {
  75. c.ExtraData = tlvRecords
  76. }
  77. return nil
  78. }
  79. // Encode serializes the target RevokeAndAck into the passed io.Writer
  80. // observing the protocol version specified.
  81. //
  82. // This is part of the lnwire.Message interface.
  83. func (c *RevokeAndAck) Encode(w *bytes.Buffer, pver uint32) error {
  84. recordProducers := make([]tlv.RecordProducer, 0, 1)
  85. c.LocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
  86. recordProducers = append(recordProducers, &localNonce)
  87. })
  88. err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
  89. if err != nil {
  90. return err
  91. }
  92. if err := WriteChannelID(w, c.ChanID); err != nil {
  93. return err
  94. }
  95. if err := WriteBytes(w, c.Revocation[:]); err != nil {
  96. return err
  97. }
  98. if err := WritePublicKey(w, c.NextRevocationKey); err != nil {
  99. return err
  100. }
  101. return WriteBytes(w, c.ExtraData)
  102. }
  103. // MsgType returns the integer uniquely identifying this message type on the
  104. // wire.
  105. //
  106. // This is part of the lnwire.Message interface.
  107. func (c *RevokeAndAck) MsgType() MessageType {
  108. return MsgRevokeAndAck
  109. }
  110. // TargetChanID returns the channel id of the link for which this message is
  111. // intended.
  112. //
  113. // NOTE: Part of peer.LinkUpdater interface.
  114. func (c *RevokeAndAck) TargetChanID() ChannelID {
  115. return c.ChanID
  116. }