Fork me on GitHub
#clojurescript
<
2023-03-01
>
Olaleye Blessing16:03:02

I am working with metamask and I log a result when a user selects a wallet. The log looks like this in my browser:

#js [#js
    {
    :id sdNyveWraMbzb9Ff9wrDU,
    :parentCapability eth_accounts,
    :invoker ,
    :caveats #js [
    #js {
    :type restrictReturnedAccounts,
    :value #js [0x0ea1a816024e5b33e5c9356d52aa75686d036d9b]}],
    :date 1677684342237
    }
   ] 
I'm trying to get the`:value` , why is the code below not working:
(get-in addrs [0 :caveats 0 :value]

thheller16:03:41

the keys in a #js object are strings, you can't get keywords out of JS maps. so (get-in addrs [0 "caveats" 0 "value"]) should be fine. converting these to CLJS collections also is an option.

Olaleye Blessing16:03:10

I got nil instead of the address, even when I converted it to cljs collections.

;; addrs is the object
(let [addr-cljs (js->clj addrs)
          addr (get-in addr-cljs [0 :caveats 0 :value])]
      (println addr))

thheller16:03:04

check what the data looks like after conversion. things aren't always what they seem when looking at JS objects

Olaleye Blessing16:03:20

[
  {
     id Av0PBnI222wCqTgVjO326,
     parentCapability eth_accounts,
     invoker ,
     caveats [
        {
           type restrictReturnedAccounts,
           value [0x0ea1a816024e5b33e5c9356d52aa75686d036d9b]
         }
      ],
      date 1677688249071
    }
]

thheller16:03:57

thats not a CLJ map/vector?

thheller16:03:30

just forget about this conversion stuff and use proper JS interop to get what you want

thheller16:03:03

instead of get-in do (-> addrs (aget 0) (.-caveats) (aget 0) (.-value))

1
Olaleye Blessing16:03:07

Thanks, I got this with (-> addrs .... :

#js [0x0ea1a816024e5b33e5c9356d52aa75686d036d9b]
Thank you

👍 1
lilactown17:03:59

js->clj doesn't convert strings to keywords by default, so the above would have worked with a bit of modification

;; addrs is the object
(let [addr-cljs (js->clj addrs)
          addr (get-in addr-cljs [0 "caveats" 0 "value"])]
      (println addr))

thheller16:03:40

it is unfortunate the keys print as keywords, as they are always strings in objects

4