[英]How to sign an ethereum transaction with metamask
我目前正在使用此代码发送交易:
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 代表我签署交易而不是提供我的私钥?
您的(面向浏览器的)应用程序可以使用Ethereum Provider API与 MetaMask 浏览器扩展通信。
特别是eth_sendTransaction方法向它传递data
字段(指定您要执行的合约中的哪个 function,以及传递的参数值),以及其他参数,例如sender
地址。
如果您是 MetaMask 的新手,请注意:要获取用户的地址列表,您需要调用eth_requestAccounts方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.