2017-02-10 00:28:32 +01:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2021-06-17 08:17:30 +02:00
|
|
|
"bytes"
|
2017-02-10 00:28:32 +01:00
|
|
|
"io"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2023-01-17 04:41:11 +01:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2017-02-10 00:28:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RevokeAndAck is sent by either side once a CommitSig message has been
|
|
|
|
// received, and validated. This message serves to revoke the prior commitment
|
|
|
|
// transaction, which was the most up to date version until a CommitSig message
|
|
|
|
// referencing the specified ChannelPoint was received. Additionally, this
|
|
|
|
// message also piggyback's the next revocation hash that Alice should use when
|
|
|
|
// constructing the Bob's version of the next commitment transaction (which
|
|
|
|
// would be done before sending a CommitSig message). This piggybacking allows
|
|
|
|
// Alice to send the next CommitSig message modifying Bob's commitment
|
|
|
|
// transaction without first asking for a revocation hash initially.
|
|
|
|
type RevokeAndAck struct {
|
2017-04-17 00:22:20 +02:00
|
|
|
// ChanID uniquely identifies to which currently active channel this
|
|
|
|
// RevokeAndAck applies to.
|
|
|
|
ChanID ChannelID
|
2017-02-10 00:28:32 +01:00
|
|
|
|
|
|
|
// Revocation is the preimage to the revocation hash of the now prior
|
|
|
|
// commitment transaction.
|
|
|
|
Revocation [32]byte
|
|
|
|
|
2017-07-29 01:22:33 +02:00
|
|
|
// NextRevocationKey is the next commitment point which should be used
|
|
|
|
// for the next commitment transaction the remote peer creates for us.
|
2020-11-25 22:12:29 +01:00
|
|
|
// This, in conjunction with revocation base point will be used to
|
2017-07-29 01:22:33 +02:00
|
|
|
// create the proper revocation key used within the commitment
|
|
|
|
// transaction.
|
2017-02-10 00:28:32 +01:00
|
|
|
NextRevocationKey *btcec.PublicKey
|
2020-01-28 02:25:36 +01:00
|
|
|
|
2023-01-17 04:41:11 +01:00
|
|
|
// LocalNonce is the next _local_ nonce for the sending party. This
|
|
|
|
// allows the receiving party to propose a new commitment using their
|
|
|
|
// remote nonce and the sender's local nonce.
|
|
|
|
LocalNonce *Musig2Nonce
|
|
|
|
|
2020-01-28 02:25:36 +01:00
|
|
|
// ExtraData is the set of data that was appended to this message to
|
|
|
|
// fill out the full maximum transport message size. These fields can
|
|
|
|
// be used to specify optional data such as custom TLV fields.
|
|
|
|
ExtraData ExtraOpaqueData
|
2017-02-10 00:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRevokeAndAck creates a new RevokeAndAck message.
|
|
|
|
func NewRevokeAndAck() *RevokeAndAck {
|
2020-01-28 02:25:36 +01:00
|
|
|
return &RevokeAndAck{
|
|
|
|
ExtraData: make([]byte, 0),
|
|
|
|
}
|
2017-02-10 00:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure RevokeAndAck implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*RevokeAndAck)(nil)
|
|
|
|
|
|
|
|
// Decode deserializes a serialized RevokeAndAck message stored in the
|
|
|
|
// passed io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *RevokeAndAck) Decode(r io.Reader, pver uint32) error {
|
2023-01-17 04:41:11 +01:00
|
|
|
err := ReadElements(r,
|
2017-04-17 00:22:20 +02:00
|
|
|
&c.ChanID,
|
2017-02-10 00:28:32 +01:00
|
|
|
c.Revocation[:],
|
|
|
|
&c.NextRevocationKey,
|
|
|
|
)
|
2023-01-17 04:41:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tlvRecords ExtraOpaqueData
|
|
|
|
if err := ReadElements(r, &tlvRecords); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var musigNonce Musig2Nonce
|
|
|
|
typeMap, err := tlvRecords.ExtractRecords(&musigNonce)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the corresponding TLV types if they were included in the stream.
|
|
|
|
if val, ok := typeMap[NonceRecordType]; ok && val == nil {
|
|
|
|
c.LocalNonce = &musigNonce
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tlvRecords) != 0 {
|
|
|
|
c.ExtraData = tlvRecords
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-02-10 00:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode serializes the target RevokeAndAck into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2021-06-17 08:17:30 +02:00
|
|
|
func (c *RevokeAndAck) Encode(w *bytes.Buffer, pver uint32) error {
|
2023-01-17 04:41:11 +01:00
|
|
|
recordProducers := make([]tlv.RecordProducer, 0, 1)
|
|
|
|
if c.LocalNonce != nil {
|
|
|
|
recordProducers = append(recordProducers, c.LocalNonce)
|
|
|
|
}
|
|
|
|
err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-18 09:11:43 +02:00
|
|
|
if err := WriteChannelID(w, c.ChanID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := WriteBytes(w, c.Revocation[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := WritePublicKey(w, c.NextRevocationKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteBytes(w, c.ExtraData)
|
2017-02-10 00:28:32 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:57:43 +02:00
|
|
|
// MsgType returns the integer uniquely identifying this message type on the
|
2017-02-10 00:28:32 +01:00
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-20 00:57:43 +02:00
|
|
|
func (c *RevokeAndAck) MsgType() MessageType {
|
|
|
|
return MsgRevokeAndAck
|
2017-02-10 00:28:32 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:00:59 +02:00
|
|
|
// TargetChanID returns the channel id of the link for which this message is
|
|
|
|
// intended.
|
|
|
|
//
|
2020-07-02 23:46:06 +02:00
|
|
|
// NOTE: Part of peer.LinkUpdater interface.
|
2019-09-25 21:00:59 +02:00
|
|
|
func (c *RevokeAndAck) TargetChanID() ChannelID {
|
|
|
|
return c.ChanID
|
|
|
|
}
|