lnwire: add custom records field to type CommitSig
This commit is contained in:
3 files changed
+193
-15
No files matched your search
+23
-15
@@ -45,6 +45,10 @@ type CommitSig struct {
|
||||
// being signed for. In this case, the above Sig type MUST be blank.
|
||||
PartialSig OptPartialSigWithNonceTLV
|
||||
|
||||
// CustomRecords maps TLV types to byte slices, storing arbitrary data
|
||||
// intended for inclusion in the ExtraData field.
|
||||
CustomRecords CustomRecords
|
||||
|
||||
// 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.
|
||||
@@ -53,9 +57,7 @@ type CommitSig struct {
|
||||
|
||||
// NewCommitSig creates a new empty CommitSig message.
|
||||
func NewCommitSig() *CommitSig {
|
||||
return &CommitSig{
|
||||
ExtraData: make([]byte, 0),
|
||||
}
|
||||
return &CommitSig{}
|
||||
}
|
||||
|
||||
// A compile time check to ensure CommitSig implements the lnwire.Message
|
||||
@@ -67,34 +69,37 @@ var _ Message = (*CommitSig)(nil)
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (c *CommitSig) Decode(r io.Reader, pver uint32) error {
|
||||
// msgExtraData is a temporary variable used to read the message extra
|
||||
// data field from the reader.
|
||||
var msgExtraData ExtraOpaqueData
|
||||
|
||||
err := ReadElements(r,
|
||||
&c.ChanID,
|
||||
&c.CommitSig,
|
||||
&c.HtlcSigs,
|
||||
&msgExtraData,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var tlvRecords ExtraOpaqueData
|
||||
if err := ReadElements(r, &tlvRecords); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Extract TLV records from the extra data field.
|
||||
partialSig := c.PartialSig.Zero()
|
||||
typeMap, err := tlvRecords.ExtractRecords(&partialSig)
|
||||
|
||||
customRecords, parsed, extraData, err := ParseAndExtractCustomRecords(
|
||||
msgExtraData, &partialSig,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set the corresponding TLV types if they were included in the stream.
|
||||
if val, ok := typeMap[c.PartialSig.TlvType()]; ok && val == nil {
|
||||
if _, ok := parsed[partialSig.TlvType()]; ok {
|
||||
c.PartialSig = tlv.SomeRecordT(partialSig)
|
||||
}
|
||||
|
||||
if len(tlvRecords) != 0 {
|
||||
c.ExtraData = tlvRecords
|
||||
}
|
||||
c.CustomRecords = customRecords
|
||||
c.ExtraData = extraData
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -108,7 +113,10 @@ func (c *CommitSig) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
c.PartialSig.WhenSome(func(sig PartialSigWithNonceTLV) {
|
||||
recordProducers = append(recordProducers, &sig)
|
||||
})
|
||||
err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
|
||||
|
||||
extraData, err := MergeAndEncode(
|
||||
recordProducers, c.ExtraData, c.CustomRecords,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -125,7 +133,7 @@ func (c *CommitSig) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, c.ExtraData)
|
||||
return WriteBytes(w, extraData)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying this message type on the
|
||||
|
||||
Reference in new issue
Block a user