2015-12-31 11:42:25 +01:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-23 22:54:34 +02:00
|
|
|
|
2016-05-15 16:17:44 +02:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2015-12-31 11:42:25 +01:00
|
|
|
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// CloseRequest is sent by either side in order to initiate the cooperative
|
|
|
|
// closure of a channel. This message is rather sparse as both side implicitly
|
|
|
|
// know to craft a transaction sending the settled funds of both parties to the
|
|
|
|
// final delivery addresses negotiated during the funding workflow.
|
|
|
|
//
|
|
|
|
// NOTE: The requester is able to only send a signature to initiate the
|
|
|
|
// cooperative channel closure 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 11:42:25 +01:00
|
|
|
type CloseRequest struct {
|
2016-05-23 22:54:34 +02:00
|
|
|
// ChannelID serves to identify which channel is to be closed.
|
|
|
|
ChannelID uint64
|
|
|
|
|
|
|
|
// RequesterCloseSig is the signature of the requester for the fully
|
|
|
|
// assembled closing transaction.
|
|
|
|
RequesterCloseSig *btcec.Signature
|
2015-12-31 11:42:25 +01:00
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// Fee is the required fee-per-KB the closing transaction must have.
|
|
|
|
// It is recommended that a "sufficient" fee be paid in order to achieve
|
|
|
|
// timely channel closure.
|
|
|
|
Fee btcutil.Amount
|
2015-12-31 11:42:25 +01:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// Decode deserializes a serialized CloseRequest stored in the passed io.Reader
|
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) Decode(r io.Reader, pver uint32) error {
|
2016-05-23 22:54:34 +02:00
|
|
|
// ChannelID (8)
|
2016-01-17 02:14:35 +01:00
|
|
|
// RequesterCloseSig (73)
|
|
|
|
// First byte length then sig
|
|
|
|
// Fee (8)
|
2015-12-31 11:42:25 +01:00
|
|
|
err := readElements(r,
|
2016-05-23 22:54:34 +02:00
|
|
|
&c.ChannelID,
|
2015-12-31 11:42:25 +01:00
|
|
|
&c.RequesterCloseSig,
|
|
|
|
&c.Fee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// NewCloseRequest creates a new CloseRequest.
|
2015-12-31 11:42:25 +01:00
|
|
|
func NewCloseRequest() *CloseRequest {
|
|
|
|
return &CloseRequest{}
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// A compile time check to ensure CloseRequest implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*CloseRequest)(nil)
|
|
|
|
|
|
|
|
// Encode serializes the target CloseRequest into the passed io.Writer observing
|
|
|
|
// the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) Encode(w io.Writer, pver uint32) error {
|
2016-05-23 22:54:34 +02:00
|
|
|
// ChannelID
|
2016-01-17 02:14:35 +01:00
|
|
|
// RequesterCloseSig
|
|
|
|
// Fee
|
2015-12-31 11:42:25 +01:00
|
|
|
err := writeElements(w,
|
2016-05-23 22:54:34 +02:00
|
|
|
c.ChannelID,
|
2015-12-31 11:42:25 +01:00
|
|
|
c.RequesterCloseSig,
|
|
|
|
c.Fee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// Command returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) Command() uint32 {
|
|
|
|
return CmdCloseRequest
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for this message
|
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *CloseRequest) MaxPayloadLength(pver uint32) uint32 {
|
2016-01-17 02:14:35 +01:00
|
|
|
// 8 + 73 + 8
|
2015-12-31 11:42:25 +01:00
|
|
|
return 89
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
|
|
|
// on the CloseRequest are valid.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) Validate() error {
|
2016-05-23 22:54:34 +02:00
|
|
|
// Fee must be greater than 0.
|
2015-12-31 11:42:25 +01:00
|
|
|
if c.Fee < 0 {
|
|
|
|
return fmt.Errorf("Fee must be greater than zero.")
|
|
|
|
}
|
2016-05-23 22:54:34 +02:00
|
|
|
|
2016-01-17 02:14:35 +01:00
|
|
|
// We're good!
|
2015-12-31 11:42:25 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:54:34 +02:00
|
|
|
// String returns the string representation of the target CloseRequest.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) String() string {
|
|
|
|
var serializedSig []byte
|
|
|
|
if c.RequesterCloseSig != nil && c.RequesterCloseSig.R != nil {
|
|
|
|
serializedSig = c.RequesterCloseSig.Serialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("\n--- Begin CloseRequest ---\n") +
|
2016-05-23 22:54:34 +02:00
|
|
|
fmt.Sprintf("ChannelID:\t\t%d\n", c.ChannelID) +
|
2015-12-31 11:42:25 +01:00
|
|
|
fmt.Sprintf("CloseSig\t\t%x\n", serializedSig) +
|
|
|
|
fmt.Sprintf("Fee:\t\t\t%d\n", c.Fee) +
|
|
|
|
fmt.Sprintf("--- End CloseRequest ---\n")
|
|
|
|
}
|