如何在不发送到元掩码的情况下对交易进行签名



我正在尝试使用wallet connect在Android元掩码中进行签名交易,将交易发送到我的后端服务器,然后提交到区块链。

然而,很难找到我如何签署交易(不发送它)并将其发送到我的后端服务器。

我可以使用personal_sign签名消息,但不知道如何签名事务。

为什么要将事务提交到后端而不执行它?立即执行交易不是更容易吗?

无论如何,Metamask不支持RPC只签名事务而不提交它们。见https://github.com/MetaMask/metamask-extension/issues/3475

编辑:发送交易发送它,只需使用以下代码,修改自我在https://stackoverflow.com/a/71941911/11628256: 的回答
// The ABI tells ethers.js or web3.js how to interact with a type of contract - the following is a generic EIP-20 ABI I found
let abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
// Your NFT contract's address
let contractAddress = "0xPUTYOURCONTRACTADDRESSHERE";
// Information for the transfer
let myAccount = "0xPUTYOURADDRESSHERE"
let amountToTransfer = 1234;
// The following setup is taken from https://docs.ethers.io/v5/getting-started/#getting-started--connecting
// A Web3Provider wraps a standard Web3 provider, which is
// what MetaMask injects as window.ethereum into each page
const provider = new ethers.providers.Web3Provider(window.ethereum);
// MetaMask requires requesting permission to connect users accounts
let accounts = await provider.send("eth_requestAccounts", []);
let account = accounts[0];
// The MetaMask plugin also allows signing transactions to
// send ether and pay to change state within the blockchain.
// For this, you need the account signer...
const signer = provider.getSigner();
// This code tells ethers.js how to interact with your token contract
const tokenContractReadonly = new ethers.Contract(contractAddress, abi, provider);
// Connecting with a signer allows you to use all the methods of the contract
const tokenContract = tokenContractReadonly.connect(signer);
// Transfer
tokenContract.transfer(myAccount, Math.pow(10, await tokenContract.decimals()) * amountToTransfer);

相关内容

最新更新