39 lines
672 B
Go
39 lines
672 B
Go
package pubkey
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ed25519"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"lukechampine.com/frand"
|
|
|
|
"github.com/mleku/transit/chk"
|
|
"github.com/mleku/transit/log"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
i := generatePubkey()
|
|
var err error
|
|
var b []byte
|
|
if b, err = json.Marshal(&i); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
log.I.F("encode %0x %s", i.Key, b)
|
|
i2 := &P{}
|
|
if err = json.Unmarshal(b, i2); chk.E(err) {
|
|
t.Fatal(err)
|
|
}
|
|
log.I.F("decode %0x", i2.Key)
|
|
if !bytes.Equal(i.Key, i2.Key) {
|
|
t.Fatal("failed to encode/decode")
|
|
}
|
|
}
|
|
|
|
func generatePubkey() (p P) {
|
|
var pub ed25519.PublicKey
|
|
pub, _, _ = ed25519.GenerateKey(frand.Reader)
|
|
pp := New(pub)
|
|
return *pp
|
|
}
|