Coder Social home page Coder Social logo

zhaohaijun / headlong Goto Github PK

View Code? Open in Web Editor NEW

This project forked from esaulpaugh/headlong

0.0 2.0 0.0 663 KB

A Contract ABI and Recursive Length Prefix library in Java for use on the Ethereum network

License: Apache License 2.0

Java 100.00%

headlong's Introduction

Maven Central

Contract ABI and Recursive Length Prefix made easy in Java (or Android). Everything heavily optimized for maximum throughput (ABI function call encoding up to 652x faster than a popular competitor).

Usage of the ABI codec:

Function f = new Function("baz(uint32,bool)"); // canonicalizes and parses any signature automatically
Tuple args = new Tuple(69L, true);

// Two equivalent styles:
ByteBuffer one = f.encodeCall(args);
ByteBuffer two = f.encodeCallWithArgs(69L, true);

System.out.println(Function.formatCall(one.array())); // a multi-line hex representation

Tuple decoded = f.decodeCall((ByteBuffer) two.flip());

System.out.println(decoded.equals(args));

And of the RLP codec:

// for an example class Student
public Student(byte[] rlp) throws DecodeException {
    SequenceIterator iter = RLP_STRICT.sequenceIterator(rlp);

    this.name = iter.next().asString(UTF_8);
    this.gpa = iter.next().asFloat();
    this.publicKey = iter.next().asBigInt();
    this.balance = new BigDecimal(iter.next().asBigInt(), iter.next().asInt());
}

@Override
public Object[] toObjectArray() {
    return new Object[] {
            Strings.decode(name, UTF_8),
            FloatingPoint.toBytes(gpa),
            publicKey.toByteArray(),
            balance.unscaledValue().toByteArray(),
            Integers.toBytes(balance.scale())
    };
}

@Override
public byte[] toRLP() {
    return RLPEncoder.encodeSequentially(toObjectArray());
}

Also includes a fast hex codec and an optimized Keccak implementation.

Build

Now available in Maven Central Repository.

Or build locally:

Clone the project and install to your local maven repository using gradle publishToMavenLocal or mvn install. Then you can use one of these:

implementation 'com.esaulpaugh:headlong:1.0-SNAPSHOT'
<dependency>
    <groupId>com.esaulpaugh</groupId>
    <artifactId>headlong</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

Alternatively:

  • Run gradle build or gradle jar which output to /build/libs
  • Use mvn package which outputs to /target
  • Add headlong as a project dependency

Tests should take less than one minute to run. Test packages require junit and gson. Otherwise headlong has no dependencies. Jar size is ~109 KB as of 01/17/19.

See the wiki for more, such as TupleTypes, packed encoding, RLP Lists, and RLP Object Notation: https://github.com/esaulpaugh/headlong/wiki

Licensed under Apache 2.0

headlong's People

Contributors

esaulpaugh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.