【问题标题】:I need a simple way to sign data via web3 and metamask我需要一种简单的方法来通过 web3 和 metamask 对数据进行签名
【发布时间】:2022-06-12 12:01:16
【问题描述】:

我需要一种非常简单的方法来使用 Metamask 和 Web3 对数据进行签名。我对在 Web3 中使用 eth 帐户签名非常熟悉,但我想将 Metamask 合并到其中。我已阅读有关签署数据的 Metamask 文档,但他们提供的示例已过时。

我做过的一件事:成功启用以太坊并获得连接用户的地址。

关于签署数据的非常简单的方法有什么建议吗?我真的只是在测试 Metamask 并想开始。

【问题讨论】:

    标签: ethereum web3 sign web3js metamask


    【解决方案1】:
     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 };
      };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-11
      • 2019-03-17
      • 1970-01-01
      • 2012-10-06
      • 2017-07-28
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      相关资源
      最近更新 更多