Add signevent method to NIP-86 (nip86 + khatru)
Implements the signevent capability from nostr-protocol/nips#2389: relay admins can ask the relay to sign an unsigned event template with its own (self) key. - nip86: SignEvent MethodParams type + "signevent" decode case (round-trips the {kind, content, tags, created_at} param into a nostr.Event). - khatru: RelayManagementAPI.SignEvent hook + dispatch returning the signed event as the result.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package nip86
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
@@ -242,6 +243,25 @@ func DecodeRequest(req Request) (MethodParams, error) {
|
||||
}, nil
|
||||
case "stats":
|
||||
return Stats{}, nil
|
||||
case "signevent":
|
||||
if len(req.Params) == 0 {
|
||||
return nil, fmt.Errorf("invalid number of params for '%s'", req.Method)
|
||||
}
|
||||
|
||||
// params[0] is an unsigned event template {kind, content, tags,
|
||||
// created_at}; it arrives as a decoded JSON value, so round-trip it
|
||||
// through JSON into an Event.
|
||||
evtJSON, err := json.Marshal(req.Params[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid event param for '%s'", req.Method)
|
||||
}
|
||||
|
||||
var evt nostr.Event
|
||||
if err := json.Unmarshal(evtJSON, &evt); err != nil {
|
||||
return nil, fmt.Errorf("invalid event param for '%s'", req.Method)
|
||||
}
|
||||
|
||||
return SignEvent{Event: evt}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown method '%s'", req.Method)
|
||||
}
|
||||
@@ -277,6 +297,7 @@ var (
|
||||
_ MethodParams = (*GrantAdmin)(nil)
|
||||
_ MethodParams = (*RevokeAdmin)(nil)
|
||||
_ MethodParams = (*Stats)(nil)
|
||||
_ MethodParams = (*SignEvent)(nil)
|
||||
)
|
||||
|
||||
type SupportedMethods struct{}
|
||||
@@ -418,3 +439,11 @@ func (RevokeAdmin) MethodName() string { return "revokeadmin" }
|
||||
type Stats struct{}
|
||||
|
||||
func (Stats) MethodName() string { return "stats" }
|
||||
|
||||
// SignEvent asks the relay to sign an unsigned event template with its own
|
||||
// (self) key and return the full signed event. See NIP-86 `signevent`.
|
||||
type SignEvent struct {
|
||||
Event nostr.Event
|
||||
}
|
||||
|
||||
func (SignEvent) MethodName() string { return "signevent" }
|
||||
|
||||
Reference in New Issue
Block a user