2023-10-20 19:57:29 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-07 15:57:43 -05:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2023-11-08 12:26:25 -05:00
|
|
|
"encoding/hex"
|
2023-10-20 20:01:11 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2023-10-20 19:57:29 -04:00
|
|
|
"os"
|
2023-10-23 07:04:28 -04:00
|
|
|
"strings"
|
2023-10-20 19:57:29 -04:00
|
|
|
|
2023-12-02 10:18:55 -05:00
|
|
|
"github.com/bgentry/speakeasy"
|
2023-11-13 12:57:35 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-12-02 10:18:55 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
2023-10-20 19:57:29 -04:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2023-11-07 15:57:43 -05:00
|
|
|
const (
|
|
|
|
LINE_PROCESSING_ERROR = iota
|
2023-12-02 10:18:55 -05:00
|
|
|
|
|
|
|
BOLD_ON = "\033[1m"
|
|
|
|
BOLD_OFF = "\033[21m"
|
2023-11-07 15:57:43 -05:00
|
|
|
)
|
|
|
|
|
2023-11-13 13:03:27 -05:00
|
|
|
var log = func(msg string, args ...any) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
|
|
|
}
|
|
|
|
|
2023-11-08 12:26:25 -05:00
|
|
|
func isPiped() bool {
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
return stat.Mode()&os.ModeCharDevice == 0
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:57:43 -05:00
|
|
|
func getStdinLinesOrBlank() chan string {
|
2023-11-08 10:50:36 -05:00
|
|
|
multi := make(chan string)
|
|
|
|
if hasStdinLines := writeStdinLinesOrNothing(multi); !hasStdinLines {
|
|
|
|
single := make(chan string, 1)
|
|
|
|
single <- ""
|
|
|
|
close(single)
|
|
|
|
return single
|
|
|
|
} else {
|
|
|
|
return multi
|
|
|
|
}
|
2023-11-07 15:57:43 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
func getStdinLinesOrFirstArgument(c *cli.Context) chan string {
|
2023-11-07 15:57:43 -05:00
|
|
|
// try the first argument
|
|
|
|
target := c.Args().First()
|
|
|
|
if target != "" {
|
2023-11-08 10:50:36 -05:00
|
|
|
single := make(chan string, 1)
|
|
|
|
single <- target
|
2023-11-08 20:54:52 -05:00
|
|
|
close(single)
|
2023-11-08 10:50:36 -05:00
|
|
|
return single
|
2023-11-07 15:57:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// try the stdin
|
2023-11-08 10:50:36 -05:00
|
|
|
multi := make(chan string)
|
|
|
|
writeStdinLinesOrNothing(multi)
|
|
|
|
return multi
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeStdinLinesOrNothing(ch chan string) (hasStdinLines bool) {
|
2023-11-08 12:26:25 -05:00
|
|
|
if isPiped() {
|
2023-11-08 10:50:36 -05:00
|
|
|
// piped
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
2024-01-11 19:48:54 -05:00
|
|
|
scanner.Buffer(make([]byte, 16*1024), 256*1024)
|
2023-11-08 10:50:36 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
ch <- strings.TrimSpace(scanner.Text())
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
// not piped
|
|
|
|
return false
|
2023-10-20 19:57:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 20:01:11 -04:00
|
|
|
func validateRelayURLs(wsurls []string) error {
|
|
|
|
for _, wsurl := range wsurls {
|
|
|
|
u, err := url.Parse(wsurl)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid relay url '%s': %s", wsurl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme != "ws" && u.Scheme != "wss" {
|
|
|
|
return fmt.Errorf("relay url must use wss:// or ws:// schemes, got '%s'", wsurl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Host == "" {
|
|
|
|
return fmt.Errorf("relay url '%s' is missing the hostname", wsurl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-07 15:57:43 -05:00
|
|
|
|
2023-11-08 12:26:25 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-09 14:32:04 -05:00
|
|
|
func connectToAllRelays(
|
|
|
|
ctx context.Context,
|
|
|
|
relayUrls []string,
|
|
|
|
opts ...nostr.PoolOption,
|
|
|
|
) (*nostr.SimplePool, []*nostr.Relay) {
|
2023-11-13 12:57:35 -05:00
|
|
|
relays := make([]*nostr.Relay, 0, len(relayUrls))
|
2023-12-09 14:32:04 -05:00
|
|
|
pool := nostr.NewSimplePool(ctx, opts...)
|
2023-11-13 12:57:35 -05:00
|
|
|
for _, url := range relayUrls {
|
2023-11-13 13:03:27 -05:00
|
|
|
log("connecting to %s... ", url)
|
2023-11-13 12:57:35 -05:00
|
|
|
if relay, err := pool.EnsureRelay(url); err == nil {
|
|
|
|
relays = append(relays, relay)
|
2023-11-13 13:03:27 -05:00
|
|
|
log("ok.\n")
|
2023-11-13 12:57:35 -05:00
|
|
|
} else {
|
2023-11-13 13:03:27 -05:00
|
|
|
log(err.Error() + "\n")
|
2023-11-13 12:57:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pool, relays
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:57:43 -05:00
|
|
|
func lineProcessingError(c *cli.Context, msg string, args ...any) {
|
|
|
|
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
|
2023-11-13 13:03:27 -05:00
|
|
|
log(msg+"\n", args...)
|
2023-11-07 15:57:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func exitIfLineProcessingError(c *cli.Context) {
|
|
|
|
if val := c.Context.Value(LINE_PROCESSING_ERROR); val != nil && val.(bool) {
|
|
|
|
os.Exit(123)
|
|
|
|
}
|
|
|
|
}
|
2023-12-02 10:18:55 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|