web3 Provided Address is invalid, the capitalization checksum test failed

Viewed 9673

I am trying to send a method on a contract using web3. I'm creating an account using the privateKeyToAccount method but when sending the method on the contract I get the following error:

Provided address [object Object] is invalid, the capitalization checksum test failed, or it's an indirect IBAN address which can't be converted.

Am I missing a step? I already created an instance of web3 and the contract interface works. I attached part of the code below. Thanks in advance for the help.

const web3 = new Web3(
    new Web3.providers.WebsocketProvider(
        'wss://rinkeby.infura.io/ws/v3/<api>'
    )
);

const dummyPrivateKey = '0x38544e1555a3553829219281253d2400fa20ebbd922fdh3918a7s2b53b9e1358';
const accounts = web3.eth.accounts.privateKeyToAccount(dummyPrivateKey);

await contract.methods // add username
    .addMessage(_username, _message)
    .send({ from: accounts });
4 Answers

Petr is right. I missed the part where you are giving the whole object instead of address.

But if you want to checksum an address. You can simply use Web3 utility function web3.utils.toChecksumAddress(address) to convert. More details here

You're passing the account object to the from field. But you need to pass just the address.

Replace the from: accounts to from: accounts.address.


Note: This is how the accounts object looks like:

{
  address: '0x29B67BB1cFE4799FDb46B49aD81cD771665E2dF7',
  privateKey: '0x38544e1555a3553829219281253d2400fa20ebbd922fdh3918a7s2b53b9e1358',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt]
}

Ensure that the address that you are providing is valid. You can get the address from either online providers like Infura or local setup like Ganache.

If still an issue persists, then try to use the following code.

web3.utils.toChecksumAddress(address)

I also encountered this problem in the project, but in my case, I also used Web3 service and same test network at the same time. Stop another service and back to normal now.

Related