[Ethereum] Signing transaction with Metamask (not sending)

metamasktransactionsweb3js

I use the following code to sign and send a transaction in my backend:

var txData = {
    nonce: nonceHex,
    gasPrice: gasPriceHex,
    gas: gasHex,
    data: contractData,
    from: ctx.mainAccount,
};

var tx = new EthTx(txData);
tx.sign(new Buffer(pKey, 'hex'));
var txHex = tx.serialize().toString('hex');

web3.eth.sendSignedTransaction(`0x${txHex}`)
    .on('receipt', receipt => { ... }); 

Now what I'd like to do is instead of using the private key here, I'd like to use Metamask just to sign the transaction (and not send it)

Is it possible? In other words does Metamask has a way to do something similar to tx.sign(new Buffer(pKey, 'hex')); I'd like then my backend to send the signed transaction, not Metamask.

I tried to use web3.eth.personal like this:

//backend
var tx = new EthTx(txData);
var txHash = tx.hash();
var txUnsignedHex = txHash.toString('hex');

//browser
web3.personal.sign('0x' + txUnsignedHex, ethAccount, (err, result) => {  ... } );

But the result I get does not work when I run web3.eth.sendSignedTransaction

Best Answer

MetaMask does not currently support signing but not submitting a transaction, although the idea has been suggested before:

https://github.com/MetaMask/metamask-extension/issues/2506

You could share your use case there, to help it gain priority.

Related Topic