2015-12-31 13:42:25 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-23 23:54:34 +03:00
|
|
|
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2015-12-31 13:42:25 +03:00
|
|
|
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// CloseComplete is sent by Bob signalling a fufillment and completion of
|
|
|
|
// Alice's prior CloseRequest message. After Alice receives Bob's CloseComplete
|
|
|
|
// message, she is able to broadcast the fully signed transaction executing a
|
|
|
|
// cooperative closure of the channel.
|
|
|
|
//
|
|
|
|
// NOTE: The responder is able to only send a signature without any additional
|
|
|
|
// message as all transactions are assembled observing BIP 69 which defines a
|
|
|
|
// cannonical ordering for input/outputs. Therefore, both sides are able to
|
|
|
|
// arrive at an identical closure transaction as they know the order of the
|
|
|
|
// inputs/outputs.
|
2015-12-31 13:42:25 +03:00
|
|
|
type CloseComplete struct {
|
2016-05-23 23:54:34 +03:00
|
|
|
// ChannelID serves to identify which channel is to be closed.
|
|
|
|
ChannelID uint64
|
2015-12-31 13:42:25 +03:00
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// ResponderCloseSig is the signature of the responder for the
|
|
|
|
// transaction which closes the previously active channel.
|
|
|
|
ResponderCloseSig *btcec.Signature
|
2015-12-31 13:42:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// NewCloseComplete creates a new empty CloseComplete message.
|
|
|
|
// TODO(roasbeef): add params to all constructors...
|
|
|
|
func NewCloseComplete() *CloseComplete {
|
|
|
|
return &CloseComplete{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure CloseComplete implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*CloseComplete)(nil)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Decode deserializes a serialized CloseComplete message stored in the passed
|
|
|
|
// io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 13:42:25 +03:00
|
|
|
func (c *CloseComplete) Decode(r io.Reader, pver uint32) error {
|
2016-05-23 23:54:34 +03:00
|
|
|
// ChannelID (8)
|
2016-01-17 04:14:35 +03:00
|
|
|
// ResponderCloseSig (73)
|
2015-12-31 13:42:25 +03:00
|
|
|
err := readElements(r,
|
2016-05-23 23:54:34 +03:00
|
|
|
&c.ChannelID,
|
|
|
|
&c.ResponderCloseSig)
|
2015-12-31 13:42:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Encode serializes the target CloseComplete into the passed io.Writer observing
|
|
|
|
// the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 13:42:25 +03:00
|
|
|
func (c *CloseComplete) Encode(w io.Writer, pver uint32) error {
|
2016-05-23 23:54:34 +03:00
|
|
|
// ChannelID (8)
|
|
|
|
// ResponderCloseSig (73)
|
2015-12-31 13:42:25 +03:00
|
|
|
err := writeElements(w,
|
2016-05-23 23:54:34 +03:00
|
|
|
c.ChannelID,
|
|
|
|
c.ResponderCloseSig)
|
2015-12-31 13:42:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Command returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 13:42:25 +03:00
|
|
|
func (c *CloseComplete) Command() uint32 {
|
|
|
|
return CmdCloseComplete
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a CloseComplete
|
|
|
|
// complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 13:42:25 +03:00
|
|
|
func (c *CloseComplete) MaxPayloadLength(uint32) uint32 {
|
2016-01-17 04:14:35 +03:00
|
|
|
// 8 + 73 + 32
|
2015-12-31 13:42:25 +03:00
|
|
|
return 113
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
|
|
|
// on the CloseComplete are valid.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 13:42:25 +03:00
|
|
|
func (c *CloseComplete) Validate() error {
|
2016-01-17 04:14:35 +03:00
|
|
|
// We're good!
|
2015-12-31 13:42:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// String returns the string representation of the target CloseComplete.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 13:42:25 +03:00
|
|
|
func (c *CloseComplete) String() string {
|
|
|
|
var serializedSig []byte
|
2016-05-23 23:54:34 +03:00
|
|
|
if c.ResponderCloseSig != nil {
|
2015-12-31 13:42:25 +03:00
|
|
|
serializedSig = c.ResponderCloseSig.Serialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("\n--- Begin CloseComplete ---\n") +
|
2016-05-23 23:54:34 +03:00
|
|
|
fmt.Sprintf("ReservationID:\t\t%d\n", c.ChannelID) +
|
2015-12-31 13:42:25 +03:00
|
|
|
fmt.Sprintf("ResponderCloseSig:\t%x\n", serializedSig) +
|
|
|
|
fmt.Sprintf("--- End CloseComplete ---\n")
|
|
|
|
}
|