35 lines
584 B
Go
35 lines
584 B
Go
|
package pnyx
|
||
|
|
||
|
import (
|
||
|
"crypto/ed25519"
|
||
|
"crypto/rand"
|
||
|
"crypto/sha512"
|
||
|
)
|
||
|
|
||
|
type Client struct {
|
||
|
Key ed25519.PrivateKey
|
||
|
}
|
||
|
|
||
|
func KeyID(key ed25519.PublicKey) ClientID {
|
||
|
hash := sha512.Sum512([]byte(key))
|
||
|
return (ClientID)((hash)[0:16])
|
||
|
}
|
||
|
|
||
|
func(client Client) ID() ClientID {
|
||
|
return KeyID(client.Key.Public().(ed25519.PublicKey))
|
||
|
}
|
||
|
|
||
|
func NewClient(key ed25519.PrivateKey) (Client, error) {
|
||
|
if key == nil {
|
||
|
var err error
|
||
|
_, key, err = ed25519.GenerateKey(rand.Reader)
|
||
|
if err != nil {
|
||
|
return Client{}, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return Client{
|
||
|
Key: key,
|
||
|
}, nil
|
||
|
}
|