support prompting for a password on nak decrypt.

This commit is contained in:
fiatjaf 2024-06-25 13:46:15 -03:00
parent 2135b68106
commit 2079ddf818
2 changed files with 33 additions and 15 deletions

View File

@ -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
}

44
key.go
View File

@ -102,29 +102,47 @@ var decrypt = &cli.Command{
Description: `uses the NIP-49 standard.`,
ArgsUsage: "<ncryptsec-code> <password>",
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
},
}