forked from lyonnee/key25519
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypair.go
67 lines (53 loc) · 1.26 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
67
package key25519
type Keypair struct {
privKey PrivateKey
pubKey PublicKey
}
func NewKeypairFromPrivKeyBytes(key []byte) (*Keypair, error) {
privKey, err := bytesToPrivKey(key)
if err != nil {
return nil, err
}
return &Keypair{
privKey: privKey,
pubKey: privKey.GetPubKey(),
}, nil
}
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 (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 SaveAsKeystore(kp.PrivateKey().Bytes(), filepath, password, false)
}
func (kp *Keypair) ExportEcdhPubKey() ([]byte, error) {
ecdhKp, err := ExportEcdhKeypair(kp.privKey)
if err != nil {
return nil, err
}
return ecdhKp.PublicKey, nil
}
func (kp *Keypair) Ecdh(peerPubKey []byte) ([]byte, error) {
return Ecdh(kp.privKey, peerPubKey)
}