From 2d3253b95d998656b81a2a9113aed39306a53aec Mon Sep 17 00:00:00 2001 From: Joseph Poon Date: Wed, 30 Dec 2015 23:34:40 -0800 Subject: [PATCH] Cleanup * Bugfixes * Removed whether to include sigs in txin for readElement/writeElement --- lnwire/funding_request.go | 12 +++++-- lnwire/funding_response.go | 13 ++++--- lnwire/funding_signaccept.go | 14 +++++--- lnwire/funding_signcomplete.go | 7 ++-- lnwire/lnwire.go | 62 +++++++++++++--------------------- lnwire/message.go | 4 +-- 6 files changed, 57 insertions(+), 55 deletions(-) diff --git a/lnwire/funding_request.go b/lnwire/funding_request.go index 76ee7e0b..cb2d0244 100644 --- a/lnwire/funding_request.go +++ b/lnwire/funding_request.go @@ -64,7 +64,7 @@ func (c *FundingRequest) Decode(r io.Reader, pver uint32) error { //Inputs: Create the TxIns // First byte is number of inputs // For each input, it's 32bytes txin & 4bytes index - err := readElements(r, false, + err := readElements(r, &c.ChannelType, &c.RequesterFundingAmount, &c.MinTotalFundingAmount, @@ -106,7 +106,7 @@ func (c *FundingRequest) Encode(w io.Writer, pver uint32) error { //DeliveryPkScript //ChangePkScript //Inputs: Append the actual Txins - err := writeElements(w, false, + err := writeElements(w, c.ChannelType, c.RequesterFundingAmount, c.MinTotalFundingAmount, @@ -204,6 +204,12 @@ func (c *FundingRequest) String() string { inputs += fmt.Sprintf("\tIndex\t%d\n", in.PreviousOutPoint.Index) } } + + var serializedPubkey []byte + if &c.Pubkey != nil && c.Pubkey.X != nil { + serializedPubkey = c.Pubkey.SerializeCompressed() + } + return fmt.Sprintf("\n--- Begin FundingRequest ---\n") + fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) + fmt.Sprintf("RequesterFundingAmount:\t\t%s\n", c.RequesterFundingAmount.String()) + @@ -215,7 +221,7 @@ func (c *FundingRequest) String() 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) + - fmt.Sprintf("Pubkey\t\t\t\t%x\n", c.Pubkey.SerializeCompressed()) + + fmt.Sprintf("Pubkey\t\t\t\t%x\n", serializedPubkey) + fmt.Sprintf("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) + fmt.Sprintf("Inputs:") + inputs + diff --git a/lnwire/funding_response.go b/lnwire/funding_response.go index 701bfd62..c8364f85 100644 --- a/lnwire/funding_response.go +++ b/lnwire/funding_response.go @@ -58,7 +58,7 @@ func (c *FundingResponse) Decode(r io.Reader, pver uint32) error { //Inputs: Create the TxIns // First byte is number of inputs // For each input, it's 32bytes txin & 4bytes index - err := readElements(r, false, + err := readElements(r, &c.ReservationID, &c.ChannelType, &c.ResponderFundingAmount, @@ -101,7 +101,7 @@ func (c *FundingResponse) Encode(w io.Writer, pver uint32) error { //ChangePkScript (change for extra from inputs) //CommitSig //Inputs - err := writeElements(w, false, + err := writeElements(w, c.ReservationID, c.ChannelType, c.ResponderFundingAmount, @@ -180,11 +180,16 @@ func (c *FundingResponse) String() string { for i, in := range c.Inputs { inputs += fmt.Sprintf("\n Slice\t%d\n", i) if &in != nil { - inputs += fmt.Sprintf("\tHash\t%s\n", in.PreviousOutPoint.Hash) inputs += fmt.Sprintf("\tIndex\t%d\n", in.PreviousOutPoint.Index) } } + + var serializedPubkey []byte + if &c.Pubkey != nil && c.Pubkey.X != nil { + serializedPubkey = c.Pubkey.SerializeCompressed() + } + return fmt.Sprintf("\n--- Begin FundingResponse ---\n") + fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) + fmt.Sprintf("ReservationID:\t\t\t%d\n", c.ReservationID) + @@ -195,7 +200,7 @@ func (c *FundingResponse) String() 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) + - fmt.Sprintf("Pubkey\t\t\t\t%x\n", c.Pubkey.SerializeCompressed()) + + 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) + diff --git a/lnwire/funding_signaccept.go b/lnwire/funding_signaccept.go index 3cbee27a..e0ce9ec2 100644 --- a/lnwire/funding_signaccept.go +++ b/lnwire/funding_signaccept.go @@ -21,7 +21,7 @@ func (c *FundingSignAccept) Decode(r io.Reader, pver uint32) error { // First byte is number of FundingTxSigs // Sorted list of the requester's input signatures // (originally provided in the Funding Request) - err := readElements(r, false, + err := readElements(r, &c.ReservationID, &c.CommitSig, &c.FundingTXSigs) @@ -43,7 +43,7 @@ func (c *FundingSignAccept) Encode(w io.Writer, pver uint32) error { //ReservationID (8) //CommitSig //FundingTxSigs - err := writeElements(w, false, + err := writeElements(w, c.ReservationID, c.CommitSig, c.FundingTXSigs) @@ -73,13 +73,19 @@ func (c *FundingSignAccept) String() string { var sigs string for i, in := range *c.FundingTXSigs { sigs += fmt.Sprintf("\n Slice\t%d\n", i) - if &in != nil { + if &in != nil && in.R != nil { sigs += fmt.Sprintf("\tSig\t%x\n", in.Serialize()) } } + + var serializedSig []byte + if &c.CommitSig != nil && c.CommitSig.R != nil { + serializedSig = c.CommitSig.Serialize() + } + return fmt.Sprintf("\n--- Begin FundingSignAccept ---\n") + fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) + - fmt.Sprintf("CommitSig\t\t%x\n", c.CommitSig.Serialize()) + + fmt.Sprintf("CommitSig\t\t%x\n", serializedSig) + fmt.Sprintf("FundingTxSigs:") + sigs + fmt.Sprintf("--- End FundingSignAccept ---\n") diff --git a/lnwire/funding_signcomplete.go b/lnwire/funding_signcomplete.go index 06b9cb7a..4fd681d8 100644 --- a/lnwire/funding_signcomplete.go +++ b/lnwire/funding_signcomplete.go @@ -21,7 +21,7 @@ func (c *FundingSignComplete) Decode(r io.Reader, pver uint32) error { // First byte is number of FundingTxSigs // Sorted list of the requester's input signatures // (originally provided in the Funding Request) - err := readElements(r, false, + err := readElements(r, &c.ReservationID, &c.TxID, &c.FundingTXSigs) @@ -43,7 +43,7 @@ func (c *FundingSignComplete) Encode(w io.Writer, pver uint32) error { //ReservationID (8) //CommitSig //FundingTxSigs - err := writeElements(w, false, + err := writeElements(w, c.ReservationID, c.TxID, c.FundingTXSigs) @@ -73,10 +73,11 @@ func (c *FundingSignComplete) String() string { var sigs string for i, in := range *c.FundingTXSigs { sigs += fmt.Sprintf("\n Slice\t%d\n", i) - if &in != nil { + if &in != nil && in.R != nil { sigs += fmt.Sprintf("\tSig\t%x\n", in.Serialize()) } } + return fmt.Sprintf("\n--- Begin FundingSignComplete ---\n") + fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) + fmt.Sprintf("TxID\t\t%s\n", c.TxID.String()) + diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index a8470636..6446734a 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -21,7 +21,7 @@ type MicroSatoshi int32 //Unified function to call when writing different types //Pre-allocate a byte-array of the correct size for cargo-cult security //More copies but whatever... -func writeElement(w io.Writer, includeSig bool, element interface{}) error { +func writeElement(w io.Writer, element interface{}) error { var err error switch e := element.(type) { case uint8: @@ -72,13 +72,13 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error { return fmt.Errorf("Too many signatures!") } //Write the size - err = writeElement(w, false, uint8(numSigs)) + err = writeElement(w, uint8(numSigs)) if err != nil { return err } //Write the data for i := 0; i < numSigs; i++ { - err = writeElement(w, false, &(*e)[i]) + err = writeElement(w, &(*e)[i]) if err != nil { return err } @@ -91,7 +91,7 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error { return fmt.Errorf("Signature too long!") } //Write the size - err = writeElement(w, false, uint8(sigLength)) + err = writeElement(w, uint8(sigLength)) if err != nil { return err } @@ -128,7 +128,7 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error { return fmt.Errorf("PkScript too long!") } //Write the size (1-byte) - err = writeElement(w, false, uint8(scriptLength)) + err = writeElement(w, uint8(scriptLength)) if err != nil { return err } @@ -144,7 +144,7 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error { if len(e) > 127 { return fmt.Errorf("Too many txins") } - err = writeElement(w, false, uint8(len(e))) + err = writeElement(w, uint8(len(e))) if err != nil { return err } @@ -165,16 +165,6 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error { if err != nil { return err } - //Signature(optional) - if includeSig { - var sig [33]byte - copy(sig[:], in.SignatureScript) - _, err = w.Write(sig[:]) - if err != nil { - return err - } - - } } return nil default: @@ -184,9 +174,9 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error { return nil } -func writeElements(w io.Writer, includeSig bool, elements ...interface{}) error { +func writeElements(w io.Writer, elements ...interface{}) error { for _, element := range elements { - err := writeElement(w, includeSig, element) + err := writeElement(w, element) if err != nil { return err } @@ -194,7 +184,7 @@ func writeElements(w io.Writer, includeSig bool, elements ...interface{}) error return nil } -func readElement(r io.Reader, includeSig bool, element interface{}) error { +func readElement(r io.Reader, element interface{}) error { var err error switch e := element.(type) { case *uint8: @@ -251,7 +241,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error { return nil case **[]btcec.Signature: var numSigs uint8 - err = readElement(r, false, &numSigs) + err = readElement(r, &numSigs) if err != nil { return err } @@ -263,14 +253,17 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error { var sigs []btcec.Signature for i := uint8(0); i < numSigs; i++ { sig := new(btcec.Signature) - readElement(r, false, &sig) + err = readElement(r, &sig) + if err != nil { + return err + } sigs = append(sigs, *sig) } *e = &sigs return nil case **btcec.Signature: var sigLength uint8 - err = readElement(r, false, &sigLength) + err = readElement(r, &sigLength) if err != nil { return err } @@ -311,7 +304,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error { case *PkScript: //Get the script length first var scriptLength uint8 - err = readElement(r, false, &scriptLength) + err = readElement(r, &scriptLength) if err != nil { return err } @@ -333,7 +326,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error { case *[]*wire.TxIn: //Read the size (1-byte number of txins) var numScripts uint8 - err = readElement(r, false, &numScripts) + err = readElement(r, &numScripts) if err != nil { return err } @@ -362,19 +355,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error { } index := binary.BigEndian.Uint32(idxBytes[:]) outPoint := wire.NewOutPoint(shaHash, index) - //Signature(optional) - if includeSig { - var sig [33]byte - _, err = io.ReadFull(r, sig[:]) - if err != nil { - return err - } - //Create TxIn - txins = append(txins, wire.NewTxIn(outPoint, sig[:])) - } else { //no signature - //Create TxIn - txins = append(txins, wire.NewTxIn(outPoint, nil)) - } + txins = append(txins, wire.NewTxIn(outPoint, nil)) } *e = *&txins @@ -386,9 +367,9 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error { return nil } -func readElements(r io.Reader, includeSig bool, elements ...interface{}) error { +func readElements(r io.Reader, elements ...interface{}) error { for _, element := range elements { - err := readElement(r, includeSig, element) + err := readElement(r, element) if err != nil { return err } @@ -398,6 +379,9 @@ func readElements(r io.Reader, includeSig bool, elements ...interface{}) error { //Validates whether a PkScript byte array is P2SH or P2PKH func ValidatePkScript(pkScript PkScript) error { + if &pkScript == nil { + return fmt.Errorf("PkScript should not be empty!") + } if len(pkScript) == 25 { //P2PKH //Begins with OP_DUP OP_HASH160 PUSHDATA(20) diff --git a/lnwire/message.go b/lnwire/message.go index 5082e8d8..509cc01d 100644 --- a/lnwire/message.go +++ b/lnwire/message.go @@ -68,7 +68,7 @@ func readMessageHeader(r io.Reader) (int, *messageHeader, error) { hdr := messageHeader{} - err = readElements(hr, false, + err = readElements(hr, &hdr.magic, &hdr.command, &hdr.length) @@ -134,7 +134,7 @@ func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet wire.BitcoinNet) // rather than directly to the writer since writeElements doesn't // return the number of bytes written. hw := bytes.NewBuffer(make([]byte, 0, MessageHeaderSize)) - writeElements(hw, false, hdr.magic, hdr.command, hdr.length) + writeElements(hw, hdr.magic, hdr.command, hdr.length) //Write header n, err := w.Write(hw.Bytes())