Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple question: about signature #254

Open
LuozhuZhang opened this issue Jun 9, 2022 · 0 comments
Open

simple question: about signature #254

LuozhuZhang opened this issue Jun 9, 2022 · 0 comments

Comments

@LuozhuZhang
Copy link

The signature of the metamask is done by personal_sign RPC?

Or do you implement a personal_sign yourself, and call this function when all users login dapps?

Why do I see that the front-end application is calling web3.js

var from = web3.eth.accounts[0];

var params = [from, msgParams];
var method = 'eth_signTypedData_v4';

web3.currentProvider.sendAsync(
  {
    method,
    params,
    from,
  },
  function (err, result) {
    const recovered = sigUtil.recoverTypedSignature_v4({
      data: JSON.parse(msgParams),
      sig: result.result,
    });

    if (
      ethUtil.toChecksumAddress(recovered) === ethUtil.toChecksumAddress(from)
    ) {
      alert('Successfully recovered signer as ' + from);
    } else {
      alert(
        'Failed to verify signer when comparing ' + result + ' to ' + from
      );
    }
  }
);

But in the eth-sig-util library, metamask itself implements the signature method

/**
 * Create an Ethereum-specific signature for a message.
 *
 * This function is equivalent to the `eth_sign` Ethereum JSON-RPC method as specified in EIP-1417,
 * as well as the MetaMask's `personal_sign` method.
 *
 * @param options - The personal sign options.
 * @param options.privateKey - The key to sign with.
 * @param options.data - The hex data to sign.
 * @returns The '0x'-prefixed hex encoded signature.
 */
export function personalSign({
  privateKey,
  data,
}: {
  privateKey: Buffer;
  data: unknown;
}): string {
  if (isNullish(data)) {
    throw new Error('Missing data parameter');
  } else if (isNullish(privateKey)) {
    throw new Error('Missing privateKey parameter');
  }

  const message = legacyToBuffer(data);
  const msgHash = hashPersonalMessage(message);
  const sig = ecsign(msgHash, privateKey);
  const serialized = concatSig(toBuffer(sig.v), sig.r, sig.s);
  return serialized;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant