-
Notifications
You must be signed in to change notification settings - Fork 27
/
keystore.go
151 lines (118 loc) · 4.06 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package zcnbridge
import (
"fmt"
"os"
"path"
hdw "github.com/0chain/gosdk/zcncore/ethhdwallet"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// ListStorageAccounts List available accounts
func ListStorageAccounts(homedir string) []common.Address {
keyDir := path.Join(homedir, EthereumWalletStorageDir)
ks := keystore.NewKeyStore(keyDir, keystore.StandardScryptN, keystore.StandardScryptP)
config := &accounts.Config{InsecureUnlockAllowed: false}
am := accounts.NewManager(config, ks)
addresses := am.Accounts()
return addresses
}
// DeleteAccount deletes account from wallet
func DeleteAccount(homedir, address string) bool {
keyDir := path.Join(homedir, EthereumWalletStorageDir)
ks := keystore.NewKeyStore(keyDir, keystore.StandardScryptN, keystore.StandardScryptP)
config := &accounts.Config{InsecureUnlockAllowed: false}
am := accounts.NewManager(config, ks)
wallet, err := am.Find(accounts.Account{
Address: common.HexToAddress(address),
})
if err != nil && wallet == nil {
fmt.Printf("failed to find account %s, error: %s", address, err)
return false
}
return true
}
// AccountExists checks if account exists
func AccountExists(homedir, address string) bool {
keyDir := path.Join(homedir, EthereumWalletStorageDir)
ks := keystore.NewKeyStore(keyDir, keystore.StandardScryptN, keystore.StandardScryptP)
config := &accounts.Config{InsecureUnlockAllowed: false}
am := accounts.NewManager(config, ks)
wallet, err := am.Find(accounts.Account{
Address: common.HexToAddress(address),
})
if err != nil && wallet == nil {
fmt.Printf("failed to find account %s, error: %s\n", address, err)
return false
}
status, _ := wallet.Status()
url := wallet.URL()
fmt.Printf("Account exists. Status: %s, Path: %s\n", status, url)
return true
}
// CreateKeyStorage create, restore or unlock key storage
func CreateKeyStorage(homedir, password string) error {
keyDir := path.Join(homedir, EthereumWalletStorageDir)
ks := keystore.NewKeyStore(keyDir, keystore.StandardScryptN, keystore.StandardScryptP)
account, err := ks.NewAccount(password)
if err != nil {
return errors.Wrap(err, "failed to create keystore")
}
fmt.Printf("Created account: %s", account.Address.Hex())
return nil
}
// UpdateClientEthereumAddress updates Ethereum address
func UpdateClientEthereumAddress(homedir, address string) (err error) {
configFile := path.Join(homedir, BridgeClientConfigName)
buf, err := os.ReadFile(configFile)
if err != nil {
return err
}
cfg := &Bridge{}
err = yaml.Unmarshal(buf, cfg)
if err != nil {
return err
}
cfg.Owner.EthereumAddress = address
text, err := yaml.Marshal(cfg)
if err != nil {
return err
}
err = os.WriteFile(configFile, text, 0644)
return err
}
// ImportAccount imports account using mnemonic
func ImportAccount(homedir, mnemonic, password string) (string, error) {
// 1. Create storage and account if it doesn't exist and add account to it
keyDir := path.Join(homedir, EthereumWalletStorageDir)
ks := keystore.NewKeyStore(keyDir, keystore.StandardScryptN, keystore.StandardScryptP)
// 2. Init wallet
wallet, err := hdw.NewFromMnemonic(mnemonic)
if err != nil {
return "", errors.Wrap(err, "failed to import from mnemonic")
}
pathD := hdw.MustParseDerivationPath("m/44'/60'/0'/0/0")
account, err := wallet.Derive(pathD, true)
if err != nil {
return "", errors.Wrap(err, "failed parse derivation path")
}
key, err := wallet.PrivateKey(account)
if err != nil {
return "", errors.Wrap(err, "failed to get private key")
}
// 3. Find key
acc, err := ks.Find(account)
if err == nil {
fmt.Printf("Account already exists: %s\nPath: %s\n\n", acc.Address.Hex(), acc.URL.Path)
return acc.Address.Hex(), nil
}
// 4. Import the key if it doesn't exist
acc, err = ks.ImportECDSA(key, password)
if err != nil {
return "", errors.Wrap(err, "failed to get import private key")
}
fmt.Printf("Imported account %s to path: %s\n", acc.Address.Hex(), acc.URL.Path)
return acc.Address.Hex(), nil
}