Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set chaindId from calling client? #48

Open
mobilekosmos opened this issue Oct 4, 2021 · 11 comments
Open

How to set chaindId from calling client? #48

mobilekosmos opened this issue Oct 4, 2021 · 11 comments

Comments

@mobilekosmos
Copy link

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
Copy link

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
Copy link
Author

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

@elee1766
Copy link

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
Copy link
Author

mobilekosmos commented Nov 2, 2021

@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
Copy link
Author

mobilekosmos commented Nov 2, 2021

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
Copy link

elee1766 commented Nov 2, 2021

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
Copy link
Author

@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
Copy link

elee1766 commented Nov 2, 2021

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
Copy link

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
Copy link
Author

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants