2023-11-02 07:10:13 -04:00
package main
import (
2024-06-25 21:18:26 -04:00
"context"
2023-11-02 07:10:13 -04:00
"encoding/json"
2024-07-23 14:23:07 -04:00
"github.com/fiatjaf/cli/v3"
2024-07-30 10:43:14 -04:00
"github.com/nbd-wtf/go-nostr"
2023-11-02 07:10:13 -04:00
)
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
2023-11-20 13:01:51 -05:00
it outputs nothing if the verification is successful . ` ,
2024-08-07 10:46:07 -04:00
DisableSliceFlagSeparator : true ,
2024-06-25 21:18:26 -04:00
Action : func ( ctx context . Context , c * cli . Command ) error {
2024-03-19 10:34:59 -04:00
for stdinEvent := range getStdinLinesOrArguments ( c . Args ( ) ) {
2023-11-07 15:57:43 -05:00
evt := nostr . Event { }
if stdinEvent != "" {
if err := json . Unmarshal ( [ ] byte ( stdinEvent ) , & evt ) ; err != nil {
2024-07-11 14:33:19 -04:00
ctx = lineProcessingError ( ctx , "invalid event: %s" , err )
2023-11-07 15:57:43 -05:00
continue
}
2023-11-02 07:10:13 -04:00
}
2023-11-07 15:57:43 -05:00
if evt . GetID ( ) != evt . ID {
2024-07-11 14:33:19 -04:00
ctx = lineProcessingError ( ctx , "invalid .id, expected %s, got %s" , evt . GetID ( ) , evt . ID )
2023-11-07 15:57:43 -05:00
continue
}
2023-11-02 07:10:13 -04:00
2023-11-07 15:57:43 -05:00
if ok , err := evt . CheckSignature ( ) ; ! ok {
2024-07-11 14:33:19 -04:00
ctx = lineProcessingError ( ctx , "invalid signature: %s" , err )
2023-11-07 15:57:43 -05:00
continue
}
2023-11-02 07:10:13 -04:00
}
2024-06-25 21:18:26 -04:00
exitIfLineProcessingError ( ctx )
2023-11-02 07:10:13 -04:00
return nil
} ,
}