bytesbay / web3-token

Web3 Token is a new way to authenticate users in a hybrid dApps using signed messages. Implementation of EIP-4361.
380 stars 50 forks source link

SIWE message #55

Open FranRom opened 8 months ago

FranRom commented 8 months ago

I've tried to add SIWE format to the message but it's not working well with the library. Is there any workaround or willing to add this feature in the future? https://docs.metamask.io/wallet/how-to/sign-data/siwe/

FranRom commented 8 months ago

This is what I tried, although the message I sign is correct, It's not working

const userSignature = async () => {
   try {
     const host = getTenantHost()
     const NONCE = 32891757
     const signedMessage = await Web3Token.sign(
       async (message: string) => {
         const issuedAtMatch = /Issued At: (.+)/.exec(message)
         const issuedAt = issuedAtMatch ? issuedAtMatch[1] : 'N/A'

         message = `${host} wants you to sign in with your account:\n${address}\n\nI accept the ${tenantName} Terms of Service\n\nURI: https://${tenantHost}\nVersion: 1\nChain ID: ${chainId}\nNonce: ${NONCE}\nIssued At: ${issuedAt}`

         return await signMessageAsync({ message })
       },
       {
         domain: tenantHost,
         statement: `${host} wants you to sign in with your account: ${address}`,
         nonce: 12345678
       }
     )

     return signedMessage
   } catch (error) {
     console.error('Error signing message: ', error)
   }
 }