2016-05-23 07:25:42 +02:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SingleFundingComplete is the message Alice sends to Bob once she is able to
|
|
|
|
// fully assemble the funding transaction, and both versions of the commitment
|
|
|
|
// transaction. The purpose of this message is to present Bob with the items
|
|
|
|
// required for him to generate a signature for Alice's version of the
|
|
|
|
// commitment transaction.
|
|
|
|
type SingleFundingComplete struct {
|
|
|
|
// ChannelID serves to uniquely identify the future channel created by
|
|
|
|
// the initiated single funder workflow.
|
2016-06-21 06:55:07 +02:00
|
|
|
// TODO(roasbeef): change all to PendingChannelID, document schema
|
2016-05-23 07:25:42 +02:00
|
|
|
ChannelID uint64
|
|
|
|
|
|
|
|
// FundingOutPoint is the outpoint (txid:index) of the funding
|
|
|
|
// transaction. With this value, Bob will be able to generate a
|
|
|
|
// signature for Alice's version of the commitment transaction.
|
2017-02-16 13:34:44 +01:00
|
|
|
FundingOutPoint wire.OutPoint
|
2016-05-23 07:25:42 +02:00
|
|
|
|
2016-05-31 00:45:44 +02:00
|
|
|
// CommitSignature is Alice's signature for Bob's version of the
|
2016-05-23 07:25:42 +02:00
|
|
|
// commitment transaction.
|
2016-05-31 00:45:44 +02:00
|
|
|
CommitSignature *btcec.Signature
|
2016-06-30 20:49:58 +02:00
|
|
|
|
|
|
|
// RevocationKey is the initial key to be used for the revocation
|
|
|
|
// clause within the self-output of the initiators's commitment
|
|
|
|
// transaction. Once an initial new state is created, the initiator
|
2017-01-13 06:01:50 +01:00
|
|
|
// will send a preimage which will allow the initiator to sweep the
|
2016-06-30 20:49:58 +02:00
|
|
|
// initiator's funds if the violate the contract.
|
|
|
|
RevocationKey *btcec.PublicKey
|
2016-11-15 04:03:55 +01:00
|
|
|
|
|
|
|
// StateHintObsfucator is the set of bytes used by the initiator to
|
|
|
|
// obsfucate the state number encoded within the sequence number for
|
|
|
|
// the commitment transaction's only input. The initiator generates
|
|
|
|
// this value by hashing the 0th' state derived from the revocation PRF
|
|
|
|
// an additional time.
|
2017-01-05 08:55:52 +01:00
|
|
|
StateHintObsfucator [6]byte
|
2016-05-31 00:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSingleFundingComplete creates, and returns a new empty
|
|
|
|
// SingleFundingResponse.
|
2017-02-16 13:34:44 +01:00
|
|
|
func NewSingleFundingComplete(chanID uint64, fundingPoint wire.OutPoint,
|
2016-11-15 04:03:55 +01:00
|
|
|
commitSig *btcec.Signature, revokeKey *btcec.PublicKey,
|
2017-01-05 08:55:52 +01:00
|
|
|
obsfucator [6]byte) *SingleFundingComplete {
|
2016-05-31 00:45:44 +02:00
|
|
|
|
|
|
|
return &SingleFundingComplete{
|
2016-11-15 04:03:55 +01:00
|
|
|
ChannelID: chanID,
|
|
|
|
FundingOutPoint: fundingPoint,
|
|
|
|
CommitSignature: commitSig,
|
|
|
|
RevocationKey: revokeKey,
|
|
|
|
StateHintObsfucator: obsfucator,
|
2016-05-31 00:45:44 +02:00
|
|
|
}
|
2016-05-23 07:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode deserializes the serialized SingleFundingComplete stored in the passed
|
|
|
|
// io.Reader into the target SingleFundingComplete using the deserialization
|
|
|
|
// rules defined by the passed protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *SingleFundingComplete) Decode(r io.Reader, pver uint32) error {
|
|
|
|
// ChannelID (8)
|
|
|
|
// FundingOutPoint (36)
|
2016-06-30 20:49:58 +02:00
|
|
|
// CommitmentSignature (73)
|
|
|
|
// RevocationKey (33)
|
2016-05-23 07:25:42 +02:00
|
|
|
err := readElements(r,
|
|
|
|
&s.ChannelID,
|
|
|
|
&s.FundingOutPoint,
|
2016-06-30 20:49:58 +02:00
|
|
|
&s.CommitSignature,
|
2016-11-15 04:03:55 +01:00
|
|
|
&s.RevocationKey,
|
2017-02-16 13:34:44 +01:00
|
|
|
s.StateHintObsfucator[:])
|
2016-05-23 07:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode serializes the target SingleFundingComplete into the passed io.Writer
|
|
|
|
// implementation. Serialization will observe the rules defined by the passed
|
|
|
|
// protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error {
|
|
|
|
// ChannelID (8)
|
|
|
|
// FundingOutPoint (36)
|
|
|
|
// Commitment Signature (73)
|
2016-06-30 20:49:58 +02:00
|
|
|
// RevocationKey (33)
|
2016-05-23 07:25:42 +02:00
|
|
|
err := writeElements(w,
|
|
|
|
s.ChannelID,
|
|
|
|
s.FundingOutPoint,
|
2016-06-30 20:49:58 +02:00
|
|
|
s.CommitSignature,
|
2016-11-15 04:03:55 +01:00
|
|
|
s.RevocationKey,
|
2017-02-16 13:34:44 +01:00
|
|
|
s.StateHintObsfucator[:])
|
2016-05-23 07:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the uint32 code which uniquely identifies this message as a
|
2017-01-31 06:45:02 +01:00
|
|
|
// SingleFundingComplete on the wire.
|
2016-05-23 07:25:42 +02:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *SingleFundingComplete) Command() uint32 {
|
|
|
|
return CmdSingleFundingComplete
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum allowed payload length for a
|
|
|
|
// SingleFundingComplete. This is calculated by summing the max length of all
|
2017-01-31 06:45:02 +01:00
|
|
|
// the fields within a SingleFundingComplete. Therefore, the final breakdown
|
2016-11-15 04:03:55 +01:00
|
|
|
// is: 8 + 36 + 33 + 73 + 4 = 154
|
2016-05-23 07:25:42 +02:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *SingleFundingComplete) MaxPayloadLength(uint32) uint32 {
|
2016-11-15 04:03:55 +01:00
|
|
|
return 154
|
2016-05-23 07:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate examines each populated field within the SingleFundingComplete for
|
|
|
|
// field sanity.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (s *SingleFundingComplete) Validate() error {
|
|
|
|
var zeroHash [32]byte
|
|
|
|
if bytes.Equal(zeroHash[:], s.FundingOutPoint.Hash[:]) {
|
|
|
|
return fmt.Errorf("funding outpoint hash must be non-zero")
|
|
|
|
}
|
|
|
|
|
2016-05-31 00:45:44 +02:00
|
|
|
if s.CommitSignature == nil {
|
2016-05-23 07:25:42 +02:00
|
|
|
return fmt.Errorf("commitment signature must be non-nil")
|
|
|
|
}
|
|
|
|
|
2016-06-30 20:49:58 +02:00
|
|
|
// TODO(roasbeef): fin validation
|
|
|
|
|
2016-05-23 07:25:42 +02:00
|
|
|
// We're good!
|
|
|
|
return nil
|
|
|
|
}
|