I have some smart contract which has this function:
function someFunction() external returns (uint256) {
require(msg.sender != address(0), "zero address");
//some code....
return 10;
}
Then I run my test it stopped working
const {constants,expectRevert,BN} = require('@openzeppelin/test-helpers');
//setting up ....
it("Must fail if caller has zero address", async () => {
await expectRevert(myContract.someFunction({from:constants.ZERO_ADDRESS}),"zero address")
})
and return this error
UnhandledRejections detected Promise { Error: nonce
generation function failed or private key is invalid
at Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:292:1)
at Transaction.exports.modules.564403.FakeTransaction.hash (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\fake.js:63:1)
ature.js:11:1)
at Transaction.fakeHash [as hash] (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:18:1)
nsaction.js:230:1)
at RuntimeError.combine (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:42:1)
at new RuntimeError (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:19:1)
\fake.js:63:1)
at Function.RuntimeError.fromResults (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:94:1)
at C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\blockchain_double.js:568:1
{
uncaught: true } } Error: nonce generation function failed or private key is invalid
at Object.exports.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\secp256k1\lib\elliptic\index.js:207:1)
at Object.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\secp256k1\lib\index.js:192:1)
at Object.exports.ecsign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\node_modules\ethereumjs-util\dist\signature.js:11:1)
at Transaction.exports.modules.994857.Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\transaction.js:230:1)
ure.js:11:1)
at Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:292:1)
action.js:230:1)
at Transaction.exports.modules.564403.FakeTransaction.hash (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\fake.js:63:1)
ake.js:63:1)
at Transaction.fakeHash [as hash] (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:18:1)
at RuntimeError.combine (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:42:1)
at new RuntimeError (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:19:1)
at Function.RuntimeError.fromResults (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:94:1)
at C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\blockchain_double.js:568:1
{ uncaught: true }
I used solidity-coverage for testing.
Is it possible to check sending transaction from zero address?
How I can fix this?
Best Answer
Nobody can have zero address. To be more precise given an address, it is impossible to find the private key correspond to that address. If anybody can do it then the whole world will collapse not just blockchain but the whole modern society as we know it.
So you don't need to check the zero address because nobody can have it. Zero address is just a place people dump their money to it. It is like a whole world garbage dump but the garbage is money