以太坊交易記錄input解析
阿新 • • 發佈:2018-12-20
引用web3j jar包
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
</dependency>
程式碼如下
package vip.coinbag.managemoneymatters;
import org.junit.Test;
import org.web3j.abi.TypeDecoder;
import org.web3j. abi.datatypes.Address;
import org.web3j.abi.datatypes.generated.Uint256;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* <p>類描述:。</p>
*
* @author jl
* @version v1.0.0.1。
* @since JDK1.8。
* <p>建立日期:2018/11/1 9:44。</p>
*/
public class test {
/**
* <p>功能描述:解析以太坊查詢交易記錄中的input資料,獲取方法名、地址和金額。</p>
* <p>jl</p>
* @since JDK1.8
* <p>建立日期:2018/11/9 17:15。</p>
* <p>更新日期:[日期YYYY-MM-DD][更改人姓名][變更描述]。</p>
*/
@Test
public void decodeInput() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String inputData = "0xa9059cbb00000000000000000000000035f5992e40facfcad742fcfcc1d94ee0581e9cb100000000000000000000000000000000000000000000003635c9adc5dea00000";
String method = inputData.substring(0, 10);
System.out.println(method);
String to = inputData.substring(10, 74);
String value = inputData.substring(74);
Method refMethod = TypeDecoder.class.getDeclaredMethod("decode", String.class, int.class, Class.class);
refMethod.setAccessible(true);
Address address = (Address) refMethod.invoke(null, to, 0, Address.class);
System.out.println(address.toString());
Uint256 amount = (Uint256) refMethod.invoke(null, value, 0, Uint256.class);
System.out.println(amount.getValue());
}
}
結果如下
0xa9059cbb
0x35f5992e40facfcad742fcfcc1d94ee0581e9cb1
1000000000000000000000