First interaction (get info) with WC relay is working.

This commit is contained in:
Robert C. Martin 2023-05-20 09:21:02 -05:00
parent 655bf5eee0
commit 95435ae64a
8 changed files with 162 additions and 40 deletions

View File

@ -14,6 +14,7 @@
[com.xtdb/xtdb-rocksdb "1.23.1"]
[org.slf4j/slf4j-nop "1.7.30"] ;1.7.30 works.
[clj-http "3.12.3"]
[org.bovinegenius/exploding-fish "0.3.6"]
[org.clojure/test.check "1.1.1"]]
:profiles {:dev {:dependencies [[speclj "3.4.3"]]}
:uberjar {:aot :all}}

View File

@ -323,12 +323,15 @@
hex-private-key (util/hexify (util/bytes->num bytes-private-key))
bytes-public-key (es/get-pub-key bytes-private-key)
public-key (util/bytes->num bytes-public-key)
hex-public-key (util/hexify public-key)]
hex-public-key (util/hexify public-key)
wallet-connect "wallet-connect-string"]
(spit @config/keys-filename {:private-key hex-private-key
:public-key hex-public-key})
:public-key hex-public-key
:wallet-connect wallet-connect})
(data-storage/read-keys)
(should= hex-public-key (mem/get-mem [:keys :public-key]))
(should= hex-private-key (mem/get-mem [:keys :private-key]))
(should= wallet-connect (mem/get-mem [:keys :wallet-connect]))
(should= nil (mem/get-mem [:keys :password]))
(should= public-key (mem/get-mem :pubkey))))
@ -342,14 +345,19 @@
encoded-private-key (->> hex-private-key
(util/xor-string "password")
(bech32/encode-str "encoded"))
]
wallet-connect "wallet-connect-string"
encoded-wallet-connect (->> wallet-connect
(util/xor-string "password")
(bech32/encode-str "encoded"))]
(spit @config/keys-filename {:private-key encoded-private-key
:public-key hex-public-key
:password password})
:password password
:wallet-connect encoded-wallet-connect})
(data-storage/read-keys)
(should= hex-public-key (mem/get-mem [:keys :public-key]))
(should= hex-private-key (mem/get-mem [:keys :private-key]))
(should= wallet-connect (mem/get-mem [:keys :wallet-connect]))
(should= "password" (mem/get-mem [:keys :password]))
(should= public-key (mem/get-mem :pubkey))))
@ -358,11 +366,14 @@
hex-private-key (util/hexify (util/bytes->num bytes-private-key))
bytes-public-key (es/get-pub-key bytes-private-key)
public-key (util/bytes->num bytes-public-key)
hex-public-key (util/hexify public-key)]
hex-public-key (util/hexify public-key)
wallet-connect "wallet-connect-string"]
(data-storage/write-keys {:private-key hex-private-key
:public-key hex-public-key})
:public-key hex-public-key
:wallet-connect wallet-connect})
(should= {:private-key hex-private-key
:public-key hex-public-key
:wallet-connect wallet-connect
:password nil}
(read-string (slurp @config/keys-filename)))))
@ -374,11 +385,17 @@
hex-public-key (util/hexify public-key)
encoded-private-key (->> hex-private-key
(util/xor-string "password")
(bech32/encode-str "encoded"))]
(bech32/encode-str "encoded"))
wallet-connect "wallet-connect-string"
encoded-wallet-connect (->> wallet-connect
(util/xor-string "password")
(bech32/encode-str "encoded"))]
(data-storage/write-keys {:private-key hex-private-key
:public-key hex-public-key
:wallet-connect wallet-connect
:password "password"})
(should= {:private-key encoded-private-key
:wallet-connect encoded-wallet-connect
:public-key hex-public-key
:password (bech32/encode-str "pw" "password")}
(read-string (slurp @config/keys-filename)))))

View File

@ -9,7 +9,9 @@
[more-speech.nostr.event-composers :as composers]
[more-speech.nostr.util :as util]
[more-speech.nostr.zaps :as zaps]
[more-speech.relay :as relay]
[more-speech.util.fortune-messages :as fortune]
[more-speech.websocket-relay :as ws-relay]
[speclj.core :refer :all]))
(declare db)
@ -56,11 +58,11 @@
(zaps/get-lnurl event))))
(it "gets lud06 zap addr from profile"
(let [event {:pubkey 1}
lnurl (bech32/encode-str "lnurl" "the-lnurl")]
(gateway/add-profile @db 1 {:name "somebody"
:lud06 lnurl})
(should= "the-lnurl" (zaps/get-lnurl event))))
(let [event {:pubkey 1}
lnurl (bech32/encode-str "lnurl" "the-lnurl")]
(gateway/add-profile @db 1 {:name "somebody"
:lud06 lnurl})
(should= "the-lnurl" (zaps/get-lnurl event))))
)
(context "lud16 parsing"
@ -206,22 +208,41 @@
(should-have-invoked :send {:with [nil "Auto Thanks" "@zapper Thank you!\n"]}))))
(it "dms thanks for a zap when auto-thanks is :dm"
(with-redefs [composers/compose-and-send-text-event (stub :send)
config/auto-thanks :dm
config/auto-thanks-fortune :off]
(let [zapper-id (rand-int 1000000)]
(gateway/add-profile @db zapper-id {:name "zapper"})
(zaps/auto-thanks zapper-id)
(should-have-invoked :send {:with [nil "Auto Thanks" "D @zapper Thank you!\n"]}))))
(with-redefs [composers/compose-and-send-text-event (stub :send)
config/auto-thanks :dm
config/auto-thanks-fortune :off]
(let [zapper-id (rand-int 1000000)]
(gateway/add-profile @db zapper-id {:name "zapper"})
(zaps/auto-thanks zapper-id)
(should-have-invoked :send {:with [nil "Auto Thanks" "D @zapper Thank you!\n"]}))))
(it "sends thanks for a zap with a fortune"
(with-redefs [composers/compose-and-send-text-event (stub :send)
fortune/get-message (stub :get-message {:return "hi"})
config/auto-thanks :on
config/auto-thanks-fortune :normal]
(let [zapper-id (rand-int 1000000)]
(gateway/add-profile @db zapper-id {:name "zapper"})
(zaps/auto-thanks zapper-id)
(should-have-invoked :send {:with [nil "Auto Thanks" "@zapper Thank you!\nhi"]}))))
(with-redefs [composers/compose-and-send-text-event (stub :send)
fortune/get-message (stub :get-message {:return "hi"})
config/auto-thanks :on
config/auto-thanks-fortune :normal]
(let [zapper-id (rand-int 1000000)]
(gateway/add-profile @db zapper-id {:name "zapper"})
(zaps/auto-thanks zapper-id)
(should-have-invoked :send {:with [nil "Auto Thanks" "@zapper Thank you!\nhi"]}))))
)
(context "wallet-connect"
(it "sends the info request"
(set-mem [:keys :wallet-connect] "nostrwalletconnect://info-id?relay=wc-relay-url")
(with-redefs [ws-relay/make (stub :relay-make {:return "some-relay"})
relay/open (stub :relay-open {:return "open-relay"})
relay/send (stub :relay-send)
relay/close (stub :relay-close)]
(let [info-promise (promise)]
(deliver info-promise "pay_invoice")
(zaps/zap-by-wallet-connect :some-event 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 :relay-close {:with ["open-relay"]})
)
)
)
)
)

View File

@ -26,6 +26,7 @@
(defn write-keys [keys]
(let [private-key (:private-key keys)
wallet-connect (:wallet-connect keys)
password (:password keys)
encoded-password (if (empty? password)
password
@ -35,12 +36,20 @@
(->> private-key
(util/xor-string password)
(bech32/encode-str "encoded")))
wallet-connect (if (some? wallet-connect)
(if (empty? password)
wallet-connect
(->> wallet-connect
(util/xor-string password)
(bech32/encode-str "encoded")))
nil)
keys (assoc keys :private-key private-key
:password encoded-password)
:password encoded-password
:wallet-connect wallet-connect)
keys-string (with-out-str (clojure.pprint/pprint keys))]
(if (config/is-test-run?)
(log-pr 2 `write-keys (if (empty? password)
(dissoc keys :private-key)
(dissoc keys :private-key :wallet-connect)
keys))
(spit @config/keys-filename keys-string))))
@ -84,9 +93,15 @@
private-key (if (some? pw)
(util/xor-string pw (bech32/address->str private-key))
private-key)
]
wallet-connect (:wallet-connect keys)
wallet-connect (if (some? wallet-connect)
(if (some? pw)
(util/xor-string pw (bech32/address->str wallet-connect))
wallet-connect)
nil)]
(set-mem :keys (assoc keys :private-key private-key
:password pw))
:password pw
:wallet-connect wallet-connect))
(set-mem :pubkey pubkey)
)
)

View File

@ -219,8 +219,8 @@
:max-time max-time
:event-counter event-counter)))
(defn handle-relay-message [url message]
(let [relay (:connection (get @relays url))
(defn handle-relay-message [relay message]
(let [url (::ws-relay/url relay)
[type id event] message]
(cond

View File

@ -12,9 +12,13 @@
[more-speech.nostr.events :as events]
[more-speech.nostr.relays :as relays]
[more-speech.nostr.util :as util]
[more-speech.relay :as relay]
[more-speech.ui.formatter-util :as formatter-util]
[more-speech.ui.formatters :as formatters]
[more-speech.util.fortune-messages :as fortune])
[more-speech.util.fortune-messages :as fortune]
[more-speech.websocket-relay :as ws-relay]
[org.bovinegenius.exploding-fish :as uri]
)
(:use (seesaw [core]))
(:import (java.net URLEncoder)))
@ -66,7 +70,7 @@
:else
(throw (Exception. "no zap tag or profile"))
)))
)))
(defn get-lnurl [event]
(let [zap-address (get-lnurl-from-tag event)]
@ -123,7 +127,7 @@
:cancel-fn (fn [_p] nil))]
(show! (pack! zap-dialog))))
(defn zap-author [event _e]
(defn zap-by-invoice [event]
(try
(let [lnurl (get-lnurl event)
ln-response (client/get lnurl)
@ -163,6 +167,59 @@
(when (not= "cancel" (.getMessage e))
(alert (str "Cannot zap. " (.getMessage e)))))))
(defn get-wc-info [promise relay msg]
(condp = (first msg)
"EOSE" (do
(relay/send relay ["CLOSE" "ms-info"]))
"EVENT" (do
(let [event (nth msg 2)
content (get event "content")]
(when (not= -1 (.indexOf ^String content "pay_invoice"))
(deliver promise "pay_invoice"))))
(do
(log-pr 1 'get-wc 'unexpected (::ws-relay/url relay) msg)))
)
(defn wc-close [relay]
(prn 'wc-close relay))
(defn pay-invoice [event wc-uri])
(defn zap-by-wallet-connect
([event]
(zap-by-wallet-connect event (promise)))
([event info-promise]
(let [wc (get-mem [:keys :wallet-connect])
wc-map (uri/query-map wc)
wc-uri (uri/uri wc)
wc-pubkey (:host wc-uri)
relay-url (get wc-map "relay")
relay (ws-relay/make relay-url {:recv (partial get-wc-info info-promise)
:close wc-close})
request ["REQ" "ms-info" {"kinds" [13194] "authors" [wc-pubkey]}]
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)
(= result "pay_invoice")
(pay-invoice event wc-uri)
:else
(log-pr 1 'zap-by-wallet-connect 'unknown-result relay-url result)
)
)
))
(defn zap-author [event _e]
(if (some? (get-mem [:keys :wallet-connect]))
(zap-by-wallet-connect [event])
(zap-by-invoice [event []])))
(defn- get-fortune []
(condp = config/auto-thanks-fortune
:off ""

View File

@ -228,8 +228,17 @@
(do
(alert "password changed.")
(set-mem [:keys :password] new)
(data-storage/write-keys (get-mem :keys)))
)
(data-storage/write-keys (get-mem :keys))))))
(defn update-wallet-connect []
(let [wc (input "Enter Wallet Connect String"
:value (if (nil? (get-mem [:keys :wallet-connect]))
"-You won't see this string again!-"
"-You've got one. Replace it here.-")
:title (str "Wallet Connect"))]
(when (some? wc)
(set-mem [:keys :wallet-connect] (if (empty? wc) nil wc))
(data-storage/write-keys (get-mem :keys)))
)
)
@ -267,7 +276,9 @@
:action (fn [_e] (dispose! profile-frame))])
pw-button (button :text "Change password"
:listen [:action (fn [_e] (change-password))])
button-panel (horizontal-panel :items [cancel-button ok-button pw-button])
wc-button (button :text "Wallet Connect"
:listen [:action (fn [_e] (update-wallet-connect))])
button-panel (horizontal-panel :items [cancel-button ok-button pw-button wc-button])
profile-panel (vertical-panel :items [name-panel
about-panel
picture-panel

View File

@ -26,7 +26,7 @@
(when last
(try
(let [envelope (json/read-str (.toString buffer))]
((:recv callbacks) (::url relay) envelope))
((:recv callbacks) relay envelope))
(catch Exception e
(log-pr 1 'onText url (.getMessage e))
(log-pr 1 (.toString buffer))))