dyn_ack.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package lnwire
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
  6. "github.com/lightningnetwork/lnd/fn"
  7. "github.com/lightningnetwork/lnd/tlv"
  8. )
  9. const (
  10. // DALocalMusig2Pubnonce is the TLV type number that identifies the
  11. // musig2 public nonce that we need to verify the commitment transaction
  12. // signature.
  13. DALocalMusig2Pubnonce tlv.Type = 0
  14. )
  15. // DynAck is the message used to accept the parameters of a dynamic commitment
  16. // negotiation. Additional optional parameters will need to be present depending
  17. // on the details of the dynamic commitment upgrade.
  18. type DynAck struct {
  19. // ChanID is the ChannelID of the channel that is currently undergoing
  20. // a dynamic commitment negotiation
  21. ChanID ChannelID
  22. // LocalNonce is an optional field that is transmitted when accepting
  23. // a dynamic commitment upgrade to Taproot Channels. This nonce will be
  24. // used to verify the first commitment transaction signature. This will
  25. // only be populated if the DynPropose we are responding to specifies
  26. // taproot channels in the ChannelType field.
  27. LocalNonce fn.Option[Musig2Nonce]
  28. // ExtraData is the set of data that was appended to this message to
  29. // fill out the full maximum transport message size. These fields can
  30. // be used to specify optional data such as custom TLV fields.
  31. ExtraData ExtraOpaqueData
  32. }
  33. // A compile time check to ensure DynAck implements the lnwire.Message
  34. // interface.
  35. var _ Message = (*DynAck)(nil)
  36. // Encode serializes the target DynAck into the passed io.Writer. Serialization
  37. // will observe the rules defined by the passed protocol version.
  38. //
  39. // This is a part of the lnwire.Message interface.
  40. func (da *DynAck) Encode(w *bytes.Buffer, _ uint32) error {
  41. if err := WriteChannelID(w, da.ChanID); err != nil {
  42. return err
  43. }
  44. var tlvRecords []tlv.Record
  45. da.LocalNonce.WhenSome(func(nonce Musig2Nonce) {
  46. tlvRecords = append(
  47. tlvRecords, tlv.MakeStaticRecord(
  48. DALocalMusig2Pubnonce, &nonce,
  49. musig2.PubNonceSize, nonceTypeEncoder,
  50. nonceTypeDecoder,
  51. ),
  52. )
  53. })
  54. tlv.SortRecords(tlvRecords)
  55. tlvStream, err := tlv.NewStream(tlvRecords...)
  56. if err != nil {
  57. return err
  58. }
  59. var extraBytesWriter bytes.Buffer
  60. if err := tlvStream.Encode(&extraBytesWriter); err != nil {
  61. return err
  62. }
  63. da.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
  64. return WriteBytes(w, da.ExtraData)
  65. }
  66. // Decode deserializes the serialized DynAck stored in the passed io.Reader into
  67. // the target DynAck using the deserialization rules defined by the passed
  68. // protocol version.
  69. //
  70. // This is a part of the lnwire.Message interface.
  71. func (da *DynAck) Decode(r io.Reader, _ uint32) error {
  72. // Parse out main message.
  73. if err := ReadElements(r, &da.ChanID); err != nil {
  74. return err
  75. }
  76. // Parse out TLV records.
  77. var tlvRecords ExtraOpaqueData
  78. if err := ReadElement(r, &tlvRecords); err != nil {
  79. return err
  80. }
  81. // Prepare receiving buffers to be filled by TLV extraction.
  82. var localNonceScratch Musig2Nonce
  83. localNonce := tlv.MakeStaticRecord(
  84. DALocalMusig2Pubnonce, &localNonceScratch, musig2.PubNonceSize,
  85. nonceTypeEncoder, nonceTypeDecoder,
  86. )
  87. // Create set of Records to read TLV bytestream into.
  88. records := []tlv.Record{localNonce}
  89. tlv.SortRecords(records)
  90. // Read TLV stream into record set.
  91. extraBytesReader := bytes.NewReader(tlvRecords)
  92. tlvStream, err := tlv.NewStream(records...)
  93. if err != nil {
  94. return err
  95. }
  96. typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
  97. if err != nil {
  98. return err
  99. }
  100. // Check the results of the TLV Stream decoding and appropriately set
  101. // message fields.
  102. if val, ok := typeMap[DALocalMusig2Pubnonce]; ok && val == nil {
  103. da.LocalNonce = fn.Some(localNonceScratch)
  104. }
  105. if len(tlvRecords) != 0 {
  106. da.ExtraData = tlvRecords
  107. }
  108. return nil
  109. }
  110. // MsgType returns the MessageType code which uniquely identifies this message
  111. // as a DynAck on the wire.
  112. //
  113. // This is part of the lnwire.Message interface.
  114. func (da *DynAck) MsgType() MessageType {
  115. return MsgDynAck
  116. }