I am running a private ethereum blockchain with POA

geth --port 3000 --networkid 42 --datadir=./blkchain --maxpeers=5  --rpc --rpcport 8543 --rpcaddr --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner" --allow-insecure-unlock  2>>eth.log

Then send transactions using Web3js

    let tx = {from:accounts[targetAccount],to:accounts[targetAccount],value: 0,gas: 100000, data:web3.utils.toHex(targetAccount)};
    web3.eth.accounts.signTransaction(tx, keys[targetAccount]).then(signed => {
       var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);

       tran.on('transactionHash', hash => {
         console.log('write blockchain ', hash);

       tran.on('error', console.error);

Then miner.start() from geth console

Everything works fine.

But when I start geth again without --allow-insecure-unlock there are pending transactions: (eth.pendingTransactions.length > 0) and sendTransaction always return: Error: Returned error: already known.

I don't want to use this param: --allow-insecure-unlock. Any hints for this? Many thanks!!

(If i start geth again with --allow-insecure-unlock, it seems to work again)


Try specifying explicitly your private network informations such as in this example from the web3js documentation :

to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
common: {
  baseChain: 'mainnet',
  hardfork: 'petersburg',
  customChain: {
    name: 'custom-chain',
    chainId: 1,
    networkId: 1
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')

Replace the parameters of the common object by those of your private blockchain.

Also, some remarks :

  • your "from" and "to" parameters are the same. Furthermore, according to the doc, the tx object used with accounts.signTransaction has no parameter "from".
  • your data is not correct, you have to use the "encodeAbi()" method on your smart contract function : https://web3js.readthedocs.io/en/v1.2.7/web3-eth-contract.html#methods-mymethod-encodeabi .
  • you should never stock in clear your private keys in your code.
  • Ethereumjs-tx is a good, well known alternative to web3 to sign transactions.

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.