diff --git a/types/key/chal.go b/types/key/chal.go index 17822feed..7c5405224 100644 --- a/types/key/chal.go +++ b/types/key/chal.go @@ -7,6 +7,7 @@ package key import ( "errors" + "go4.org/mem" "tailscale.com/types/structs" ) @@ -76,3 +77,8 @@ func (k ChallengePublic) String() string { func (k ChallengePublic) MarshalText() ([]byte, error) { return toHex(k.k[:], chalPublicHexPrefix), nil } + +// UnmarshalText implements encoding.TextUnmarshaler. +func (k *ChallengePublic) UnmarshalText(b []byte) error { + return parseHex(k.k[:], mem.B(b), mem.S(chalPublicHexPrefix)) +} diff --git a/types/key/node_test.go b/types/key/node_test.go index 321edf458..b5b9a952b 100644 --- a/types/key/node_test.go +++ b/types/key/node_test.go @@ -142,3 +142,19 @@ func TestNodeWriteRawWithoutAllocating(t *testing.T) { t.Fatalf("WriteRawWithoutAllocating got %f allocs, want %f", got, want) } } + +func TestChallenge(t *testing.T) { + priv := NewChallenge() + pub := priv.Public() + txt, err := pub.MarshalText() + if err != nil { + t.Fatal(err) + } + var back ChallengePublic + if err := back.UnmarshalText(txt); err != nil { + t.Fatal(err) + } + if back != pub { + t.Errorf("didn't round trip: %v != %v", back, pub) + } +}