2016-01-05 19:19:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-23 23:54:34 +03:00
|
|
|
"io"
|
|
|
|
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2016-06-21 07:55:07 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// CommitSignature is sent by either side to stage any pending HTLC's in the
|
|
|
|
// reciever's pending set which has not explcitly been rejected via an
|
|
|
|
// HTLCAddReject message. Implictly, the new commitment transaction constructed
|
|
|
|
// which has been signed by CommitSig includes all HTLC's in the remote node's
|
|
|
|
// pending set. A CommitSignature message may be sent after a series of HTLCAdd
|
|
|
|
// messages in order to batch add several HTLC's with a single signature
|
|
|
|
// covering all implicitly accepted HTLC's.
|
2016-01-05 19:19:22 +03:00
|
|
|
type CommitSignature struct {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint uniquely identifies to which currently active channel this
|
2016-05-23 23:54:34 +03:00
|
|
|
// CommitSignature applies to.
|
2016-06-21 07:55:07 +03:00
|
|
|
ChannelPoint *wire.OutPoint
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-06-30 21:55:12 +03:00
|
|
|
// LogIndex is the index into the reciever's HTLC log to which this
|
|
|
|
// commitment signature covers. In order to properly verify this
|
|
|
|
// signature, the receiver should include all the HTLC's within their
|
|
|
|
// log with an index less-than-or-equal to the listed log-index.
|
|
|
|
LogIndex uint64
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Fee represents the total miner's fee that was used when constructing
|
|
|
|
// the new commitment transaction.
|
|
|
|
// TODO(roasbeef): is the above comment correct?
|
2016-01-05 19:19:22 +03:00
|
|
|
Fee btcutil.Amount
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// CommitSig is Alice's signature for Bob's new commitment transaction.
|
|
|
|
// Alice is able to send this signature without requesting any additional
|
|
|
|
// data due to the piggybacking of Bob's next revocation hash in his
|
|
|
|
// prior CommitRevocation message, as well as the cannonical ordering
|
|
|
|
// used for all inputs/outputs within commitment transactions.
|
|
|
|
CommitSig *btcec.Signature
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// NewCommitSignature creates a new empty CommitSignature message.
|
|
|
|
func NewCommitSignature() *CommitSignature {
|
|
|
|
return &CommitSignature{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure CommitSignature implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*CommitSignature)(nil)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Decode deserializes a serialized CommitSignature message stored in the
|
|
|
|
// passed io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitSignature) Decode(r io.Reader, pver uint32) error {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint(8)
|
2016-06-30 21:55:12 +03:00
|
|
|
// LogIndex(8)
|
2016-01-17 04:14:35 +03:00
|
|
|
// Fee(8)
|
|
|
|
// RequesterCommitSig(73max+2)
|
2016-01-05 19:19:22 +03:00
|
|
|
err := readElements(r,
|
2016-06-21 07:55:07 +03:00
|
|
|
&c.ChannelPoint,
|
2016-06-30 21:55:12 +03:00
|
|
|
&c.LogIndex,
|
2016-01-05 19:19:22 +03:00
|
|
|
&c.Fee,
|
|
|
|
&c.CommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Encode serializes the target CommitSignature into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *CommitSignature) Encode(w io.Writer, pver uint32) error {
|
2016-06-30 21:55:12 +03:00
|
|
|
err := writeElements(w,
|
|
|
|
c.ChannelPoint,
|
|
|
|
c.LogIndex,
|
|
|
|
c.Fee,
|
|
|
|
c.CommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitSignature) Command() uint32 {
|
|
|
|
return CmdCommitSignature
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a
|
|
|
|
// CommitSignature complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitSignature) MaxPayloadLength(uint32) uint32 {
|
2016-06-21 07:55:07 +03:00
|
|
|
// 36 + 8 + 73
|
|
|
|
return 117
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
|
|
|
// on the CommitSignature are valid.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitSignature) Validate() error {
|
|
|
|
if c.Fee < 0 {
|
2016-01-17 04:14:35 +03:00
|
|
|
// While fees can be negative, it's too confusing to allow
|
|
|
|
// negative payments. Maybe for some wallets, but not this one!
|
2016-01-05 19:19:22 +03:00
|
|
|
return fmt.Errorf("Amount paid cannot be negative.")
|
|
|
|
}
|
2016-05-23 23:54:34 +03:00
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// We're good!
|
2016-01-05 19:19:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// String returns the string representation of the target CommitSignature.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitSignature) String() string {
|
|
|
|
var serializedSig []byte
|
2016-05-23 23:54:34 +03:00
|
|
|
if c.CommitSig != nil {
|
2016-01-05 19:19:22 +03:00
|
|
|
serializedSig = c.CommitSig.Serialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("\n--- Begin CommitSignature ---\n") +
|
2016-07-13 03:05:13 +03:00
|
|
|
fmt.Sprintf("ChannelPoint:\t%v\n", c.ChannelPoint) +
|
|
|
|
fmt.Sprintf("LogIndex:\t\t%v\n", c.LogIndex) +
|
2016-01-05 19:19:22 +03:00
|
|
|
fmt.Sprintf("Fee:\t\t\t%s\n", c.Fee.String()) +
|
|
|
|
fmt.Sprintf("CommitSig:\t\t%x\n", serializedSig) +
|
|
|
|
fmt.Sprintf("--- End CommitSignature ---\n")
|
|
|
|
}
|