题目描述

image.png

解题过程

题目源码

  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. contract Instance {
  4. string public password;
  5. uint8 public infoNum = 42;
  6. string public theMethodName = 'The method name is method7123949.';
  7. bool private cleared = false;
  8. // constructor
  9. constructor(string memory _password) public {
  10. password = _password;
  11. }
  12. function info() public pure returns (string memory) {
  13. return 'You will find what you need in info1().';
  14. }
  15. function info1() public pure returns (string memory) {
  16. return 'Try info2(), but with "hello" as a parameter.';
  17. }
  18. function info2(string memory param) public pure returns (string memory) {
  19. if(keccak256(abi.encodePacked(param)) == keccak256(abi.encodePacked('hello'))) {
  20. return 'The property infoNum holds the number of the next info method to call.';
  21. }
  22. return 'Wrong parameter.';
  23. }
  24. function info42() public pure returns (string memory) {
  25. return 'theMethodName is the name of the next method.';
  26. }
  27. function method7123949() public pure returns (string memory) {
  28. return 'If you know the password, submit it to authenticate().';
  29. }
  30. function authenticate(string memory passkey) public {
  31. if(keccak256(abi.encodePacked(passkey)) == keccak256(abi.encodePacked(password))) {
  32. cleared = true;
  33. }
  34. }
  35. function getCleared() public view returns (bool) {
  36. return cleared;
  37. }
  38. }

主要是了解使用方法和基本操作

  1. await contract.info()
  2. // "You will find what you need in info1()."
  3. await contract.info1()
  4. // "Try info2(), but with "hello" as a parameter."
  5. await contract.info2('hello')
  6. // "The property infoNum holds the number of the next info method to call."
  7. await contract.infoNum()
  8. // 42
  9. await contract.info42()
  10. // "theMethodName is the name of the next method."
  11. await contract.theMethodName()
  12. // "The method name is method7123949."
  13. await contract.method7123949()
  14. // "If you know the password, submit it to authenticate()."
  15. await contract.password()
  16. // "ethernaut0"
  17. await contract.authenticate('ethernaut0')

image.png