0

I am trying to use the eth_sign method

However i am getting the following error:

code: -32602, message: 'Invalid parameters: must provide an Ethereum address.'

My function is as follows:

const interact = async () => {
        const { ethereum } = window;
        const web3 = new Web3(ethereum);
        const contract = new web3.eth.Contract(ContractAbi, "0xc778417E063141139Fce010982780140Aa0cD5Ab");
        let userBalance = await contract.methods.balanceOf(currentAccount).call();

        let tx_builder =  contract.methods.approve("0x832B984Bf318fB50A5D2F1E1E4F43B7728e3606f", userBalance.toString());
        let encoded_tx = tx_builder.encodeABI();
        const accounts = await ethereum.request({method: "eth_accounts"});
        let tx_obj = {
          from: accounts[0],
          node : "0x0", // this is ignored by metamask
          gasLimit: "300000",
          to: "0x832B984Bf318fB50A5D2F1E1E4F43B7728e3606f",
          data: encoded_tx
        }

        const signature = await ethereum.request({ method: 'eth_sign', params: [ tx_obj, web3.utils.toChecksumAddress(accounts[0]) ] });
        console.log(signature);
      }

There seems to be very limited documentation on usage of this function and im struggling to find if its even still useable. I know the address is valid because i signed a regular utf-8 message via "personal_sign" however that method doesnt seem to work for tx objects.

1 Answer 1

1

eth_sign always takes two parameters (see https://eth.wiki/json-rpc/API#eth_sign): an address (account that signes the message and a string (a message to sign). Some ethereum provider support eth_signTransaction (see https://eth.wiki/json-rpc/API#eth_signTransaction). Otherwise you need to use eth_sendTransaction.

Note: Most wallets prevent that you can sign raw transaction data by prepending the "Ethereum Message" prefix. In the past (and even now for MetaMask) you could rlp encode the transaction and use the rlp encoded transaction as the string input.

4
  • Related question: ethereum.stackexchange.com/questions/90009/…
    – Richard
    Commented Jan 4, 2022 at 13:13
  • Thank you @richard I have tried the following: using the encode method from the rlp library in the following order: nonce, gasprice, gasLimit, to, value, data. I am then attempting to use the eth_sign method (as stated in the related question) to sign this data. However I am still getting the same error message, this is what my paramaters look like : params: [ rawTransaction.toString("hex"), web3.utils.toChecksumAddress(accounts[0]) ] it works fine signing personally, but eth_sign just seems to not work.
    – nemo
    Commented Jan 4, 2022 at 13:24
  • Ahh yeah, so the order of parameters is different for eth_sign and personal_sign. For eth_sign the first parameter is the address and the second the message. I will update my answer
    – Richard
    Commented Jan 4, 2022 at 13:50
  • @Richard hi can you please have a look at my question. ethereum.stackexchange.com/questions/120470/…
    – kd12345
    Commented Feb 1, 2022 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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