From e2db3b52b984d021b2d1b0266558d8ff8aba7bfd Mon Sep 17 00:00:00 2001 From: "Robert C. Martin" Date: Mon, 22 May 2023 08:57:26 -0500 Subject: [PATCH] WC Request works. WC Response is not being recieved. --- spec/more_speech/nostr/zaps_spec.clj | 12 +++--- src/more_speech/nostr/event_composers.clj | 9 +++-- src/more_speech/nostr/zaps.clj | 47 +++++++++++++++++------ 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/spec/more_speech/nostr/zaps_spec.clj b/spec/more_speech/nostr/zaps_spec.clj index c142f0f..df95b79 100644 --- a/spec/more_speech/nostr/zaps_spec.clj +++ b/spec/more_speech/nostr/zaps_spec.clj @@ -12,6 +12,7 @@ [more-speech.relay :as relay] [more-speech.util.fortune-messages :as fortune] [more-speech.websocket-relay :as ws-relay] + [org.bovinegenius.exploding-fish :as uri] [speclj.core :refer :all]) (:import (ecdhJava SECP256K1))) @@ -235,15 +236,16 @@ relay/open (stub :relay-open {:return "open-relay"}) relay/send (stub :relay-send) relay/close (stub :relay-close) - zaps/pay-invoice (stub :pay-invoice)] + zaps/get-wc-request-event (stub :request-event {:return "request-event"})] (let [info-promise (promise)] (deliver info-promise "pay_invoice") - (zaps/zap-by-wallet-connect :some-event info-promise) + (zaps/zap-by-wallet-connect "event-to-zap" info-promise) (should-have-invoked :relay-make {:with ["wc-relay-url" :*]}) (should-have-invoked :relay-open {:with ["some-relay"]}) (should-have-invoked :relay-send {:with ["open-relay" ["REQ" "ms-info" {"kinds" [13194], "authors" ["info-id"]}]]}) + (should-have-invoked :request-event {:with ["event-to-zap" :*]}) + (should-have-invoked :relay-send {:with ["open-relay" "request-event"]}) (should-have-invoked :relay-close {:with ["open-relay"]}) - (should-have-invoked :pay-invoice {:with [:some-event :*]}) ) ) ) @@ -252,7 +254,7 @@ (should= "{\"method\":\"pay_invoice\",\"params\":{\"invoice\":\"invoice\"}}" (zaps/make-wc-json-request "invoice"))) - (it "generates a wc request event" + (it "composes a wc request event" (with-redefs [config/proof-of-work-default 0] (let [sender-private-key-bytes (util/make-private-key) sender-private-key (util/bytes->num sender-private-key-bytes) @@ -269,7 +271,7 @@ sender-public-key) request "request" _ (set-mem :pubkey 1) - [_ event] (zaps/make-wc-request-event recipient-public-key-hex sender-private-key-hex request)] + [_ event] (zaps/compose-wc-request-event recipient-public-key-hex sender-private-key-hex request)] (should= 23194 (:kind event)) (should= [[:p recipient-public-key-hex]] (filter #(= :p (first %)) (:tags event))) (should= request (SECP256K1/decrypt inbound-shared-secret (:content event))) diff --git a/src/more_speech/nostr/event_composers.clj b/src/more_speech/nostr/event_composers.clj index d67b35c..53c2d47 100644 --- a/src/more_speech/nostr/event_composers.clj +++ b/src/more_speech/nostr/event_composers.clj @@ -18,13 +18,14 @@ which must include kind, tags, and content. The body is put into an EVENT wrapper that is ready to send." ([body] - (body->event body (get-mem [:keys :private-key]))) + (body->event body + (get-mem [:keys :private-key]) + (get-mem [:keys :public-key]))) - ([body private-key] + ([body private-key public-key] (let [private-key (util/hex-string->bytes private-key) - pubkey (get-mem :pubkey) now (util/get-now) - body (assoc body :pubkey (util/hexify pubkey) + body (assoc body :pubkey public-key :created_at now) [id body] (events/make-id-with-pow config/proof-of-work-default body) aux-rand (util/num->bytes 32 (biginteger (System/currentTimeMillis))) diff --git a/src/more_speech/nostr/zaps.clj b/src/more_speech/nostr/zaps.clj index 1935418..98c1c69 100644 --- a/src/more_speech/nostr/zaps.clj +++ b/src/more_speech/nostr/zaps.clj @@ -8,6 +8,7 @@ [more-speech.db.gateway :as gateway] [more-speech.logger.default :refer [log-pr]] [more-speech.mem :refer :all] + [more-speech.nostr.elliptic-signature :as es] [more-speech.nostr.event-composers :as composers] [more-speech.nostr.events :as events] [more-speech.nostr.relays :as relays] @@ -176,10 +177,16 @@ "EOSE" (do (relay/send relay ["CLOSE" "ms-info"])) "EVENT" (do + (prn 'get-wc-info 'EVENT (::ws-relay/url relay) msg) (let [event (nth msg 2) content (get event "content")] (when (not= -1 (.indexOf ^String content "pay_invoice")) (deliver promise "pay_invoice")))) + "OK" (do + (prn 'get-wc-info (::ws-relay/url relay) msg) + (when-not (nth msg 2) + (log-pr 1 'get-wc-info 'zap-request-failed (::ws-relay/url relay) msg))) + (do (log-pr 1 'get-wc 'unexpected (::ws-relay/url relay) msg))) ) @@ -191,10 +198,13 @@ (events/to-json {"method" "pay_invoice" "params" {"invoice" invoice}})) -(defn make-wc-request-event [wc-pubkey-hex secret-hex request] +(defn compose-wc-request-event [wc-pubkey-hex secret-hex request] (let [kind 23194 tags [[:p wc-pubkey-hex]] content request + secret-key-bytes (util/hex-string->bytes secret-hex) + secret-pubkey-bytes (es/get-pub-key secret-key-bytes) + secret-pubkey-hex (util/bytes->hex-string secret-pubkey-bytes) recipient-key (util/hex-string->num wc-pubkey-hex) sender-key (util/hex-string->num secret-hex) shared-secret (SECP256K1/calculateKeyAgreement sender-key recipient-key) @@ -202,16 +212,20 @@ body {:kind kind :tags tags :content encrypted-content} - event (composers/body->event body secret-hex)] + event (composers/body->event body + secret-hex + secret-pubkey-hex + )] event)) -(defn pay-invoice [event wc-uri] - (let [wc-pubkey-hex (:host wc-uri) - wc-map (uri/query-keys wc-uri) +(defn get-wc-request-event [event wc] + (let [wc-map (uri/query-map wc) + wc-uri (uri/uri wc) + wc-pubkey-hex (:host wc-uri) secret-hex (get wc-map "secret") invoice (get-zap-invoice event) - request (make-wc-json-request invoice) - request-event (make-wc-request-event wc-pubkey-hex secret-hex request)])) + request (make-wc-json-request invoice)] + (compose-wc-request-event wc-pubkey-hex secret-hex request))) (defn zap-by-wallet-connect ([event] @@ -230,23 +244,29 @@ open-relay (relay/open relay) _ (relay/send open-relay request) result (deref info-promise 10000 :timeout)] - (relay/close open-relay) + (cond (= result :timeout) - (log-pr 1 'zap-by-wallet-connect 'info-timeout relay-url) + (do + (log-pr 1 'zap-by-wallet-connect 'info-timeout relay-url) + (relay/close open-relay)) (= result "pay_invoice") - (pay-invoice event wc-uri) + (do + (relay/send open-relay (get-wc-request-event event wc)) + (Thread/sleep 2000) + (relay/close open-relay)) :else - (log-pr 1 'zap-by-wallet-connect 'unknown-result relay-url result) + (do (log-pr 1 'zap-by-wallet-connect 'unknown-result relay-url result) + (relay/close open-relay)) ) ) )) (defn zap-author [event _e] (if (some? (get-mem [:keys :wallet-connect])) - (zap-by-wallet-connect [event]) + (zap-by-wallet-connect event) (zap-by-invoice event))) (defn- get-fortune [] @@ -307,3 +327,6 @@ :amount sats :comment comment}) (update-mem :pending-zaps dissoc receipt-invoice))))) + +(defn process-zap-response [event] + (prn 'zap-response event))