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,21 +26,33 @@
enode enode
(fn [res] (fn [res]
(log/info "offline inbox: add peer" enode res) (log/info "offline inbox: add peer" enode res)
(keys/get-sym-key web3 password (let [args {:jsonrpc "2.0"
(fn [sym-key-id] :id 1
(log/info "offline inbox: sym-key-id" sym-key-id) :method "shh_markTrustedPeer"
(let [args {:jsonrpc "2.0" :params [enode]}
:id 1 payload (.stringify js/JSON (clj->js args))]
:method "shh_requestMessages" (log/info "offline inbox: mark-trusted-peer request")
:params [{:enode enode (status/call-web3
:topic topic payload
:symKeyID sym-key-id (fn [res2]
:from 0 (log/info "offline inbox: mark-trusted-peer response" enode res2)
:to 1612505820}]} (keys/get-sym-key web3 password
payload (.stringify js/JSON (clj->js args))] (fn [sym-key-id]
(log/info "offline inbox: request-messages request") (log/info "offline inbox: sym-key-id" sym-key-id)
(status/call-web3 payload callback))))))) (let [args {:jsonrpc "2.0"
:id 2
:method "shh_requestMessages"
:params [{:enode enode
:topic topic
:symKeyID sym-key-id
:from 0
:to 1612505820}]}
payload (.stringify js/JSON (clj->js args))]
(log/info "offline inbox: request-messages request")
(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