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 return sec, nil, nil
} }
func promptDecrypt(ncryptsec1 string) (string, error) { func promptDecrypt(ncryptsec string) (string, error) {
for i := 1; i < 4; i++ { for i := 1; i < 4; i++ {
var attemptStr string var attemptStr string
if i > 1 { if i > 1 {
@ -198,7 +198,7 @@ func promptDecrypt(ncryptsec1 string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
sec, err := nip49.Decrypt(ncryptsec1, password) sec, err := nip49.Decrypt(ncryptsec, password)
if err != nil { if err != nil {
continue continue
} }

32
key.go
View File

@ -102,20 +102,34 @@ var decrypt = &cli.Command{
Description: `uses the NIP-49 standard.`, Description: `uses the NIP-49 standard.`,
ArgsUsage: "<ncryptsec-code> <password>", ArgsUsage: "<ncryptsec-code> <password>",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
var content string var ncryptsec string
var password string var password string
switch c.Args().Len() { switch c.Args().Len() {
case 1:
content = ""
password = c.Args().Get(0)
case 2: case 2:
content = c.Args().Get(0) ncryptsec = c.Args().Get(0)
password = c.Args().Get(1) password = c.Args().Get(1)
}
if password == "" { if password == "" {
return fmt.Errorf("no password given") return fmt.Errorf("no password given")
} }
for ncryptsec := range getStdinLinesOrArgumentsFromSlice([]string{content}) { sec, err := nip49.Decrypt(ncryptsec, password)
if err != nil {
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) sec, err := nip49.Decrypt(ncryptsec, password)
if err != nil { if err != nil {
lineProcessingError(c, "failed to decrypt: %s", err) lineProcessingError(c, "failed to decrypt: %s", err)
@ -125,6 +139,10 @@ var decrypt = &cli.Command{
stdout(nsec) stdout(nsec)
} }
return nil return nil
}
default:
return fmt.Errorf("invalid number of arguments")
}
}, },
} }