合同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)”); // canonicalize和解析任何簽名// orfunction f2 = function.fromjson(“ {“ type”:“ function”:“ function”,“ name”:“ foo”,“ inputs”,“ inputs”:[{“ name 'name'':“ complect_nums”,“ type”類型“:”元組[],“組件”:[{“ name”:“ real”,“ type”:“ field168x10”},{“ name”:“ imaginary”,“ type”,“ type”:“ fixed168x10”}]}]}} ]}”); pair <long,boolean> bazargs = tuple.of(69l,true); tuple confffersnums = single.of(new tuple [] {tuple.of(new BigDecimal(new BigDecimal(“ 0.0090000000”) 1.9500000000“”)}); // //兩種等式樣式:bytebuffer bazcall = baz.encodeCall(bazargs); bytebuffer bazcall2 = baz. encodecallwithargs(69l,true); system.ut.print.ut.print.ut.println( .encode(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(“ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000來州) 0000000E“+” 59616F62616E674569676768779390000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Y000000000000000000000000000000來 ); 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(“(“(bool,address,int72 [] [])”); bytebuffer b0 = tt.Encode(tuple.of(false,address.wrap(“ 0x529084000840009852786E) 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 = event.fromjson(“ {“ type”:“ event”,“ name”:“ an_event”,“ inputs”:[{“ name”:“ a”,“ a”,“ type”: “ bytes”, “索引”:true},{“ name”:“ b”,“ type”:“ uint256”,“ indexed”:false}],“匿名”:true}“:true}”); tuple args = event.decodeargs(depecodeargs.decodeargs) [] [] {new byte [32]},new Byte [32]); System.out.println(event); System.out.println(args); // / /直接創建任何類型object>,?,對象> at = typefactory.create(“(address,int)[]); arrayType <tupletype <tuple>,tuple,tuple,tuple []> at2 = typefactory.create.create .create(“(地址,int)[,int)[ ]”); arrayType <tupletype <pair <地址,biginteger >>,Pair <地址,biginteger>,Pair <地址,biginteger> []> at3 = typefactory. create.create(“(“地址,int)[] [] <obsect> 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條款許可