2

I am developing an online store BNB as a payment. Customers will sign transactions using Metamask and then frontend code(js) need to post call to our backend server with the txHash, and then we will broadcast the transaction to the network using our backend service.

But I am not able to find it using web3.js( web.eth.account.signTransaction() will not work in my case as we don't customer private keys).

I Need to know how to with web3 or with Metamask js library, any example of code will be helpful.

1 Answer 1

1

Unfortunately it is not currently possible to sign a transaction without broadcasting it with Metamask. The reason behind this is that such an approach would break the current account nonce management system of Metamask. There is a Github thread about this : https://github.com/MetaMask/metamask-extension/issues/3475.

I see two ways to overcome this limitation :

  • Use the deprecated Metamask eth_sign method which enables to sign an arbitrary hash and therefore a transaction hash (this would not be possible with personal_sign which adds a specific prefix to the message before hashing it, making the signature of transactions impossible). Note that this way of doing is not very clean and could be hard to implement. Furthermore, it is very unsafe for the user who doesn't know what he is actually signing. In fact, trying to get the user to sign a transaction with eth_sign could be considered an attack.

  • Implement a meta-transaction system : A much cleaner way of doing. Ask the user to sign a message and from this message sign a transaction in your backend. I wrote a simple exemple in an answer to this post : ETH - Pre-Paid smart contract so users don't pay transactions.

16
  • [web3]web3js.readthedocs.io/en/v1.2.0/web3-eth.html#signtransaction and [wiki]eth.wiki/json-rpc/API#json-rpc-methods shows the method but when I used them it shows 'eth_signTransaction' does not exist. Nov 9, 2020 at 11:05
  • Metamask does not support this method. So if you try to call it with Metamask in your frontend it will fail. As I said in my answer, there is currently no way to properly sign a transaction without broadcasting it with Metamask.
    – clement
    Nov 9, 2020 at 11:08
  • Thanks for the quick revert can we eth_sign for payment as well? It does not support the same params as other methods for payment. The second way looks more complex to implement so are there any other browser-wallets available that support the signTransaction feature. Nov 9, 2020 at 11:18
  • What do you mean by "can we eth_sign for payment as well? It does not support the same params as other methods for payment." ?
    – clement
    Nov 9, 2020 at 11:20
  • For the other browser wallets I don't know.
    – clement
    Nov 9, 2020 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.