mirror of
https://github.com/fiatjaf/nak.git
synced 2024-10-30 00:59:07 -04:00
fix panic (#12)
* c.Args().Len() - 1 might be negative value * fix getStdinLinesOrFirstArgument * fix getStdinLinesOrFirstArgument
This commit is contained in:
parent
0b9e861f90
commit
01e1f52a70
|
@ -33,7 +33,7 @@ var decode = &cli.Command{
|
|||
},
|
||||
ArgsUsage: "<npub | nprofile | nip05 | nevent | naddr | nsec>",
|
||||
Action: func(c *cli.Context) error {
|
||||
for input := range getStdinLinesOrFirstArgument(c) {
|
||||
for input := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
if strings.HasPrefix(input, "nostr:") {
|
||||
input = input[6:]
|
||||
}
|
||||
|
|
10
encode.go
10
encode.go
|
@ -29,7 +29,7 @@ var encode = &cli.Command{
|
|||
Name: "npub",
|
||||
Usage: "encode a hex public key into bech32 'npub' format",
|
||||
Action: func(c *cli.Context) error {
|
||||
for target := range getStdinLinesOrFirstArgument(c) {
|
||||
for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
if ok := nostr.IsValidPublicKey(target); !ok {
|
||||
lineProcessingError(c, "invalid public key: %s", target)
|
||||
continue
|
||||
|
@ -50,7 +50,7 @@ var encode = &cli.Command{
|
|||
Name: "nsec",
|
||||
Usage: "encode a hex private key into bech32 'nsec' format",
|
||||
Action: func(c *cli.Context) error {
|
||||
for target := range getStdinLinesOrFirstArgument(c) {
|
||||
for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
if ok := nostr.IsValid32ByteHex(target); !ok {
|
||||
lineProcessingError(c, "invalid private key: %s", target)
|
||||
continue
|
||||
|
@ -78,7 +78,7 @@ var encode = &cli.Command{
|
|||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
for target := range getStdinLinesOrFirstArgument(c) {
|
||||
for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
if ok := nostr.IsValid32ByteHex(target); !ok {
|
||||
lineProcessingError(c, "invalid public key: %s", target)
|
||||
continue
|
||||
|
@ -115,7 +115,7 @@ var encode = &cli.Command{
|
|||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
for target := range getStdinLinesOrFirstArgument(c) {
|
||||
for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
if ok := nostr.IsValid32ByteHex(target); !ok {
|
||||
lineProcessingError(c, "invalid event id: %s", target)
|
||||
continue
|
||||
|
@ -212,7 +212,7 @@ var encode = &cli.Command{
|
|||
Name: "note",
|
||||
Usage: "generate note1 event codes (not recommended)",
|
||||
Action: func(c *cli.Context) error {
|
||||
for target := range getStdinLinesOrFirstArgument(c) {
|
||||
for target := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
if ok := nostr.IsValid32ByteHex(target); !ok {
|
||||
lineProcessingError(c, "invalid event id: %s", target)
|
||||
continue
|
||||
|
|
2
fetch.go
2
fetch.go
|
@ -31,7 +31,7 @@ var fetch = &cli.Command{
|
|||
})
|
||||
}()
|
||||
|
||||
for code := range getStdinLinesOrFirstArgument(c) {
|
||||
for code := range getStdinLinesOrFirstArgument(c.Args().First()) {
|
||||
filter := nostr.Filter{}
|
||||
|
||||
prefix, value, err := nip19.Decode(code)
|
||||
|
|
|
@ -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
|
||||
target := c.Args().First()
|
||||
if target != "" {
|
||||
if arg != "" {
|
||||
single := make(chan string, 1)
|
||||
single <- target
|
||||
single <- arg
|
||||
close(single)
|
||||
return single
|
||||
}
|
||||
|
|
32
key.go
32
key.go
|
@ -39,7 +39,7 @@ var public = &cli.Command{
|
|||
Description: ``,
|
||||
ArgsUsage: "[secret]",
|
||||
Action: func(c *cli.Context) error {
|
||||
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c) {
|
||||
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c, c.Args().First()) {
|
||||
pubkey, err := nostr.GetPublicKey(sec)
|
||||
if err != nil {
|
||||
lineProcessingError(c, "failed to derive public key: %s", err)
|
||||
|
@ -65,11 +65,20 @@ var encrypt = &cli.Command{
|
|||
},
|
||||
},
|
||||
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 == "" {
|
||||
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)
|
||||
if err != nil {
|
||||
lineProcessingError(c, "failed to encrypt: %s", err)
|
||||
|
@ -87,11 +96,20 @@ var decrypt = &cli.Command{
|
|||
Description: `uses the NIP-49 standard.`,
|
||||
ArgsUsage: "<ncryptsec-code> <password>",
|
||||
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 == "" {
|
||||
return fmt.Errorf("no password given")
|
||||
}
|
||||
for ncryptsec := range getStdinLinesOrFirstArgument(c) {
|
||||
for ncryptsec := range getStdinLinesOrFirstArgument(content) {
|
||||
sec, err := nip49.Decrypt(ncryptsec, password)
|
||||
if err != nil {
|
||||
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)
|
||||
go func() {
|
||||
for sec := range getStdinLinesOrFirstArgument(c) {
|
||||
for sec := range getStdinLinesOrFirstArgument(content) {
|
||||
if sec == "" {
|
||||
continue
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user