routing/route: add vertex constructor for hex string
This commit is contained in:
parent
8c44cf4a22
commit
fd63ed13ff
@ -3,6 +3,7 @@ package route
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
@ -47,6 +48,22 @@ func NewVertexFromBytes(b []byte) (Vertex, error) {
|
||||
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
|
||||
// hex-encoding of the serialized compressed public key.
|
||||
func (v Vertex) String() string {
|
||||
|
Loading…
Reference in New Issue
Block a user