Offline inbox: Mark mailserver as trusted peer with RPC

Also document get-sym-key memoization
This commit is contained in:
Oskar Thoren 2017-12-07 14:01:11 +01:00
parent a68f065237
commit 6aed718984
No known key found for this signature in database
GPG Key ID: 5128AB0637CD85AF
2 changed files with 31 additions and 15 deletions

View File

@ -16,6 +16,8 @@
;; TODO(oskarth): Rewrite callback-heavy code with CSP and/or coeffects ;; TODO(oskarth): Rewrite callback-heavy code with CSP and/or coeffects
;; NOTE(oskarth): Exact params still being determined ;; NOTE(oskarth): Exact params still being determined
;; TODO(oskarth): Memoize addPeer and markTrusted, similar to keys/get-sym-key
;; TODO(oskarth): Actually deal with errors, all in same cb - outside scope of this
(defn request-messages! [web3 {:keys [enode topic password] (defn request-messages! [web3 {:keys [enode topic password]
:or {enode default-enode :or {enode default-enode
password inbox-password password inbox-password
@ -24,11 +26,21 @@
enode enode
(fn [res] (fn [res]
(log/info "offline inbox: add peer" enode res) (log/info "offline inbox: add peer" enode res)
(let [args {:jsonrpc "2.0"
:id 1
:method "shh_markTrustedPeer"
:params [enode]}
payload (.stringify js/JSON (clj->js args))]
(log/info "offline inbox: mark-trusted-peer request")
(status/call-web3
payload
(fn [res2]
(log/info "offline inbox: mark-trusted-peer response" enode res2)
(keys/get-sym-key web3 password (keys/get-sym-key web3 password
(fn [sym-key-id] (fn [sym-key-id]
(log/info "offline inbox: sym-key-id" sym-key-id) (log/info "offline inbox: sym-key-id" sym-key-id)
(let [args {:jsonrpc "2.0" (let [args {:jsonrpc "2.0"
:id 1 :id 2
:method "shh_requestMessages" :method "shh_requestMessages"
:params [{:enode enode :params [{:enode enode
:topic topic :topic topic
@ -37,8 +49,10 @@
:to 1612505820}]} :to 1612505820}]}
payload (.stringify js/JSON (clj->js args))] payload (.stringify js/JSON (clj->js args))]
(log/info "offline inbox: request-messages request") (log/info "offline inbox: request-messages request")
(status/call-web3 payload callback))))))) (status/call-web3 payload callback))))))))))
;; TODO(oskarth): Use web3 binding to do (.markTrustedPeer web3 enode cb)
;;
;; TODO(oskarth): Use web3 binding instead of raw RPC above, pending binding and deps: ;; TODO(oskarth): Use web3 binding instead of raw RPC above, pending binding and deps:
;; (.requestMessages (utils/shh web3) ;; (.requestMessages (utils/shh web3)
;; (clj->js opts) ;; (clj->js opts)

View File

@ -11,7 +11,9 @@
-shh -shh
(generateSymKeyFromPassword password callback))) (generateSymKeyFromPassword password callback)))
(defn get-sym-key [web3 password callback] (defn get-sym-key
"Memoizes expensive calls by password."
[web3 password callback]
(if-let [key-id (get @password->keys password)] (if-let [key-id (get @password->keys password)]
(callback key-id) (callback key-id)
(add-sym-key-from-password (add-sym-key-from-password