2016-05-18 07:28:42 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-05-23 23:46:18 +03:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2016-12-06 17:12:21 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2016-05-18 07:28:42 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:46:18 +03:00
|
|
|
// SingleFundingResponse is the message Bob sends to Alice after she initiates
|
|
|
|
// the single funder channel workflow via a SingleFundingRequest message. Once
|
2017-04-17 01:22:20 +03:00
|
|
|
// Alice receives Bob's response, then she has all the items necessary to
|
2016-05-23 23:46:18 +03:00
|
|
|
// construct the funding transaction, and both commitment transactions.
|
|
|
|
type SingleFundingResponse struct {
|
2017-04-17 01:22:20 +03:00
|
|
|
// PendingChannelID serves to uniquely identify the future channel
|
|
|
|
// created by the initiated single funder workflow.
|
|
|
|
PendingChannelID [32]byte
|
2016-05-23 23:46:18 +03:00
|
|
|
|
2016-06-30 21:49:58 +03:00
|
|
|
// ChannelDerivationPoint is an secp256k1 point which will be used to
|
|
|
|
// derive the public key the responder will use for the half of the
|
|
|
|
// 2-of-2 multi-sig. Using the channel derivation point (CDP), and the
|
|
|
|
// responder's identity public key (A), the channel public key is
|
|
|
|
// computed as: C = A + CDP. In order to be valid all CDP's MUST have
|
|
|
|
// an odd y-coordinate.
|
|
|
|
ChannelDerivationPoint *btcec.PublicKey
|
2016-05-23 23:46:18 +03:00
|
|
|
|
2016-06-21 07:55:07 +03:00
|
|
|
// CommitmentKey is key the responder to the funding workflow wishes to
|
|
|
|
// use within their versino of the commitment transaction for any
|
|
|
|
// delayed (CSV) or immediate outputs to them.
|
|
|
|
CommitmentKey *btcec.PublicKey
|
|
|
|
|
2016-06-30 21:49:58 +03:00
|
|
|
// RevocationKey is the initial key to be used for the revocation
|
|
|
|
// clause within the self-output of the responder's commitment
|
|
|
|
// transaction. Once an initial new state is created, the responder
|
2017-01-13 08:01:50 +03:00
|
|
|
// will send a preimage which will allow the initiator to sweep the
|
2016-06-30 21:49:58 +03:00
|
|
|
// responder's funds if the violate the contract.
|
|
|
|
RevocationKey *btcec.PublicKey
|
|
|
|
|
2016-06-21 07:55:07 +03:00
|
|
|
// CsvDelay is the number of blocks to use for the relative time lock
|
|
|
|
// in the pay-to-self output of both commitment transactions.
|
|
|
|
CsvDelay uint32
|
|
|
|
|
2016-05-23 23:46:18 +03:00
|
|
|
// DeliveryPkScript defines the public key script that the initiator
|
|
|
|
// would like to use to receive their balance in the case of a
|
|
|
|
// cooperative close. Only the following script templates are
|
|
|
|
// supported: P2PKH, P2WKH, P2SH, and P2WSH.
|
|
|
|
DeliveryPkScript PkScript
|
2016-12-06 17:12:21 +03:00
|
|
|
|
|
|
|
// DustLimit is the threshold below which no HTLC output should be
|
|
|
|
// generated for remote commitment transaction; ie. HTLCs below
|
|
|
|
// this amount are not enforceable onchain for their point of view.
|
|
|
|
DustLimit btcutil.Amount
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
// ConfirmationDepth is the number of confirmations that the initiator
|
|
|
|
// of a funding workflow is requesting be required before the channel
|
|
|
|
// is considered fully open.
|
|
|
|
ConfirmationDepth uint32
|
2016-05-18 07:28:42 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 01:45:44 +03:00
|
|
|
// NewSingleFundingResponse creates, and returns a new empty
|
|
|
|
// SingleFundingResponse.
|
2017-04-17 01:22:20 +03:00
|
|
|
func NewSingleFundingResponse(chanID [32]byte, rk, ck, cdp *btcec.PublicKey,
|
2016-12-06 17:12:21 +03:00
|
|
|
delay uint32, deliveryScript PkScript,
|
2017-01-24 05:19:54 +03:00
|
|
|
dustLimit btcutil.Amount, confDepth uint32) *SingleFundingResponse {
|
2016-05-31 01:45:44 +03:00
|
|
|
|
|
|
|
return &SingleFundingResponse{
|
2017-04-17 01:22:20 +03:00
|
|
|
PendingChannelID: chanID,
|
2016-06-30 21:49:58 +03:00
|
|
|
ChannelDerivationPoint: cdp,
|
2016-06-21 07:55:07 +03:00
|
|
|
CommitmentKey: ck,
|
2016-06-30 21:49:58 +03:00
|
|
|
RevocationKey: rk,
|
2016-06-21 07:55:07 +03:00
|
|
|
CsvDelay: delay,
|
2016-05-31 01:45:44 +03:00
|
|
|
DeliveryPkScript: deliveryScript,
|
2016-12-06 17:12:21 +03:00
|
|
|
DustLimit: dustLimit,
|
2017-01-24 05:19:54 +03:00
|
|
|
ConfirmationDepth: confDepth,
|
2016-05-31 01:45:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:49:58 +03:00
|
|
|
// A compile time check to ensure SingleFundingResponse implements the
|
|
|
|
// lnwire.Message interface.
|
|
|
|
var _ Message = (*SingleFundingResponse)(nil)
|
|
|
|
|
2016-05-23 23:46:18 +03:00
|
|
|
// Decode deserializes the serialized SingleFundingResponse stored in the passed
|
|
|
|
// io.Reader into the target SingleFundingResponse using the deserialization
|
|
|
|
// rules defined by the passed protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *SingleFundingResponse) Decode(r io.Reader, pver uint32) error {
|
2017-03-09 07:44:32 +03:00
|
|
|
return readElements(r,
|
2017-04-17 01:22:20 +03:00
|
|
|
c.PendingChannelID[:],
|
2016-05-23 23:46:18 +03:00
|
|
|
&c.ChannelDerivationPoint,
|
2016-06-30 21:49:58 +03:00
|
|
|
&c.CommitmentKey,
|
|
|
|
&c.RevocationKey,
|
2016-06-21 07:55:07 +03:00
|
|
|
&c.CsvDelay,
|
2016-12-06 17:12:21 +03:00
|
|
|
&c.DeliveryPkScript,
|
2017-01-24 05:19:54 +03:00
|
|
|
&c.DustLimit,
|
|
|
|
&c.ConfirmationDepth)
|
2016-05-18 07:28:42 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:46:18 +03:00
|
|
|
// Encode serializes the target SingleFundingResponse 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 (c *SingleFundingResponse) Encode(w io.Writer, pver uint32) error {
|
2017-03-09 07:44:32 +03:00
|
|
|
return writeElements(w,
|
2017-04-17 01:22:20 +03:00
|
|
|
c.PendingChannelID[:],
|
2016-05-23 23:46:18 +03:00
|
|
|
c.ChannelDerivationPoint,
|
2016-06-30 21:49:58 +03:00
|
|
|
c.CommitmentKey,
|
|
|
|
c.RevocationKey,
|
2016-06-21 07:55:07 +03:00
|
|
|
c.CsvDelay,
|
2016-12-06 17:12:21 +03:00
|
|
|
c.DeliveryPkScript,
|
2017-01-24 05:19:54 +03:00
|
|
|
c.DustLimit,
|
|
|
|
c.ConfirmationDepth)
|
2016-05-18 07:28:42 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 01:57:43 +03:00
|
|
|
// MsgType returns the uint32 code which uniquely identifies this message as a
|
2017-01-31 08:45:02 +03:00
|
|
|
// SingleFundingResponse on the wire.
|
2016-05-23 23:46:18 +03:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-20 01:57:43 +03:00
|
|
|
func (c *SingleFundingResponse) MsgType() MessageType {
|
|
|
|
return MsgSingleFundingResponse
|
2016-05-18 07:28:42 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:46:18 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload length for a
|
|
|
|
// SingleFundingResponse. This is calculated by summing the max length of all
|
|
|
|
// the fields within a SingleFundingResponse. To enforce a maximum
|
|
|
|
// DeliveryPkScript size, the size of a P2PKH public key script is used.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *SingleFundingResponse) MaxPayloadLength(uint32) uint32 {
|
2017-01-24 05:19:54 +03:00
|
|
|
var length uint32
|
|
|
|
|
2017-04-17 01:22:20 +03:00
|
|
|
// PendingChannelID - 32 bytes
|
|
|
|
length += 32
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
// ChannelDerivationPoint - 33 bytes
|
|
|
|
length += 33
|
|
|
|
|
|
|
|
// CommitmentKey - 33 bytes
|
|
|
|
length += 33
|
|
|
|
|
|
|
|
// RevocationKey - 33 bytes
|
|
|
|
length += 33
|
|
|
|
|
|
|
|
// CsvDelay - 4 bytes
|
|
|
|
length += 4
|
|
|
|
|
|
|
|
// DeliveryPkScript - 25 bytes
|
|
|
|
length += 25
|
|
|
|
|
|
|
|
// DustLimit - 8 bytes
|
|
|
|
length += 8
|
|
|
|
|
|
|
|
// ConfirmationDepth - 4 bytes
|
|
|
|
length += 4
|
|
|
|
|
|
|
|
return length
|
2016-05-18 07:28:42 +03:00
|
|
|
}
|