[SOLVED] WalletConnect Merge Address

Would like to find out how I can link WalletConnect address using Moralis.Web3.link.

The problem I have is that we’re using Username/Password authentication. After that, we want to allow the users to connect to our dApp with WalletConnect and link their wallets from different sources. The problem is that to trigger the WalletConnect QR code, I have to call await Moralis.Web3.authenticate({provider:‘walletConnect’});. This will then create a new user in the Moralis database.

Is there a way to retrieve the walletconnect address without Moralis creating a new user?

Thank you.

you could try to call Moralis.enableWeb3, for example as in: 1inch WalletConnect

You may be using an older version of Moralis SDK with Moralis.Web3 syntax.

I am able to get the wallet address from version 0.2.8 by calling: web3.currentProvider.accounts[0]

What’s happening now is that after I scan the WalletConnect QR code from TrustWallet it will ask me to sign, and clicking on the sign will on trustwallet actually opens up my Metamask chrome extension. Which seems odd.

Here’s my code snippet:

 const web3 = await Moralis.Web3.enableWeb3({provider:'walletconnect'});
        console.log(`Logged in user`, walletUser);
        console.log(`Wallet Connect`, web3);
        const walletConnectAddress = web3.currentProvider.accounts[0];
        if (walletConnectAddress) {
          const isLinked = walletUser.attributes.accounts.includes(walletConnectAddress);
          if (!isLinked) {
            const confirm = window.confirm(`Would you like to link this wallet ${walletConnectAddress} account to your user profile?`);
            if (confirm) {
              try {
                // console.log(`Wallet address: ${window.ethereum.selectedAddress}`);
                const currentUser = await Moralis.Web3.link(walletConnectAddress);
                // const currentUser = Moralis.User.current();
                if (currentUser) {
                  console.log(`Account linked: ${walletConnectAddress}.`);
                  const cloneUser = _.cloneDeep(currentUser);
              } catch (error) {
                const code = error.code;
                const message = error.message;
                console.error(`${code}: ${message}`);
          } else {
            alert('Your current wallet address is already linked.');

you say that it fails here? if yes, how it fails? do you get to see a pop up to sign a message?

you could also try to use Moralis.link and Moralis.enableWeb3, Moralis.Web3 is deprecated.

@cryptokid I updated the code to use Moralis.link and Moralis.enableWeb3. When the Moralis.Link method is called and I’m passing in the trust wallet address, I got 2 errors:
“MetaMask - RPC Error: Already processing eth_requestAccounts. Please wait.”
“32002: Already processing eth_requestAccounts. Please wait.”

Strange is that I’m using trust wallet through walletconnect but it mentions Metamask

how far did it go, did it start trust wallet application and ask for signature?

Yes, it asked me for signature and once I click no connect, web3 returned back an object. That is when I retrieved the wallet address by calling web3.currentProvider.accounts[0]. Then tried to pass that into Moralis.link function and the error occured.

Is it because I’m using the username/password authentication flow and that is why is not working?

  static async link(account, options) {
    const web3 = await MoralisWeb3.enableWeb3(options);

it looks like this is what link function does on first line

it looks like you have to pass in options also the options for walletconnect, like {provider:'walletconnect'}

That was it! Thank you @cryptokid!

1 Like