Skip to content

Commit 80b8c2f

Browse files
committed
initial commit
1 parent 4c65d3e commit 80b8c2f

File tree

30 files changed

+1388
-0
lines changed

30 files changed

+1388
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/abi-parser/target/
2+
/abi-parser-simple/target/
3+
/abi-parser-spring/target/
4+
/abi-parser-microprofile/target/
5+
.idea/

abi-parser-microprofile/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.open-elements.solidity</groupId>
9+
<artifactId>solidity-enterprise</artifactId>
10+
<version>0.1.0-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>hiero-enterprise-abi-parser-microprofile</artifactId>
15+
16+
<name>ABI Parser support for Microprofile</name>
17+
<description>Library that provides an implementation of the ABI parser for Microprofile</description>
18+
<url>https://github.com/OpenElements/hiero-enterprise-java</url>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>${groupId}</groupId>
23+
<artifactId>abi-parser</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>jakarta.enterprise</groupId>
27+
<artifactId>jakarta.enterprise.cdi-api</artifactId>
28+
<scope>provided</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>jakarta.json</groupId>
32+
<artifactId>jakarta.json-api</artifactId>
33+
<scope>provided</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.jspecify</groupId>
37+
<artifactId>jspecify</artifactId>
38+
<scope>compile</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-deploy-plugin</artifactId>
47+
<configuration>
48+
<skip>true</skip>
49+
</configuration>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.jreleaser</groupId>
53+
<artifactId>jreleaser-maven-plugin</artifactId>
54+
<configuration>
55+
<skip>true</skip>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.openelements.hiero.smartcontract.abi.microprofile;
2+
3+
import com.openelements.hiero.smartcontract.abi.AbiParser;
4+
import jakarta.enterprise.context.ApplicationScoped;
5+
import jakarta.enterprise.inject.Produces;
6+
import org.jspecify.annotations.NonNull;
7+
8+
@ApplicationScoped
9+
public class AbiParserProvider {
10+
11+
@NonNull
12+
@Produces
13+
@ApplicationScoped
14+
AbiParser createAbiParser() {
15+
return new MicroprofileAbiParser();
16+
}
17+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.openelements.hiero.smartcontract.abi.microprofile;
2+
3+
import com.openelements.hiero.smartcontract.abi.AbiParser;
4+
import com.openelements.hiero.smartcontract.abi.model.AbiEntry;
5+
import com.openelements.hiero.smartcontract.abi.model.AbiEntryType;
6+
import com.openelements.hiero.smartcontract.abi.model.AbiFunction;
7+
import com.openelements.hiero.smartcontract.abi.model.AbiModel;
8+
import com.openelements.hiero.smartcontract.abi.model.AbiParameter;
9+
import com.openelements.hiero.smartcontract.abi.model.StateMutability;
10+
import jakarta.json.Json;
11+
import jakarta.json.JsonArray;
12+
import jakarta.json.JsonObject;
13+
import jakarta.json.JsonValue.ValueType;
14+
import jakarta.json.stream.JsonParser;
15+
import java.io.Reader;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import org.jspecify.annotations.NonNull;
19+
20+
public class MicroprofileAbiParser implements AbiParser {
21+
22+
@Override
23+
public @NonNull AbiModel parse(@NonNull Reader abiReader) {
24+
final JsonParser parser = Json.createParser(abiReader);
25+
final JsonArray array = parser.getArray();
26+
final List<AbiEntry> entries = array.stream()
27+
.map(value -> {
28+
if (value.getValueType() != ValueType.OBJECT) {
29+
throw new IllegalArgumentException("Must be an JSON object");
30+
}
31+
return value.asJsonObject();
32+
}).map(jsonObject -> convertToAbiEntry(jsonObject))
33+
.toList();
34+
return new AbiModel(entries);
35+
}
36+
37+
@NonNull
38+
private AbiEntry convertToAbiEntry(@NonNull JsonObject jsonObject) {
39+
final AbiEntryType type = AbiEntryType.of(jsonObject.getString("type"));
40+
if (type == AbiEntryType.CONSTRUCTOR) {
41+
final List<AbiParameter> input = new ArrayList<>();
42+
if (jsonObject.containsKey("input")) {
43+
jsonObject.getJsonArray("input").stream()
44+
.map(jsonValue -> jsonValue.asJsonObject())
45+
.map(value -> convertToAbiParameter(value))
46+
.forEach(param -> input.add(param));
47+
}
48+
final StateMutability stateMutability = StateMutability.of(jsonObject.getString("stateMutability"));
49+
return new AbiFunction(type, null, input, List.of(), stateMutability);
50+
}
51+
throw new RuntimeException("Not yet implemented");
52+
}
53+
54+
@NonNull
55+
private AbiParameter convertToAbiParameter(@NonNull JsonObject jsonObject) {
56+
return new AbiParameter(null, null, null, false);
57+
}
58+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module com.openelements.hiero.smartcontract.abi.microprofile {
2+
requires transitive com.openelements.hiero.smartcontract.abi;
3+
requires org.jspecify;
4+
requires jakarta.cdi;
5+
requires jakarta.json;
6+
}
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" standalone="yes"?>
2+
<!--
3+
4+
Copyright (C) 2016, 2017 Antonio Goncalves and others.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15+
implied.
16+
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
20+
-->
21+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
22+
bean-discovery-mode="annotated" version="1.1"
23+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"/>

abi-parser-simple/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.open-elements.solidity</groupId>
9+
<artifactId>solidity-enterprise</artifactId>
10+
<version>0.1.0-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>abi-parser-simple</artifactId>
15+
16+
<name>Hiero Enterprise ABI Parser Simple</name>
17+
<description>Library that provides a basic (GSON based) implementation of the API to parse ABI data</description>
18+
<url>https://github.com/OpenElements/hiero-enterprise-java</url>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>${groupId}</groupId>
23+
<artifactId>abi-parser</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.code.gson</groupId>
27+
<artifactId>gson</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.jspecify</groupId>
31+
<artifactId>jspecify</artifactId>
32+
<scope>compile</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-deploy-plugin</artifactId>
46+
<configuration>
47+
<skip>true</skip>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.jreleaser</groupId>
52+
<artifactId>jreleaser-maven-plugin</artifactId>
53+
<configuration>
54+
<skip>true</skip>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.openelements.hiero.smartcontract.abi.simple;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.openelements.hiero.smartcontract.abi.AbiParser;
6+
import com.openelements.hiero.smartcontract.abi.AbiParserException;
7+
import com.openelements.hiero.smartcontract.abi.model.AbiModel;
8+
import com.openelements.hiero.smartcontract.abi.simple.implementation.AbiTypeAdapterFactory;
9+
import java.io.Reader;
10+
import java.util.Objects;
11+
import org.jspecify.annotations.NonNull;
12+
13+
public class GsonAbiParser implements AbiParser {
14+
15+
private final Gson gson;
16+
17+
public GsonAbiParser() {
18+
this.gson = new GsonBuilder()
19+
.registerTypeAdapterFactory(new AbiTypeAdapterFactory())
20+
.create();
21+
22+
}
23+
24+
public @NonNull AbiModel parse(@NonNull Reader abiReader) throws AbiParserException {
25+
Objects.requireNonNull(abiReader, "abiReader");
26+
try {
27+
return gson.fromJson(abiReader, AbiModel.class);
28+
} catch (Exception e) {
29+
throw new AbiParserException("Error in parsing abi", e);
30+
}
31+
}
32+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.openelements.hiero.smartcontract.abi.simple.implementation;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.stream.JsonReader;
6+
import com.openelements.hiero.smartcontract.abi.model.AbiEntry;
7+
import com.openelements.hiero.smartcontract.abi.model.AbiEntryType;
8+
import com.openelements.hiero.smartcontract.abi.model.AbiFunction;
9+
import com.openelements.hiero.smartcontract.abi.model.AbiParameter;
10+
import com.openelements.hiero.smartcontract.abi.model.StateMutability;
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
15+
16+
public class AbiEntryTypeAdapter extends BasicTypeAdapter<AbiEntry> {
17+
18+
public AbiEntryTypeAdapter(Gson gson) {
19+
super(gson);
20+
}
21+
22+
@Override
23+
public AbiEntry read(JsonReader in) throws IOException {
24+
final JsonObject abiEntryObject = readObject(in);
25+
final AbiEntryType type = getStringValue(abiEntryObject, "type")
26+
.map(AbiEntryType::of)
27+
.orElseThrow(() -> new IllegalArgumentException("AbiEntry must have a type"));
28+
if (type == AbiEntryType.CONSTRUCTOR) {
29+
final List<AbiParameter> inputs = new ArrayList<>();
30+
getArray(abiEntryObject, "inputs").forEach(jsonElement -> {
31+
inputs.add(fromJson(jsonElement, AbiParameter.class));
32+
});
33+
final List<AbiParameter> outputs = new ArrayList<>();
34+
getArray(abiEntryObject, "outputs").forEach(jsonElement -> {
35+
outputs.add(fromJson(jsonElement, AbiParameter.class));
36+
});
37+
final StateMutability stateMutability = getStringValue(abiEntryObject, "stateMutability")
38+
.map(StateMutability::of)
39+
.orElseThrow(() -> new IllegalArgumentException("AbiEntry must have a stateMutability"));
40+
return new AbiFunction(type, null, Collections.unmodifiableList(inputs),
41+
Collections.unmodifiableList(outputs), stateMutability);
42+
}
43+
if (type.isCompatibleWithFunction()) {
44+
final String name = getStringValue(abiEntryObject, "name")
45+
.orElseThrow(() -> new IllegalArgumentException("AbiEntry must have a name"));
46+
final List<AbiParameter> inputs = new ArrayList<>();
47+
getArray(abiEntryObject, "inputs").forEach(jsonElement -> {
48+
inputs.add(fromJson(jsonElement, AbiParameter.class));
49+
});
50+
final List<AbiParameter> outputs = new ArrayList<>();
51+
getArray(abiEntryObject, "outputs").forEach(jsonElement -> {
52+
outputs.add(fromJson(jsonElement, AbiParameter.class));
53+
});
54+
final StateMutability stateMutability = getStringValue(abiEntryObject, "stateMutability")
55+
.map(StateMutability::of)
56+
.orElseThrow(() -> new IllegalArgumentException("AbiEntry must have a stateMutability"));
57+
return new AbiFunction(type, name, Collections.unmodifiableList(inputs),
58+
Collections.unmodifiableList(outputs), stateMutability);
59+
}
60+
throw new IllegalArgumentException("Unsupported AbiEntry type: " + type);
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.openelements.hiero.smartcontract.abi.simple.implementation;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.stream.JsonReader;
6+
import com.openelements.hiero.smartcontract.abi.model.AbiEntry;
7+
import com.openelements.hiero.smartcontract.abi.model.AbiModel;
8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import org.jspecify.annotations.NonNull;
13+
14+
public class AbiModelTypeAdapter extends BasicTypeAdapter<AbiModel> {
15+
16+
public AbiModelTypeAdapter(@NonNull Gson gson) {
17+
super(gson);
18+
}
19+
20+
@Override
21+
public AbiModel read(JsonReader in) throws IOException {
22+
JsonArray array = readArray(in);
23+
final List<AbiEntry> entries = new ArrayList<>();
24+
array.forEach(jsonElement -> {
25+
entries.add(fromJson(jsonElement, AbiEntry.class));
26+
});
27+
return new AbiModel(Collections.unmodifiableList(entries));
28+
}
29+
}

0 commit comments

Comments
 (0)