nip46: make everything strings because pablo did it like that.
This commit is contained in:
@@ -14,13 +14,13 @@ import (
|
|||||||
type Request struct {
|
type Request struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Params []any `json:"params"`
|
Params []string `json:"params"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
Result any `json:"result,omitempty"`
|
Result string `json:"result,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
@@ -42,7 +42,7 @@ func (s Session) ParseRequest(event *nostr.Event) (Request, error) {
|
|||||||
func (s Session) MakeResponse(
|
func (s Session) MakeResponse(
|
||||||
id string,
|
id string,
|
||||||
requester string,
|
requester string,
|
||||||
result any,
|
result string,
|
||||||
err error,
|
err error,
|
||||||
) (resp Response, evt nostr.Event, error error) {
|
) (resp Response, evt nostr.Event, error error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -50,7 +50,7 @@ func (s Session) MakeResponse(
|
|||||||
ID: id,
|
ID: id,
|
||||||
Error: err.Error(),
|
Error: err.Error(),
|
||||||
}
|
}
|
||||||
} else if result != nil {
|
} else if result != "" {
|
||||||
resp = Response{
|
resp = Response{
|
||||||
ID: id,
|
ID: id,
|
||||||
Result: result,
|
Result: result,
|
||||||
@@ -139,12 +139,12 @@ func (p *Pool) HandleRequest(event *nostr.Event) (req Request, resp Response, ev
|
|||||||
return req, resp, eventResponse, false, fmt.Errorf("error parsing request: %w", err)
|
return req, resp, eventResponse, false, fmt.Errorf("error parsing request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var result any
|
var result string
|
||||||
var resultErr error
|
var resultErr error
|
||||||
|
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "connect":
|
case "connect":
|
||||||
result = map[string]any{}
|
result = "ack"
|
||||||
harmless = true
|
harmless = true
|
||||||
case "get_public_key":
|
case "get_public_key":
|
||||||
pubkey, err := nostr.GetPublicKey(p.secretKey)
|
pubkey, err := nostr.GetPublicKey(p.secretKey)
|
||||||
@@ -160,13 +160,8 @@ func (p *Pool) HandleRequest(event *nostr.Event) (req Request, resp Response, ev
|
|||||||
resultErr = fmt.Errorf("wrong number of arguments to 'sign_event'")
|
resultErr = fmt.Errorf("wrong number of arguments to 'sign_event'")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
jevt, err := json.Marshal(req.Params[0])
|
|
||||||
if err != nil {
|
|
||||||
resultErr = fmt.Errorf("failed to decode event/1: %w", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
evt := nostr.Event{}
|
evt := nostr.Event{}
|
||||||
err = easyjson.Unmarshal(jevt, &evt)
|
err = easyjson.Unmarshal([]byte(req.Params[0]), &evt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resultErr = fmt.Errorf("failed to decode event/2: %w", err)
|
resultErr = fmt.Errorf("failed to decode event/2: %w", err)
|
||||||
break
|
break
|
||||||
@@ -176,25 +171,23 @@ func (p *Pool) HandleRequest(event *nostr.Event) (req Request, resp Response, ev
|
|||||||
resultErr = fmt.Errorf("failed to sign event: %w", err)
|
resultErr = fmt.Errorf("failed to sign event: %w", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
result = evt
|
jrevt, _ := easyjson.Marshal(evt)
|
||||||
|
result = string(jrevt)
|
||||||
case "get_relays":
|
case "get_relays":
|
||||||
result = p.RelaysToAdvertise
|
jrelays, _ := json.Marshal(p.RelaysToAdvertise)
|
||||||
|
result = string(jrelays)
|
||||||
harmless = true
|
harmless = true
|
||||||
case "nip04_encrypt":
|
case "nip04_encrypt":
|
||||||
if len(req.Params) != 2 {
|
if len(req.Params) != 2 {
|
||||||
resultErr = fmt.Errorf("wrong number of arguments to 'nip04_encrypt'")
|
resultErr = fmt.Errorf("wrong number of arguments to 'nip04_encrypt'")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
thirdPartyPubkey, ok := req.Params[0].(string)
|
thirdPartyPubkey := req.Params[0]
|
||||||
if !ok || !nostr.IsValidPublicKeyHex(thirdPartyPubkey) {
|
if !nostr.IsValidPublicKeyHex(thirdPartyPubkey) {
|
||||||
resultErr = fmt.Errorf("first argument to 'nip04_encrypt' is not a pubkey string")
|
resultErr = fmt.Errorf("first argument to 'nip04_encrypt' is not a pubkey string")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
plaintext, ok := req.Params[1].(string)
|
plaintext := req.Params[1]
|
||||||
if !ok {
|
|
||||||
resultErr = fmt.Errorf("second argument to 'nip04_encrypt' is not a string")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
sharedSecret, err := nip04.ComputeSharedSecret(thirdPartyPubkey, p.secretKey)
|
sharedSecret, err := nip04.ComputeSharedSecret(thirdPartyPubkey, p.secretKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resultErr = fmt.Errorf("failed to compute shared secret: %w", err)
|
resultErr = fmt.Errorf("failed to compute shared secret: %w", err)
|
||||||
@@ -211,16 +204,12 @@ func (p *Pool) HandleRequest(event *nostr.Event) (req Request, resp Response, ev
|
|||||||
resultErr = fmt.Errorf("wrong number of arguments to 'nip04_decrypt'")
|
resultErr = fmt.Errorf("wrong number of arguments to 'nip04_decrypt'")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
thirdPartyPubkey, ok := req.Params[0].(string)
|
thirdPartyPubkey := req.Params[0]
|
||||||
if !ok || !nostr.IsValidPublicKeyHex(thirdPartyPubkey) {
|
if !nostr.IsValidPublicKeyHex(thirdPartyPubkey) {
|
||||||
resultErr = fmt.Errorf("first argument to 'nip04_decrypt' is not a pubkey string")
|
resultErr = fmt.Errorf("first argument to 'nip04_decrypt' is not a pubkey string")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
ciphertext, ok := req.Params[1].(string)
|
ciphertext := req.Params[1]
|
||||||
if !ok {
|
|
||||||
resultErr = fmt.Errorf("second argument to 'nip04_decrypt' is not a string")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
sharedSecret, err := nip04.ComputeSharedSecret(thirdPartyPubkey, p.secretKey)
|
sharedSecret, err := nip04.ComputeSharedSecret(thirdPartyPubkey, p.secretKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resultErr = fmt.Errorf("failed to compute shared secret: %w", err)
|
resultErr = fmt.Errorf("failed to compute shared secret: %w", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user