routing/route: add vertex constructor from bytes
This commit is contained in:
parent
b610f417d2
commit
a332990d2c
@ -11,13 +11,16 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// VertexSize is the size of the array to store a vertex.
|
||||||
|
const VertexSize = 33
|
||||||
|
|
||||||
// ErrNoRouteHopsProvided is returned when a caller attempts to construct a new
|
// ErrNoRouteHopsProvided is returned when a caller attempts to construct a new
|
||||||
// sphinx packet, but provides an empty set of hops for each route.
|
// sphinx packet, but provides an empty set of hops for each route.
|
||||||
var ErrNoRouteHopsProvided = fmt.Errorf("empty route hops provided")
|
var ErrNoRouteHopsProvided = fmt.Errorf("empty route hops provided")
|
||||||
|
|
||||||
// Vertex is a simple alias for the serialization of a compressed Bitcoin
|
// Vertex is a simple alias for the serialization of a compressed Bitcoin
|
||||||
// public key.
|
// public key.
|
||||||
type Vertex [33]byte
|
type Vertex [VertexSize]byte
|
||||||
|
|
||||||
// NewVertex returns a new Vertex given a public key.
|
// NewVertex returns a new Vertex given a public key.
|
||||||
func NewVertex(pub *btcec.PublicKey) Vertex {
|
func NewVertex(pub *btcec.PublicKey) Vertex {
|
||||||
@ -26,6 +29,20 @@ func NewVertex(pub *btcec.PublicKey) Vertex {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewVertexFromBytes returns a new Vertex based on a serialized pubkey in a
|
||||||
|
// byte slice.
|
||||||
|
func NewVertexFromBytes(b []byte) (Vertex, error) {
|
||||||
|
vertexLen := len(b)
|
||||||
|
if vertexLen != VertexSize {
|
||||||
|
return Vertex{}, fmt.Errorf("invalid vertex length of %v, "+
|
||||||
|
"want %v", vertexLen, VertexSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
var v Vertex
|
||||||
|
copy(v[:], b)
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
// String returns a human readable version of the Vertex which is the
|
// String returns a human readable version of the Vertex which is the
|
||||||
// hex-encoding of the serialized compressed public key.
|
// hex-encoding of the serialized compressed public key.
|
||||||
func (v Vertex) String() string {
|
func (v Vertex) String() string {
|
||||||
|
Loading…
Reference in New Issue
Block a user