mirror of https://github.com/fiatjaf/nak.git
verify: better handling of stdout and verbose logging output.
fixes: https://github.com/fiatjaf/nak/issues/74
This commit is contained in:
parent
ff02e6890b
commit
7c58948924
20
verify.go
20
verify.go
|
@ -9,31 +9,41 @@ import (
|
||||||
|
|
||||||
var verify = &cli.Command{
|
var verify = &cli.Command{
|
||||||
Name: "verify",
|
Name: "verify",
|
||||||
Usage: "checks the hash and signature of an event given through stdin",
|
Usage: "checks the hash and signature of an event given through stdin or as the first argument",
|
||||||
Description: `example:
|
Description: `example:
|
||||||
echo '{"id":"a889df6a387419ff204305f4c2d296ee328c3cd4f8b62f205648a541b4554dfb","pubkey":"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5","created_at":1698623783,"kind":1,"tags":[],"content":"hello from the nostr army knife","sig":"84876e1ee3e726da84e5d195eb79358b2b3eaa4d9bd38456fde3e8a2af3f1cd4cda23f23fda454869975b3688797d4c66e12f4c51c1b43c6d2997c5e61865661"}' | nak verify
|
echo '{"id":"a889df6a387419ff204305f4c2d296ee328c3cd4f8b62f205648a541b4554dfb","pubkey":"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5","created_at":1698623783,"kind":1,"tags":[],"content":"hello from the nostr army knife","sig":"84876e1ee3e726da84e5d195eb79358b2b3eaa4d9bd38456fde3e8a2af3f1cd4cda23f23fda454869975b3688797d4c66e12f4c51c1b43c6d2997c5e61865661"}' | nak verify
|
||||||
|
|
||||||
it outputs nothing if the verification is successful.`,
|
it outputs nothing if the verification is successful.`,
|
||||||
DisableSliceFlagSeparator: true,
|
DisableSliceFlagSeparator: true,
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
Action: func(ctx context.Context, c *cli.Command) error {
|
||||||
for stdinEvent := range getStdinLinesOrArguments(c.Args()) {
|
for stdinEvent := range getJsonsOrBlank() {
|
||||||
evt := nostr.Event{}
|
evt := nostr.Event{}
|
||||||
if stdinEvent != "" {
|
if stdinEvent == "" {
|
||||||
if err := json.Unmarshal([]byte(stdinEvent), &evt); err != nil {
|
stdinEvent = c.Args().First()
|
||||||
ctx = lineProcessingError(ctx, "invalid event: %s", err)
|
if stdinEvent == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(stdinEvent), &evt); err != nil {
|
||||||
|
ctx = lineProcessingError(ctx, "invalid event: %s", err)
|
||||||
|
logverbose("<>: invalid event.\n", evt.ID.Hex())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if evt.GetID() != evt.ID {
|
if evt.GetID() != evt.ID {
|
||||||
ctx = lineProcessingError(ctx, "invalid .id, expected %s, got %s", evt.GetID(), evt.ID)
|
ctx = lineProcessingError(ctx, "invalid .id, expected %s, got %s", evt.GetID(), evt.ID)
|
||||||
|
logverbose("%s: invalid id.\n", evt.ID.Hex())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !evt.VerifySignature() {
|
if !evt.VerifySignature() {
|
||||||
ctx = lineProcessingError(ctx, "invalid signature")
|
ctx = lineProcessingError(ctx, "invalid signature")
|
||||||
|
logverbose("%s: invalid signature.\n", evt.ID.Hex())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logverbose("%s: valid.\n", evt.ID.Hex())
|
||||||
}
|
}
|
||||||
|
|
||||||
exitIfLineProcessingError(ctx)
|
exitIfLineProcessingError(ctx)
|
||||||
|
|
Loading…
Reference in New Issue