Compare commits

...

2 Commits

Author SHA1 Message Date
franzap
6daf0f4e5c Update zapstore.yaml 2025-05-20 23:15:31 -03:00
Alex Gleason
bfeaf0710f Allow --prompt-sec to be used with pipes 2025-05-03 07:27:37 -03:00
2 changed files with 56 additions and 35 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
"github.com/nbd-wtf/go-nostr/nip46" "github.com/nbd-wtf/go-nostr/nip46"
"github.com/nbd-wtf/go-nostr/nip49" "github.com/nbd-wtf/go-nostr/nip49"
"golang.org/x/term"
) )
var defaultKeyFlags = []cli.Flag{ var defaultKeyFlags = []cli.Flag{
@@ -84,9 +85,6 @@ func gatherSecretKeyOrBunkerFromArguments(ctx context.Context, c *cli.Command) (
} }
if c.Bool("prompt-sec") { if c.Bool("prompt-sec") {
if isPiped() {
return "", nil, fmt.Errorf("can't prompt for a secret key when processing data from a pipe, try again without --prompt-sec")
}
sec, err = askPassword("type your secret key as ncryptsec, nsec or hex: ", nil) sec, err = askPassword("type your secret key as ncryptsec, nsec or hex: ", nil)
if err != nil { if err != nil {
return "", nil, fmt.Errorf("failed to get secret key: %w", err) return "", nil, fmt.Errorf("failed to get secret key: %w", err)
@@ -133,6 +131,35 @@ func promptDecrypt(ncryptsec string) (string, error) {
} }
func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, error) { func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, error) {
if isPiped() {
// Use TTY method when stdin is piped
tty, err := os.Open("/dev/tty")
if err != nil {
return "", fmt.Errorf("can't prompt for a secret key when processing data from a pipe on this system (failed to open /dev/tty: %w), try again without --prompt-sec or provide the key via --sec or NOSTR_SECRET_KEY environment variable", err)
}
defer tty.Close()
for {
// Print the prompt to stderr so it's visible to the user
fmt.Fprintf(color.Error, color.YellowString(msg))
// Read password from TTY with masking
password, err := term.ReadPassword(int(tty.Fd()))
if err != nil {
return "", err
}
// Print newline after password input
fmt.Fprintln(color.Error)
answer := strings.TrimSpace(string(password))
if shouldAskAgain != nil && shouldAskAgain(answer) {
continue
}
return answer, nil
}
} else {
// Use normal readline method when stdin is not piped
config := &readline.Config{ config := &readline.Config{
Stdout: color.Error, Stdout: color.Error,
Prompt: color.YellowString(msg), Prompt: color.YellowString(msg),
@@ -158,4 +185,5 @@ func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, e
} }
return answer, err return answer, err
} }
}
} }

View File

@@ -1,14 +1,7 @@
nak: repository: https://github.com/fiatjaf/nak
cli: assets:
name: nak - nak-v\d+\.\d+\.\d+-darwin-arm64
summary: a command line tool for doing all things nostr - nak-v\d+\.\d+\.\d+-linux-amd64
repository: https://github.com/fiatjaf/nak - nak-v\d+\.\d+\.\d+-linux-arm64
artifacts: remote_metadata:
nak-v%v-darwin-arm64: - github
platforms: [darwin-arm64]
nak-v%v-darwin-amd64:
platforms: [darwin-x86_64]
nak-v%v-linux-arm64:
platforms: [linux-aarch64]
nak-v%v-linux-amd64:
platforms: [linux-x86_64]