I am trying to create a password in the constructor when deploying a smart contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract passwordContract {
bytes32 password;
address owner;
constructor(uint32 _password) {
owner = msg.sender;
password = keccak256(abi.encodePacked(_password,msg.sender));
}
modifier onlyOwner() {
require(msg.sender == owner, "only owner");
_;
}
function getPassword() external view onlyOwner returns(bytes32) {
return password;
}
// can anyone call this function, but need a password in parameter.
function canCallAnyOne(bytes32 _password) public {
require(_password == password,"wrong password");
// do something
}
}
As in the example above, put the password in the function's parameter so that only those who know the password can run the function.
I have two questions.
When deploying a smart contract, can the miner figure out the password?
I would like to create a function like the one above. Can anyone advise a better way?