From 2079ddf818c2ae5c95925a660cf878f3de58bd49 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 25 Jun 2024 13:46:15 -0300 Subject: [PATCH] support prompting for a password on nak decrypt. --- helpers.go | 4 ++-- key.go | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/helpers.go b/helpers.go index 77abe36..5e0cb9a 100644 --- a/helpers.go +++ b/helpers.go @@ -188,7 +188,7 @@ func gatherSecretKeyOrBunkerFromArguments(c *cli.Context) (string, *nip46.Bunker return sec, nil, nil } -func promptDecrypt(ncryptsec1 string) (string, error) { +func promptDecrypt(ncryptsec string) (string, error) { for i := 1; i < 4; i++ { var attemptStr string if i > 1 { @@ -198,7 +198,7 @@ func promptDecrypt(ncryptsec1 string) (string, error) { if err != nil { return "", err } - sec, err := nip49.Decrypt(ncryptsec1, password) + sec, err := nip49.Decrypt(ncryptsec, password) if err != nil { continue } diff --git a/key.go b/key.go index fe2fbbd..e14600c 100644 --- a/key.go +++ b/key.go @@ -102,29 +102,47 @@ var decrypt = &cli.Command{ Description: `uses the NIP-49 standard.`, ArgsUsage: " ", Action: func(c *cli.Context) error { - var content string + var ncryptsec string var password string switch c.Args().Len() { - case 1: - content = "" - password = c.Args().Get(0) case 2: - content = c.Args().Get(0) + ncryptsec = c.Args().Get(0) password = c.Args().Get(1) - } - if password == "" { - return fmt.Errorf("no password given") - } - for ncryptsec := range getStdinLinesOrArgumentsFromSlice([]string{content}) { + if password == "" { + return fmt.Errorf("no password given") + } sec, err := nip49.Decrypt(ncryptsec, password) if err != nil { - lineProcessingError(c, "failed to decrypt: %s", err) - continue + return fmt.Errorf("failed to decrypt: %s", err) } nsec, _ := nip19.EncodePrivateKey(sec) stdout(nsec) + return nil + case 1: + if arg := c.Args().Get(0); strings.HasPrefix(arg, "ncryptsec1") { + ncryptsec = arg + if res, err := promptDecrypt(ncryptsec); err != nil { + return err + } else { + stdout(res) + return nil + } + } else { + password = c.Args().Get(0) + for ncryptsec := range getStdinLinesOrArgumentsFromSlice([]string{ncryptsec}) { + sec, err := nip49.Decrypt(ncryptsec, password) + if err != nil { + lineProcessingError(c, "failed to decrypt: %s", err) + continue + } + nsec, _ := nip19.EncodePrivateKey(sec) + stdout(nsec) + } + return nil + } + default: + return fmt.Errorf("invalid number of arguments") } - return nil }, }