/
keystore.go
57 lines (49 loc) · 1.51 KB
/
keystore.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
// Package crypto provides the crypto utils to the program.
package crypto
import (
"fmt"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
)
type KeyType int
const (
Encrypt KeyType = 0
Sign KeyType = 1
)
type Keystore interface {
Unlock(signkeymap map[string]string, password string) error
Lock() error
NewKey(keyname string, keytype KeyType, password string) (string, error)
NewKeyWithDefaultPassword(keyname string, keytype KeyType) (string, error)
Import(keyname string, encodedkey string, keytype KeyType, password string) (string, error)
Sign(data []byte, privKey p2pcrypto.PrivKey) ([]byte, error)
VerifySign(data, signature []byte, pubKey p2pcrypto.PubKey) (bool, error)
SignByKeyName(keyname string, data []byte, opts ...string) ([]byte, error)
VerifySignByKeyName(keyname string, data []byte, sig []byte, opts ...string) (bool, error)
EncryptTo(to []string, data []byte) ([]byte, error)
Decrypt(keyname string, data []byte) ([]byte, error)
GetEncodedPubkey(keyname string, keytype KeyType) (string, error)
GetPeerInfo(keyname string) (peerid peer.ID, ethaddr string, err error)
}
var ks Keystore
func GetKeystore() Keystore {
return ks
}
func (kt KeyType) NameString(keyname string) string {
switch kt {
case Encrypt:
return fmt.Sprintf("encrypt_%s", keyname)
case Sign:
return fmt.Sprintf("sign_%s", keyname)
}
return ""
}
func (kt KeyType) Prefix() string {
switch kt {
case Encrypt:
return "encrypt_"
case Sign:
return "sign_"
}
return ""
}