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

前言

abi.encodePacked 函数将其参数按顺序拼接起来,不添加任何类型标识符或分隔符,并返回一个紧凑的字节数组,不包含长度信息。这种编码方式适用于构造哈希值和签名的情况,因为它生成的字节数组不受字节对齐的影响。

abi.encode 函数则将其参数编码为包含类型标识符和长度信息的字节数组,并返回一个动态字节数组,它包含了参数所需的所有信息。这种编码方式适用于在 Solidity 智能合约中调用其他合约函数的情况,因为它生成的字节数组可以被正确地解码为对应的参数类型。

ethersjs 中的 new ethers.utils.AbiCoder().encode(['type'], [value]) <=> 相当于 solidity中的 abi.encode()

ethersjs 中的 ethers.utils.solidityKeccak256(['type'],[value]) <=> 相当于 Solidity中的keccak256(abi.encodePacked())

ethersjs 中的 ethers.utils.keccak256() <=> 相当于 Solidity中的 keccak256()

ethersjs 中的 ethers.utils.solidityPack(['type'], [value]) <=> 相当于 Solidity中的 abi.encodePacked()

js代码

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { ethers } from "ethers";
import fs from "fs";

// 获取本地的provider
const provider = new ethers.providers.JsonRpcProvider("HTTP://127.0.0.1:8545");

// 读取文件的abi
const ABI = JSON.parse(fs.readFileSync("D:\\VSCode\\VSCode_Data\\ethersjsWorkspace\\LearningSelf\\test\\day04\\getHash.abi").toString());

// 通过ABI获取接口类
const infe = new ethers.utils.Interface(ABI);

// 获取函数选择器
const getHashByAddress = infe.getSighash("getHashByAddress");
console.log(`getHashByAddress is => ${getHashByAddress}`);


/**
* ethersjs 中的 ethers.utils.AbiCoder() <=> 相当于 solidity中的 abi.encode()
*
*/
const abiCoder = new ethers.utils.AbiCoder();
// 创建消息
let msg1 = "getHashByAddress";
let msg1Hash = abiCoder.encode(
['string'],
[msg1]
)
let hash = ethers.utils.keccak256(msg1Hash);
console.log(`hash is => ${hash}`);


// 创建消息
const msg = "getHashByAddress";
// 等效于Solidity中的keccak256(abi.encodePacked())
const msgHash1 = ethers.utils.solidityKeccak256(
['string'],
[msg]
)
console.log(`msgHash: ${msgHash1}`)
// msgHash:0x1bf2c0ce4546651a1a2feb457b39d891a6b83931cc2454434f39961345ac378c

// 先对 msg 进行打包
const msgHash2 = ethers.utils.solidityPack(
['string'],
[msg]
)

// 这两步等效于 `solidityPack()`
const hash1 = ethers.utils.keccak256(msgHash2);

console.log(`msgHash2: ${msgHash2}`);

console.log(`hash1 is: ${hash1}`);

solidity 合约

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract getHash {

constructor() {

}

function getHashByAddress(address _address) external pure returns(bytes32) {
bytes32 result = keccak256(abi.encodePacked(_address));
return result;
}


function getHashByString1(string memory _str) external pure returns(bytes32) {
// abi.encodePacked(_str);
// bytes32 result = keccak256(abi.encodePacked(_str));
// bytes32 result = keccak256(abi.encode(_str));
bytes32 result = keccak256(abi.encode(_str));
return result;
}


function getHashByString2(string memory _str) external pure returns(bytes32) {
// bytes32 str = bytes32(abi.encodePacked(_str));
// bytes32 result = keccak256(abi.encodePacked(_str));
bytes32 result = keccak256(abi.encodeWithSignature(_str));
// bytes32 result = keccak256(abi.encode(str));
return result;
}

function getHashByString3(string memory _str) external pure returns(bytes32) {
// bytes32 str = bytes32(abi.encodePacked(_str));
bytes32 result = keccak256(abi.encodePacked(_str));
// bytes32 result = keccak256(abi.encode(_str));
// bytes32 result = keccak256(abi.encode(str));

return result;
}


function getHashByString4(string memory _str) external pure returns(bytes32) {
// bytes32 str = bytes32(abi.encodePacked(_str));
bytes32 result = sha256(abi.encodePacked(_str));
// bytes32 result = keccak256(abi.encode(_str));
// bytes32 result = keccak256(abi.encode(str));

return result;
}

function getEncode(string memory _str) external pure returns(bytes memory result) {
result = abi.encode(_str);
// return ;
}


function getEncodePacked(string memory _str) external pure returns(bytes memory result) {
result = abi.encodePacked(_str);
// return ;
}

}

abi文件

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "string",
"name": "_str",
"type": "string"
}
],
"name": "getEncode",
"outputs": [
{
"internalType": "bytes",
"name": "result",
"type": "bytes"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_str",
"type": "string"
}
],
"name": "getEncodePacked",
"outputs": [
{
"internalType": "bytes",
"name": "result",
"type": "bytes"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_address",
"type": "address"
}
],
"name": "getHashByAddress",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_str",
"type": "string"
}
],
"name": "getHashByString1",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_str",
"type": "string"
}
],
"name": "getHashByString2",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_str",
"type": "string"
}
],
"name": "getHashByString3",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_str",
"type": "string"
}
],
"name": "getHashByString4",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "pure",
"type": "function"
}
]

remix上的结果:

image-20240412172415532

vscode的结果:

image-20240412172425475

评论



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