lnwire: add missing cases in [read/write]Element

Add cases to encode/decode wire.OutPoint, and a final line to properly
write out signatures fully.
This commit is contained in:
Olaoluwa Osuntokun 2016-05-30 15:42:53 -07:00
parent 61994bc294
commit 9978d889b7
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 39 additions and 3 deletions

View File

@ -200,6 +200,10 @@ func writeElement(w io.Writer, element interface{}) error {
if sliceLength > MaxSliceLength {
return fmt.Errorf("Slice length too long!")
}
if err := wire.WriteVarBytes(w, 0, e); err != nil {
return err
}
case PkScript:
// Make sure it's P2PKH or P2SH size or less.
scriptLength := len(e)
@ -252,6 +256,21 @@ func writeElement(w io.Writer, element interface{}) error {
if _, err := w.Write(idx[:]); err != nil {
return err
}
case wire.OutPoint:
// TODO(roasbeef): consolidate with above
// First write out the previous txid.
var h [32]byte
copy(h[:], e.Hash[:])
if _, err := w.Write(h[:]); err != nil {
return err
}
// Then the exact index of this output.
var idx [4]byte
binary.BigEndian.PutUint32(idx[:], e.Index)
if _, err := w.Write(idx[:]); err != nil {
return err
}
// TODO(roasbeef): *MsgTx
default:
return fmt.Errorf("Unknown type in writeElement: %T", e)
@ -478,6 +497,25 @@ func readElement(r io.Reader, element interface{}) error {
}
(*e).PreviousOutPoint.Index = binary.BigEndian.Uint32(idxBytes[:])
return nil
case *wire.OutPoint:
// TODO(roasbeef): consolidate with above
var h [32]byte
if _, err = io.ReadFull(r, h[:]); err != nil {
return err
}
hash, err := wire.NewShaHash(h[:])
if err != nil {
return err
}
(*e).Hash = *hash
// Index
var idxBytes [4]byte
_, err = io.ReadFull(r, idxBytes[:])
if err != nil {
return err
}
(*e).Index = binary.BigEndian.Uint32(idxBytes[:])
default:
return fmt.Errorf("Unknown type in readElement: %T", e)
}

View File

@ -173,9 +173,7 @@ func discardInput(r io.Reader, n uint32) {
// WriteMessage writes a lightning Message to w including the necessary header
// information and returns the number of bytes written.
func WriteMessage(w io.Writer, msg Message, pver uint32,
btcnet wire.BitcoinNet) (int, error) {
func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet wire.BitcoinNet) (int, error) {
totalBytes := 0
cmd := msg.Command()