/
keystore.go
119 lines (95 loc) · 2.73 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
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
// Package keystore defines a local key manager for OrbitDB and IPFS Log.
package keystore // import "berty.tech/go-ipfs-log/keystore"
import (
"crypto/rand"
"encoding/base64"
lru "github.com/hashicorp/golang-lru"
datastore "github.com/ipfs/go-datastore"
crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/pkg/errors"
)
type Keystore struct {
store datastore.Datastore
cache *lru.Cache
}
// Sign signs a value using a given private key.
func (k *Keystore) Sign(privKey crypto.PrivKey, bytes []byte) ([]byte, error) {
return privKey.Sign(bytes)
}
// Verify verifies a signature.
func (k *Keystore) Verify(signature []byte, publicKey crypto.PubKey, data []byte) error {
ok, err := publicKey.Verify(data, signature)
if err != nil {
return err
}
if !ok {
return errors.New("signature is not valid for the supplied data")
}
return nil
}
// NewKeystore creates a new keystore.
func NewKeystore(store datastore.Datastore) (*Keystore, error) {
cache, err := lru.New(128)
if err != nil {
return nil, err
}
return &Keystore{
store: store,
cache: cache,
}, nil
}
// HasKey checks whether a given key ID exist in the keystore.
func (k *Keystore) HasKey(id string) (bool, error) {
storedKey, ok := k.cache.Peek(id)
if ok == false {
value, err := k.store.Get(datastore.NewKey(id))
if err != nil {
return false, err
}
if storedKey != nil {
k.cache.Add(id, base64.StdEncoding.EncodeToString(value))
}
}
return storedKey != nil, nil
}
// CreateKey creates a new key in the key store.
func (k *Keystore) CreateKey(id string) (crypto.PrivKey, error) {
// FIXME: I kept Secp256k1 for compatibility with OrbitDB, should we change this?
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
if err != nil {
return nil, err
}
keyBytes, err := priv.Raw()
if err != nil {
return nil, err
}
if err := k.store.Put(datastore.NewKey(id), keyBytes); err != nil {
return nil, err
}
k.cache.Add(id, base64.StdEncoding.EncodeToString(keyBytes))
return priv, nil
}
// GetKey retrieves a key from the keystore.
func (k *Keystore) GetKey(id string) (crypto.PrivKey, error) {
var err error
var keyBytes []byte
cachedKey, ok := k.cache.Get(id)
if !ok || cachedKey == nil {
keyBytes, err = k.store.Get(datastore.NewKey(id))
if err != nil {
return nil, errors.Wrap(err, "unable to fetch a private key from keystore")
}
k.cache.Add(id, base64.StdEncoding.EncodeToString(keyBytes))
} else {
keyBytes, err = base64.StdEncoding.DecodeString(cachedKey.(string))
if err != nil {
return nil, errors.Wrap(err, "unable to cast private key to bytes")
}
}
privateKey, err := crypto.UnmarshalSecp256k1PrivateKey(keyBytes)
if err != nil {
return nil, err
}
return privateKey, nil
}
var _ Interface = &Keystore{}