2016-01-05 19:19:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Multiple Clearing Requests are possible by putting this inside an array of
|
|
|
|
// clearing requests
|
2016-01-05 19:19:22 +03:00
|
|
|
type CommitSignature struct {
|
2016-01-17 04:14:35 +03:00
|
|
|
// We can use a different data type for this if necessary...
|
2016-01-05 19:19:22 +03:00
|
|
|
ChannelID uint64
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Height of the commitment
|
|
|
|
// You should have the most recent commitment height stored locally
|
|
|
|
// This should be validated!
|
|
|
|
// This is used for shachain.
|
|
|
|
// Each party increments their own CommitmentHeight, they can differ for
|
|
|
|
// each part of the Commitment.
|
2016-01-05 19:19:22 +03:00
|
|
|
CommitmentHeight uint64
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// List of HTLC Keys which are updated from all parties
|
2016-01-15 04:02:23 +03:00
|
|
|
UpdatedHTLCKeys []uint64
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Hash of the revocation to use
|
2016-01-05 19:19:22 +03:00
|
|
|
RevocationHash [20]byte
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Total miners' fee that was used
|
2016-01-05 19:19:22 +03:00
|
|
|
Fee btcutil.Amount
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Signature for the new Commitment
|
|
|
|
CommitSig *btcec.Signature // Requester's Commitment
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommitSignature) Decode(r io.Reader, pver uint32) error {
|
2016-01-17 04:14:35 +03:00
|
|
|
// ChannelID(8)
|
|
|
|
// CommitmentHeight(8)
|
|
|
|
// c.UpdatedHTLCKeys(8*1000max)
|
|
|
|
// RevocationHash(20)
|
|
|
|
// Fee(8)
|
|
|
|
// RequesterCommitSig(73max+2)
|
2016-01-05 19:19:22 +03:00
|
|
|
err := readElements(r,
|
|
|
|
&c.ChannelID,
|
|
|
|
&c.CommitmentHeight,
|
2016-01-15 04:02:23 +03:00
|
|
|
&c.UpdatedHTLCKeys,
|
2016-01-05 19:19:22 +03:00
|
|
|
&c.RevocationHash,
|
|
|
|
&c.Fee,
|
|
|
|
&c.CommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Creates a new CommitSignature
|
2016-01-05 19:19:22 +03:00
|
|
|
func NewCommitSignature() *CommitSignature {
|
|
|
|
return &CommitSignature{}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Serializes the item from the CommitSignature struct
|
|
|
|
// Writes the data to w
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitSignature) Encode(w io.Writer, pver uint32) error {
|
|
|
|
err := writeElements(w,
|
|
|
|
c.ChannelID,
|
|
|
|
c.CommitmentHeight,
|
2016-01-15 04:02:23 +03:00
|
|
|
c.UpdatedHTLCKeys,
|
2016-01-05 19:19:22 +03:00
|
|
|
c.RevocationHash,
|
|
|
|
c.Fee,
|
|
|
|
c.CommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommitSignature) Command() uint32 {
|
|
|
|
return CmdCommitSignature
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommitSignature) MaxPayloadLength(uint32) uint32 {
|
2016-01-15 04:02:23 +03:00
|
|
|
return 8192
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
|
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-01-17 04:14:35 +03:00
|
|
|
// We're good!
|
2016-01-05 19:19:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommitSignature) String() string {
|
2016-01-17 04:14:35 +03:00
|
|
|
// c.ChannelID,
|
|
|
|
// c.CommitmentHeight,
|
|
|
|
// c.RevocationHash,
|
|
|
|
// c.UpdatedHTLCKeys,
|
|
|
|
// c.Fee,
|
|
|
|
// c.CommitSig,
|
2016-01-05 19:19:22 +03:00
|
|
|
var serializedSig []byte
|
|
|
|
if &c.CommitSig != nil && c.CommitSig.R != nil {
|
|
|
|
serializedSig = c.CommitSig.Serialize()
|
|
|
|
}
|
2016-01-15 04:02:23 +03:00
|
|
|
var items string
|
|
|
|
for i := 0; i < len(c.UpdatedHTLCKeys); i++ {
|
|
|
|
items += fmt.Sprintf("%d ", c.UpdatedHTLCKeys[i])
|
|
|
|
}
|
2016-01-05 19:19:22 +03:00
|
|
|
|
|
|
|
return fmt.Sprintf("\n--- Begin CommitSignature ---\n") +
|
|
|
|
fmt.Sprintf("ChannelID:\t\t%d\n", c.ChannelID) +
|
|
|
|
fmt.Sprintf("CommitmentHeight:\t%d\n", c.CommitmentHeight) +
|
2016-01-15 04:02:23 +03:00
|
|
|
fmt.Sprintf("UpdatedHTLCKeys:\t%s\n", items) +
|
2016-01-05 19:19:22 +03:00
|
|
|
fmt.Sprintf("RevocationHash:\t\t%x\n", c.RevocationHash) +
|
|
|
|
fmt.Sprintf("Fee:\t\t\t%s\n", c.Fee.String()) +
|
|
|
|
fmt.Sprintf("CommitSig:\t\t%x\n", serializedSig) +
|
|
|
|
fmt.Sprintf("--- End CommitSignature ---\n")
|
|
|
|
}
|