nsecbunker work-in-progress.

This commit is contained in:
fiatjaf
2023-12-02 12:18:55 -03:00
parent 5657fdc6a7
commit bc7cd0939c
6 changed files with 167 additions and 30 deletions

View File

@@ -9,12 +9,17 @@ import (
"os"
"strings"
"github.com/bgentry/speakeasy"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/urfave/cli/v2"
)
const (
LINE_PROCESSING_ERROR = iota
BOLD_ON = "\033[1m"
BOLD_OFF = "\033[21m"
)
var log = func(msg string, args ...any) {
@@ -129,3 +134,33 @@ func exitIfLineProcessingError(c *cli.Context) {
os.Exit(123)
}
}
func gatherSecretKeyFromArguments(c *cli.Context) (string, error) {
sec := c.String("sec")
if c.Bool("prompt-sec") {
if isPiped() {
return "", fmt.Errorf("can't prompt for a secret key when processing data from a pipe, try again without --prompt-sec")
}
var err error
sec, err = speakeasy.FAsk(os.Stderr, "type your secret key as nsec or hex: ")
if err != nil {
return "", fmt.Errorf("failed to get secret key: %w", err)
}
}
if strings.HasPrefix(sec, "nsec1") {
_, hex, err := nip19.Decode(sec)
if err != nil {
return "", fmt.Errorf("invalid nsec: %w", err)
}
sec = hex.(string)
}
if len(sec) > 64 {
return "", fmt.Errorf("invalid secret key: too large")
}
sec = strings.Repeat("0", 64-len(sec)) + sec // left-pad
if err := validate32BytesHex(sec); err != nil {
return "", fmt.Errorf("invalid secret key")
}
return sec, nil
}