routing/route: add vertex constructor for hex string

This commit is contained in:
Joost Jager 2019-08-29 13:03:30 +02:00
parent 8c44cf4a22
commit fd63ed13ff
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -3,6 +3,7 @@ package route
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/hex"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
@ -47,6 +48,22 @@ func NewVertexFromBytes(b []byte) (Vertex, error) {
return v, nil return v, nil
} }
// NewVertexFromStr returns a new Vertex given its hex-encoded string format.
func NewVertexFromStr(v string) (Vertex, error) {
// Return error if hex string is of incorrect length.
if len(v) != VertexSize*2 {
return Vertex{}, fmt.Errorf("invalid vertex string length of "+
"%v, want %v", len(v), VertexSize*2)
}
vertex, err := hex.DecodeString(v)
if err != nil {
return Vertex{}, err
}
return NewVertexFromBytes(vertex)
}
// 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 {