2015-12-28 12:24:16 +01:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FundingRequest struct {
|
2015-12-31 10:19:54 +01:00
|
|
|
ReservationID uint64
|
|
|
|
|
2015-12-28 12:24:16 +01:00
|
|
|
ChannelType uint8
|
|
|
|
|
2015-12-31 07:28:14 +01:00
|
|
|
RequesterFundingAmount btcutil.Amount
|
|
|
|
RequesterReserveAmount btcutil.Amount
|
|
|
|
MinFeePerKb btcutil.Amount
|
|
|
|
|
|
|
|
//The funding requester can request payment
|
|
|
|
//This wallet only allows positive values,
|
|
|
|
//which is a payment to the responder
|
|
|
|
//(This can be used to fund the Reserve)
|
|
|
|
//If the responder disagrees, then the funding request fails
|
2015-12-31 09:41:55 +01:00
|
|
|
//THIS VALUE GOES INTO THE RESPONDER'S FUNDING AMOUNT
|
2015-12-31 09:48:41 +01:00
|
|
|
//total requester input value = RequesterFundingAmount + PaymentAmount + "Total Change" + Fees(?)
|
|
|
|
//RequesterFundingAmount = "Available Balance" + RequesterReserveAmount
|
2015-12-31 10:19:54 +01:00
|
|
|
//Payment SHOULD NOT be acknowledged until the minimum confirmation has elapsed
|
|
|
|
//(Due to double-spend risks the recipient will not want to acknolwedge confirmation until later)
|
|
|
|
//This is to make a payment as part of opening the channel
|
2015-12-31 07:28:14 +01:00
|
|
|
PaymentAmount btcutil.Amount
|
|
|
|
|
|
|
|
//Minimum number of confirmations to validate transaction
|
|
|
|
MinDepth uint32
|
2015-12-28 12:24:16 +01:00
|
|
|
|
|
|
|
//Should double-check the total funding later
|
|
|
|
MinTotalFundingAmount btcutil.Amount
|
|
|
|
|
2015-12-30 14:38:57 +01:00
|
|
|
//CLTV/CSV lock-time to use
|
2015-12-28 12:24:16 +01:00
|
|
|
LockTime uint32
|
|
|
|
|
|
|
|
//Who pays the fees
|
|
|
|
//0: (default) channel initiator
|
|
|
|
//1: split
|
|
|
|
//2: channel responder
|
|
|
|
FeePayer uint8
|
|
|
|
|
|
|
|
RevocationHash [20]byte
|
|
|
|
Pubkey *btcec.PublicKey
|
|
|
|
DeliveryPkScript PkScript //*MUST* be either P2PKH or P2SH
|
2015-12-30 14:38:57 +01:00
|
|
|
ChangePkScript PkScript //*MUST* be either P2PKH or P2SH
|
2015-12-28 12:24:16 +01:00
|
|
|
|
|
|
|
Inputs []*wire.TxIn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FundingRequest) Decode(r io.Reader, pver uint32) error {
|
2015-12-31 10:19:54 +01:00
|
|
|
//Reservation ID (8)
|
2015-12-31 07:28:14 +01:00
|
|
|
//Channel Type (1)
|
|
|
|
//Funding Amount (8)
|
|
|
|
//Channel Minimum Capacity (8)
|
|
|
|
//Revocation Hash (20)
|
|
|
|
//Commitment Pubkey (32)
|
|
|
|
//Reserve Amount (8)
|
|
|
|
//Minimum Transaction Fee Per Kb (8)
|
|
|
|
//PaymentAmount (8)
|
|
|
|
//MinDepth (4)
|
|
|
|
//LockTime (4)
|
|
|
|
//FeePayer (1)
|
2015-12-30 14:38:57 +01:00
|
|
|
//DeliveryPkScript (final delivery)
|
|
|
|
// First byte length then pkscript
|
|
|
|
//ChangePkScript (change for extra from inputs)
|
2015-12-28 12:24:16 +01:00
|
|
|
// First byte length then pkscript
|
|
|
|
//Inputs: Create the TxIns
|
|
|
|
// First byte is number of inputs
|
|
|
|
// For each input, it's 32bytes txin & 4bytes index
|
2015-12-31 08:34:40 +01:00
|
|
|
err := readElements(r,
|
2015-12-31 10:19:54 +01:00
|
|
|
&c.ReservationID,
|
2015-12-28 12:24:16 +01:00
|
|
|
&c.ChannelType,
|
2015-12-31 07:28:14 +01:00
|
|
|
&c.RequesterFundingAmount,
|
2015-12-28 12:24:16 +01:00
|
|
|
&c.MinTotalFundingAmount,
|
|
|
|
&c.RevocationHash,
|
|
|
|
&c.Pubkey,
|
2015-12-31 07:28:14 +01:00
|
|
|
&c.RequesterReserveAmount,
|
2015-12-28 12:24:16 +01:00
|
|
|
&c.MinFeePerKb,
|
2015-12-31 07:28:14 +01:00
|
|
|
&c.PaymentAmount,
|
|
|
|
&c.MinDepth,
|
2015-12-28 12:24:16 +01:00
|
|
|
&c.LockTime,
|
|
|
|
&c.FeePayer,
|
|
|
|
&c.DeliveryPkScript,
|
2015-12-30 14:38:57 +01:00
|
|
|
&c.ChangePkScript,
|
2015-12-28 12:24:16 +01:00
|
|
|
&c.Inputs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-30 14:38:57 +01:00
|
|
|
return nil
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Creates a new FundingRequest
|
|
|
|
func NewFundingRequest() *FundingRequest {
|
|
|
|
return &FundingRequest{}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Serializes the item from the FundingRequest struct
|
|
|
|
//Writes the data to w
|
|
|
|
func (c *FundingRequest) Encode(w io.Writer, pver uint32) error {
|
|
|
|
//Channel Type
|
|
|
|
//Funding Amont
|
|
|
|
//Channel Minimum Capacity
|
|
|
|
//Revocation Hash
|
|
|
|
//Commitment Pubkey
|
|
|
|
//Reserve Amount
|
|
|
|
//Minimum Transaction Fee Per KB
|
|
|
|
//LockTime
|
|
|
|
//FeePayer
|
|
|
|
//DeliveryPkScript
|
2015-12-30 14:38:57 +01:00
|
|
|
//ChangePkScript
|
2015-12-28 12:24:16 +01:00
|
|
|
//Inputs: Append the actual Txins
|
2015-12-31 08:34:40 +01:00
|
|
|
err := writeElements(w,
|
2015-12-31 10:19:54 +01:00
|
|
|
c.ReservationID,
|
2015-12-28 12:24:16 +01:00
|
|
|
c.ChannelType,
|
2015-12-31 07:28:14 +01:00
|
|
|
c.RequesterFundingAmount,
|
2015-12-28 12:24:16 +01:00
|
|
|
c.MinTotalFundingAmount,
|
|
|
|
c.RevocationHash,
|
|
|
|
c.Pubkey,
|
2015-12-31 07:28:14 +01:00
|
|
|
c.RequesterReserveAmount,
|
2015-12-28 12:24:16 +01:00
|
|
|
c.MinFeePerKb,
|
2015-12-31 07:28:14 +01:00
|
|
|
c.PaymentAmount,
|
|
|
|
c.MinDepth,
|
2015-12-28 12:24:16 +01:00
|
|
|
c.LockTime,
|
|
|
|
c.FeePayer,
|
|
|
|
c.DeliveryPkScript,
|
2015-12-30 14:38:57 +01:00
|
|
|
c.ChangePkScript,
|
2015-12-28 12:24:16 +01:00
|
|
|
c.Inputs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FundingRequest) Command() uint32 {
|
|
|
|
return CmdFundingRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FundingRequest) MaxPayloadLength(uint32) uint32 {
|
2015-12-31 10:19:54 +01:00
|
|
|
//110 (base size) + 26 (pkscript) + 26 (pkscript) + 1 (numTxes) + 127*36(127 inputs * sha256+idx)
|
|
|
|
return 4735
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
|
|
|
|
func (c *FundingRequest) Validate() error {
|
2015-12-30 14:38:57 +01:00
|
|
|
var err error
|
|
|
|
|
2015-12-28 12:24:16 +01:00
|
|
|
//No negative values
|
2015-12-31 07:28:14 +01:00
|
|
|
if c.RequesterFundingAmount < 0 {
|
|
|
|
return fmt.Errorf("RequesterFundingAmount cannot be negative")
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
2015-12-31 07:28:14 +01:00
|
|
|
if c.RequesterReserveAmount < 0 {
|
|
|
|
return fmt.Errorf("RequesterReserveAmount cannot be negative")
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.MinFeePerKb < 0 {
|
|
|
|
return fmt.Errorf("MinFeePerKb cannot be negative")
|
|
|
|
}
|
|
|
|
if c.MinTotalFundingAmount < 0 {
|
|
|
|
return fmt.Errorf("MinTotalFundingAmount cannot be negative")
|
|
|
|
}
|
|
|
|
|
2015-12-31 07:28:14 +01:00
|
|
|
//Validation of what makes sense...
|
|
|
|
if c.MinTotalFundingAmount < c.RequesterFundingAmount {
|
|
|
|
return fmt.Errorf("Requester's minimum too low.")
|
|
|
|
}
|
|
|
|
if c.RequesterFundingAmount < c.RequesterReserveAmount {
|
|
|
|
return fmt.Errorf("Reserve must be below Funding Amount")
|
|
|
|
}
|
|
|
|
|
|
|
|
//This wallet only allows payment from the requester to responder
|
|
|
|
if c.PaymentAmount < 0 {
|
|
|
|
return fmt.Errorf("This wallet requieres payment to be greater than zero.")
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make sure there's not more than 127 inputs
|
|
|
|
if len(c.Inputs) > 127 {
|
|
|
|
return fmt.Errorf("Too many inputs")
|
|
|
|
}
|
|
|
|
|
2015-12-30 14:38:57 +01:00
|
|
|
//DeliveryPkScript is either P2SH or P2PKH
|
|
|
|
err = ValidatePkScript(c.DeliveryPkScript)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//ChangePkScript is either P2SH or P2PKH
|
|
|
|
err = ValidatePkScript(c.ChangePkScript)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//We're good!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FundingRequest) String() string {
|
|
|
|
var inputs string
|
|
|
|
for i, in := range c.Inputs {
|
|
|
|
inputs += fmt.Sprintf("\n Slice\t%d\n", i)
|
2015-12-31 07:28:14 +01:00
|
|
|
if &in != nil {
|
|
|
|
inputs += fmt.Sprintf("\tHash\t%s\n", in.PreviousOutPoint.Hash)
|
|
|
|
inputs += fmt.Sprintf("\tIndex\t%d\n", in.PreviousOutPoint.Index)
|
|
|
|
}
|
2015-12-28 12:24:16 +01:00
|
|
|
}
|
2015-12-31 08:34:40 +01:00
|
|
|
|
|
|
|
var serializedPubkey []byte
|
|
|
|
if &c.Pubkey != nil && c.Pubkey.X != nil {
|
|
|
|
serializedPubkey = c.Pubkey.SerializeCompressed()
|
|
|
|
}
|
|
|
|
|
2015-12-28 12:24:16 +01:00
|
|
|
return fmt.Sprintf("\n--- Begin FundingRequest ---\n") +
|
2015-12-31 10:19:54 +01:00
|
|
|
fmt.Sprintf("ReservationID:\t\t\t%d\n", c.ReservationID) +
|
2015-12-31 07:28:14 +01:00
|
|
|
fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) +
|
|
|
|
fmt.Sprintf("RequesterFundingAmount:\t\t%s\n", c.RequesterFundingAmount.String()) +
|
|
|
|
fmt.Sprintf("RequesterReserveAmount:\t\t%s\n", c.RequesterReserveAmount.String()) +
|
|
|
|
fmt.Sprintf("MinFeePerKb:\t\t\t%s\n", c.MinFeePerKb.String()) +
|
|
|
|
fmt.Sprintf("PaymentAmount:\t\t\t%s\n", c.PaymentAmount.String()) +
|
|
|
|
fmt.Sprintf("MinDepth:\t\t\t%d\n", c.MinDepth) +
|
|
|
|
fmt.Sprintf("MinTotalFundingAmount\t\t%s\n", c.MinTotalFundingAmount.String()) +
|
|
|
|
fmt.Sprintf("LockTime\t\t\t%d\n", c.LockTime) +
|
|
|
|
fmt.Sprintf("FeePayer\t\t\t%x\n", c.FeePayer) +
|
|
|
|
fmt.Sprintf("RevocationHash\t\t\t%x\n", c.RevocationHash) +
|
2015-12-31 08:34:40 +01:00
|
|
|
fmt.Sprintf("Pubkey\t\t\t\t%x\n", serializedPubkey) +
|
2015-12-31 07:28:14 +01:00
|
|
|
fmt.Sprintf("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) +
|
2015-12-28 12:24:16 +01:00
|
|
|
fmt.Sprintf("Inputs:") +
|
|
|
|
inputs +
|
|
|
|
fmt.Sprintf("--- End FundingRequest ---\n")
|
|
|
|
}
|