2017-03-09 00:32:11 +01:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2021-06-17 08:17:30 +02:00
|
|
|
"bytes"
|
2017-03-09 00:32:11 +01:00
|
|
|
"io"
|
2023-01-17 04:43:25 +01:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
|
|
|
)
|
|
|
|
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
type (
|
|
|
|
// ShutdownNonceType is the type of the shutdown nonce TLV record.
|
|
|
|
ShutdownNonceType = tlv.TlvType8
|
2023-01-17 04:43:25 +01:00
|
|
|
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
// ShutdownNonceTLV is the TLV record that contains the shutdown nonce.
|
|
|
|
ShutdownNonceTLV = tlv.OptionalRecordT[ShutdownNonceType, Musig2Nonce]
|
|
|
|
)
|
2023-01-17 04:43:25 +01:00
|
|
|
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
// SomeShutdownNonce returns a ShutdownNonceTLV with the given nonce.
|
|
|
|
func SomeShutdownNonce(nonce Musig2Nonce) ShutdownNonceTLV {
|
|
|
|
return tlv.SomeRecordT(
|
|
|
|
tlv.NewRecordT[ShutdownNonceType, Musig2Nonce](nonce),
|
2023-01-17 04:43:25 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-09 00:32:11 +01:00
|
|
|
// Shutdown is sent by either side in order to initiate the cooperative closure
|
|
|
|
// of a channel. This message is sparse as both sides implicitly have the
|
|
|
|
// information necessary to construct a transaction that will send the settled
|
|
|
|
// funds of both parties to the final delivery addresses negotiated during the
|
|
|
|
// funding workflow.
|
|
|
|
type Shutdown struct {
|
|
|
|
// ChannelID serves to identify which channel is to be closed.
|
|
|
|
ChannelID ChannelID
|
|
|
|
|
2017-03-25 02:26:09 +01:00
|
|
|
// Address is the script to which the channel funds will be paid.
|
2017-03-09 00:32:11 +01:00
|
|
|
Address DeliveryAddress
|
2020-01-28 02:25:36 +01:00
|
|
|
|
2023-01-17 04:43:25 +01:00
|
|
|
// ShutdownNonce is the nonce the sender will use to sign the first
|
|
|
|
// co-op sign offer.
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
ShutdownNonce ShutdownNonceTLV
|
2023-01-17 04:43:25 +01:00
|
|
|
|
2020-01-28 02:25:36 +01:00
|
|
|
// 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.
|
|
|
|
ExtraData ExtraOpaqueData
|
2017-03-09 00:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewShutdown creates a new Shutdown message.
|
|
|
|
func NewShutdown(cid ChannelID, addr DeliveryAddress) *Shutdown {
|
|
|
|
return &Shutdown{
|
|
|
|
ChannelID: cid,
|
|
|
|
Address: addr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time check to ensure Shutdown implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*Shutdown)(nil)
|
|
|
|
|
|
|
|
// Decode deserializes a serialized Shutdown stored in the passed io.Reader
|
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *Shutdown) Decode(r io.Reader, pver uint32) error {
|
2023-01-17 04:43:25 +01:00
|
|
|
err := ReadElements(r, &s.ChannelID, &s.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tlvRecords ExtraOpaqueData
|
|
|
|
if err := ReadElements(r, &tlvRecords); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
musigNonce := s.ShutdownNonce.Zero()
|
2023-01-17 04:43:25 +01:00
|
|
|
typeMap, err := tlvRecords.ExtractRecords(&musigNonce)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the corresponding TLV types if they were included in the stream.
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
if val, ok := typeMap[s.ShutdownNonce.TlvType()]; ok && val == nil {
|
|
|
|
s.ShutdownNonce = tlv.SomeRecordT(musigNonce)
|
2023-01-17 04:43:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tlvRecords) != 0 {
|
|
|
|
s.ExtraData = tlvRecords
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-09 00:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode serializes the target Shutdown into the passed io.Writer observing
|
|
|
|
// the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2021-06-17 08:17:30 +02:00
|
|
|
func (s *Shutdown) Encode(w *bytes.Buffer, pver uint32) error {
|
2023-01-17 04:43:25 +01:00
|
|
|
recordProducers := make([]tlv.RecordProducer, 0, 1)
|
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
In this commit, we update new Taproot related TLVs (nonces, partial sig,
sig with nonce, etc). Along the way we were able to get rid of some
boiler plate, but most importantly, we're able to better protect against
API misuse (using a nonce that isn't initialized, etc) with the new
options API. In some areas this introduces a bit of extra boiler plate,
and where applicable I used some new helper functions to help cut down
on the noise.
Note to reviewers: this is done as a single commit, as changing the API
breaks all callers, so if we want things to compile it needs to be in a
wumbo commit.
2024-02-24 03:04:51 +01:00
|
|
|
s.ShutdownNonce.WhenSome(
|
|
|
|
func(nonce tlv.RecordT[ShutdownNonceType, Musig2Nonce]) {
|
|
|
|
recordProducers = append(recordProducers, &nonce)
|
|
|
|
},
|
|
|
|
)
|
2023-01-17 04:43:25 +01:00
|
|
|
err := EncodeMessageExtraData(&s.ExtraData, recordProducers...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-18 09:11:43 +02:00
|
|
|
if err := WriteChannelID(w, s.ChannelID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := WriteDeliveryAddress(w, s.Address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteBytes(w, s.ExtraData)
|
2017-03-09 00:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MsgType returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *Shutdown) MsgType() MessageType {
|
|
|
|
return MsgShutdown
|
|
|
|
}
|