合同ABI和递归长度前缀使JVM变得容易。
ABI规格:https://solity.readthedocs.io/en/latest/abi-spec.html
RLP规格:https://ethereum.org/en/developers/docs/data-sonstructures-and-encoding/rlp
SHA-256(Headlong-122.3.3.jar):9B577AF538D30FFC05133425C8BFFF3A79BCBA999999999999995B322222DD2D825B27150CC
函数baz = function.parse(“ baz(uint32,bool)”); //规范和解析任何签名// orfunction f2 = function.fromjson(“ {“ type”:“ function”,“ name”:“ foo”,“ inputs”:[{“ name”:“ complect_nums”,“ type”:“ tuple [],“ com” ponents“:[{“ name”:“ real”,“ type”:“ field168x10”},{“ name”:“ himiminary”,“ type”,“ field168x10”}]}]}}”); pair <long, boolean> bazargs = tuple.of(69l,true); tuple complextnums = single.of(new tuple [] {tuple.of(new bigdecimal(“ 0.0090000000”)(“ 0.0090000000”),new BigDecimal(“ 1.950000000000”); // //两种同等样式:ByteBuffer BazCall = BAZ.EncodeCall(Bazargs); ByteBuffer BazCall2 = Baz.EncodeCallWithArgs(69L,true); System.Out.Println(“ Baz Call hex:n” + strings.Encode.Encode.Encode.Encode(bazcall)(bazcall) +“ n”); //十六进制编码(不带0x前缀)元组回收= baz.decodecall(bazcall2); //将编码解码回原始argssystem.out.println(“ baz args:n” + encounteredArgs +“ n”); // toString()system.out.println(“等于:n” + encountedArgs.equals(bazargs) +“ n”); // equalityStem.out.println的测试(“ baz call debug:n” + baz.annotatecall(bazcall.array()) +“ n”); // human-读取,用于调试功能调用(期望输入以4个字节选择器开始)system.out.println(“ baz args debug:n” + baz.getInputs()。antotate(bazargs) +“ n”) ; // HUMAN-REDABLE,用于无需选择的调试编码。 + f2.getInputs()。antotate(confffectNums));
function foo = function.parse(“ foo((固定[],int8)[1] [1] [5])”,“(int,string)”); // decode return type(int256,string)tuple decoded = = foo.decodereturn(fasthex.decode(“ 000000000000000000000000000000000000000000000000000000000000000000000000000000002A”+ “ 0000000000000000000000000000000000000000000000000000000000000000000000000000000040”+“ 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E”+ “ 59616F62616E67456967687793939000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Y来”) ); system.out.println(解码。Equals(tuple.of.of(biginteger.valueof(42L)),“ yaobangeighty9”)));
函数footwo = function.parse(“ footwo()”,“(uint8)”); int返回= footwo.decodesingletonretonretrunt(fasthex.decode(“ 000000000000000000000000000000000000000000000000000000000000000000000000000000000000ff)(” // uint8对应于intsystem.out.println(返回);
tupletype <tuple> tt = tupletype.parse(“(“(布尔,地址,int72 [] [])”); bytebuffer b0 = tt.Encode(tuple.of(false,address.wrap)(“ 0x5290840008400098527886E biginteger [0] [])); // tuple t = tt.decode(b0); //解码元组(具有推进字节卷的位置的副作用)//或...地址a = tt.decode(b0,1); //仅解码index 1system.out.println(a); tuple t2 = tt.decode(b0,0,2); //仅解码索引0和2system.out.println(t2); bytebuffer b1 = tt。<abitype <biginteger [] [] [] >> get(2).encode(new biginteger [] [] [] [] [] [] {] {}); //仅编码INT72 [] [] []
事件<?> event = event.fromjson(“ {“ type”:“ event”,“ name”:“ an_event”,“ inputs”:[{“ name”:“ a”,“ type”:“ bytes”,“ indexed”:true} ,{“ name”:“ b”,“ type”:“ uint256”,“索引”:false}],“匿名”:true}”); tuple args = event.decodeargs(new byte [] [] {new Byte [32]},new Byte [32]); System.out.println(event); system.out.out.println(args); //直接创建任何类型(高级)arrayType <abitype <obsect>?,object> at = typefactory.create(“(地址,int)[]”); arrayType <tupletype <tuple>,tuple,tuple,tuple []> typefactory.create.create(“(“(advelion,int)[]); arrayType <tupletype <tupletype <tupletype <papletype <Paip <Paip <Paip <Paip <Pair <Pair <Pair <Pair <Pair <Pair <Pair <Pair <Pair <Paip <Pair <Pair <Pair <Pair <Pair <地址,biginteger >>,Pair <地址,BigInteger>,Pair <地址,BigInteger> []> AT3 = typefactory.create(“(address,int)[]”); abitype <botice> unknown = typeFactory.Create(at.getCanonicalType());
//对于实施某些示例的示例学生interfacePublic Student(byte [] rlp){iterator <rlpitem> iter = rlpdecoder.rlp_strict.sequence.seperence.secoreiterator(rlp); this.name = iter.next()。asstring(strings.utf_8); this.gpa = iter.next()。新的bigdecimal(iter.next()。asbigint(),iter.next()。asint()); } @OverRidePublic对象[] (),integers.tobytes(balance.scale())//包括一个对象[]或具有峰值及其元素将被编码为RLP列表(其中可能包括其他列表)}}; } @OverRidePublic byte [] torlp(){return rlpencoder.sequence(toObjectArray()); }
现在可在Maven Central存储库中找到。
或在本地建造:
克隆项目,并使用gradle publishToMavenLocal
或mvn install
安装到您的本地Maven存储库,然后将其声明为依赖项:
实施(“ com.esaulpaugh:headlong:12.3.4-snapshot”)
<依赖项> <groupId> com.esaulpaugh </groupid> <Artifactid>头长</artifactid> <版本> 12.3.4-snapshot </version> </dependency>
或者:
运行gradle build
或gradle jar
,输出要build/libs
使用输出target
mvn package
执行ant all build-jar
,以build/lib
将headlong添加为项目依赖性
graalvm 20.0.2在x86-64上
https://github.com/esaulpaugh/headlong-cli
https://github.com/esaulpaugh/headheadlong-android
还包括以下优化的实现:
EIP-778以太坊节点记录
EIP-55混合案例校验和地址编码
凯卡克
十六进制
Headlong取决于ABI软件包的GSON v2.10.1。测试套件应不到一分钟才能运行。测试包需要Junit。罐子尺寸为〜128 KIB。 Java 8+。
有关更多信息,请参见Wiki,例如包装编码(和解码)和RLP对象符号:https://github.com/esaulpaugh/headlong/headlong/wiki
根据Apache 2.0条款许可