/
filekeystore.go
53 lines (44 loc) · 1.56 KB
/
filekeystore.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msp
import (
"encoding/hex"
"os"
"path/filepath"
"strings"
"github.com/VoneChain-CS/fabric-sdk-go-gm/pkg/common/providers/core"
"github.com/VoneChain-CS/fabric-sdk-go-gm/pkg/common/providers/msp"
"github.com/VoneChain-CS/fabric-sdk-go-gm/pkg/fab/keyvaluestore"
"github.com/pkg/errors"
)
// NewFileKeyStore loads keys stored in the cryptoconfig directory layout.
// This function will detect if private keys are stored in v1 or v2 format.
func NewFileKeyStore(cryptoConfigMSPPath string) (core.KVStore, error) {
opts := &keyvaluestore.FileKeyValueStoreOptions{
Path: cryptoConfigMSPPath,
KeySerializer: func(key interface{}) (string, error) {
pkk, ok := key.(*msp.PrivKeyKey)
if !ok {
return "", errors.New("converting key to PrivKeyKey failed")
}
if pkk == nil || pkk.MSPID == "" || pkk.ID == "" || pkk.SKI == nil {
return "", errors.New("invalid key")
}
return cryptoConfigPrivateKeyPath(cryptoConfigMSPPath, pkk.ID, pkk.SKI), nil
},
}
return keyvaluestore.New(opts)
}
func cryptoConfigPrivateKeyPath(cryptoConfigMSPPath, id string, ski []byte) string {
// TODO: refactor to case insensitive or remove eventually.
r := strings.NewReplacer("{userName}", id, "{username}", id)
keyDir := filepath.Join(r.Replace(cryptoConfigMSPPath), "keystore")
keyPathPriv := filepath.Join(keyDir, "priv_sk")
_, err := os.Stat(keyPathPriv)
if err == nil {
return keyPathPriv
}
return filepath.Join(keyDir, hex.EncodeToString(ski)+"_sk")
}