Getting the wallet public key after sign in with wallet (MetaMask or WalletConnect)

I’m following this tutorial (Using Server Side Verification with Web3Auth | Web3Auth) to get the wallet public key from the idToken and it works if I sign in with email.

But if I sign in with WalletConnect for example, it doesn’t. How can I accomplish that?

@felipe Thanks for your recent communication.

After a user is logged in using one of the supported Wallets including Metamask, WalletConnect. One can obtain the idToken by calling web3auth.authenticateUser() of Web3Auth.

authenticateUser

await web3auth.authenticateUser();

Returns

{  "idToken": "<jwtToken issued by Web3Auth>"}

Sample idToken payload

{  "iat": 1661158877,  "iss": "<issuer-name>",  "aud": "https://requesting.website",  "wallets": [    {      "address": "0x809d4310d578649d8539e718030ee11e603ee8f3",      "type": "ethereum"    }  ],  "exp": 1661245277}

Hi @vjgee . I am able to get the idToken after signing in with my wallet, and that’s working fine. However, the code that retrieves the wallet public key (which is here Using Server Side Verification with Web3Auth | Web3Auth) only works if I sign in with email.

How can I get the wallet public key if I sign in with a wallet?

const base64Url = idToken.split(".")[1];
const base64 = base64Url.replace("-", "+").replace("_", "/");
const parsedToken = JSON.parse(window.atob(base64));
const appPubKey = parsedToken.wallets[0].public_key

@felipe Can you try this For Ethereum (EVM) :

import publicKeyToAddress from 'ethereum-public-key-to-address';

const parseTokenAndReturnAddress = (token) => {
  if (!token) return null
  const base64Url = token.split('.')[1]
  const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')
  const jsonPayload = decodeURIComponent(
    atob(base64)
      .split('')
      .map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
      .join('')
  )
  console.log(JSON.parse(jsonPayload).wallets[0].public_key)
  return publicKeyToAddress(JSON.parse(jsonPayload)?.wallets[0]?.public_key || '')
}

the problem is that the wallet[0] has no public_key available. this is my JSON payload:

{
   "iat":1708945461,
   "iss":"wallet-connect-v2",
   "aud":"localhost",
   "wallets":[
      {
         "address":"0xEB0Ac1C18832ec29F23Bd8138a0302dE1B0A0d40",
         "type":"ethereum"
      }
   ],
   "exp":1709031861
}

The External Wallet’s idToken payload is different than the Social login’s idToken and the result you get is the expected Payload.

So it’s not possible to get the public_key if I sign in with an external wallet?

I just found out the validation for external wallet is different: External Wallets | Documentation | Web3Auth

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.