Ether

無法解碼輸入數據

  • November 6, 2022

我正在嘗試從交易中解碼輸入數據,但沒有收到來自各種解碼方法的預期結果。根據我的故障排除,我懷疑在解碼交易的輸入數據時提供的 ABI 輸入存在一些問題。我使用的 ABI 內容是從 Etherscan 收到的 Aave 收集器契約,如下所示。

[{"inputs":[{"internalType":"address","name":"admin","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"implementation","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_logic","type":"address"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"initialize","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"}]

當我執行以下命令時,我沒有收到預期的輸出。

   const InputDataDecoder = require('ethereum-input-data-decoder');
   const abi = require('../data/abi.json');
   const decoder = new InputDataDecoder(abi);

   const data = '0xa415bcad000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000005d21dba00000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000006576f802d9b685896e1b710cccae9af8f3ce368f';

   const result = decoder.decodeData(data);

相反,我收到該方法的空響應

{
 method: null,
 types: [ 'address' ],
 inputs: [ '0x00000000000000000000000000000005d21Dba00' ],
 names: [ 'admin' ]
}

在 Etherscan 上,解碼此事務的輸入數據時,我期望得到以下結果:0xffc11133bd95549b6682a8c6f3bfe9b8a0518af894824bf82db6c0c4939a31f2 在此處輸入圖像描述

我也嘗試過使用 ConsenSys/abi-decoder,但收到未定義的輸出。

const abiDecoder = require('abi-decoder'); 
const abi = require('../data/abi.json');

abiDecoder.addABI(abi);
const testData = "0xa415bcad000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000005d21dba00000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000006576f802d9b685896e1b710cccae9af8f3ce368f";
const decodedData = abiDecoder.decodeMethod(testData);

I am unable to determine why the decoding will not function as expected.

您共享的那個 abi 沒有borrow(address,uint256,uint256,uint16,address)定義和輸入。也許這就是為什麼abi-decoder無法辨識並且無法解碼它的原因。

keccak-256函式雜湊的前 4 個字節borrow(address,uint256,uint256,uint16,address)是:0xa415bcad. 當abi-decoder嘗試將數據的前 4 個字節 , 匹配0xa415bcad到提供的 abi 的函式時,它沒有找到它,因此它不知道如何解碼它。

我建議您更新該 abi 以包含borrow(address,uint256,uint256,uint16,address)函式定義。

如果你不能,並且你知道輸入的類型,那麼你可以手動刪除函式選擇器並使用如下方式解碼數據web3

const removeFunctionSelector = (data) => {
 // Removes the function signature from the data (the first 10 characters)
 // It assumes that the `data` always has the `0x` prefix. It adds it again after removing the function signature.
 return `0x${data.substring(10)}`;
};

const data =
 "0xa415bcad000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000005d21dba00000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000006576f802d9b685896e1b710cccae9af8f3ce368f";

const decoded = web3.eth.abi.decodeParameters(
 // Decoding event data. Exclude the `indexed` parameters.
 ["address", "uint256", "uint256", "uint16", "address"],
 removeFunctionSelector(data)
);

console.log("decoded: ", decoded);

為我列印的:

decoded:  Result {
 '0': '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
 '1': '25000000000',
 '2': '2',
 '3': '0',
 '4': '0x6576F802D9b685896e1b710CccAE9af8F3cE368F',
 __length__: 5
}

引用自:https://ethereum.stackexchange.com/questions/138854