• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12058234999

27 Nov 2024 09:06PM UTC coverage: 57.847% (-1.1%) from 58.921%
12058234999

Pull #9148

github

ProofOfKeags
lnwire: convert DynPropose and DynCommit to use typed tlv records
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

142 of 177 new or added lines in 4 files covered. (80.23%)

19365 existing lines in 251 files now uncovered.

100876 of 174383 relevant lines covered (57.85%)

25338.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.61
/lnwire/dyn_ack.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "io"
6

7
        "github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
8
        "github.com/lightningnetwork/lnd/fn"
9
        "github.com/lightningnetwork/lnd/tlv"
10
)
11

12
const (
13
        // DALocalMusig2Pubnonce is the TLV type number that identifies the
14
        // musig2 public nonce that we need to verify the commitment transaction
15
        // signature.
16
        DALocalMusig2Pubnonce tlv.Type = 0
17
)
18

19
// DynAck is the message used to accept the parameters of a dynamic commitment
20
// negotiation. Additional optional parameters will need to be present depending
21
// on the details of the dynamic commitment upgrade.
22
type DynAck struct {
23
        // ChanID is the ChannelID of the channel that is currently undergoing
24
        // a dynamic commitment negotiation
25
        ChanID ChannelID
26

27
        // Sig is a signature that acknowledges and approves the parameters
28
        // that were requested in the DynPropose
29
        Sig Sig
30

31
        // LocalNonce is an optional field that is transmitted when accepting
32
        // a dynamic commitment upgrade to Taproot Channels. This nonce will be
33
        // used to verify the first commitment transaction signature. This will
34
        // only be populated if the DynPropose we are responding to specifies
35
        // taproot channels in the ChannelType field.
36
        LocalNonce fn.Option[Musig2Nonce]
37

38
        // ExtraData is the set of data that was appended to this message to
39
        // fill out the full maximum transport message size. These fields can
40
        // be used to specify optional data such as custom TLV fields.
41
        ExtraData ExtraOpaqueData
42
}
43

44
// A compile time check to ensure DynAck implements the lnwire.Message
45
// interface.
46
var _ Message = (*DynAck)(nil)
47

48
// Encode serializes the target DynAck into the passed io.Writer. Serialization
49
// will observe the rules defined by the passed protocol version.
50
//
51
// This is a part of the lnwire.Message interface.
52
func (da *DynAck) Encode(w *bytes.Buffer, _ uint32) error {
100✔
53
        if err := WriteChannelID(w, da.ChanID); err != nil {
100✔
54
                return err
×
55
        }
×
56

57
        if err := WriteSig(w, da.Sig); err != nil {
100✔
NEW
58
                return err
×
NEW
59
        }
×
60

61
        var tlvRecords []tlv.Record
100✔
62
        da.LocalNonce.WhenSome(func(nonce Musig2Nonce) {
156✔
63
                tlvRecords = append(
56✔
64
                        tlvRecords, tlv.MakeStaticRecord(
56✔
65
                                DALocalMusig2Pubnonce, &nonce,
56✔
66
                                musig2.PubNonceSize, nonceTypeEncoder,
56✔
67
                                nonceTypeDecoder,
56✔
68
                        ),
56✔
69
                )
56✔
70
        })
56✔
71
        tlv.SortRecords(tlvRecords)
100✔
72

100✔
73
        tlvStream, err := tlv.NewStream(tlvRecords...)
100✔
74
        if err != nil {
100✔
75
                return err
×
76
        }
×
77

78
        var extraBytesWriter bytes.Buffer
100✔
79
        if err := tlvStream.Encode(&extraBytesWriter); err != nil {
100✔
80
                return err
×
81
        }
×
82

83
        da.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
100✔
84

100✔
85
        return WriteBytes(w, da.ExtraData)
100✔
86
}
87

88
// Decode deserializes the serialized DynAck stored in the passed io.Reader into
89
// the target DynAck using the deserialization rules defined by the passed
90
// protocol version.
91
//
92
// This is a part of the lnwire.Message interface.
93
func (da *DynAck) Decode(r io.Reader, _ uint32) error {
154✔
94
        // Parse out main message.
154✔
95
        if err := ReadElements(r, &da.ChanID, &da.Sig); err != nil {
185✔
96
                return err
31✔
97
        }
31✔
98

99
        // Parse out TLV records.
100
        var tlvRecords ExtraOpaqueData
123✔
101
        if err := ReadElement(r, &tlvRecords); err != nil {
123✔
102
                return err
×
103
        }
×
104

105
        // Prepare receiving buffers to be filled by TLV extraction.
106
        var localNonceScratch Musig2Nonce
123✔
107
        localNonce := tlv.MakeStaticRecord(
123✔
108
                DALocalMusig2Pubnonce, &localNonceScratch, musig2.PubNonceSize,
123✔
109
                nonceTypeEncoder, nonceTypeDecoder,
123✔
110
        )
123✔
111

123✔
112
        // Create set of Records to read TLV bytestream into.
123✔
113
        records := []tlv.Record{localNonce}
123✔
114
        tlv.SortRecords(records)
123✔
115

123✔
116
        // Read TLV stream into record set.
123✔
117
        extraBytesReader := bytes.NewReader(tlvRecords)
123✔
118
        tlvStream, err := tlv.NewStream(records...)
123✔
119
        if err != nil {
123✔
120
                return err
×
121
        }
×
122
        typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
123✔
123
        if err != nil {
146✔
124
                return err
23✔
125
        }
23✔
126

127
        // Check the results of the TLV Stream decoding and appropriately set
128
        // message fields.
129
        if val, ok := typeMap[DALocalMusig2Pubnonce]; ok && val == nil {
156✔
130
                da.LocalNonce = fn.Some(localNonceScratch)
56✔
131
        }
56✔
132

133
        if len(tlvRecords) != 0 {
156✔
134
                da.ExtraData = tlvRecords
56✔
135
        }
56✔
136

137
        return nil
100✔
138
}
139

140
// MsgType returns the MessageType code which uniquely identifies this message
141
// as a DynAck on the wire.
142
//
143
// This is part of the lnwire.Message interface.
144
func (da *DynAck) MsgType() MessageType {
100✔
145
        return MsgDynAck
100✔
146
}
100✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc