Kontrak ABI dan awalan panjang rekursif memudahkan JVM.
ABI Spec: https://solidity.readthedocs.io/en/latest/abi-pec.html
Spec RLP: https://ethereum.org/en/developers/docs/data-struktur-and-encoding/rlp
SHA-256 (headlong-12.3.3.jar): 9b577af538d30ffc05133425c8b0bff3a79bcba99955b3222ddd2d825b27150cc
Function baz = function.parse ("baz (uint32, bool)"); // Canonicalize dan parse apa pun tanda tangan // orfunction f2 = Function.fromjson ("{" type ":" function "," name ":" foo "," inputs ": [{" name ":" complex_nums "," type ":" tuple [] "," com Ponents ": [{" name ":" real "," type ":" fixed168x10 "}, {" name ":" imaginary "," type ":" fixed168x10 "}]}]}"); pair <long, Boolean> bazargs = tuple.of (69l, true); tuple complexnums = single.of (tuple baru [] {tuple.of (BigDecimal baru ("0,0090000000"), BigDecimal baru ("1.9500000000"))); // Dua Gaya Setara: Bytebuffer Bazcall = baz.encodecall (bazargs); bytebuffer bazcall2 = baz.encodecallwithargs (69l, true); System.out.println ("Baz Call Hex: N" + Strings.encode (Bazcall) + "n"); // Hexadecimal Encoding (tanpa 0x awalan) tuple recoveredArgs = baz.decodecall (bazcall2); // Dekode pengkodean kembali ke argssystem.out.println asli ("Baz args: n" + recoveredArgs + "n"); // tostring () system.out.println ("sama: n" + recoveredArgs.equals (bazargs) + "n"); // Tes untuk EqualitySystem.out.println ("Baz Call Debug: n" + baz.AnnotateCall (bazcall.array ()) + "n"); // Manusia-dibaca, untuk panggilan fungsi debugging (mengharapkan input akan dimulai dengan 4-byte selector) system.out.println ("Baz Args Debug: n" + baz.getInputs (). Annotate (bazargs) + "n") ; // Manusia-dibaca, untuk pengkodean debugging tanpa selectorsystem.out.println ("f2 panggilan debug: n" + f2.annateCall (complexnums) + "n"); System.out.println ("F2 Args Debug: N" + f2.getInputs (). Annotate (complexnums));
Function foo = function.parse ("foo ((fixed [], int8) [1] [] [5])", "(int, string)"); // decode type return (int256, string) tuple decoded = foo.decodereturn (fasthex.decode ("00000000000000000000000000000000000000000000000000000000000000002A"+ "00000000000000000000000000000000000000000000000000000000000000000040"+ "000000000000000000000000000000000000000000000000000000000000000000.000E"+ "59616F62616E674569676874793900000000000000000000000000000000000000") ); System.out.println (decoded.equals (tuple.of (biginteger.valueof (42L), "Yaobangeighty9")));
Function footwo = function.parse ("footwo ()", "(uint8)"); int returned = footwo.decodesingletonRurn (fasthex.decode ("0000000000000000000000000000000000000000000000000000000000000000FF"); // uint8 sesuai dengan intsystem.out.println (dikembalikan);
Tupletype <puPle> tt = tupletype.parse ("(bool, alamat, int72 [] [])"); bytebuffer b0 = tt.encode (tuple.of (false, address.wrap ("0x52908985278886e0f7030069816") ")"), new -wrap, "new News5290998527788827e42989.of70300 Biginteger [0] [])); // tuple t = tt.decode (b0); // Dekode tuple (memiliki efek samping dari memajukan posisi Bytebuffer) // atau ... alamat a = tt.decode (b0, 1); // decode hanya indeks 1system.out.println (a); tuple t2 = tt.decode (b0, 0, 2); // decode hanya indeks 0 dan 2system.out.println (t2); bytebuffer b1 = tt. <abitype <biginteger [] [] >> get (2) .encode (biginteger baru [] [] {}); // encode hanya int72 [] []
Event <?> Event = event.fromjson ("{" type ":" event "," name ":" an_event "," inputs ": [{" name ":" a "," type ":" bytes ", "Diindeks": true}, {"name": "b", "type": "uint256", "diindeks": false}], "anonymous": true} "); tuple args = event.decodeArgs (byte baru [] [] {byte baru [32]}, byte baru [32]); System.out.println (event); System.out.println (args); // Buat semua jenis apa pun ArrayType Langsung (Lanjutan) <AbityPe <Peject>, ?, Object> at = Typefactory.create ("(alamat, int) []"); arraytype <tupletype <duple>, tuple, tuple []> at2 = typeFactory.create ("(alamat, int) []"); arraytype <tupletype <pair <pair <Alamat, BigInteger >>, Pasangan <Alamat, BigInteger>, Pair <Address, BigInteger> []> at3 = TypeFactory.Create ("(alamat, int) []"); Abitype <Peject> unknown = TypeFactory.Create (at.getCanonicalType ());
// Sebagai contoh kelas siswa yang menerapkan beberapa contoh antarmuka siswa (byte [] rlp) {iterator <rlpitem> iter = rlpdecoder.rlp_strict.SequenceIterator (rlp); this.name = iter.next (). astring (string.utf_8); this.gpa = iter.next (). asFloat (false); this.publickey = iter.next (). asbytes (); this.balance = BigDecimal baru (iter.next (). asbigint (), iter.next (). asInt ()); } @Overridepublic objek [] toObjectArray () {return Object baru [] {// Instances of Byte [] string.decode (name, string.utf_8), floatingpoint.tobytes (GPA), publicKey, balance.unscaledValue (). (), Integers.tobytes (balance.scale ()) // Sertakan objek [] atau iterable dan elemen -elemennya akan dikodekan sebagai daftar RLP (yang mungkin termasuk daftar lainnya)}; } @OverridEpublic byte [] torlp () {return rlpencoder.Sequence (toobjectArray ()); }
Sekarang tersedia di repositori pusat Maven.
Atau membangun secara lokal:
Klon proyek dan instal ke repositori Maven lokal Anda menggunakan gradle publishToMavenLocal
atau mvn install
, lalu nyatakan sebagai ketergantungan:
Implementasi ("com.esaulpaugh: headlong: 12.3.4-snapshot")
<sgenepency> <GroupId> com.esaulpaugh </groupid> <ArtifactId> headlong </artifactid> <version> 12.3.4-Snapshot </version> </dependency>
Atau:
Jalankan gradle build
atau gradle jar
yang output untuk build/libs
Gunakan mvn package
yang output untuk target
Jalankan ant all build-jar
yang menghasilkan build/lib
Tambahkan headlong sebagai ketergantungan proyek
Graalvm 20.0.2 di x86-64
https://github.com/esaulpaugh/headlong-li
https://github.com/esaulpaugh/headlong-android
Juga termasuk implementasi yang dioptimalkan dari:
Catatan simpul EIP-778 Ethereum
EIP-55 Pengkodean Alamat Centang Case
Keccak
hexadecimal
Headlong tergantung pada GSON V2.10.1 untuk paket ABI. Test Suite harus membutuhkan waktu kurang dari satu menit untuk dijalankan. Paket uji membutuhkan junit. Ukuran toples ~ 128 kib. Java 8+.
Lihat wiki untuk lebih lanjut, seperti pengkodean yang dikemas (dan decoding) dan notasi objek RLP: https://github.com/esaulpaugh/headlong/wiki
Dilisensikan dengan ketentuan Apache 2.0