Decode JWT Token and get Wallet Address

Is it possible to decode the JWT to get the User’s Wallet Address?

Hi @neil.delarosa,

I’m happy to help you! To accomplish it, you can use an external package and implement the following code snippet in your project:

For Ethereum (EVM), please follow the below code:

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 || '')
}

For Solana(Ed25519 keys), please follow the below code:

const bs58 = require('bs58')

function publicKeyToSolanaAddress(publicKey) {
    let buffer = Buffer.from(publicKey, "hex")
    let address = bs58.encode(buffer)
    return address
}

You can simply pass the Web3Auth token to this function.

Let me know if you have any other questions or concerns!

2 Likes