mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 22:46:40 +01:00
We have the same helpers for writing and reading a wire.Outpoint type defined separately in a couple places. We will want to use these from the graph db package soon though so instead of defining them again there, this commit unifies things and creates a single exported set of helpers. The next commit will make use of these.
39 lines
853 B
Go
39 lines
853 B
Go
package graphdb
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"io"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
)
|
|
|
|
var (
|
|
// byteOrder defines the preferred byte order, which is Big Endian.
|
|
byteOrder = binary.BigEndian
|
|
)
|
|
|
|
// WriteOutpoint writes an outpoint to the passed writer using the minimal
|
|
// amount of bytes possible.
|
|
func WriteOutpoint(w io.Writer, o *wire.OutPoint) error {
|
|
if _, err := w.Write(o.Hash[:]); err != nil {
|
|
return err
|
|
}
|
|
if err := binary.Write(w, byteOrder, o.Index); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ReadOutpoint reads an outpoint from the passed reader that was previously
|
|
// written using the WriteOutpoint struct.
|
|
func ReadOutpoint(r io.Reader, o *wire.OutPoint) error {
|
|
if _, err := io.ReadFull(r, o.Hash[:]); err != nil {
|
|
return err
|
|
}
|
|
if err := binary.Read(r, byteOrder, &o.Index); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|