lnd/lnwire/close_request.go

87 lines
1.8 KiB
Go
Raw Normal View History

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
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 {
// 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
}
// Creates a new CloseRequest
2015-12-31 11:42:25 +01:00
func NewCloseRequest() *CloseRequest {
return &CloseRequest{}
}
// 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 {
// 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 {
// 8 + 73 + 8
2015-12-31 11:42:25 +01:00
return 89
}
// 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 {
// 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.")
}
// 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")
}