-
Notifications
You must be signed in to change notification settings - Fork 75
/
single_keystore.go
90 lines (73 loc) · 1.89 KB
/
single_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
package accounts
import (
"crypto/ecdsa"
"fmt"
"io/ioutil"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/mitchellh/go-homedir"
"github.com/sonm-io/core/util"
)
func OpenSingleKeystore(path, pass string, pf PassPhraser) (*ecdsa.PrivateKey, error) {
var acc accounts.Account
var err error
path, err = getKeystoreDir(path)
if err != nil {
return nil, err
}
ks := keystore.NewKeyStore(path, keystore.LightScryptN, keystore.LightScryptP)
pass, err = getPassPhrase(pass, pf)
if err != nil {
return nil, err
}
if len(ks.Accounts()) == 0 {
acc, err = ks.NewAccount(pass)
if err != nil {
return nil, err
}
} else {
acc = ks.Accounts()[0]
}
key, err := decryptKeyFile(acc.URL.Path, pass)
if err != nil {
return nil, err
}
return key, nil
}
func getPassPhrase(pass string, pf PassPhraser) (string, error) {
var err error
if len(pass) == 0 {
// fallback to passPhrase reader if no password provided
pass, err = pf.GetPassPhrase()
if err != nil {
return "", fmt.Errorf("cannot read pass phrase: %v", err)
}
}
return pass, nil
}
func getKeystoreDir(path string) (string, error) {
var err error
path, err = homedir.Expand(path)
if err != nil {
return "", fmt.Errorf("cannot expand path `%s`: %v", path, err)
}
// Use default key store dir if not specified in config.
if path == "" {
path, err = util.GetDefaultKeyStoreDir()
if err != nil {
return "", fmt.Errorf("cannot obtain default keystore dir: %v", err)
}
}
return path, nil
}
func decryptKeyFile(path string, pass string) (*ecdsa.PrivateKey, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot open account file: %v", err)
}
key, err := keystore.DecryptKey(file, pass)
if err != nil {
return nil, fmt.Errorf("cannot decrypt key with given pass phrase: %v", err)
}
return key.PrivateKey, nil
}