forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencryptkey.go
54 lines (47 loc) · 1.33 KB
/
encryptkey.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
package main
import (
"log"
"strings"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2"
)
const (
encryptKeyFlagPrivateKey = "privateKey"
encryptKeyFlagPassword = "password"
encryptKeyFlagOutput = "output"
)
var encryptKeyFlags = []cli.Flag{
&cli.StringFlag{
Name: encryptKeyFlagPrivateKey,
Aliases: []string{"pk"},
Usage: "Private key hash",
Required: true,
},
&cli.StringFlag{
Name: encryptKeyFlagPassword,
Aliases: []string{"pw"},
Usage: "Password to encrypt the private key",
Required: true,
},
&cli.StringFlag{
Name: encryptKeyFlagOutput,
Aliases: []string{"o"},
Usage: "Output directory to save the encrypted private key file",
Required: true,
},
}
func encryptKey(ctx *cli.Context) error {
privateKeyHash := ctx.String(encryptKeyFlagPrivateKey)
password := ctx.String(encryptKeyFlagPassword)
outputDir := ctx.String(encryptKeyFlagOutput)
privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(privateKeyHash, "0x"))
if err != nil {
log.Fatal("Invalid private key: ", err)
}
ks := keystore.NewKeyStore(outputDir, keystore.StandardScryptN, keystore.StandardScryptP)
if _, err := ks.ImportECDSA(privateKey, password); err != nil {
log.Fatal("Failed to encrypt private key: ", err)
}
return nil
}