-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
revoke_and_ack.go
138 lines (116 loc) · 4.02 KB
/
revoke_and_ack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package lnwire
import (
"bytes"
"io"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/tlv"
)
// 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 {
// ChanID uniquely identifies to which currently active channel this
// RevokeAndAck applies to.
ChanID ChannelID
// Revocation is the preimage to the revocation hash of the now prior
// commitment transaction.
Revocation [32]byte
// NextRevocationKey is the next commitment point which should be used
// for the next commitment transaction the remote peer creates for us.
// This, in conjunction with revocation base point will be used to
// create the proper revocation key used within the commitment
// transaction.
NextRevocationKey *btcec.PublicKey
// 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
// 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
}
// NewRevokeAndAck creates a new RevokeAndAck message.
func NewRevokeAndAck() *RevokeAndAck {
return &RevokeAndAck{
ExtraData: make([]byte, 0),
}
}
// 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 {
err := ReadElements(r,
&c.ChanID,
c.Revocation[:],
&c.NextRevocationKey,
)
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
}
// Encode serializes the target RevokeAndAck into the passed io.Writer
// observing the protocol version specified.
//
// This is part of the lnwire.Message interface.
func (c *RevokeAndAck) Encode(w *bytes.Buffer, pver uint32) error {
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
}
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)
}
// MsgType returns the integer uniquely identifying this message type on the
// wire.
//
// This is part of the lnwire.Message interface.
func (c *RevokeAndAck) MsgType() MessageType {
return MsgRevokeAndAck
}
// TargetChanID returns the channel id of the link for which this message is
// intended.
//
// NOTE: Part of peer.LinkUpdater interface.
func (c *RevokeAndAck) TargetChanID() ChannelID {
return c.ChanID
}