Skip to content

Commit 5adcf7c

Browse files
committed
add abi_Chainsql project
1 parent eaf94fb commit 5adcf7c

180 files changed

Lines changed: 6523 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

abi_chainsql/.classpath

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>

abi_chainsql/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>abi_chainsql</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

abi_chainsql/pom.xml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.peersafe</groupId>
7+
<artifactId>chainsql-java-api</artifactId>
8+
<version>1.4.4</version>
9+
</parent>
10+
<groupId>com.peersafe</groupId>
11+
<artifactId>abi_chainsql</artifactId>
12+
<version>1.4.4</version>
13+
<name>abi_chainsql</name>
14+
<url>http://maven.apache.org</url>
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.bouncycastle</groupId>
21+
<artifactId>bcprov-jdk15on</artifactId>
22+
<version>1.56</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.11</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mockito</groupId>
32+
<artifactId>mockito-core</artifactId>
33+
<version>1.3</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
37+
<dependency>
38+
<groupId>org.hamcrest</groupId>
39+
<artifactId>hamcrest-core</artifactId>
40+
<version>1.3</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<!-- https://mvnrepository.com/artifact/org.objenesis/objenesis -->
44+
<dependency>
45+
<groupId>org.objenesis</groupId>
46+
<artifactId>objenesis</artifactId>
47+
<version>2.1</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<!-- https://mvnrepository.com/artifact/org.web3j/utils -->
51+
<dependency>
52+
<groupId>org.web3j</groupId>
53+
<artifactId>utils</artifactId>
54+
<version>3.4.0</version>
55+
</dependency>
56+
</dependencies>
57+
<profiles>
58+
<profile>
59+
<id>release</id>
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>3.2</version>
66+
<configuration>
67+
<source>1.8</source>
68+
<target>1.8</target>
69+
</configuration>
70+
</plugin>
71+
<!-- Source -->
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-source-plugin</artifactId>
75+
<version>2.2.1</version>
76+
<executions>
77+
<execution>
78+
<phase>package</phase>
79+
<goals>
80+
<goal>jar-no-fork</goal>
81+
</goals>
82+
</execution>
83+
</executions>
84+
</plugin>
85+
<!-- Javadoc -->
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-javadoc-plugin</artifactId>
89+
<version>2.9.1</version>
90+
<executions>
91+
<execution>
92+
<phase>package</phase>
93+
<goals>
94+
<goal>jar</goal>
95+
</goals>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
<!-- GPG -->
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-gpg-plugin</artifactId>
103+
<version>1.6</version>
104+
<executions>
105+
<execution>
106+
<phase>verify</phase>
107+
<goals>
108+
<goal>sign</goal>
109+
</goals>
110+
</execution>
111+
</executions>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
<distributionManagement>
116+
<snapshotRepository>
117+
<id>oss</id>
118+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
119+
</snapshotRepository>
120+
<repository>
121+
<id>oss</id>
122+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
123+
</repository>
124+
</distributionManagement>
125+
</profile>
126+
</profiles>
127+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.peersafe.abi;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import com.peersafe.abi.datatypes.Event;
8+
import com.peersafe.abi.datatypes.Type;
9+
import org.web3j.crypto.Hash;
10+
import org.web3j.utils.Numeric;
11+
12+
/**
13+
* <p>Ethereum filter encoding.
14+
* Further limited details are available
15+
* <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#events">here</a>.
16+
* </p>
17+
*/
18+
public class EventEncoder {
19+
20+
private EventEncoder() { }
21+
22+
public static String encode(Event function) {
23+
List<TypeReference<Type>> indexedParameters = function.getIndexedParameters();
24+
List<TypeReference<Type>> nonIndexedParameters = function.getNonIndexedParameters();
25+
26+
String methodSignature = buildMethodSignature(function.getName(),
27+
indexedParameters, nonIndexedParameters);
28+
29+
return buildEventSignature(methodSignature);
30+
}
31+
32+
static <T extends Type> String buildMethodSignature(
33+
String methodName, List<TypeReference<T>> indexParameters,
34+
List<TypeReference<T>> nonIndexedParameters) {
35+
36+
List<TypeReference<T>> parameters = new ArrayList<>(indexParameters);
37+
parameters.addAll(nonIndexedParameters);
38+
39+
StringBuilder result = new StringBuilder();
40+
result.append(methodName);
41+
result.append("(");
42+
String params = parameters.stream()
43+
.map(p -> Utils.getTypeName(p))
44+
.collect(Collectors.joining(","));
45+
result.append(params);
46+
result.append(")");
47+
return result.toString();
48+
}
49+
50+
public static String buildEventSignature(String methodSignature) {
51+
byte[] input = methodSignature.getBytes();
52+
byte[] hash = Hash.sha3(input);
53+
return Numeric.toHexString(hash);
54+
}
55+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.peersafe.abi;
2+
3+
import java.util.List;
4+
5+
import com.peersafe.abi.datatypes.Type;
6+
7+
/**
8+
* Persisted solidity event parameters.
9+
*/
10+
public class EventValues {
11+
private final List<Type> indexedValues;
12+
private final List<Type> nonIndexedValues;
13+
14+
public EventValues(List<Type> indexedValues, List<Type> nonIndexedValues) {
15+
this.indexedValues = indexedValues;
16+
this.nonIndexedValues = nonIndexedValues;
17+
}
18+
19+
public List<Type> getIndexedValues() {
20+
return indexedValues;
21+
}
22+
23+
public List<Type> getNonIndexedValues() {
24+
return nonIndexedValues;
25+
}
26+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.peersafe.abi;
2+
3+
import java.math.BigInteger;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import com.peersafe.abi.datatypes.Function;
8+
import com.peersafe.abi.datatypes.StaticArray;
9+
import com.peersafe.abi.datatypes.Type;
10+
import com.peersafe.abi.datatypes.Uint;
11+
import org.web3j.crypto.Hash;
12+
import org.web3j.utils.Numeric;
13+
14+
/**
15+
* <p>Ethereum Contract Application Binary Interface (ABI) encoding for functions.
16+
* Further details are available
17+
* <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI">here</a>.
18+
* </p>
19+
*/
20+
public class FunctionEncoder {
21+
22+
private FunctionEncoder() { }
23+
24+
public static String encode(Function function) {
25+
List<Type> parameters = function.getInputParameters();
26+
27+
String methodSignature = buildMethodSignature(function.getName(), parameters);
28+
String methodId = buildMethodId(methodSignature);
29+
30+
StringBuilder result = new StringBuilder();
31+
result.append(methodId);
32+
33+
return encodeParameters(parameters, result);
34+
}
35+
36+
public static String encodeConstructor(List<Type> parameters) {
37+
return encodeParameters(parameters, new StringBuilder());
38+
}
39+
40+
private static String encodeParameters(List<Type> parameters, StringBuilder result) {
41+
int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
42+
StringBuilder dynamicData = new StringBuilder();
43+
44+
for (Type parameter:parameters) {
45+
String encodedValue = TypeEncoder.encode(parameter);
46+
47+
if (TypeEncoder.isDynamic(parameter)) {
48+
String encodedDataOffset = TypeEncoder.encodeNumeric(
49+
new Uint(BigInteger.valueOf(dynamicDataOffset)));
50+
result.append(encodedDataOffset);
51+
dynamicData.append(encodedValue);
52+
dynamicDataOffset += encodedValue.length() >> 1;
53+
} else {
54+
result.append(encodedValue);
55+
}
56+
}
57+
result.append(dynamicData);
58+
59+
return result.toString();
60+
}
61+
62+
private static int getLength(List<Type> parameters) {
63+
int count = 0;
64+
for (Type type:parameters) {
65+
if (type instanceof StaticArray) {
66+
count += ((StaticArray) type).getValue().size();
67+
} else {
68+
count++;
69+
}
70+
}
71+
return count;
72+
}
73+
74+
static String buildMethodSignature(String methodName, List<Type> parameters) {
75+
StringBuilder result = new StringBuilder();
76+
result.append(methodName);
77+
result.append("(");
78+
String params = parameters.stream()
79+
.map(Type::getTypeAsString)
80+
.collect(Collectors.joining(","));
81+
result.append(params);
82+
result.append(")");
83+
return result.toString();
84+
}
85+
86+
static String buildMethodId(String methodSignature) {
87+
byte[] input = methodSignature.getBytes();
88+
byte[] hash = Hash.sha3(input);
89+
return Numeric.toHexString(hash).substring(0, 10);
90+
}
91+
}

0 commit comments

Comments
 (0)