Created
August 18, 2020 08:20
-
-
Save schaueho/99882da7e8130b2d93e0d8247741d20e to your computer and use it in GitHub Desktop.
ClojureScript interop calls of SodiumPlus code triggers type inference warnings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))] | |
-------------------------------------------------------------------------------- | |
) |
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
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.