抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Choose a nickname

1. 题目

  • 1.1 智能合约CaptureTheEther会记录每个玩家的昵称。要完成此挑战,请将您的昵称设置为非空字符串。智能合约在地址 的 Ropsten 测试网络上运行0x71c46Ed333C35e4E6c62D32dc7C8F00D125b4fee
  • 1.2 题目代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPDX-License-Identifier: MIT
pragma solidity ^0.4.21;

// Relevant part of the CaptureTheEther contract.
contract CaptureTheEther {
mapping (address => bytes32) public nicknameOf;

function setNickname(bytes32 nickname) public {
nicknameOf[msg.sender] = nickname;
}
}

// Challenge contract. You don't need to do anything with this; it just verifies
// that you set a nickname for yourself.
contract NicknameChallenge {
CaptureTheEther cte = CaptureTheEther(msg.sender);
address player;

// Your address gets passed in as a constructor parameter.
function NicknameChallenge(address _player) public {
player = _player;
}

// Check that the first character is not null.
function isComplete() public view returns (bool) {
return cte.nicknameOf(player)[0] != 0;
}
}


2. 分析

  • 2.1 只需要将成功调用setNickname() 函数即可
  • 2.2 考点在于如何将字符串转为64位 16进制的数据类型
  • 我在idea 编写了一个程序来解决这个问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package daily_temp;

/**
* @Auther: 柚子
* @Date:2023/3/24年03月24日 15:43
* @Description:daily_temp
* @version: 1.0
*/
public class convertStringToHex {
public static void main(String[] args) {
covertStringToHex("biyou");
}

static void covertStringToHex(String str) {
StringBuffer sb = new StringBuffer();

char[] chars = str.toCharArray();

for(char c : chars) {
String charToHex = Integer.toHexString(c);
sb.append(charToHex);
}

int length = sb.length();
for (int i = 0; i < 64 - length; i++) {
sb.append(0);
}

System.out.println("0x" + sb.toString());
}

}

  • 输入为 biyou 运行结果为:
  • ![image-20240412144152362](Choose a nickname/image-20240412144152362.png)
  • 0x6269796f75000000000000000000000000000000000000000000000000000000 为参数,调用 setNickname() 函数即可

3. 解题

  • 3.1 部署
  • 3.2 将转化好的数据以参数的形式传入
  • 3.3
  • ![image-20240412144208239](Choose a nickname/image-20240412144208239.png)

评论



政策 · 统计 | 本站使用 Volantis 主题设计