我试图将地址转换为字节,以便对值执行按位操作,但注意到在0.8.0版本中有一个中断的变化。如何通过转换address值来修改此代码以对地址执行操作:
function register(
bytes32[] memory _proofs,
uint8 _bits,
address _index,
bytes32 _leaf
) internal pure returns (bytes32) {
require(_index < SIZE, "_index bigger than tree size");
require(_proofs.length <= DEPTH, "Invalid _proofs length");
// test bitwise logic
for (uint256 d = 0; d < DEPTH; d++) {
if ((_index & 1) == 1) {
if ((_bits & 1) == 1) {
_leaf = hash(_proofs[d], _leaf);
} else {
_leaf = hash(0, _leaf);
}
} else {
if ((_bits & 1) == 1) {
_leaf = hash(_leaf, _proofs[d]);
} else {
_leaf = hash(_leaf, 0);
}
}
_bits = _bits >> 1;
_index = _index >> 1;
}
return _leaf;
}这是SMT实现的一部分,我希望index是address,叶是1的uint值。
发布于 2023-01-26 21:23:23
从Solity0.8.0开始,您不能再直接将地址转换为bytes32。您必须执行两个单独的转换:首先是对bytes20的转换,它将类型从address更改为固定字节,然后才转换到bytes32,后者扩展了长度。
bytes32(bytes20(address))https://ethereum.stackexchange.com/questions/143730
复制相似问题