From 7bce92f56d764e75befd234c41f734537778dee6 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 2 Nov 2023 08:10:13 -0300 Subject: [PATCH] nak verify --- README.md | 6 ++++++ main.go | 1 + verify.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 verify.go diff --git a/README.md b/README.md index bb8e68f..fa4ba53 100644 --- a/README.md +++ b/README.md @@ -70,3 +70,9 @@ publishing to wss://eden.nostr.land... failed: msg: blocked: not on white-list publishing to wss://atlas.nostr.land... failed: msg: blocked: not on white-list publishing to wss://relayable.org... success. ``` + +### verify if an event is good +```shell +~> echo '{"content":"hello world","created_at":1698923350,"id":"05bd99d54cb835f327e0092c4275ee44c7ff51219eff417c19f70c9e2c53ad5a","kind":1,"pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","sig":"0a04a296321ed933858577f36fb2fb9a0933e966f9ee32b539493f5a4d00120891b1ca9152ebfbc04fb403bdaa7c73f415e7c4954e55726b4b4fa8cebf008cd6","tags":[]}' | nak verify +invalid .id, expected 05bd99d54cb835f427e0092c4275ee44c7ff51219eff417c19f70c9e2c53ad5a, got 05bd99d54cb835f327e0092c4275ee44c7ff51219eff417c19f70c9e2c53ad5a +``` diff --git a/main.go b/main.go index 15551ca..d3c73c1 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { event, decode, encode, + verify, }, } diff --git a/verify.go b/verify.go new file mode 100644 index 0000000..b428b18 --- /dev/null +++ b/verify.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/json" + "fmt" + + "github.com/nbd-wtf/go-nostr" + "github.com/urfave/cli/v2" +) + +var verify = &cli.Command{ + Name: "verify", + Usage: "checks the hash and signature of an event given through stdin", + Description: `example: + 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. +`, + Action: func(c *cli.Context) error { + evt := nostr.Event{} + if stdinEvent := getStdin(); stdinEvent != "" { + if err := json.Unmarshal([]byte(stdinEvent), &evt); err != nil { + return fmt.Errorf("invalid JSON: %w", err) + } + } + + if evt.GetID() != evt.ID { + return fmt.Errorf("invalid .id, expected %s, got %s", evt.GetID(), evt.ID) + } + + if ok, err := evt.CheckSignature(); !ok { + return fmt.Errorf("invalid signature: %w", err) + } + + return nil + }, +}