Zachary Baker
Zachary Baker

Reputation: 23

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.

Upvotes: 0

Views: 564

Answers (1)

Yilmaz
Yilmaz

Reputation: 49619

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 };
};

Upvotes: 1

Related Questions