2023-10-20 19:57:29 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-07 15:57:43 -05:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2024-01-29 07:47:26 -05:00
|
|
|
"encoding/hex"
|
2023-10-20 20:01:11 -04:00
|
|
|
"fmt"
|
2024-05-15 16:31:01 -04:00
|
|
|
"math/rand"
|
2023-10-20 20:01:11 -04:00
|
|
|
"net/url"
|
2023-10-20 19:57:29 -04:00
|
|
|
"os"
|
2023-10-23 07:04:28 -04:00
|
|
|
"strings"
|
2024-07-10 13:48:02 -04:00
|
|
|
"time"
|
2023-10-20 19:57:29 -04:00
|
|
|
|
2024-01-29 07:47:26 -05:00
|
|
|
"github.com/chzyer/readline"
|
|
|
|
"github.com/fatih/color"
|
2024-07-30 10:43:14 -04:00
|
|
|
"github.com/fiatjaf/cli/v3"
|
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"
|
2024-02-05 22:58:26 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip46"
|
2024-01-29 07:47:26 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip49"
|
2023-10-20 19:57:29 -04:00
|
|
|
)
|
|
|
|
|
2023-11-07 15:57:43 -05:00
|
|
|
const (
|
|
|
|
LINE_PROCESSING_ERROR = iota
|
|
|
|
)
|
|
|
|
|
2023-11-13 13:03:27 -05:00
|
|
|
var log = func(msg string, args ...any) {
|
2024-03-28 11:19:20 -04:00
|
|
|
fmt.Fprintf(color.Error, msg, args...)
|
2023-11-13 13:03:27 -05:00
|
|
|
}
|
|
|
|
|
2024-01-24 20:38:51 -05:00
|
|
|
var stdout = fmt.Println
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-19 10:34:59 -04:00
|
|
|
func getStdinLinesOrArguments(args cli.Args) chan string {
|
|
|
|
return getStdinLinesOrArgumentsFromSlice(args.Slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStdinLinesOrArgumentsFromSlice(args []string) chan string {
|
2023-11-07 15:57:43 -05:00
|
|
|
// try the first argument
|
2024-03-19 10:34:59 -04:00
|
|
|
if len(args) > 0 {
|
|
|
|
argsCh := make(chan string, 1)
|
|
|
|
go func() {
|
|
|
|
for _, arg := range args {
|
|
|
|
argsCh <- arg
|
|
|
|
}
|
|
|
|
close(argsCh)
|
|
|
|
}()
|
|
|
|
return argsCh
|
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-07-17 12:16:40 -04:00
|
|
|
scanner.Buffer(make([]byte, 16*1024*1024), 256*1024*1024)
|
2024-02-06 10:47:58 -05:00
|
|
|
hasEmittedAtLeastOne := false
|
2023-11-08 10:50:36 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
ch <- strings.TrimSpace(scanner.Text())
|
2024-02-06 10:47:58 -05:00
|
|
|
hasEmittedAtLeastOne = true
|
|
|
|
}
|
|
|
|
if !hasEmittedAtLeastOne {
|
|
|
|
ch <- ""
|
2023-11-08 10:50:36 -05:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
// not piped
|
|
|
|
return false
|
2023-10-20 19:57:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:38:40 -04:00
|
|
|
func normalizeAndValidateRelayURLs(wsurls []string) error {
|
|
|
|
for i, wsurl := range wsurls {
|
|
|
|
wsurl = nostr.NormalizeURL(wsurl)
|
|
|
|
wsurls[i] = wsurl
|
|
|
|
|
2023-10-20 20:01:11 -04:00
|
|
|
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-12-09 14:32:04 -05:00
|
|
|
func connectToAllRelays(
|
|
|
|
ctx context.Context,
|
|
|
|
relayUrls []string,
|
2024-07-10 13:48:02 -04:00
|
|
|
forcePreAuth bool,
|
2023-12-09 14:32:04 -05:00
|
|
|
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...)
|
2024-07-10 13:48:02 -04:00
|
|
|
relayLoop:
|
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 {
|
2024-07-10 13:48:02 -04:00
|
|
|
if forcePreAuth {
|
|
|
|
log("waiting for auth challenge... ")
|
|
|
|
signer := opts[0].(nostr.WithAuthHandler)
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
challengeWaitLoop:
|
|
|
|
for {
|
|
|
|
// beginhack
|
|
|
|
// here starts the biggest and ugliest hack of this codebase
|
|
|
|
if err := relay.Auth(ctx, func(authEvent *nostr.Event) error {
|
|
|
|
challengeTag := authEvent.Tags.GetFirst([]string{"challenge", ""})
|
|
|
|
if (*challengeTag)[1] == "" {
|
|
|
|
return fmt.Errorf("auth not received yet *****")
|
|
|
|
}
|
|
|
|
return signer(authEvent)
|
|
|
|
}); err == nil {
|
|
|
|
// auth succeeded
|
|
|
|
break challengeWaitLoop
|
|
|
|
} else {
|
|
|
|
// auth failed
|
|
|
|
if strings.HasSuffix(err.Error(), "auth not received yet *****") {
|
|
|
|
// it failed because we didn't receive the challenge yet, so keep waiting
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue challengeWaitLoop
|
|
|
|
} else {
|
|
|
|
// it failed for some other reason, so skip this relay
|
|
|
|
log(err.Error() + "\n")
|
|
|
|
continue relayLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// endhack
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 12:57:35 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-11 14:33:19 -04:00
|
|
|
func lineProcessingError(ctx context.Context, msg string, args ...any) context.Context {
|
2023-11-13 13:03:27 -05:00
|
|
|
log(msg+"\n", args...)
|
2024-07-11 14:33:19 -04:00
|
|
|
return context.WithValue(ctx, LINE_PROCESSING_ERROR, true)
|
2023-11-07 15:57:43 -05:00
|
|
|
}
|
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
func exitIfLineProcessingError(ctx context.Context) {
|
|
|
|
if val := ctx.Value(LINE_PROCESSING_ERROR); val != nil && val.(bool) {
|
2023-11-07 15:57:43 -05:00
|
|
|
os.Exit(123)
|
|
|
|
}
|
|
|
|
}
|
2023-12-02 10:18:55 -05:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
func gatherSecretKeyOrBunkerFromArguments(ctx context.Context, c *cli.Command) (string, *nip46.BunkerClient, error) {
|
2024-01-29 07:47:26 -05:00
|
|
|
var err error
|
2024-02-05 22:58:26 -05:00
|
|
|
|
|
|
|
if bunkerURL := c.String("connect"); bunkerURL != "" {
|
2024-02-06 10:47:46 -05:00
|
|
|
clientKey := c.String("connect-as")
|
|
|
|
if clientKey != "" {
|
|
|
|
clientKey = strings.Repeat("0", 64-len(clientKey)) + clientKey
|
|
|
|
} else {
|
|
|
|
clientKey = nostr.GeneratePrivateKey()
|
|
|
|
}
|
2024-06-25 21:18:26 -04:00
|
|
|
bunker, err := nip46.ConnectBunker(ctx, clientKey, bunkerURL, nil, func(s string) {
|
2024-03-28 11:19:20 -04:00
|
|
|
fmt.Fprintf(color.Error, color.CyanString("[nip46]: open the following URL: %s"), s)
|
2024-03-02 06:18:40 -05:00
|
|
|
})
|
2024-02-05 22:58:26 -05:00
|
|
|
return "", bunker, err
|
|
|
|
}
|
2024-06-27 03:22:01 -04:00
|
|
|
|
2023-12-02 10:18:55 -05:00
|
|
|
sec := c.String("sec")
|
2024-07-23 14:21:14 -04:00
|
|
|
|
|
|
|
// check in the environment for the secret key
|
|
|
|
if sec == "" {
|
|
|
|
if key, ok := os.LookupEnv("NOSTR_PRIVATE_KEY"); ok {
|
|
|
|
sec = key
|
|
|
|
}
|
2024-06-27 03:22:01 -04:00
|
|
|
}
|
|
|
|
|
2023-12-02 10:18:55 -05:00
|
|
|
if c.Bool("prompt-sec") {
|
|
|
|
if isPiped() {
|
2024-02-05 22:58:26 -05:00
|
|
|
return "", nil, fmt.Errorf("can't prompt for a secret key when processing data from a pipe, try again without --prompt-sec")
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
2024-01-29 07:47:26 -05:00
|
|
|
sec, err = askPassword("type your secret key as ncryptsec, nsec or hex: ", nil)
|
2023-12-02 10:18:55 -05:00
|
|
|
if err != nil {
|
2024-02-05 22:58:26 -05:00
|
|
|
return "", nil, fmt.Errorf("failed to get secret key: %w", err)
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-29 07:47:26 -05:00
|
|
|
if strings.HasPrefix(sec, "ncryptsec1") {
|
|
|
|
sec, err = promptDecrypt(sec)
|
2023-12-02 10:18:55 -05:00
|
|
|
if err != nil {
|
2024-02-05 22:58:26 -05:00
|
|
|
return "", nil, fmt.Errorf("failed to decrypt: %w", err)
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
2024-07-12 13:04:14 -04:00
|
|
|
} else if bsec, err := hex.DecodeString(leftPadKey(sec)); err == nil {
|
2024-02-02 12:11:58 -05:00
|
|
|
sec = hex.EncodeToString(bsec)
|
2024-01-29 07:47:26 -05:00
|
|
|
} else if prefix, hexvalue, err := nip19.Decode(sec); err != nil {
|
2024-02-05 22:58:26 -05:00
|
|
|
return "", nil, fmt.Errorf("invalid nsec: %w", err)
|
2024-01-29 07:47:26 -05:00
|
|
|
} else if prefix == "nsec" {
|
|
|
|
sec = hexvalue.(string)
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
2024-01-29 07:47:26 -05:00
|
|
|
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValid32ByteHex(sec); !ok {
|
2024-02-05 22:58:26 -05:00
|
|
|
return "", nil, fmt.Errorf("invalid secret key")
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
2024-02-05 22:58:26 -05:00
|
|
|
return sec, nil, nil
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
2024-01-29 07:47:26 -05:00
|
|
|
|
2024-06-25 12:46:15 -04:00
|
|
|
func promptDecrypt(ncryptsec string) (string, error) {
|
2024-01-29 07:47:26 -05:00
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
var attemptStr string
|
|
|
|
if i > 1 {
|
|
|
|
attemptStr = fmt.Sprintf(" [%d/3]", i)
|
|
|
|
}
|
|
|
|
password, err := askPassword("type the password to decrypt your secret key"+attemptStr+": ", nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-06-25 12:46:15 -04:00
|
|
|
sec, err := nip49.Decrypt(ncryptsec, password)
|
2024-01-29 07:47:26 -05:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return sec, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("couldn't decrypt private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, error) {
|
|
|
|
config := &readline.Config{
|
2024-03-28 11:19:20 -04:00
|
|
|
Stdout: color.Error,
|
2024-01-29 07:47:26 -05:00
|
|
|
Prompt: color.YellowString(msg),
|
|
|
|
InterruptPrompt: "^C",
|
|
|
|
DisableAutoSaveHistory: true,
|
|
|
|
EnableMask: true,
|
|
|
|
MaskRune: '*',
|
|
|
|
}
|
|
|
|
|
|
|
|
rl, err := readline.NewEx(config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
answer, err := rl.Readline()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-08-06 10:05:07 -04:00
|
|
|
answer = strings.TrimSpace(answer)
|
2024-01-29 07:47:26 -05:00
|
|
|
if shouldAskAgain != nil && shouldAskAgain(answer) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return answer, err
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 16:31:01 -04:00
|
|
|
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
|
|
func randString(n int) string {
|
|
|
|
b := make([]byte, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
2024-07-12 13:04:14 -04:00
|
|
|
|
|
|
|
func leftPadKey(k string) string {
|
|
|
|
return strings.Repeat("0", 64-len(k)) + k
|
|
|
|
}
|