2023-07-05 14:03:26 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-25 21:18:26 -04:00
|
|
|
"context"
|
2023-07-05 14:03:26 -04:00
|
|
|
"fmt"
|
|
|
|
|
2024-07-30 10:43:14 -04:00
|
|
|
"github.com/fiatjaf/cli/v3"
|
2024-01-21 05:45:22 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-07-05 14:03:26 -04:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
|
|
|
)
|
|
|
|
|
|
|
|
var encode = &cli.Command{
|
|
|
|
Name: "encode",
|
|
|
|
Usage: "encodes notes and other stuff to nip19 entities",
|
|
|
|
Description: `example usage:
|
|
|
|
nak encode npub <pubkey-hex>
|
|
|
|
nak encode nprofile <pubkey-hex>
|
|
|
|
nak encode nprofile --relay <relay-url> <pubkey-hex>
|
|
|
|
nak encode nevent <event-id>
|
|
|
|
nak encode nevent --author <pubkey-hex> --relay <relay-url> --relay <other-relay> <event-id>
|
|
|
|
nak encode nsec <privkey-hex>`,
|
2024-06-25 21:18:26 -04:00
|
|
|
Before: func(ctx context.Context, c *cli.Command) error {
|
2023-10-23 07:04:21 -04:00
|
|
|
if c.Args().Len() < 1 {
|
|
|
|
return fmt.Errorf("expected more than 1 argument.")
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-07-08 19:52:50 -04:00
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
2024-08-07 10:46:07 -04:00
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Commands: []*cli.Command{
|
2023-07-05 14:03:26 -04:00
|
|
|
{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "npub",
|
|
|
|
Usage: "encode a hex public key into bech32 'npub' format",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-03-19 10:34:59 -04:00
|
|
|
for target := range getStdinLinesOrArguments(c.Args()) {
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValidPublicKey(target); !ok {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "invalid public key: %s", target)
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-07-08 19:52:50 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if npub, err := nip19.EncodePublicKey(target); err == nil {
|
2024-01-24 20:38:51 -05:00
|
|
|
stdout(npub)
|
2023-11-08 10:50:36 -05:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
exitIfLineProcessingError(ctx)
|
2023-11-08 10:50:36 -05:00
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "nsec",
|
|
|
|
Usage: "encode a hex private key into bech32 'nsec' format",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-03-19 10:34:59 -04:00
|
|
|
for target := range getStdinLinesOrArguments(c.Args()) {
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValid32ByteHex(target); !ok {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "invalid private key: %s", target)
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-07-08 19:52:50 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if npub, err := nip19.EncodePrivateKey(target); err == nil {
|
2024-01-24 20:38:51 -05:00
|
|
|
stdout(npub)
|
2023-11-08 10:50:36 -05:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
exitIfLineProcessingError(ctx)
|
2023-11-08 10:50:36 -05:00
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "nprofile",
|
|
|
|
Usage: "generate profile codes with attached relay information",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "relay",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "attach relay hints to nprofile code",
|
|
|
|
},
|
|
|
|
},
|
2024-08-07 10:46:07 -04:00
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-03-19 10:34:59 -04:00
|
|
|
for target := range getStdinLinesOrArguments(c.Args()) {
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValid32ByteHex(target); !ok {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "invalid public key: %s", target)
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-07-08 19:52:50 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
2024-06-06 14:38:40 -04:00
|
|
|
if err := normalizeAndValidateRelayURLs(relays); err != nil {
|
2023-11-08 10:50:36 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if npub, err := nip19.EncodeProfile(target, relays); err == nil {
|
2024-01-24 20:38:51 -05:00
|
|
|
stdout(npub)
|
2023-11-08 10:50:36 -05:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
exitIfLineProcessingError(ctx)
|
2023-11-08 10:50:36 -05:00
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "nevent",
|
|
|
|
Usage: "generate event codes with optionally attached relay information",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "relay",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "attach relay hints to nevent code",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2024-06-13 11:05:27 -04:00
|
|
|
Name: "author",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "attach an author pubkey as a hint to the nevent code",
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
2024-08-07 10:46:07 -04:00
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-03-19 10:34:59 -04:00
|
|
|
for target := range getStdinLinesOrArguments(c.Args()) {
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValid32ByteHex(target); !ok {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "invalid event id: %s", target)
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
author := c.String("author")
|
|
|
|
if author != "" {
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValidPublicKey(author); !ok {
|
|
|
|
return fmt.Errorf("invalid 'author' public key")
|
2023-11-08 10:50:36 -05:00
|
|
|
}
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
2024-06-06 14:38:40 -04:00
|
|
|
if err := normalizeAndValidateRelayURLs(relays); err != nil {
|
2023-07-08 19:52:50 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if npub, err := nip19.EncodeEvent(target, relays, author); err == nil {
|
2024-01-24 20:38:51 -05:00
|
|
|
stdout(npub)
|
2023-11-08 10:50:36 -05:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
exitIfLineProcessingError(ctx)
|
2023-11-08 10:50:36 -05:00
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "naddr",
|
|
|
|
Usage: "generate codes for NIP-33 parameterized replaceable events",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "identifier",
|
|
|
|
Aliases: []string{"d"},
|
2023-11-08 10:50:36 -05:00
|
|
|
Usage: "the \"d\" tag identifier of this replaceable event -- can also be read from stdin",
|
2023-07-05 14:03:26 -04:00
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pubkey",
|
|
|
|
Usage: "pubkey of the naddr author",
|
2024-06-13 11:05:27 -04:00
|
|
|
Aliases: []string{"author", "a", "p"},
|
2023-07-05 14:03:26 -04:00
|
|
|
Required: true,
|
|
|
|
},
|
2024-06-25 21:18:26 -04:00
|
|
|
&cli.IntFlag{
|
2023-07-05 14:03:26 -04:00
|
|
|
Name: "kind",
|
|
|
|
Aliases: []string{"k"},
|
|
|
|
Usage: "kind of referred replaceable event",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "relay",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "attach relay hints to naddr code",
|
|
|
|
},
|
|
|
|
},
|
2024-08-07 10:46:07 -04:00
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for d := range getStdinLinesOrBlank() {
|
|
|
|
pubkey := c.String("pubkey")
|
2024-01-21 05:45:22 -05:00
|
|
|
if ok := nostr.IsValidPublicKey(pubkey); !ok {
|
|
|
|
return fmt.Errorf("invalid 'pubkey'")
|
2023-11-08 10:50:36 -05:00
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
kind := c.Int("kind")
|
|
|
|
if kind < 30000 || kind >= 40000 {
|
|
|
|
return fmt.Errorf("kind must be between 30000 and 39999, as per NIP-16, got %d", kind)
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if d == "" {
|
|
|
|
d = c.String("identifier")
|
|
|
|
if d == "" {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "\"d\" tag identifier can't be empty")
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
2024-06-06 14:38:40 -04:00
|
|
|
if err := normalizeAndValidateRelayURLs(relays); err != nil {
|
2023-11-08 10:50:36 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
if npub, err := nip19.EncodeEntity(pubkey, int(kind), d, relays); err == nil {
|
2024-01-24 20:38:51 -05:00
|
|
|
stdout(npub)
|
2023-11-08 10:50:36 -05:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
exitIfLineProcessingError(ctx)
|
2023-11-08 10:50:36 -05:00
|
|
|
return nil
|
2023-10-10 10:28:17 -04:00
|
|
|
},
|
|
|
|
},
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
}
|