-
Notifications
You must be signed in to change notification settings - Fork 212
/
keys.go
64 lines (50 loc) · 1.42 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package signing
import (
"crypto/ed25519"
"encoding/hex"
"github.com/spacemeshos/go-spacemesh/log"
)
// PrivateKey is an alias to ed25519.PrivateKey.
type PrivateKey = ed25519.PrivateKey
// PrivateKeySize size of the private key in bytes.
const PrivateKeySize = ed25519.PrivateKeySize
// PublicKey is the type describing a public key.
type PublicKey struct {
ed25519.PublicKey
}
func Public(priv PrivateKey) ed25519.PublicKey {
return priv.Public().(ed25519.PublicKey)
}
// NewPublicKey constructs a new public key instance from a byte array.
func NewPublicKey(pub []byte) *PublicKey {
return &PublicKey{pub}
}
// Field returns a log field. Implements the LoggableField interface.
func (p *PublicKey) Field() log.Field {
return log.String("public_key", p.ShortString())
}
// Bytes returns the public key as byte array.
func (p *PublicKey) Bytes() []byte {
// Prevent segfault if unset
if p != nil {
return p.PublicKey
}
return nil
}
// String returns the public key as a hex representation string.
func (p *PublicKey) String() string {
return hex.EncodeToString(p.Bytes())
}
const shortStringSize = 5
// ShortString returns a representative sub string.
func (p *PublicKey) ShortString() string {
s := p.String()
if len(s) < shortStringSize {
return s
}
return s[:shortStringSize]
}
// Equals returns true iff the public keys are equal.
func (p *PublicKey) Equals(o *PublicKey) bool {
return p.PublicKey.Equal(o.PublicKey)
}