kotlin-walletconnect-lib icon indicating copy to clipboard operation
kotlin-walletconnect-lib copied to clipboard

How to set chaindId from calling client?

Open mobilekosmos opened this issue 4 years ago • 11 comments
trafficstars

Should it be possible to set the chainId when constructing WCSession? I saw that the code works with chainIds is some parts, but if I understand it correctly this is only set AFTER the wallet connection was approved, you cannot predefine it before, right? I need to set the chainID, but don't know what part of the code to change to allow this.

mobilekosmos avatar Oct 04 '21 14:10 mobilekosmos

val params = mutableMapOf<String, Any?>()
val requestId = createCallId()
params["peerId"] = this.clientData.id
this.clientData.meta?.intoMap(params)
params["chainId"] = 137

   
send(Session.MethodCall.Custom(requestId,"wc_sessionRequest", listOf(params)), topic = config.handshakeTopic, callback = { resp ->
        (resp.result as? Map<String, *>)?.extractSessionParams()?.let { params ->
                    peerId = params.peerData?.id
                    peerMeta = params.peerData?.meta
                    approvedAccounts = params.accounts
                    chainId = params.chainId
                    storeSession()
                    propagateToCallbacks { onStatus(if (params.approved) Session.Status.Approved else Session.Status.Closed) }
                }
            })

i guess you can do it with a custom request ? i made my own version of the WCSession object tho - u might have to do the same, not sure if you actually have to or u can do it another way

elee1766 avatar Oct 18 '21 03:10 elee1766

@elee1766 Please make a PR with your additions, that would be nice. I hope they add that functionality to v2.

mobilekosmos avatar Oct 19 '21 02:10 mobilekosmos

honestly what really should happen is chainid should be added to the sessionRequest params... it's in the spec as an optional nullable field...

elee1766 avatar Oct 19 '21 02:10 elee1766

@elee1766 I'm trying to add the chainID to my fork but was not successful yet, also tried your code. When you connect with metamask on mobile should then Metamask ask about the chain when passing a chainID, or what expectation can I have to test my implementation? Btw. I will still be very happy if you could PR your solution since v1 is not really maintained anymore and I losing already a good amount of time with it, would really like to support that.

mobilekosmos avatar Nov 02 '21 05:11 mobilekosmos

Metamask: Chain ID [currently ignored] Chain ID is currently derived by the user's current selected network at ethereum.networkVersion. In the future we will probably allow a way to connect to multiple networks at once, at which point this parameter will become important, so it may be useful to be in the habit of including now.

mobilekosmos avatar Nov 02 '21 06:11 mobilekosmos

im not going to PR it b/c it's not how it should be implemented (as a custom request)

just load the library and then just rewrite this class https://github.com/WalletConnect/kotlin-walletconnect-lib/blob/master/lib/src/main/kotlin/org/walletconnect/impls/WCSession.kt

in your source, except change the offer function to make the extra function call

then just instantiate that class instead of the one within the library when creating your session obj ez

elee1766 avatar Nov 02 '21 10:11 elee1766

@elee1766 what about testing it? When you connect with metamask on mobile should then Metamask ask about the chain when passing a chainID, or what expectation can I have to test my implementation?

mobilekosmos avatar Nov 02 '21 15:11 mobilekosmos

it's all in the tech spec https://docs.walletconnect.com/1.0/tech-spec

just print out the messages ur sending/receiving on both clients and you'll see if the chain id is populated or not

elee1766 avatar Nov 02 '21 15:11 elee1766

val params = mutableMapOf<String, Any?>()
val requestId = createCallId()
params["peerId"] = this.clientData.id
this.clientData.meta?.intoMap(params)
params["chainId"] = 137

   
send(Session.MethodCall.Custom(requestId,"wc_sessionRequest", listOf(params)), topic = config.handshakeTopic, callback = { resp ->
        (resp.result as? Map<String, *>)?.extractSessionParams()?.let { params ->
                    peerId = params.peerData?.id
                    peerMeta = params.peerData?.meta
                    approvedAccounts = params.accounts
                    chainId = params.chainId
                    storeSession()
                    propagateToCallbacks { onStatus(if (params.approved) Session.Status.Approved else Session.Status.Closed) }
                }
            })

i guess you can do it with a custom request ? i made my own version of the WCSession object tho - u might have to do the same, not sure if you actually have to or u can do it another way

========================================= Hi buddy, trust wallt is ok after I use your method, but Metamask is still ethereum by default

sfz009900 avatar Jun 08 '22 15:06 sfz009900

@sfz009900 Metamask ignores the chainId, it's stated somewhere in their docs. Also as of walletConnect V2 you shouldn't rely on chainId anymore, they say, have not look into it myself yet though.

mobilekosmos avatar Jun 08 '22 20:06 mobilekosmos

@mobilekosmos thanks.Is there any other way for Metamask to set chaind?

sfz009900 avatar Jun 09 '22 12:06 sfz009900