WalletConnect / kotlin-walletconnect-lib

library to use WalletConnect with Kotlin or Java
MIT License
156 stars 98 forks source link

How to set chaindId from calling client? #48

Open mobilekosmos opened 3 years ago

mobilekosmos commented 3 years ago

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.

elee1766 commented 3 years ago
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

mobilekosmos commented 3 years ago

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

elee1766 commented 3 years ago

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

mobilekosmos commented 3 years ago

@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 commented 3 years ago

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.

elee1766 commented 3 years ago

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

mobilekosmos commented 3 years ago

@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?

elee1766 commented 3 years ago

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

sfz009900 commented 2 years ago
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

mobilekosmos commented 2 years ago

@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.

sfz009900 commented 2 years ago

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