8 - Vault

reading storage slot

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Vault {
  bool public locked;
  bytes32 private password;

  constructor(bytes32 _password) {
    locked = true;
    password = _password;
  }

  function unlock(bytes32 _password) public {
    if (password == _password) {
      locked = false;
    }
  }
}

Goal of this level

  • Make the value of locked to be false

What you should know before

Solution

chevron-rightKey to solve this problem πŸ”‘hashtag

locked variable will be stored at slot 0, and password variable will be stored at slot 1.

You can read storage variables using web3.eth.getStorageAt()

We can read slot1 and pass it as a parameter calling unlock().

Done! 😎

Key Takeaways

  • You can always read storage variables

  • So make sure not to store private information!

Last updated