ethereum - 如何使用 metamask 签署以太坊交易

标签 ethereum

我目前正在使用此代码发送交易:

      const provider = ethers.getDefaultProvider("ropsten", {
        infura:
          "https://ropsten.infura.io/v3/ee11be9f1d1c43199618db4a7b22aa79",
      });

      const signer = new ethers.Wallet(PRIVATE_KEY);
      const account = signer.connect(provider);
      const uniswap = new ethers.Contract(
        ropstenUniswapContract,
        [
          "function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)",
        ],
        account
      );
      const gasCost = ethers.BigNumber.from((+gasPrice/10) * Math.pow(10, 9));
      console.log('Computed gas cost ->', gasCost);

      const tx = await uniswap.swapExactETHForTokens(
        amountOutMin,
        path,
        to,
        deadline,
        { value, gasPrice: gasCost }
      );

      // Transaction Hash and Block
      setTransactionHash(tx.hash);
      const receipt = await tx.wait();
      console.log(receipt);

我的问题是:

如何让 MetaMask 代表我签署交易而不是提供我的私钥?

最佳答案

使用 web3.js 或 Ethers.js 抽象出交易签名.您可以在页内 JavaScript 代码中直接将 Ethers.js 连接到 MetaMask 提供程序 (windows.ethereum)。

An example here .

关于ethereum - 如何使用 metamask 签署以太坊交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67880184/

相关文章:

blockchain - 如何查看智能合约的tr余额是多少?

ethereum - Solidity中的HashSet数据结构

python - 如何使用 Python 从已知私钥生成以太坊公钥

ethereum - Solidity:如何在 compile.js 文件中编译多个智能合约?

node.js - 如何在 ethers.js 中正确使用 swapExactETHForTokens 方法?

ethereum - 公共(public)kovan测试网

ethereum - 如何在 Web 服务器上与公共(public)以太坊区 block 链通信?

node.js - 今天用 web3.js 发送以太坊不工作(结果是失败)

blockchain - 我无法使用 ipfs

php - 使用 php 将 Wei 转换为 Ethereum