mirror of
https://github.com/fiatjaf/nak.git
synced 2024-10-30 09:09:08 -04:00
do target validation on a case-by-case basis and don't validate empty -author on nevent.
This commit is contained in:
parent
fb7c49bb5c
commit
f9cf01b48b
40
encode.go
40
encode.go
|
@ -21,27 +21,22 @@ var encode = &cli.Command{
|
||||||
nak encode nevent --author <pubkey-hex> --relay <relay-url> --relay <other-relay> <event-id>
|
nak encode nevent --author <pubkey-hex> --relay <relay-url> --relay <other-relay> <event-id>
|
||||||
nak encode nsec <privkey-hex>`,
|
nak encode nsec <privkey-hex>`,
|
||||||
Before: func(c *cli.Context) error {
|
Before: func(c *cli.Context) error {
|
||||||
if c.Args().First() == "naddr" {
|
|
||||||
// validation will be done on the specific handler
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.Args().Len() < 2 {
|
if c.Args().Len() < 2 {
|
||||||
return fmt.Errorf("expected more than 2 arguments.")
|
return fmt.Errorf("expected more than 2 arguments.")
|
||||||
}
|
}
|
||||||
target := c.Args().Get(c.Args().Len() - 1)
|
|
||||||
if target == "--help" {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
return validate32BytesHex(target)
|
|
||||||
},
|
},
|
||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
{
|
{
|
||||||
Name: "npub",
|
Name: "npub",
|
||||||
Usage: "encode a hex private key into bech32 'npub' format",
|
Usage: "encode a hex private key into bech32 'npub' format",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if npub, err := nip19.EncodePublicKey(c.Args().First()); err == nil {
|
target := c.Args().First()
|
||||||
|
if err := validate32BytesHex(target); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if npub, err := nip19.EncodePublicKey(target); err == nil {
|
||||||
fmt.Println(npub)
|
fmt.Println(npub)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -53,7 +48,12 @@ var encode = &cli.Command{
|
||||||
Name: "nsec",
|
Name: "nsec",
|
||||||
Usage: "encode a hex private key into bech32 'nsec' format",
|
Usage: "encode a hex private key into bech32 'nsec' format",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if npub, err := nip19.EncodePrivateKey(c.Args().First()); err == nil {
|
target := c.Args().First()
|
||||||
|
if err := validate32BytesHex(target); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if npub, err := nip19.EncodePrivateKey(target); err == nil {
|
||||||
fmt.Println(npub)
|
fmt.Println(npub)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -72,12 +72,17 @@ var encode = &cli.Command{
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
target := c.Args().First()
|
||||||
|
if err := validate32BytesHex(target); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
relays := c.StringSlice("relay")
|
relays := c.StringSlice("relay")
|
||||||
if err := validateRelayURLs(relays); err != nil {
|
if err := validateRelayURLs(relays); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if npub, err := nip19.EncodeProfile(c.Args().First(), relays); err == nil {
|
if npub, err := nip19.EncodeProfile(target, relays); err == nil {
|
||||||
fmt.Println(npub)
|
fmt.Println(npub)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,17 +105,24 @@ var encode = &cli.Command{
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
target := c.Args().First()
|
||||||
|
if err := validate32BytesHex(target); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
author := c.String("author")
|
author := c.String("author")
|
||||||
|
if author != "" {
|
||||||
if err := validate32BytesHex(author); err != nil {
|
if err := validate32BytesHex(author); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
relays := c.StringSlice("relay")
|
relays := c.StringSlice("relay")
|
||||||
if err := validateRelayURLs(relays); err != nil {
|
if err := validateRelayURLs(relays); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if npub, err := nip19.EncodeEvent(c.Args().First(), relays, author); err == nil {
|
if npub, err := nip19.EncodeEvent(target, relays, author); err == nil {
|
||||||
fmt.Println(npub)
|
fmt.Println(npub)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user