forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add encryptKey cli command (okx#221)
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters