0

how can I pass a parameter to an access modifier both in Solidity and Web3. Let's assume I have the following code:

modifier passwordRequired(string _pw){
  require(keccak256(_pw) == keccak256("topSecret"));
  _;
}

function doSomething(unit _param) public passwortRequired(_pw){
  ...
}

How can I call function doSomething and pass a string to the access modifier?

0

When you pass parameter in solidity function then same parameter is passed to modifier. you don't need to pass separate parameter for modifier.

Lets change your code as:

 modifier passwordRequired(string _pw){
  require(keccak256(_pw) == keccak256("topSecret"));
  _;
}

function doSomething(string _param) public passwordRequired(_param){
  //...
} 

This code says that the parameter _param you sent is the password . If you want to pass separate password other then _param then you have to pass other parameter in function.

function doSomething(string _param, string _pw) public passwordRequired(_pw){...}

Now you see that same function parameter _pw is passed to modifier passwordRequired(_pw).

| improve this answer | |
1

Add modifier parameter to function parameter as well:

modifier passwordRequired(string _pw){
  require(keccak256(_pw) == keccak256("topSecret"));
  _;
}

function doSomething(unit _param, string _pw) public passwortRequired(_pw){
  ...
}
| improve this answer | |

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.