2023-07-05 14:03:26 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
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>`,
|
|
|
|
Before: func(c *cli.Context) 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
|
|
|
},
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "npub",
|
2023-11-08 10:50:36 -05:00
|
|
|
Usage: "encode a hex public key into bech32 'npub' format",
|
2023-07-05 14:03:26 -04:00
|
|
|
Action: func(c *cli.Context) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for target := range getStdinLinesOrFirstArgument(c) {
|
|
|
|
if err := validate32BytesHex(target); err != nil {
|
|
|
|
lineProcessingError(c, "invalid public key: %s", target, err)
|
|
|
|
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 {
|
|
|
|
fmt.Println(npub)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
|
|
|
exitIfLineProcessingError(c)
|
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "nsec",
|
|
|
|
Usage: "encode a hex private key into bech32 'nsec' format",
|
|
|
|
Action: func(c *cli.Context) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for target := range getStdinLinesOrFirstArgument(c) {
|
|
|
|
if err := validate32BytesHex(target); err != nil {
|
|
|
|
lineProcessingError(c, "invalid private key: %s", target, err)
|
|
|
|
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 {
|
|
|
|
fmt.Println(npub)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
|
|
|
exitIfLineProcessingError(c)
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for target := range getStdinLinesOrFirstArgument(c) {
|
|
|
|
if err := validate32BytesHex(target); err != nil {
|
|
|
|
lineProcessingError(c, "invalid public key: %s", target, err)
|
|
|
|
continue
|
|
|
|
}
|
2023-07-08 19:52:50 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
|
|
|
if err := validateRelayURLs(relays); err != nil {
|
|
|
|
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 {
|
|
|
|
fmt.Println(npub)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
|
|
|
exitIfLineProcessingError(c)
|
|
|
|
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{
|
|
|
|
Name: "author",
|
|
|
|
Usage: "attach an author pubkey as a hint to the nevent code",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for target := range getStdinLinesOrFirstArgument(c) {
|
|
|
|
if err := validate32BytesHex(target); err != nil {
|
|
|
|
lineProcessingError(c, "invalid event id: %s", target, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
author := c.String("author")
|
|
|
|
if author != "" {
|
|
|
|
if err := validate32BytesHex(author); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
|
|
|
if err := validateRelayURLs(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 {
|
|
|
|
fmt.Println(npub)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
exitIfLineProcessingError(c)
|
|
|
|
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",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.Int64Flag{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for d := range getStdinLinesOrBlank() {
|
|
|
|
pubkey := c.String("pubkey")
|
|
|
|
if err := validate32BytesHex(pubkey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 == "" {
|
|
|
|
lineProcessingError(c, "\"d\" tag identifier can't be empty")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
|
|
|
if err := validateRelayURLs(relays); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if npub, err := nip19.EncodeEntity(pubkey, kind, d, relays); err == nil {
|
|
|
|
fmt.Println(npub)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:03:26 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
|
|
|
exitIfLineProcessingError(c)
|
|
|
|
return nil
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
},
|
2023-10-10 10:28:17 -04:00
|
|
|
{
|
|
|
|
Name: "note",
|
|
|
|
Usage: "generate note1 event codes (not recommended)",
|
|
|
|
Action: func(c *cli.Context) error {
|
2023-11-08 10:50:36 -05:00
|
|
|
for target := range getStdinLinesOrFirstArgument(c) {
|
|
|
|
if err := validate32BytesHex(target); err != nil {
|
|
|
|
lineProcessingError(c, "invalid event id: %s", target, err)
|
|
|
|
continue
|
|
|
|
}
|
2023-10-10 10:28:17 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if note, err := nip19.EncodeNote(target); err == nil {
|
|
|
|
fmt.Println(note)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-10 10:28:17 -04:00
|
|
|
}
|
2023-11-08 10:50:36 -05:00
|
|
|
|
|
|
|
exitIfLineProcessingError(c)
|
|
|
|
return nil
|
2023-10-10 10:28:17 -04:00
|
|
|
},
|
|
|
|
},
|
2023-07-05 14:03:26 -04:00
|
|
|
},
|
|
|
|
}
|