Ethereum 在发送到以太坊网络之前,智能合约功能的输入数据如何打包为十六进制字符串
我想了解这一点,以便解析私有链事务中的数据,并获取为特定事务发送的输入数据,我尝试了许多解码器,但在某些情况下,它们失败了。 这是我尝试使用混音的简单智能合约Ethereum 在发送到以太坊网络之前,智能合约功能的输入数据如何打包为十六进制字符串,ethereum,Ethereum,我想了解这一点,以便解析私有链事务中的数据,并获取为特定事务发送的输入数据,我尝试了许多解码器,但在某些情况下,它们失败了。 这是我尝试使用混音的简单智能合约 contract simple{ uint256 deliveryID; string status; function stringAndUint(string _status,uint256 _deliveryID){ status=_status;
contract simple{
uint256 deliveryID;
string status;
function stringAndUint(string _status,uint256 _deliveryID){
status=_status;
deliveryID=_deliveryID;
}
}
生成的输入数据:-0x3C38B7FD000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000674617475730000000000000000000000000000000000000000000000000000000000
我可以从上面解释以下内容
- 函数签名:0x3c38b7fd
- _状态值:737461747573
- _送货ID:0c但我不知道为什么在737461747573之前会有4个,而额外的6个李>
- 功能“stringAndUint”的输入为:“状态”,12 有人能帮助我理解输入数据是如何生成的,以及长十六进制字符串中的数据包吗
- 3c38b7fd(功能签名)
- 00000000000000000000000000000000000000000000000000000000 40(第一个参数的数据部分的位置,从arguments块开始以字节为单位测量)
- 0000000000000000000000000000000000000000000000000000000000000000C(12)
- 0000000000000000000000000000000000000000000000000000000000000000006(状态的长度)。前面的0..040点在这里)
- 7374617475730000000000000000000000000000000000000000(“状态”然后零填充到下一个32字节的倍数)
- 3c38b7fd(功能签名)
- 00000000000000000000000000000000000000000000000000000000 40(第一个参数的数据部分的位置,从arguments块开始以字节为单位测量)
- 0000000000000000000000000000000000000000000000000000000000000000C(12)
- 0000000000000000000000000000000000000000000000000000000000000000006(状态的长度)。前面的0..040点在这里)
- 7374617475730000000000000000000000000000000000000000(“状态”然后零填充到下一个32字节的倍数)
from eth_utils import to_bytes
encoded = to_bytes(hexstr="0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000067374617475730000000000000000000000000000000000000000000000000000")
from eth_abi import decode_abi
decoded = decode_abi(['string', 'uint256'], encoded)
assert decoded == (b'status', 12)
使用的编码是什么?
Solidity使用一个字符进行编码
额外的(十六进制)40和6是怎么回事?
@Brendan关于这些值的回答比我的好,所以我将删除这一部分。我会把答案贴出来,因为下面的部分仍然有用
程序复制
python中有一个ABI解码工具,名为,您可以像这样使用它:
from eth_utils import to_bytes
encoded = to_bytes(hexstr="0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000067374617475730000000000000000000000000000000000000000000000000000")
from eth_abi import decode_abi
decoded = decode_abi(['string', 'uint256'], encoded)
assert decoded == (b'status', 12)
顺便说一句,
解码后的
看起来坏了:我希望它会作为'status'
而不是b'status'
返回。我将在上面提交一张票证。顺便说一句,解码后的
看起来坏了:我希望它会作为'status'
而不是b'status'
返回。我要去登记一下。