简体   繁体   中英

I need a simple way to sign data via web3 and metamask

I need a very simple way to sign data with Metamask and Web3. I am very familiar with using eth account sign with Web3, but I want to incorporate Metamask into it. I have read the Metamask docs on signing data, but the examples they gave are outdated.

The one thing I have done: Successfully enabled Ethereum and got access to the user's address who connected.

Any suggestions on a very very simple way to sign data? I'm really just testing things out Metamask and want to get started.

const getSignedData = async () => {
  const messageToSign = "any message you create or fetch";
  const accounts = (await ethereum?.request({
    method: "eth_requestAccounts",
  })) as string[];
  // account will be the signer of this message
  const account = accounts[0];
  // password is the third param as uuid
  const signedData = await ethereum?.request({
    method: "personal_sign",
    params: [
      JSON.stringify(messageToSign.data),
      account,
      messageToSign.data.id,
    ],
  });
  return { signedData, account };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM