* Bugfixes
* Removed whether to include sigs in txin for readElement/writeElement
This commit is contained in:
Joseph Poon 2015-12-30 23:34:40 -08:00 committed by Olaoluwa Osuntokun
parent 1772108544
commit 2d3253b95d
6 changed files with 57 additions and 55 deletions

@ -64,7 +64,7 @@ func (c *FundingRequest) Decode(r io.Reader, pver uint32) error {
//Inputs: Create the TxIns //Inputs: Create the TxIns
// First byte is number of inputs // First byte is number of inputs
// For each input, it's 32bytes txin & 4bytes index // For each input, it's 32bytes txin & 4bytes index
err := readElements(r, false, err := readElements(r,
&c.ChannelType, &c.ChannelType,
&c.RequesterFundingAmount, &c.RequesterFundingAmount,
&c.MinTotalFundingAmount, &c.MinTotalFundingAmount,
@ -106,7 +106,7 @@ func (c *FundingRequest) Encode(w io.Writer, pver uint32) error {
//DeliveryPkScript //DeliveryPkScript
//ChangePkScript //ChangePkScript
//Inputs: Append the actual Txins //Inputs: Append the actual Txins
err := writeElements(w, false, err := writeElements(w,
c.ChannelType, c.ChannelType,
c.RequesterFundingAmount, c.RequesterFundingAmount,
c.MinTotalFundingAmount, c.MinTotalFundingAmount,
@ -204,6 +204,12 @@ func (c *FundingRequest) String() string {
inputs += fmt.Sprintf("\tIndex\t%d\n", in.PreviousOutPoint.Index) 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") + return fmt.Sprintf("\n--- Begin FundingRequest ---\n") +
fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) + fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) +
fmt.Sprintf("RequesterFundingAmount:\t\t%s\n", c.RequesterFundingAmount.String()) + 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("LockTime\t\t\t%d\n", c.LockTime) +
fmt.Sprintf("FeePayer\t\t\t%x\n", c.FeePayer) + fmt.Sprintf("FeePayer\t\t\t%x\n", c.FeePayer) +
fmt.Sprintf("RevocationHash\t\t\t%x\n", c.RevocationHash) + 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("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) +
fmt.Sprintf("Inputs:") + fmt.Sprintf("Inputs:") +
inputs + inputs +

@ -58,7 +58,7 @@ func (c *FundingResponse) Decode(r io.Reader, pver uint32) error {
//Inputs: Create the TxIns //Inputs: Create the TxIns
// First byte is number of inputs // First byte is number of inputs
// For each input, it's 32bytes txin & 4bytes index // For each input, it's 32bytes txin & 4bytes index
err := readElements(r, false, err := readElements(r,
&c.ReservationID, &c.ReservationID,
&c.ChannelType, &c.ChannelType,
&c.ResponderFundingAmount, &c.ResponderFundingAmount,
@ -101,7 +101,7 @@ func (c *FundingResponse) Encode(w io.Writer, pver uint32) error {
//ChangePkScript (change for extra from inputs) //ChangePkScript (change for extra from inputs)
//CommitSig //CommitSig
//Inputs //Inputs
err := writeElements(w, false, err := writeElements(w,
c.ReservationID, c.ReservationID,
c.ChannelType, c.ChannelType,
c.ResponderFundingAmount, c.ResponderFundingAmount,
@ -180,11 +180,16 @@ func (c *FundingResponse) String() string {
for i, in := range c.Inputs { for i, in := range c.Inputs {
inputs += fmt.Sprintf("\n Slice\t%d\n", i) inputs += fmt.Sprintf("\n Slice\t%d\n", i)
if &in != nil { if &in != nil {
inputs += fmt.Sprintf("\tHash\t%s\n", in.PreviousOutPoint.Hash) inputs += fmt.Sprintf("\tHash\t%s\n", in.PreviousOutPoint.Hash)
inputs += fmt.Sprintf("\tIndex\t%d\n", in.PreviousOutPoint.Index) 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") + return fmt.Sprintf("\n--- Begin FundingResponse ---\n") +
fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) + fmt.Sprintf("ChannelType:\t\t\t%x\n", c.ChannelType) +
fmt.Sprintf("ReservationID:\t\t\t%d\n", c.ReservationID) + 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("LockTime\t\t\t%d\n", c.LockTime) +
fmt.Sprintf("FeePayer\t\t\t%x\n", c.FeePayer) + fmt.Sprintf("FeePayer\t\t\t%x\n", c.FeePayer) +
fmt.Sprintf("RevocationHash\t\t\t%x\n", c.RevocationHash) + 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("CommitSig\t\t\t%x\n", c.CommitSig.Serialize()) +
fmt.Sprintf("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) + fmt.Sprintf("DeliveryPkScript\t\t%x\n", c.DeliveryPkScript) +
fmt.Sprintf("ChangePkScript\t\t%x\n", c.ChangePkScript) + fmt.Sprintf("ChangePkScript\t\t%x\n", c.ChangePkScript) +

@ -21,7 +21,7 @@ func (c *FundingSignAccept) Decode(r io.Reader, pver uint32) error {
// First byte is number of FundingTxSigs // First byte is number of FundingTxSigs
// Sorted list of the requester's input signatures // Sorted list of the requester's input signatures
// (originally provided in the Funding Request) // (originally provided in the Funding Request)
err := readElements(r, false, err := readElements(r,
&c.ReservationID, &c.ReservationID,
&c.CommitSig, &c.CommitSig,
&c.FundingTXSigs) &c.FundingTXSigs)
@ -43,7 +43,7 @@ func (c *FundingSignAccept) Encode(w io.Writer, pver uint32) error {
//ReservationID (8) //ReservationID (8)
//CommitSig //CommitSig
//FundingTxSigs //FundingTxSigs
err := writeElements(w, false, err := writeElements(w,
c.ReservationID, c.ReservationID,
c.CommitSig, c.CommitSig,
c.FundingTXSigs) c.FundingTXSigs)
@ -73,13 +73,19 @@ func (c *FundingSignAccept) String() string {
var sigs string var sigs string
for i, in := range *c.FundingTXSigs { for i, in := range *c.FundingTXSigs {
sigs += fmt.Sprintf("\n Slice\t%d\n", i) 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()) 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") + return fmt.Sprintf("\n--- Begin FundingSignAccept ---\n") +
fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) + 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:") + fmt.Sprintf("FundingTxSigs:") +
sigs + sigs +
fmt.Sprintf("--- End FundingSignAccept ---\n") fmt.Sprintf("--- End FundingSignAccept ---\n")

@ -21,7 +21,7 @@ func (c *FundingSignComplete) Decode(r io.Reader, pver uint32) error {
// First byte is number of FundingTxSigs // First byte is number of FundingTxSigs
// Sorted list of the requester's input signatures // Sorted list of the requester's input signatures
// (originally provided in the Funding Request) // (originally provided in the Funding Request)
err := readElements(r, false, err := readElements(r,
&c.ReservationID, &c.ReservationID,
&c.TxID, &c.TxID,
&c.FundingTXSigs) &c.FundingTXSigs)
@ -43,7 +43,7 @@ func (c *FundingSignComplete) Encode(w io.Writer, pver uint32) error {
//ReservationID (8) //ReservationID (8)
//CommitSig //CommitSig
//FundingTxSigs //FundingTxSigs
err := writeElements(w, false, err := writeElements(w,
c.ReservationID, c.ReservationID,
c.TxID, c.TxID,
c.FundingTXSigs) c.FundingTXSigs)
@ -73,10 +73,11 @@ func (c *FundingSignComplete) String() string {
var sigs string var sigs string
for i, in := range *c.FundingTXSigs { for i, in := range *c.FundingTXSigs {
sigs += fmt.Sprintf("\n Slice\t%d\n", i) 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()) sigs += fmt.Sprintf("\tSig\t%x\n", in.Serialize())
} }
} }
return fmt.Sprintf("\n--- Begin FundingSignComplete ---\n") + return fmt.Sprintf("\n--- Begin FundingSignComplete ---\n") +
fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) + fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) +
fmt.Sprintf("TxID\t\t%s\n", c.TxID.String()) + fmt.Sprintf("TxID\t\t%s\n", c.TxID.String()) +

@ -21,7 +21,7 @@ type MicroSatoshi int32
//Unified function to call when writing different types //Unified function to call when writing different types
//Pre-allocate a byte-array of the correct size for cargo-cult security //Pre-allocate a byte-array of the correct size for cargo-cult security
//More copies but whatever... //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 var err error
switch e := element.(type) { switch e := element.(type) {
case uint8: case uint8:
@ -72,13 +72,13 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error {
return fmt.Errorf("Too many signatures!") return fmt.Errorf("Too many signatures!")
} }
//Write the size //Write the size
err = writeElement(w, false, uint8(numSigs)) err = writeElement(w, uint8(numSigs))
if err != nil { if err != nil {
return err return err
} }
//Write the data //Write the data
for i := 0; i < numSigs; i++ { for i := 0; i < numSigs; i++ {
err = writeElement(w, false, &(*e)[i]) err = writeElement(w, &(*e)[i])
if err != nil { if err != nil {
return err return err
} }
@ -91,7 +91,7 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error {
return fmt.Errorf("Signature too long!") return fmt.Errorf("Signature too long!")
} }
//Write the size //Write the size
err = writeElement(w, false, uint8(sigLength)) err = writeElement(w, uint8(sigLength))
if err != nil { if err != nil {
return err return err
} }
@ -128,7 +128,7 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error {
return fmt.Errorf("PkScript too long!") return fmt.Errorf("PkScript too long!")
} }
//Write the size (1-byte) //Write the size (1-byte)
err = writeElement(w, false, uint8(scriptLength)) err = writeElement(w, uint8(scriptLength))
if err != nil { if err != nil {
return err return err
} }
@ -144,7 +144,7 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error {
if len(e) > 127 { if len(e) > 127 {
return fmt.Errorf("Too many txins") return fmt.Errorf("Too many txins")
} }
err = writeElement(w, false, uint8(len(e))) err = writeElement(w, uint8(len(e)))
if err != nil { if err != nil {
return err return err
} }
@ -165,16 +165,6 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error {
if err != nil { if err != nil {
return err 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 return nil
default: default:
@ -184,9 +174,9 @@ func writeElement(w io.Writer, includeSig bool, element interface{}) error {
return nil return nil
} }
func writeElements(w io.Writer, includeSig bool, elements ...interface{}) error { func writeElements(w io.Writer, elements ...interface{}) error {
for _, element := range elements { for _, element := range elements {
err := writeElement(w, includeSig, element) err := writeElement(w, element)
if err != nil { if err != nil {
return err return err
} }
@ -194,7 +184,7 @@ func writeElements(w io.Writer, includeSig bool, elements ...interface{}) error
return nil return nil
} }
func readElement(r io.Reader, includeSig bool, element interface{}) error { func readElement(r io.Reader, element interface{}) error {
var err error var err error
switch e := element.(type) { switch e := element.(type) {
case *uint8: case *uint8:
@ -251,7 +241,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error {
return nil return nil
case **[]btcec.Signature: case **[]btcec.Signature:
var numSigs uint8 var numSigs uint8
err = readElement(r, false, &numSigs) err = readElement(r, &numSigs)
if err != nil { if err != nil {
return err return err
} }
@ -263,14 +253,17 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error {
var sigs []btcec.Signature var sigs []btcec.Signature
for i := uint8(0); i < numSigs; i++ { for i := uint8(0); i < numSigs; i++ {
sig := new(btcec.Signature) sig := new(btcec.Signature)
readElement(r, false, &sig) err = readElement(r, &sig)
if err != nil {
return err
}
sigs = append(sigs, *sig) sigs = append(sigs, *sig)
} }
*e = &sigs *e = &sigs
return nil return nil
case **btcec.Signature: case **btcec.Signature:
var sigLength uint8 var sigLength uint8
err = readElement(r, false, &sigLength) err = readElement(r, &sigLength)
if err != nil { if err != nil {
return err return err
} }
@ -311,7 +304,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error {
case *PkScript: case *PkScript:
//Get the script length first //Get the script length first
var scriptLength uint8 var scriptLength uint8
err = readElement(r, false, &scriptLength) err = readElement(r, &scriptLength)
if err != nil { if err != nil {
return err return err
} }
@ -333,7 +326,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error {
case *[]*wire.TxIn: case *[]*wire.TxIn:
//Read the size (1-byte number of txins) //Read the size (1-byte number of txins)
var numScripts uint8 var numScripts uint8
err = readElement(r, false, &numScripts) err = readElement(r, &numScripts)
if err != nil { if err != nil {
return err return err
} }
@ -362,19 +355,7 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error {
} }
index := binary.BigEndian.Uint32(idxBytes[:]) index := binary.BigEndian.Uint32(idxBytes[:])
outPoint := wire.NewOutPoint(shaHash, index) outPoint := wire.NewOutPoint(shaHash, index)
//Signature(optional) txins = append(txins, wire.NewTxIn(outPoint, nil))
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))
}
} }
*e = *&txins *e = *&txins
@ -386,9 +367,9 @@ func readElement(r io.Reader, includeSig bool, element interface{}) error {
return nil return nil
} }
func readElements(r io.Reader, includeSig bool, elements ...interface{}) error { func readElements(r io.Reader, elements ...interface{}) error {
for _, element := range elements { for _, element := range elements {
err := readElement(r, includeSig, element) err := readElement(r, element)
if err != nil { if err != nil {
return err 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 //Validates whether a PkScript byte array is P2SH or P2PKH
func ValidatePkScript(pkScript PkScript) error { func ValidatePkScript(pkScript PkScript) error {
if &pkScript == nil {
return fmt.Errorf("PkScript should not be empty!")
}
if len(pkScript) == 25 { if len(pkScript) == 25 {
//P2PKH //P2PKH
//Begins with OP_DUP OP_HASH160 PUSHDATA(20) //Begins with OP_DUP OP_HASH160 PUSHDATA(20)

@ -68,7 +68,7 @@ func readMessageHeader(r io.Reader) (int, *messageHeader, error) {
hdr := messageHeader{} hdr := messageHeader{}
err = readElements(hr, false, err = readElements(hr,
&hdr.magic, &hdr.magic,
&hdr.command, &hdr.command,
&hdr.length) &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 // rather than directly to the writer since writeElements doesn't
// return the number of bytes written. // return the number of bytes written.
hw := bytes.NewBuffer(make([]byte, 0, MessageHeaderSize)) 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 //Write header
n, err := w.Write(hw.Bytes()) n, err := w.Write(hw.Bytes())