Skip to content

Commit

Permalink
feat: add option to change password for generated keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Feb 2, 2024
1 parent 571cb0c commit 3663f60
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ type Keystore struct {
Version uint `json:"version"`
}

type Options struct {
Cipher string
InputFile string
OutputFile string
Password string
}

var Flags = []cli.Flag{
&cli.StringFlag{
Name: "cipher",
Expand All @@ -51,7 +44,12 @@ var Flags = []cli.Flag{
Name: "password",
Value: "",
Aliases: []string{"p"},
Usage: "Keystore password",
Usage: "Keystore decrypting password",
},
&cli.StringFlag{
Name: "new-password",
Value: "",
Usage: "Set another password for generated keystore",
},
&cli.BoolFlag{
Name: "raw",
Expand All @@ -78,11 +76,12 @@ func Run(cCtx *cli.Context) error {
var (
err error

inputFile = cCtx.String("input")
outputFile = cCtx.String("output")
password = cCtx.String("password")
cipher = cCtx.String("cipher")
raw = cCtx.Bool("raw")
inputFile = cCtx.String("input")
outputFile = cCtx.String("output")
password = cCtx.String("password")
newPassword = cCtx.String("new-password")
cipher = cCtx.String("cipher")
raw = cCtx.Bool("raw")
)

encryptor := keystorev4.New(keystorev4.WithCipher(cipher))
Expand Down Expand Up @@ -110,7 +109,7 @@ func Run(cCtx *cli.Context) error {

err = json.NewDecoder(input).Decode(&keystore)
if err != nil {
return err
return errors.New("could not decode keystore json")
}

if keystore.Pubkey == "" {
Expand All @@ -119,24 +118,28 @@ func Run(cCtx *cli.Context) error {

secret, err := encryptor.Decrypt(keystore.Crypto, password)
if err != nil {
return err
return fmt.Errorf("could not decrypt keystore: %w", err)
}

if raw {
fmt.Fprintf(output, "0x%x", secret)
return nil
}

if newPassword != "" {
password = newPassword
}

crypto, err := encryptor.Encrypt(secret, password)
if err != nil {
return err
return fmt.Errorf("could not encrypt keystore: %w", err)
}

keystore.Crypto = crypto

keystore2, err := json.Marshal(keystore)
if err != nil {
return err
return fmt.Errorf("could not encode keystore to json: %w", err)
}

fmt.Fprint(output, string(keystore2))
Expand Down

0 comments on commit 3663f60

Please sign in to comment.