@ethersproject/wallet#verifyTypedData TypeScript Examples

The following examples show how to use @ethersproject/wallet#verifyTypedData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From snapshot.js with MIT License 7 votes vote down vote up
export async function verify(address, sig, data) {
  const { domain, types, message } = data;

  const hash = getHash(data);
  console.log('Hash', hash);
  console.log('Address', address);

  try {
    const recoverAddress = verifyTypedData(domain, types, message, sig);
    console.log('Recover address', recoverAddress);
    if (address === recoverAddress) return true;
  } catch (e) {
    console.log('Could not recoverAddress:' + e.message);
  }

  console.log('Check EIP1271 signature');
  return await verifyEIP1271(address, sig, hash);
}
Example #2
Source File: verifyTransaction.ts    From bodhi.js with Apache License 2.0 6 votes vote down vote up
verifyTransaction = (tx: AcalaEvmTXPayload, signature: string): string => {
  const payload = createTransactionPayload(tx);

  return verifyTypedData(
    payload.domain,
    {
      AccessList: payload.types.AccessList,
      Transaction: payload.types.Transaction
    },
    payload.message,
    signature
  );
}
Example #3
Source File: sign.test.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
describe('SignTypedData', () => {
  it('test signature', async () => {
    const payload = createTransactionPayload(value);

    const ethersHash = transactionHash(value);
    const ethersSig = signTransaction(privateKey, value);

    expect(
      verifyTypedData(
        payload.domain,
        {
          AccessList: payload.types.AccessList,
          Transaction: payload.types.Transaction
        },
        payload.message,
        ethersSig
      )
    ).equal(address);

    // console.log(ethersHash);
    // console.log(ethersSig);

    // const metamaskPayload = {
    //   ...payload,
    //   domain: {
    //     ...payload.domain,
    //     salt: Buffer.from(arrayify(payload.domain.salt))
    //   }
    // };

    // @ts-ignore
    // const metamaskHash = TypedDataUtils.eip712Hash(metamaskPayload, SignTypedDataVersion.V4);

    // const metamaskSig = signTypedData({
    //   privateKey: Buffer.from(arrayify(privateKey)),
    //   data: metamaskPayload,
    //   version: SignTypedDataVersion.V4
    // });

    // console.log(metamaskSig);

    // expect(ethersHash).equal('0x' + metamaskHash.toString('hex'));
    // expect(ethersSig).equal(metamaskSig);

    // expect(
    //   getAddress(
    //     recoverTypedSignature({
    //       data: metamaskPayload,
    //       signature: metamaskSig,
    //       version: SignTypedDataVersion.V4
    //     })
    //   )
    // ).equal(address);
  });
});