--prompt-sec for getting a secret key from a prompt.

This commit is contained in:
fiatjaf
2023-11-08 14:26:25 -03:00
parent 200e4e61f7
commit d95b6f50ff
5 changed files with 60 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"bufio"
"context"
"encoding/hex"
"fmt"
"net/url"
"os"
@@ -15,6 +16,11 @@ const (
LINE_PROCESSING_ERROR = iota
)
func isPiped() bool {
stat, _ := os.Stdin.Stat()
return stat.Mode()&os.ModeCharDevice == 0
}
func getStdinLinesOrBlank() chan string {
multi := make(chan string)
if hasStdinLines := writeStdinLinesOrNothing(multi); !hasStdinLines {
@@ -43,7 +49,7 @@ func getStdinLinesOrFirstArgument(c *cli.Context) chan string {
}
func writeStdinLinesOrNothing(ch chan string) (hasStdinLines bool) {
if stat, _ := os.Stdin.Stat(); stat.Mode()&os.ModeCharDevice == 0 {
if isPiped() {
// piped
go func() {
scanner := bufio.NewScanner(os.Stdin)
@@ -78,6 +84,20 @@ func validateRelayURLs(wsurls []string) error {
return nil
}
func validate32BytesHex(target string) error {
if _, err := hex.DecodeString(target); err != nil {
return fmt.Errorf("target '%s' is not valid hex: %s", target, err)
}
if len(target) != 64 {
return fmt.Errorf("expected '%s' to be 64 characters (32 bytes), got %d", target, len(target))
}
if strings.ToLower(target) != target {
return fmt.Errorf("expected target to be all lowercase hex. try again with '%s'", strings.ToLower(target))
}
return nil
}
func lineProcessingError(c *cli.Context, msg string, args ...any) {
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
fmt.Fprintf(os.Stderr, msg+"\n", args...)