channel_ready.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package lnwire
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/btcsuite/btcd/btcec/v2"
  6. "github.com/lightningnetwork/lnd/tlv"
  7. )
  8. // ChannelReady is the message that both parties to a new channel creation
  9. // send once they have observed the funding transaction being confirmed on the
  10. // blockchain. ChannelReady contains the signatures necessary for the channel
  11. // participants to advertise the existence of the channel to the rest of the
  12. // network.
  13. type ChannelReady struct {
  14. // ChanID is the outpoint of the channel's funding transaction. This
  15. // can be used to query for the channel in the database.
  16. ChanID ChannelID
  17. // NextPerCommitmentPoint is the secret that can be used to revoke the
  18. // next commitment transaction for the channel.
  19. NextPerCommitmentPoint *btcec.PublicKey
  20. // AliasScid is an alias ShortChannelID used to refer to the underlying
  21. // channel. It can be used instead of the confirmed on-chain
  22. // ShortChannelID for forwarding.
  23. AliasScid *ShortChannelID
  24. // NextLocalNonce is an optional field that stores a local musig2 nonce.
  25. // This will only be populated if the simple taproot channels type was
  26. // negotiated. This is the local nonce that will be used by the sender
  27. // to accept a new commitment state transition.
  28. NextLocalNonce OptMusig2NonceTLV
  29. // ExtraData is the set of data that was appended to this message to
  30. // fill out the full maximum transport message size. These fields can
  31. // be used to specify optional data such as custom TLV fields.
  32. ExtraData ExtraOpaqueData
  33. }
  34. // NewChannelReady creates a new ChannelReady message, populating it with the
  35. // necessary IDs and revocation secret.
  36. func NewChannelReady(cid ChannelID, npcp *btcec.PublicKey) *ChannelReady {
  37. return &ChannelReady{
  38. ChanID: cid,
  39. NextPerCommitmentPoint: npcp,
  40. ExtraData: nil,
  41. }
  42. }
  43. // A compile time check to ensure ChannelReady implements the lnwire.Message
  44. // interface.
  45. var _ Message = (*ChannelReady)(nil)
  46. // Decode deserializes the serialized ChannelReady message stored in the
  47. // passed io.Reader into the target ChannelReady using the deserialization
  48. // rules defined by the passed protocol version.
  49. //
  50. // This is part of the lnwire.Message interface.
  51. func (c *ChannelReady) Decode(r io.Reader, _ uint32) error {
  52. // Read all the mandatory fields in the message.
  53. err := ReadElements(r,
  54. &c.ChanID,
  55. &c.NextPerCommitmentPoint,
  56. )
  57. if err != nil {
  58. return err
  59. }
  60. var tlvRecords ExtraOpaqueData
  61. if err := ReadElements(r, &tlvRecords); err != nil {
  62. return err
  63. }
  64. // Next we'll parse out the set of known records. For now, this is just
  65. // the AliasScidRecordType.
  66. var (
  67. aliasScid ShortChannelID
  68. localNonce = c.NextLocalNonce.Zero()
  69. )
  70. typeMap, err := tlvRecords.ExtractRecords(
  71. &aliasScid, &localNonce,
  72. )
  73. if err != nil {
  74. return err
  75. }
  76. // We'll only set AliasScid if the corresponding TLV type was included
  77. // in the stream.
  78. if val, ok := typeMap[AliasScidRecordType]; ok && val == nil {
  79. c.AliasScid = &aliasScid
  80. }
  81. if val, ok := typeMap[c.NextLocalNonce.TlvType()]; ok && val == nil {
  82. c.NextLocalNonce = tlv.SomeRecordT(localNonce)
  83. }
  84. if len(tlvRecords) != 0 {
  85. c.ExtraData = tlvRecords
  86. }
  87. return nil
  88. }
  89. // Encode serializes the target ChannelReady message into the passed io.Writer
  90. // implementation. Serialization will observe the rules defined by the passed
  91. // protocol version.
  92. //
  93. // This is part of the lnwire.Message interface.
  94. func (c *ChannelReady) Encode(w *bytes.Buffer, _ uint32) error {
  95. if err := WriteChannelID(w, c.ChanID); err != nil {
  96. return err
  97. }
  98. if err := WritePublicKey(w, c.NextPerCommitmentPoint); err != nil {
  99. return err
  100. }
  101. // We'll only encode the AliasScid in a TLV segment if it exists.
  102. recordProducers := make([]tlv.RecordProducer, 0, 2)
  103. if c.AliasScid != nil {
  104. recordProducers = append(recordProducers, c.AliasScid)
  105. }
  106. c.NextLocalNonce.WhenSome(func(localNonce Musig2NonceTLV) {
  107. recordProducers = append(recordProducers, &localNonce)
  108. })
  109. err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
  110. if err != nil {
  111. return err
  112. }
  113. return WriteBytes(w, c.ExtraData)
  114. }
  115. // MsgType returns the uint32 code which uniquely identifies this message as a
  116. // ChannelReady message on the wire.
  117. //
  118. // This is part of the lnwire.Message interface.
  119. func (c *ChannelReady) MsgType() MessageType {
  120. return MsgChannelReady
  121. }