funding_signed.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package lnwire
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/lightningnetwork/lnd/tlv"
  6. )
  7. // FundingSigned is sent from Bob (the responder) to Alice (the initiator)
  8. // after receiving the funding outpoint and her signature for Bob's version of
  9. // the commitment transaction.
  10. type FundingSigned struct {
  11. // ChannelPoint is the particular active channel that this
  12. // FundingSigned is bound to.
  13. ChanID ChannelID
  14. // CommitSig is Bob's signature for Alice's version of the commitment
  15. // transaction.
  16. CommitSig Sig
  17. // PartialSig is used to transmit a musig2 extended partial signature
  18. // that also carries along the public nonce of the signer.
  19. //
  20. // NOTE: This field is only populated if a musig2 taproot channel is
  21. // being signed for. In this case, the above Sig type MUST be blank.
  22. PartialSig OptPartialSigWithNonceTLV
  23. // ExtraData is the set of data that was appended to this message to
  24. // fill out the full maximum transport message size. These fields can
  25. // be used to specify optional data such as custom TLV fields.
  26. ExtraData ExtraOpaqueData
  27. }
  28. // A compile time check to ensure FundingSigned implements the lnwire.Message
  29. // interface.
  30. var _ Message = (*FundingSigned)(nil)
  31. // Encode serializes the target FundingSigned into the passed io.Writer
  32. // implementation. Serialization will observe the rules defined by the passed
  33. // protocol version.
  34. //
  35. // This is part of the lnwire.Message interface.
  36. func (f *FundingSigned) Encode(w *bytes.Buffer, pver uint32) error {
  37. recordProducers := make([]tlv.RecordProducer, 0, 1)
  38. f.PartialSig.WhenSome(func(sig PartialSigWithNonceTLV) {
  39. recordProducers = append(recordProducers, &sig)
  40. })
  41. err := EncodeMessageExtraData(&f.ExtraData, recordProducers...)
  42. if err != nil {
  43. return err
  44. }
  45. if err := WriteChannelID(w, f.ChanID); err != nil {
  46. return err
  47. }
  48. if err := WriteSig(w, f.CommitSig); err != nil {
  49. return err
  50. }
  51. return WriteBytes(w, f.ExtraData)
  52. }
  53. // Decode deserializes the serialized FundingSigned stored in the passed
  54. // io.Reader into the target FundingSigned using the deserialization rules
  55. // defined by the passed protocol version.
  56. //
  57. // This is part of the lnwire.Message interface.
  58. func (f *FundingSigned) Decode(r io.Reader, pver uint32) error {
  59. err := ReadElements(r, &f.ChanID, &f.CommitSig)
  60. if err != nil {
  61. return err
  62. }
  63. var tlvRecords ExtraOpaqueData
  64. if err := ReadElements(r, &tlvRecords); err != nil {
  65. return err
  66. }
  67. partialSig := f.PartialSig.Zero()
  68. typeMap, err := tlvRecords.ExtractRecords(&partialSig)
  69. if err != nil {
  70. return err
  71. }
  72. // Set the corresponding TLV types if they were included in the stream.
  73. if val, ok := typeMap[f.PartialSig.TlvType()]; ok && val == nil {
  74. f.PartialSig = tlv.SomeRecordT(partialSig)
  75. }
  76. if len(tlvRecords) != 0 {
  77. f.ExtraData = tlvRecords
  78. }
  79. return nil
  80. }
  81. // MsgType returns the uint32 code which uniquely identifies this message as a
  82. // FundingSigned on the wire.
  83. //
  84. // This is part of the lnwire.Message interface.
  85. func (f *FundingSigned) MsgType() MessageType {
  86. return MsgFundingSigned
  87. }