fix panic (#12)

* c.Args().Len() - 1 might be negative value

* fix getStdinLinesOrFirstArgument

* fix getStdinLinesOrFirstArgument
This commit is contained in:
mattn 2024-02-03 22:03:32 +09:00 committed by GitHub
parent 0b9e861f90
commit 01e1f52a70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 18 deletions

View File

@ -33,7 +33,7 @@ var decode = &cli.Command{
}, },
ArgsUsage: "<npub | nprofile | nip05 | nevent | naddr | nsec>", ArgsUsage: "<npub | nprofile | nip05 | nevent | naddr | nsec>",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
for input := range getStdinLinesOrFirstArgument(c) { for input := range getStdinLinesOrFirstArgument(c.Args().First()) {
if strings.HasPrefix(input, "nostr:") { if strings.HasPrefix(input, "nostr:") {
input = input[6:] input = input[6:]
} }

View File

@ -29,7 +29,7 @@ var encode = &cli.Command{
Name: "npub", Name: "npub",
Usage: "encode a hex public key into bech32 'npub' format", Usage: "encode a hex public key into bech32 'npub' format",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
for target := range getStdinLinesOrFirstArgument(c) { for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
if ok := nostr.IsValidPublicKey(target); !ok { if ok := nostr.IsValidPublicKey(target); !ok {
lineProcessingError(c, "invalid public key: %s", target) lineProcessingError(c, "invalid public key: %s", target)
continue continue
@ -50,7 +50,7 @@ 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 {
for target := range getStdinLinesOrFirstArgument(c) { for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
if ok := nostr.IsValid32ByteHex(target); !ok { if ok := nostr.IsValid32ByteHex(target); !ok {
lineProcessingError(c, "invalid private key: %s", target) lineProcessingError(c, "invalid private key: %s", target)
continue continue
@ -78,7 +78,7 @@ var encode = &cli.Command{
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
for target := range getStdinLinesOrFirstArgument(c) { for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
if ok := nostr.IsValid32ByteHex(target); !ok { if ok := nostr.IsValid32ByteHex(target); !ok {
lineProcessingError(c, "invalid public key: %s", target) lineProcessingError(c, "invalid public key: %s", target)
continue continue
@ -115,7 +115,7 @@ var encode = &cli.Command{
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
for target := range getStdinLinesOrFirstArgument(c) { for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
if ok := nostr.IsValid32ByteHex(target); !ok { if ok := nostr.IsValid32ByteHex(target); !ok {
lineProcessingError(c, "invalid event id: %s", target) lineProcessingError(c, "invalid event id: %s", target)
continue continue
@ -212,7 +212,7 @@ var encode = &cli.Command{
Name: "note", Name: "note",
Usage: "generate note1 event codes (not recommended)", Usage: "generate note1 event codes (not recommended)",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
for target := range getStdinLinesOrFirstArgument(c) { for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
if ok := nostr.IsValid32ByteHex(target); !ok { if ok := nostr.IsValid32ByteHex(target); !ok {
lineProcessingError(c, "invalid event id: %s", target) lineProcessingError(c, "invalid event id: %s", target)
continue continue

View File

@ -31,7 +31,7 @@ var fetch = &cli.Command{
}) })
}() }()
for code := range getStdinLinesOrFirstArgument(c) { for code := range getStdinLinesOrFirstArgument(c.Args().First()) {
filter := nostr.Filter{} filter := nostr.Filter{}
prefix, value, err := nip19.Decode(code) prefix, value, err := nip19.Decode(code)

View File

@ -47,12 +47,11 @@ func getStdinLinesOrBlank() chan string {
} }
} }
func getStdinLinesOrFirstArgument(c *cli.Context) chan string { func getStdinLinesOrFirstArgument(arg string) chan string {
// try the first argument // try the first argument
target := c.Args().First() if arg != "" {
if target != "" {
single := make(chan string, 1) single := make(chan string, 1)
single <- target single <- arg
close(single) close(single)
return single return single
} }

32
key.go
View File

@ -39,7 +39,7 @@ var public = &cli.Command{
Description: ``, Description: ``,
ArgsUsage: "[secret]", ArgsUsage: "[secret]",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c) { for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c, c.Args().First()) {
pubkey, err := nostr.GetPublicKey(sec) pubkey, err := nostr.GetPublicKey(sec)
if err != nil { if err != nil {
lineProcessingError(c, "failed to derive public key: %s", err) lineProcessingError(c, "failed to derive public key: %s", err)
@ -65,11 +65,20 @@ var encrypt = &cli.Command{
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
password := c.Args().Get(c.Args().Len() - 1) var content string
var password string
switch c.Args().Len() {
case 1:
content = ""
password = c.Args().Get(0)
case 2:
content = c.Args().Get(0)
password = c.Args().Get(1)
}
if password == "" { if password == "" {
return fmt.Errorf("no password given") return fmt.Errorf("no password given")
} }
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c) { for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c, content) {
ncryptsec, err := nip49.Encrypt(sec, password, uint8(c.Int("logn")), 0x02) ncryptsec, err := nip49.Encrypt(sec, password, uint8(c.Int("logn")), 0x02)
if err != nil { if err != nil {
lineProcessingError(c, "failed to encrypt: %s", err) lineProcessingError(c, "failed to encrypt: %s", err)
@ -87,11 +96,20 @@ var decrypt = &cli.Command{
Description: `uses the NIP-49 standard.`, Description: `uses the NIP-49 standard.`,
ArgsUsage: "<ncryptsec-code> <password>", ArgsUsage: "<ncryptsec-code> <password>",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
password := c.Args().Get(c.Args().Len() - 1) var content string
var password string
switch c.Args().Len() {
case 1:
content = ""
password = c.Args().Get(0)
case 2:
content = c.Args().Get(0)
password = c.Args().Get(1)
}
if password == "" { if password == "" {
return fmt.Errorf("no password given") return fmt.Errorf("no password given")
} }
for ncryptsec := range getStdinLinesOrFirstArgument(c) { for ncryptsec := range getStdinLinesOrFirstArgument(content) {
sec, err := nip49.Decrypt(ncryptsec, password) sec, err := nip49.Decrypt(ncryptsec, password)
if err != nil { if err != nil {
lineProcessingError(c, "failed to decrypt: %s", err) lineProcessingError(c, "failed to decrypt: %s", err)
@ -104,10 +122,10 @@ var decrypt = &cli.Command{
}, },
} }
func getSecretKeyFromStdinLinesOrFirstArgument(c *cli.Context) chan string { func getSecretKeyFromStdinLinesOrFirstArgument(c *cli.Context, content string) chan string {
ch := make(chan string) ch := make(chan string)
go func() { go func() {
for sec := range getStdinLinesOrFirstArgument(c) { for sec := range getStdinLinesOrFirstArgument(content) {
if sec == "" { if sec == "" {
continue continue
} }