Wire update

More in sync with other code! :D
This commit is contained in:
Joseph Poon 2016-05-17 21:28:42 -07:00
parent fcff17c336
commit 566bf47cbf
11 changed files with 399 additions and 308 deletions

@ -1,67 +0,0 @@
package lnwire
import (
"fmt"
"io"
)
type HTLCAddAccept struct {
ChannelID uint64
HTLCKey HTLCKey
}
func (c *HTLCAddAccept) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// HTLCKey(8)
err := readElements(r,
&c.ChannelID,
&c.HTLCKey,
)
if err != nil {
return err
}
return nil
}
// Creates a new HTLCAddAccept
func NewHTLCAddAccept() *HTLCAddAccept {
return &HTLCAddAccept{}
}
// Serializes the item from the HTLCAddAccept struct
// Writes the data to w
func (c *HTLCAddAccept) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.HTLCKey,
)
if err != nil {
return err
}
return nil
}
func (c *HTLCAddAccept) Command() uint32 {
return CmdHTLCAddAccept
}
func (c *HTLCAddAccept) MaxPayloadLength(uint32) uint32 {
// 16 base size
return 16
}
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *HTLCAddAccept) Validate() error {
// We're good!
return nil
}
func (c *HTLCAddAccept) String() string {
return fmt.Sprintf("\n--- Begin HTLCAddAccept ---\n") +
fmt.Sprintf("ChannelID:\t\t%d\n", c.ChannelID) +
fmt.Sprintf("HTLCKey:\t\t%d\n", c.HTLCKey) +
fmt.Sprintf("--- End HTLCAddAccept ---\n")
}

@ -1,32 +0,0 @@
package lnwire
import (
"testing"
)
var (
htlcAddAccept = &HTLCAddAccept{
ChannelID: uint64(12345678),
HTLCKey: HTLCKey(12345),
}
htlcAddAcceptSerializedString = "0000000000bc614e0000000000003039"
htlcAddAcceptSerializedMessage = "0709110b000003f2000000100000000000bc614e0000000000003039"
)
func TestHTLCAddAcceptEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, htlcAddAccept, htlcAddAcceptSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewHTLCAddAccept()
DeserializeTest(t, s, newMessage, htlcAddAccept)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, htlcAddAccept, htlcAddAcceptSerializedMessage)
}

@ -7,7 +7,7 @@ import (
type HTLCAddReject struct {
ChannelID uint64
HTLCKey HTLCKey
HTLCKey HTLCKey
}
func (c *HTLCAddReject) Decode(r io.Reader, pver uint32) error {

@ -12,7 +12,8 @@ type HTLCAddRequest struct {
ChannelID uint64
// ID of this request
HTLCKey HTLCKey
// implicit
// HTLCKey HTLCKey
// When the HTLC expires
Expiry uint32
@ -27,13 +28,14 @@ type HTLCAddRequest struct {
// Contract Type
// first 4 bits is n, second for is m, in n-of-m "multisig"
// default is 0.
ContractType uint8
// Redemption Hashes
RedemptionHashes []*[20]byte
// Data to parse&pass on to the next node
// Eventually, we need to make this into a group of 2 nested structs?
// Nested HTLCAddRequests with a uint32 in front for the size
Blob []byte
}

@ -1,71 +0,0 @@
package lnwire
import (
"fmt"
"io"
)
// Multiple Clearing Requests are possible by putting this inside an array of
// clearing requests
type HTLCSettleAccept struct {
// We can use a different data type for this if necessary...
ChannelID uint64
// ID of this request
HTLCKey HTLCKey
}
func (c *HTLCSettleAccept) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// HTLCKey(8)
err := readElements(r,
&c.ChannelID,
&c.HTLCKey,
)
if err != nil {
return err
}
return nil
}
// Creates a new HTLCSettleAccept
func NewHTLCSettleAccept() *HTLCSettleAccept {
return &HTLCSettleAccept{}
}
// Serializes the item from the HTLCSettleAccept struct
// Writes the data to w
func (c *HTLCSettleAccept) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.HTLCKey,
)
if err != nil {
return err
}
return nil
}
func (c *HTLCSettleAccept) Command() uint32 {
return CmdHTLCSettleAccept
}
func (c *HTLCSettleAccept) MaxPayloadLength(uint32) uint32 {
// 16
return 16
}
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *HTLCSettleAccept) Validate() error {
// We're good!
return nil
}
func (c *HTLCSettleAccept) String() string {
return fmt.Sprintf("\n--- Begin HTLCSettleAccept ---\n") +
fmt.Sprintf("ChannelID:\t%d\n", c.ChannelID) +
fmt.Sprintf("HTLCKey:\t%d\n", c.HTLCKey) +
fmt.Sprintf("--- End HTLCSettleAccept ---\n")
}

@ -1,32 +0,0 @@
package lnwire
import (
"testing"
)
var (
htlcSettleAccept = &HTLCSettleAccept{
ChannelID: uint64(12345678),
HTLCKey: HTLCKey(12345),
}
htlcSettleAcceptSerializedString = "0000000000bc614e0000000000003039"
htlcSettleAcceptSerializedMessage = "0709110b00000456000000100000000000bc614e0000000000003039"
)
func TestHTLCSettleAcceptEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, htlcSettleAccept, htlcSettleAcceptSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewHTLCSettleAccept()
DeserializeTest(t, s, newMessage, htlcSettleAccept)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, htlcSettleAccept, htlcSettleAcceptSerializedMessage)
}

@ -1,71 +0,0 @@
package lnwire
import (
"fmt"
"io"
)
// Multiple Clearing Requests are possible by putting this inside an array of
// clearing requests
type HTLCTimeoutAccept struct {
// We can use a different data type for this if necessary...
ChannelID uint64
// ID of this request
HTLCKey HTLCKey
}
func (c *HTLCTimeoutAccept) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// HTLCKey(8)
err := readElements(r,
&c.ChannelID,
&c.HTLCKey,
)
if err != nil {
return err
}
return nil
}
// Creates a new HTLCTimeoutAccept
func NewHTLCTimeoutAccept() *HTLCTimeoutAccept {
return &HTLCTimeoutAccept{}
}
// Serializes the item from the HTLCTimeoutAccept struct
// Writes the data to w
func (c *HTLCTimeoutAccept) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.HTLCKey,
)
if err != nil {
return err
}
return nil
}
func (c *HTLCTimeoutAccept) Command() uint32 {
return CmdHTLCTimeoutAccept
}
func (c *HTLCTimeoutAccept) MaxPayloadLength(uint32) uint32 {
// 16
return 16
}
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *HTLCTimeoutAccept) Validate() error {
// We're good!
return nil
}
func (c *HTLCTimeoutAccept) String() string {
return fmt.Sprintf("\n--- Begin HTLCTimeoutAccept ---\n") +
fmt.Sprintf("ChannelID:\t%d\n", c.ChannelID) +
fmt.Sprintf("HTLCKey:\t%d\n", c.HTLCKey) +
fmt.Sprintf("--- End HTLCTimeoutAccept ---\n")
}

@ -1,32 +0,0 @@
package lnwire
import (
"testing"
)
var (
htlcTimeoutAccept = &HTLCTimeoutAccept{
ChannelID: uint64(12345678),
HTLCKey: HTLCKey(12345),
}
htlcTimeoutAcceptSerializedString = "0000000000bc614e0000000000003039"
htlcTimeoutAcceptSerializedMessage = "0709110b0000051e000000100000000000bc614e0000000000003039"
)
func TestHTLCTimeoutAcceptEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, htlcTimeoutAccept, htlcTimeoutAcceptSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewHTLCTimeoutAccept()
DeserializeTest(t, s, newMessage, htlcTimeoutAccept)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, htlcTimeoutAccept, htlcTimeoutAcceptSerializedMessage)
}

@ -0,0 +1,192 @@
package lnwire
import (
"fmt"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
"io"
)
type SingleFundingRequest struct {
//ChannelID
ChannelID uint64
//Default 0
ChannelType uint8
//Bitcoin: 0.
CoinType uint64
//Amount of fees per kb
//Assumes requester pays
FeePerKb 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
// THIS VALUE GOES INTO THE RESPONDER'S FUNDING AMOUNT
// total requester input value = RequesterFundingAmount + PaymentAmount + "Total Change" + Fees(?)
// RequesterFundingAmount = "Available Balance" + RequesterReserveAmount
// 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
PaymentAmount btcutil.Amount
// CLTV/CSV lock-time to use
LockTime uint32
RevocationHash [20]byte
Pubkey *btcec.PublicKey
DeliveryPkScript PkScript // *MUST* be either P2PKH or P2SH
ChangePkScript PkScript // *MUST* be either P2PKH or P2SH
}
func (c *SingleFundingRequest) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// ChannelType (1)
// CoinType (8)
// FeePerKb (8)
// PaymentAmount (8)
// LockTime (4)
// Revocation Hash (20)
// Pubkey (32)
// DeliveryPkScript (final delivery)
// ChangePkScript (change for extra from inputs)
err := readElements(r,
&c.ChannelID,
&c.ChannelType,
&c.CoinType,
&c.FeePerKb,
&c.PaymentAmount,
&c.LockTime,
&c.RevocationHash,
&c.Pubkey,
&c.DeliveryPkScript,
&c.ChangePkScript)
if err != nil {
return err
}
return nil
}
// Creates a new SingleFundingRequest
func NewSingleFundingRequest() *SingleFundingRequest {
return &SingleFundingRequest{}
}
// Serializes the item from the SingleFundingRequest struct
// Writes the data to w
func (c *SingleFundingRequest) Encode(w io.Writer, pver uint32) error {
// ChannelID (8)
// ChannelType (1)
// CoinType (8)
// FeePerKb (8)
// PaymentAmount (8)
// LockTime (4)
// Revocation Hash (20)
// Pubkey (32)
// DeliveryPkScript (final delivery)
// ChangePkScript (change for extra from inputs)
err := writeElements(w,
c.ChannelID,
c.ChannelType,
c.CoinType,
c.FeePerKb,
c.PaymentAmount,
c.LockTime,
c.RevocationHash,
c.Pubkey,
c.DeliveryPkScript,
c.ChangePkScript)
if err != nil {
return err
}
return nil
}
func (c *SingleFundingRequest) Command() uint32 {
return CmdSingleFundingRequest
}
func (c *SingleFundingRequest) MaxPayloadLength(uint32) uint32 {
return 141
}
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *SingleFundingRequest) Validate() error {
var err error
// No negative values
if c.RequesterFundingAmount < 0 {
return fmt.Errorf("RequesterFundingAmount cannot be negative")
}
if c.RequesterReserveAmount < 0 {
return fmt.Errorf("RequesterReserveAmount cannot be negative")
}
if c.MinFeePerKb < 0 {
return fmt.Errorf("MinFeePerKb cannot be negative")
}
if c.MinTotalFundingAmount < 0 {
return fmt.Errorf("MinTotalFundingAmount cannot be negative")
}
// 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")
}
// 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
}
// We're good!
return nil
}
func (c *SingleFundingRequest) String() string {
var serializedPubkey []byte
if &c.Pubkey != nil && c.Pubkey.X != nil {
serializedPubkey = c.Pubkey.SerializeCompressed()
}
return fmt.Sprintf("\n--- Begin SingleFundingRequest ---\n") +
fmt.Sprintf("ChannelID:\t\t\t%d\n", c.ChannelID) +
fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) +
fmt.Sprintf("CoinType:\t\t\t%d\n", c.CoinType) +
fmt.Sprintf("FeePerKb:\t\t\t%s\n", c.FeePerKb.String()) +
fmt.Sprintf("PaymentAmount:\t\t\t%s\n", c.PaymentAmount.String()) +
fmt.Sprintf("LockTime\t\t\t%d\n", c.LockTime) +
fmt.Sprintf("RevocationHash\t\t\t%x\n", c.RevocationHash) +
fmt.Sprintf("Pubkey\t\t\t\t%x\n", serializedPubkey) +
fmt.Sprintf("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) +
fmt.Sprintf("ChangePkScript\t\t%x\n", c.ChangePkScript) +
fmt.Sprintf("--- End SingleFundingRequest ---\n")
}

@ -0,0 +1,134 @@
package lnwire
import (
"fmt"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
"io"
)
type FundingResponse struct {
ChannelID uint64
RevocationHash [20]byte
Pubkey *btcec.PublicKey
CommitSig *btcec.Signature // Requester's Commitment
DeliveryPkScript PkScript // *MUST* be either P2PKH or P2SH
ChangePkScript PkScript // *MUST* be either P2PKH or P2SH
}
func (c *FundingResponse) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// Revocation Hash (20)
// Pubkey (32)
// CommitSig (73)
// DeliveryPkScript (final delivery)
// ChangePkScript (change for extra from inputs)
err := readElements(r,
&c.ChannelID,
&c.RevocationHash,
&c.Pubkey,
&c.CommitSig,
&c.DeliveryPkScript,
&c.ChangePkScript)
if err != nil {
return err
}
return nil
}
// Creates a new FundingResponse
func NewFundingResponse() *FundingResponse {
return &FundingResponse{}
}
// Serializes the item from the FundingResponse struct
// Writes the data to w
func (c *FundingResponse) Encode(w io.Writer, pver uint32) error {
// ChannelID (8)
// Revocation Hash (20)
// Pubkey (32)
// CommitSig (73)
// DeliveryPkScript (final delivery)
// ChangePkScript (change for extra from inputs)
err := writeElements(w,
c.ChannelID,
c.RevocationHash,
c.Pubkey,
c.CommitSig,
c.DeliveryPkScript,
c.ChangePkScript)
if err != nil {
return err
}
return nil
}
func (c *FundingResponse) Command() uint32 {
return CmdFundingResponse
}
func (c *FundingResponse) MaxPayloadLength(uint32) uint32 {
return 186
}
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *FundingResponse) Validate() error {
var err error
// No negative values
if c.ResponderFundingAmount < 0 {
return fmt.Errorf("ResponderFundingAmount cannot be negative")
}
if c.ResponderReserveAmount < 0 {
return fmt.Errorf("ResponderReserveAmount cannot be negative")
}
if c.MinFeePerKb < 0 {
return fmt.Errorf("MinFeePerKb cannot be negative")
}
// Validation of what makes sense...
if c.ResponderFundingAmount < c.ResponderReserveAmount {
return fmt.Errorf("Reserve must be below Funding Amount")
}
// Make sure there's not more than 127 inputs
if len(c.Inputs) > 127 {
return fmt.Errorf("Too many inputs")
}
// Delivery PkScript is either P2SH or P2PKH
err = ValidatePkScript(c.DeliveryPkScript)
if err != nil {
return err
}
// Change PkScript is either P2SH or P2PKH
err = ValidatePkScript(c.ChangePkScript)
if err != nil {
return err
}
// We're good!
return nil
}
func (c *FundingResponse) String() string {
var serializedPubkey []byte
if &c.Pubkey != nil && c.Pubkey.X != nil {
serializedPubkey = c.Pubkey.SerializeCompressed()
}
return fmt.Sprintf("\n--- Begin FundingResponse ---\n") +
fmt.Sprintf("ChannelID:\t\t\t%d\n", c.ChannelID) +
fmt.Sprintf("RevocationHash\t\t\t%x\n", c.RevocationHash) +
fmt.Sprintf("Pubkey\t\t\t\t%x\n", serializedPubkey) +
fmt.Sprintf("CommitSig\t\t\t%x\n", c.CommitSig.Serialize()) +
fmt.Sprintf("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) +
fmt.Sprintf("ChangePkScript\t\t%x\n", c.ChangePkScript) +
fmt.Sprintf("--- End FundingResponse ---\n")
}

@ -0,0 +1,68 @@
package lnwire
import (
"fmt"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
"io"
)
//Both parties send this message and then it is activated
type FundingSignComplete struct {
ChannelID uint64
TxID *wire.ShaHash
}
func (c *FundingSignComplete) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// TxID (32)
err := readElements(r,
&c.ChannelID,
&c.TxID)
if err != nil {
return err
}
return nil
}
// Creates a new FundingSignComplete
func NewFundingSignComplete() *FundingSignComplete {
return &FundingSignComplete{}
}
// Serializes the item from the FundingSignComplete struct
// Writes the data to w
func (c *FundingSignComplete) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.TxID)
if err != nil {
return err
}
return nil
}
func (c *FundingSignComplete) Command() uint32 {
return CmdFundingSignComplete
}
func (c *FundingSignComplete) MaxPayloadLength(uint32) uint32 {
// 8 (base size) + 32 + (73maxSigSize*127maxInputs)
return 40
}
// Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *FundingSignComplete) Validate() error {
// We're good!
return nil
}
func (c *FundingSignComplete) String() string {
return fmt.Sprintf("\n--- Begin FundingSignComplete ---\n") +
fmt.Sprintf("ChannelID:\t\t%d\n", c.ChannelID) +
fmt.Sprintf("TxID\t\t%s\n", c.TxID.String()) +
fmt.Sprintf("--- End FundingSignComplete ---\n")
}