p2p/discover: Make NodeID implement the encoding.TextMarshaler interface

Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
Lewis Marshall 2017-08-15 11:55:35 +01:00
parent 0227fd1941
commit 8ed18c1193
2 changed files with 70 additions and 0 deletions

View file

@ -225,6 +225,11 @@ func (n *Node) UnmarshalText(text []byte) error {
// The node identifier is a marshaled elliptic curve public key. // The node identifier is a marshaled elliptic curve public key.
type NodeID [NodeIDBits / 8]byte type NodeID [NodeIDBits / 8]byte
// Bytes returns a byte slice representation of the NodeID
func (n NodeID) Bytes() []byte {
return n[:]
}
// NodeID prints as a long hexadecimal number. // NodeID prints as a long hexadecimal number.
func (n NodeID) String() string { func (n NodeID) String() string {
return fmt.Sprintf("%x", n[:]) return fmt.Sprintf("%x", n[:])
@ -240,6 +245,41 @@ func (n NodeID) TerminalString() string {
return hex.EncodeToString(n[:8]) return hex.EncodeToString(n[:8])
} }
// MarshalText implements the encoding.TextMarshaler interface.
func (n NodeID) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(n[:])), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (n *NodeID) UnmarshalText(text []byte) error {
id, err := HexID(string(text))
if err != nil {
return err
}
*n = id
return nil
}
// BytesID converts a byte slice to a NodeID
func BytesID(b []byte) (NodeID, error) {
var id NodeID
if len(b) != len(id) {
return id, fmt.Errorf("wrong length, want %d bytes", len(id))
}
copy(id[:], b)
return id, nil
}
// MustBytesID converts a byte slice to a NodeID.
// It panics if the byte slice is not a valid NodeID.
func MustBytesID(b []byte) NodeID {
id, err := BytesID(b)
if err != nil {
panic(err)
}
return id
}
// HexID converts a hex string to a NodeID. // HexID converts a hex string to a NodeID.
// The string may be prefixed with 0x. // The string may be prefixed with 0x.
func HexID(in string) (NodeID, error) { func HexID(in string) (NodeID, error) {

View file

@ -17,6 +17,7 @@
package discover package discover
import ( import (
"bytes"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand"
@ -192,6 +193,35 @@ func TestHexID(t *testing.T) {
} }
} }
func TestNodeID_textEncoding(t *testing.T) {
ref := NodeID{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60,
0x61, 0x62, 0x63, 0x64,
}
hex := "01020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364"
text, err := ref.MarshalText()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(text, []byte(hex)) {
t.Fatalf("text encoding did not match\nexpected: %s\ngot: %s", hex, text)
}
id := new(NodeID)
if err := id.UnmarshalText(text); err != nil {
t.Fatal(err)
}
if *id != ref {
t.Fatalf("text decoding did not match\nexpected: %s\ngot: %s", ref, id)
}
}
func TestNodeID_recover(t *testing.T) { func TestNodeID_recover(t *testing.T) {
prv := newkey() prv := newkey()
hash := make([]byte, 32) hash := make([]byte, 32)