From a332990d2c073f07d47443030f7699833e7b956a Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 7 Aug 2019 13:03:23 +0200 Subject: [PATCH] routing/route: add vertex constructor from bytes --- routing/route/route.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/routing/route/route.go b/routing/route/route.go index ad639b53..50688c57 100644 --- a/routing/route/route.go +++ b/routing/route/route.go @@ -11,13 +11,16 @@ import ( "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 // sphinx packet, but provides an empty set of hops for each route. var ErrNoRouteHopsProvided = fmt.Errorf("empty route hops provided") // Vertex is a simple alias for the serialization of a compressed Bitcoin // public key. -type Vertex [33]byte +type Vertex [VertexSize]byte // NewVertex returns a new Vertex given a public key. func NewVertex(pub *btcec.PublicKey) Vertex { @@ -26,6 +29,20 @@ func NewVertex(pub *btcec.PublicKey) Vertex { 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 // hex-encoding of the serialized compressed public key. func (v Vertex) String() string {