-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbiBase.java
More file actions
80 lines (67 loc) · 2.63 KB
/
AbiBase.java
File metadata and controls
80 lines (67 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.arkecosystem.crypto.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.web3j.crypto.Hash;
import org.web3j.utils.Numeric;
public abstract class AbiBase {
protected List<Map<String, Object>> abi;
public AbiBase() throws IOException {
this("Abi.Consensus.json");
}
public AbiBase(String abiFilePath) throws IOException {
InputStream abiInputStream = getClass().getClassLoader().getResourceAsStream(abiFilePath);
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> abiJson = mapper.readValue(abiInputStream, Map.class);
this.abi = (List<Map<String, Object>>) abiJson.get("abi");
}
protected String[] getArrayComponents(String type) {
Pattern pattern = Pattern.compile("^(.*)\\[(\\d*)\\]$");
Matcher matcher = pattern.matcher(type);
if (matcher.find()) {
String innerType = matcher.group(1);
String lengthStr = matcher.group(2);
return new String[] {lengthStr, innerType};
}
return null;
}
protected String stripHexPrefix(String hex) {
return Numeric.cleanHexPrefix(hex);
}
protected boolean isValidAddress(String address) {
return address != null
&& address.startsWith("0x")
&& address.length() == 42
&& address.substring(2).matches("[0-9a-fA-F]+");
}
protected String keccak256(String input) {
return Hash.sha3String(input);
}
protected String getFunctionSignature(Map<String, Object> abiItem) {
String name = (String) abiItem.get("name");
List<Map<String, Object>> inputs = (List<Map<String, Object>>) abiItem.get("inputs");
List<String> types = new ArrayList<>();
for (Map<String, Object> input : inputs) {
types.add((String) input.get("type"));
}
return name + "(" + String.join(",", types) + ")";
}
protected String toFunctionSelector(Map<String, Object> abiItem) {
String signature = getFunctionSignature(abiItem);
String hash = keccak256(signature);
return "0x" + stripHexPrefix(hash).substring(0, 8);
}
protected String concatHex(List<String> hexes) {
StringBuilder result = new StringBuilder("0x");
for (String hex : hexes) {
if (hex == null || hex.isEmpty() || hex.equals("0x")) {
continue;
}
result.append(stripHexPrefix(hex));
}
return result.toString();
}
}