2015-12-31 11:42:25 +01:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CloseRequest struct {
|
|
|
|
ReservationID uint64
|
|
|
|
|
2016-01-17 02:14:35 +01:00
|
|
|
RequesterCloseSig *btcec.Signature // Requester's Commitment
|
2015-12-31 11:42:25 +01:00
|
|
|
Fee btcutil.Amount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloseRequest) Decode(r io.Reader, pver uint32) error {
|
2016-01-17 02:14:35 +01:00
|
|
|
// ReservationID (8)
|
|
|
|
// RequesterCloseSig (73)
|
|
|
|
// First byte length then sig
|
|
|
|
// Fee (8)
|
2015-12-31 11:42:25 +01:00
|
|
|
err := readElements(r,
|
|
|
|
&c.ReservationID,
|
|
|
|
&c.RequesterCloseSig,
|
|
|
|
&c.Fee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-17 02:14:35 +01:00
|
|
|
// Creates a new CloseRequest
|
2015-12-31 11:42:25 +01:00
|
|
|
func NewCloseRequest() *CloseRequest {
|
|
|
|
return &CloseRequest{}
|
|
|
|
}
|
|
|
|
|
2016-01-17 02:14:35 +01:00
|
|
|
// Serializes the item from the CloseRequest struct
|
|
|
|
// Writes the data to w
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) Encode(w io.Writer, pver uint32) error {
|
2016-01-17 02:14:35 +01:00
|
|
|
// ReservationID
|
|
|
|
// RequesterCloseSig
|
|
|
|
// Fee
|
2015-12-31 11:42:25 +01:00
|
|
|
err := writeElements(w,
|
|
|
|
c.ReservationID,
|
|
|
|
c.RequesterCloseSig,
|
|
|
|
c.Fee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloseRequest) Command() uint32 {
|
|
|
|
return CmdCloseRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloseRequest) MaxPayloadLength(uint32) uint32 {
|
2016-01-17 02:14:35 +01:00
|
|
|
// 8 + 73 + 8
|
2015-12-31 11:42:25 +01:00
|
|
|
return 89
|
|
|
|
}
|
|
|
|
|
2016-01-17 02:14:35 +01:00
|
|
|
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
|
2015-12-31 11:42:25 +01:00
|
|
|
func (c *CloseRequest) Validate() error {
|
2016-01-17 02:14:35 +01: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-01-17 02:14:35 +01:00
|
|
|
// We're good!
|
2015-12-31 11:42:25 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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") +
|
|
|
|
fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) +
|
|
|
|
fmt.Sprintf("CloseSig\t\t%x\n", serializedSig) +
|
|
|
|
fmt.Sprintf("Fee:\t\t\t%d\n", c.Fee) +
|
|
|
|
fmt.Sprintf("--- End CloseRequest ---\n")
|
|
|
|
}
|