lnwallet: move txout serialization out of lnwire

This commit is contained in:
Conner Fromknecht authored and Olaoluwa Osuntokun committed 2017-09-14 13:56:25 +02:00
1 parent 4bae23a9a7
commit 8ef2263e46
3 files changed
+10 -11

No files matched your search

+2 -3
View File
@@ -5,7 +5,6 @@ import (
"errors"
"io"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/txscript"
"github.com/roasbeef/btcd/wire"
@@ -104,7 +103,7 @@ func WriteSignDescriptor(w io.Writer, sd *SignDescriptor) error {
return err
}
if err := lnwire.WriteTxOut(w, sd.Output); err != nil {
if err := writeTxOut(w, sd.Output); err != nil {
return err
}
@@ -174,7 +173,7 @@ func ReadSignDescriptor(r io.Reader, sd *SignDescriptor) error {
sd.WitnessScript = witnessScript
txOut := &wire.TxOut{}
if err := lnwire.ReadTxOut(r, txOut); err != nil {
if err := readTxOut(r, txOut); err != nil {
return err
}
sd.Output = txOut
+46
View File
@@ -0,0 +1,46 @@
package lnwallet
import (
"encoding/binary"
"io"
"github.com/roasbeef/btcd/wire"
)
// writeTxOut serializes a wire.TxOut struct into the passed io.Writer stream.
func writeTxOut(w io.Writer, txo *wire.TxOut) error {
var scratch [8]byte
binary.BigEndian.PutUint64(scratch[:], uint64(txo.Value))
if _, err := w.Write(scratch[:]); err != nil {
return err
}
if err := wire.WriteVarBytes(w, 0, txo.PkScript); err != nil {
return err
}
return nil
}
// readTxOut deserializes a wire.TxOut struct from the passed io.Reader stream.
func readTxOut(r io.Reader, txo *wire.TxOut) error {
var scratch [8]byte
if _, err := io.ReadFull(r, scratch[:]); err != nil {
return err
}
value := int64(binary.BigEndian.Uint64(scratch[:]))
pkScript, err := wire.ReadVarBytes(r, 0, 80, "pkScript")
if err != nil {
return err
}
*txo = wire.TxOut{
Value: value,
PkScript: pkScript,
}
return nil
}
+46
View File
@@ -0,0 +1,46 @@
package lnwallet
import (
"bytes"
"reflect"
"testing"
"github.com/roasbeef/btcd/wire"
)
func TestTxOutSerialization(t *testing.T) {
txo := wire.TxOut{
Value: 1e7,
PkScript: []byte{
0x41, // OP_DATA_65
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
0xa6, // 65-byte signature
0xac, // OP_CHECKSIG
},
}
var buf bytes.Buffer
if err := writeTxOut(&buf, &txo); err != nil {
t.Fatalf("unable to serialize txout: %v", err)
}
var deserializedTxo wire.TxOut
if err := readTxOut(&buf, &deserializedTxo); err != nil {
t.Fatalf("unable to deserialize txout: %v", err)
}
if !reflect.DeepEqual(txo, deserializedTxo) {
t.Fatalf("original and deserialized txouts are different:\n"+
"original : %+v\n"+
"deserialized : %+v\n",
txo, deserializedTxo)
}
}