-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystore.go
47 lines (38 loc) · 1.16 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/ethereum/go-ethereum/accounts/keystore"
)
// CreateKeystore create a file containing an encrypted wallet and private key
func CreateKeystore() {
ks := keystore.NewKeyStore("/tmp", keystore.StandardScryptN, keystore.StandardScryptP)
password := "thenewkeystore"
account, err := ks.NewAccount(password)
if err != nil {
log.Fatalf("create account error %s", err.Error())
}
fmt.Println("account created: ", account.Address.Hex())
}
// ImportKeystore load a file containing an encrypted account.
func ImportKeystore(filepath, password string) {
ks := keystore.NewKeyStore("/tmp", keystore.StandardScryptN, keystore.StandardScryptP)
jsonBytes, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatalf("import key error: %s", err.Error())
}
account, err := ks.Import(jsonBytes, password, password)
if err != nil {
log.Fatalf("import key error: %s", err.Error())
}
fmt.Println("account: ", account.Address.Hex())
if err := os.Remove(filepath); err != nil {
log.Fatalf("remove file error: %s", err.Error())
}
}
func main() {
CreateKeystore()
ImportKeystore("/tmp/some-file", "thnewkeystore")
}