[Ethereum] Invalid sender error when creating contract through web3

contract-deploymentweb3js

I am trying to create a managed account and deploy a custom wallet contract through web3 on my own private network. The code I'm using is:

// Create account
let account = this.web3.eth.accounts.create();
let encryptedAccount = account.encrypt(password);

// Create and deploy contract
let walletCreationData = new this.web3.eth.Contract(walletContractAbi, null, {
    data: walletContractData
}).deploy().encodeABI();

// Sign contract creation with created account
this.web3.eth.accounts.signTransaction({
    data: walletCreationData,
    from: account.address,
    gasPrice: 0,
    gas: 200000
}, account.privateKey).then((signedTransaction) => {
    this.web3.eth.sendSignedTransaction(signedTransaction.rawTransaction).then(console.log);
});

Now this will give me an error with the message invalid sender at the sendSignedTransaction promise. I have tried many combinations, among which using an in-memory wallet and loading an account with an existing balance instead of freshly creating one, but they all ended up giving the same error. What crucial part is missing that would allow me to create a smart contract?

This was tested using web3 1.0.0-beta.24 and geth 1.7.1-stable.

Best Answer

I launched the geth node using a different network id than the chain id configured in genesis.json. By default, web3 will look up the network id from the node and uses it in the transaction, but this would be an invalid transaction as the genesis block states a different chain id.

I had no idea chain id and network id were one and the same. I adjusted the network id on the node to match the configured chain id and there were no issues anymore. Alternatively specifying the correct chainId when signing the transaction will also work.

Related Topic