mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-12-04 01:41:14 +01:00
nip-46 dynamic and static signers.
This commit is contained in:
79
nip46/nip46.go
Normal file
79
nip46/nip46.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package nip46
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip04"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
ID string `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params []string `json:"params"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
ID string `json:"id"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Result string `json:"result,omitempty"`
|
||||
}
|
||||
|
||||
type Signer interface {
|
||||
GetSession(clientPubkey string) (Session, bool)
|
||||
HandleRequest(event *nostr.Event) (req Request, resp Response, eventResponse nostr.Event, harmless bool, err error)
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
SharedKey []byte
|
||||
}
|
||||
|
||||
type RelayReadWrite struct {
|
||||
Read bool `json:"read"`
|
||||
Write bool `json:"write"`
|
||||
}
|
||||
|
||||
func (s Session) ParseRequest(event *nostr.Event) (Request, error) {
|
||||
var req Request
|
||||
|
||||
plain, err := nip04.Decrypt(event.Content, s.SharedKey)
|
||||
if err != nil {
|
||||
return req, fmt.Errorf("failed to decrypt event from %s: %w", event.PubKey, err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(plain), &req)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func (s Session) MakeResponse(
|
||||
id string,
|
||||
requester string,
|
||||
result string,
|
||||
err error,
|
||||
) (resp Response, evt nostr.Event, error error) {
|
||||
if err != nil {
|
||||
resp = Response{
|
||||
ID: id,
|
||||
Error: err.Error(),
|
||||
}
|
||||
} else if result != "" {
|
||||
resp = Response{
|
||||
ID: id,
|
||||
Result: result,
|
||||
}
|
||||
}
|
||||
|
||||
jresp, _ := json.Marshal(resp)
|
||||
ciphertext, err := nip04.Encrypt(string(jresp), s.SharedKey)
|
||||
if err != nil {
|
||||
return resp, evt, fmt.Errorf("failed to encrypt result: %w", err)
|
||||
}
|
||||
evt.Content = ciphertext
|
||||
|
||||
evt.CreatedAt = nostr.Now()
|
||||
evt.Kind = nostr.KindNostrConnect
|
||||
evt.Tags = nostr.Tags{nostr.Tag{"p", requester}}
|
||||
|
||||
return resp, evt, nil
|
||||
}
|
||||
Reference in New Issue
Block a user