mirror of
https://github.com/fiatjaf/nak.git
synced 2025-08-31 22:10:47 -04:00
nsecbunker work-in-progress.
This commit is contained in:
35
helpers.go
35
helpers.go
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user