-
Notifications
You must be signed in to change notification settings - Fork 2
/
keypair.go
66 lines (52 loc) · 1.31 KB
/
keypair.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
package key25519
import "github.com/lyonnee/key25519/keystore"
type KeyPair struct {
privKey PrivateKey
pubKey PublicKey
}
func NewKeyPair() *KeyPair {
privKey := NewPrivateKey(nil)
return &KeyPair{
privKey: privKey,
pubKey: privKey.GetPubKey(),
}
}
func NewKeyPairWithSeed(seed []byte) *KeyPair {
privKey := NewPrivateKey(seed)
return &KeyPair{
privKey: privKey,
pubKey: privKey.GetPubKey(),
}
}
func NewKeyPairFromPrivKeyBytes(key []byte) (*KeyPair, error) {
privKey, err := bytesToPrivKey(key)
if err != nil {
return nil, err
}
return &KeyPair{
privKey: privKey,
pubKey: privKey.GetPubKey(),
}, nil
}
// 加载keystore文件还原KeyPair
func NewKeyPairFromKeystore(filepath, password string) (*KeyPair, error) {
privKey, err := keystore.LoadPrivKeyFromKeystore(filepath, password)
if err != nil {
return nil, err
}
return NewKeyPairFromPrivKeyBytes(privKey)
}
func (kp *KeyPair) PrivateKey() PrivateKey {
return kp.privKey
}
func (kp *KeyPair) PublicKey() PublicKey {
return kp.pubKey
}
func (kp *KeyPair) LoadFromPrivKey(privKey PrivateKey) {
kp.privKey = privKey
kp.pubKey = privKey.GetPubKey()
}
// 导出keystore文件
func (kp *KeyPair) ExportKeystore(filepath, password string) error {
return keystore.SaveAsKeystore(kp.PrivateKey().Bytes(), filepath, password, false)
}