Skip to content

Instantly share code, notes, and snippets.

@schaueho
Created August 18, 2020 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schaueho/99882da7e8130b2d93e0d8247741d20e to your computer and use it in GitHub Desktop.
Save schaueho/99882da7e8130b2d93e0d8247741d20e to your computer and use it in GitHub Desktop.
ClojureScript interop calls of SodiumPlus code triggers type inference warnings
(defn encrypt-test [plaintext]
(go
(let [sodium (.-sodium js/window) ;; <-- this
nonce (<p! (.randombytes_buf sodium 24))
key (<p! (.crypto_secretbox_keygen sodium))
ciphertext (<p! (.crypto_secretbox sodium plaintext nonce key))]
(println "key: " (.toString key "hex"))
(println "cipher:" (.toString ciphertext "hex"))
(println "decrypted:" (.toString (<p! (.crypto_secretbox_open sodium ciphertext nonce key)))))))
(comment
This triggers the following warnings
[:app] Compiling ...
[:app] Build completed. (6643 files, 2 compiled, 3 warnings, 1,90s)
------ WARNING #1 - :infer-warning ---------------------------------------------
File: crypto.cljs:84:3
--------------------------------------------------------------------------------
82 |
83 | (defn encrypt-test [plaintext]
84 | (go
---------^----------------------------------------------------------------------
Cannot infer target type in expression (. inst_52999 (crypto_secretbox_open inst_53041 inst_53013 inst_53027))
--------------------------------------------------------------------------------
85 | (let [sodium (.-sodium js/window)
86 | nonce (<p! (.randombytes_buf sodium 24))
87 | key (<p! (.crypto_secretbox_keygen sodium))
88 | ciphertext (<p! (.crypto_secretbox sodium plaintext nonce key))]
--------------------------------------------------------------------------------
------ WARNING #2 - :infer-warning ---------------------------------------------
File: crypto.cljs:84:3
--------------------------------------------------------------------------------
82 |
83 | (defn encrypt-test [plaintext]
84 | (go
---------^----------------------------------------------------------------------
Cannot infer target type in expression (. inst_52999 (crypto_secretbox plaintext inst_53013 inst_53027))
--------------------------------------------------------------------------------
85 | (let [sodium (.-sodium js/window)
86 | nonce (<p! (.randombytes_buf sodium 24))
87 | key (<p! (.crypto_secretbox_keygen sodium))
88 | ciphertext (<p! (.crypto_secretbox sodium plaintext nonce key))]
--------------------------------------------------------------------------------
------ WARNING #3 - :infer-warning ---------------------------------------------
File: crypto.cljs:84:3
--------------------------------------------------------------------------------
82 |
83 | (defn encrypt-test [plaintext]
84 | (go
---------^----------------------------------------------------------------------
Cannot infer target type in expression (. inst_52999 (crypto_secretbox_keygen))
--------------------------------------------------------------------------------
85 | (let [sodium (.-sodium js/window)
86 | nonce (<p! (.randombytes_buf sodium 24))
87 | key (<p! (.crypto_secretbox_keygen sodium))
88 | ciphertext (<p! (.crypto_secretbox sodium plaintext nonce key))]
--------------------------------------------------------------------------------
)
@schaueho
Copy link
Author

I tried changing (<p! (.crypto_secretbox sodium plaintext nonce key)) to (<p! ^CryptographyKey (.crypto_secretbox sodium plaintext nonce key)) (and similar adding a type hint ^Buffer for the other warnings) to no avail.

@schaueho
Copy link
Author

schaueho commented Aug 18, 2020

With the help of thheller in #clojurescript channel on clojurians, the solution is to add a type hint of ^js to the sodium object and to pull out the (.sodium js/window) bit out of the context of the go macro, as this rewrites the code, messing up the type hint. I.e., I added a (def ^js sodium (.-sodium js/window)) on top and removed the local let binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment