-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkey.go
121 lines (95 loc) · 2.05 KB
/
key.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package vault
import (
"crypto/ed25519"
"errors"
"time"
"github.com/mr-tron/base58/base58"
)
var (
ErrKeyNotFound = errors.New("no records could be found")
ErrInvalidKey = errors.New("invalid key")
ErrInvalidPrefixLength = errors.New("invalid prefix length")
)
type State uint
const (
StateUnknown State = iota
StateAvailable
StateReserved
StateDeprecated
StateRevoked
)
type Record struct {
Id uint64
PublicKey string
PrivateKey string
State State
CreatedAt time.Time
}
func CreateKey() (*Record, error) {
return GrindKey("")
}
func GrindKey(prefix string) (*Record, error) {
if len(prefix) > 5 {
return nil, ErrInvalidPrefixLength
}
var priv ed25519.PrivateKey
var pub ed25519.PublicKey
var err error
for {
pub, priv, err = ed25519.GenerateKey(nil)
if err != nil {
return nil, err
}
if base58.Encode(pub)[:len(prefix)] == prefix {
break
}
}
return &Record{
PublicKey: base58.Encode(pub[:]),
PrivateKey: base58.Encode(priv[:]),
State: StateAvailable,
}, nil
}
func (r *Record) IsAvailable() bool {
return r.State == StateReserved
}
func (r *Record) IsReserved() bool {
return r.State == StateReserved
}
func (r *Record) IsDeprecated() bool {
return r.State == StateDeprecated
}
func (r *Record) IsRevoked() bool {
return r.State == StateRevoked
}
func (r *Record) GetPublicKey() (ed25519.PublicKey, error) {
return base58.Decode(r.PublicKey)
}
func (r *Record) GetPrivateKey() (ed25519.PrivateKey, error) {
return base58.Decode(r.PrivateKey)
}
func (r *Record) Clone() Record {
return Record{
Id: r.Id,
PublicKey: r.PublicKey,
PrivateKey: r.PrivateKey,
State: r.State,
CreatedAt: r.CreatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.PublicKey = r.PublicKey
dst.PrivateKey = r.PrivateKey
dst.State = r.State
dst.CreatedAt = r.CreatedAt
}
func (r *Record) Validate() error {
if len(r.PublicKey) == 0 {
return errors.New("public key is required")
}
if len(r.PrivateKey) == 0 {
return errors.New("private key is required")
}
return nil
}