Skip to content

Commit 05c9fa4

Browse files
committed
'client-sdk-java 0.6.0 version'
1 parent e38caa0 commit 05c9fa4

72 files changed

Lines changed: 7886 additions & 1177 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.

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Overview
2+
> Java SDK is a Java development kit for PlatON public chain provided by PlatON for Java developers.
3+
4+
# Build
5+
```
6+
git clone https://github.com/PlatONnetwork/client-sdk-java.git
7+
cd client-sdk-java/
8+
./gradlew clean jar //Generate jar package
9+
./gradlew clean distZip //Generate code generation skeleton tool
10+
11+
```
12+
13+
# Use
14+
15+
* config maven repository: https://sdk.platon.network/nexus/content/groups/public/
16+
* config maven or gradle in project
17+
18+
```
19+
<dependency>
20+
<groupId>com.platon.client</groupId>
21+
<artifactId>core</artifactId>
22+
<version>0.4.0</version>
23+
</dependency>
24+
```
25+
26+
or
27+
28+
```
29+
compile "com.platon.client:core:0.4.0"
30+
```
31+
32+
* use in project
33+
34+
```
35+
Web3j web3 = Web3j.build(new HttpService("https://host:port"));
36+
```
37+
38+
39+
# Other
40+
[more reference wiki](https://github.com/PlatONnetwork/wiki/wiki)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.web3j.abi;
2+
3+
import org.web3j.abi.datatypes.Event;
4+
import org.web3j.crypto.Hash;
5+
import org.web3j.utils.Numeric;
6+
7+
8+
public class PlatOnEventEncoder {
9+
10+
private PlatOnEventEncoder() { }
11+
12+
public static String encode(Event event) {
13+
String methodSignature = buildMethodSignature(event.getName());
14+
return buildEventSignature(methodSignature);
15+
}
16+
17+
private static String buildMethodSignature(String methodName) {
18+
StringBuilder result = new StringBuilder();
19+
result.append(methodName);
20+
return result.toString();
21+
}
22+
23+
private static String buildEventSignature(String methodSignature) {
24+
byte[] input = methodSignature.getBytes();
25+
byte[] hash = Hash.sha3(input);
26+
return Numeric.toHexString(hash);
27+
}
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.web3j.abi;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.math.BigInteger;
5+
import java.nio.charset.StandardCharsets;
6+
7+
import org.web3j.abi.datatypes.NumericType;
8+
import org.web3j.abi.datatypes.Type;
9+
import org.web3j.abi.datatypes.Utf8String;
10+
11+
public class PlatOnTypeDecoder {
12+
13+
@SuppressWarnings("unchecked")
14+
public static <T extends Type> T decode(byte[] data, Class<T> type) {
15+
if (NumericType.class.isAssignableFrom(type)) {
16+
return (T) decodeNumeric(data, (Class<NumericType>) type);
17+
} else if (Utf8String.class.isAssignableFrom(type)) {
18+
return (T)decodeUtf8String(data);
19+
} else {
20+
throw new UnsupportedOperationException(
21+
"Type cannot be encoded: " + type.getClass());
22+
}
23+
}
24+
25+
26+
private static <T extends NumericType> T decodeNumeric(byte[] input, Class<T> type) {
27+
try {
28+
BigInteger value;
29+
if (input.length == 0) {
30+
value = BigInteger.ZERO;
31+
}else {
32+
value = new BigInteger(input);
33+
}
34+
return type.getConstructor(BigInteger.class).newInstance(value);
35+
} catch (NoSuchMethodException | SecurityException
36+
| InstantiationException | IllegalAccessException
37+
| IllegalArgumentException | InvocationTargetException e) {
38+
throw new UnsupportedOperationException(
39+
"Unable to create instance of " + type.getName(), e);
40+
}
41+
}
42+
43+
44+
private static Utf8String decodeUtf8String(byte[] input) {
45+
return new Utf8String(new String(input, StandardCharsets.UTF_8));
46+
}
47+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.web3j.abi;
2+
3+
import static org.web3j.abi.datatypes.Type.MAX_BIT_LENGTH;
4+
import static org.web3j.abi.datatypes.Type.MAX_BYTE_LENGTH;
5+
6+
import java.math.BigInteger;
7+
import java.nio.charset.StandardCharsets;
8+
9+
import org.bouncycastle.util.encoders.Hex;
10+
import org.web3j.abi.datatypes.IntType;
11+
import org.web3j.abi.datatypes.NumericType;
12+
import org.web3j.abi.datatypes.Type;
13+
import org.web3j.abi.datatypes.Ufixed;
14+
import org.web3j.abi.datatypes.Uint;
15+
import org.web3j.abi.datatypes.Utf8String;
16+
import org.web3j.utils.Numeric;
17+
18+
/**
19+
* <p>Ethereum Contract Application Binary Interface (ABI) encoding for types.
20+
* Further details are available
21+
* <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI">here</a>.
22+
* </p>
23+
*/
24+
public class PlatOnTypeEncoder {
25+
26+
private PlatOnTypeEncoder() { }
27+
28+
@SuppressWarnings("unchecked")
29+
public static String encode(Type parameter) {
30+
if (parameter instanceof IntType) {
31+
return encodeInt(((IntType) parameter));
32+
}else if (parameter instanceof Utf8String) {
33+
return encodeString((Utf8String) parameter);
34+
}else {
35+
throw new UnsupportedOperationException(
36+
"Type cannot be encoded: " + parameter.getClass());
37+
}
38+
}
39+
40+
private static String encodeInt(IntType intType) {
41+
byte[] rawValue = toByteArray(intType);
42+
byte paddingValue = getPaddingValue(intType);
43+
byte[] paddedRawValue = new byte[intType.getBitSize()/8];
44+
if (paddingValue != 0) {
45+
for (int i = 0; i < paddedRawValue.length; i++) {
46+
paddedRawValue[i] = paddingValue;
47+
}
48+
}
49+
50+
System.arraycopy(
51+
rawValue, 0,
52+
paddedRawValue, paddedRawValue.length - rawValue.length,
53+
rawValue.length);
54+
return Numeric.toHexStringNoPrefix(paddedRawValue);
55+
}
56+
57+
58+
private static String encodeString(Utf8String string) {
59+
byte[] utfEncoded = string.getValue().getBytes(StandardCharsets.UTF_8);
60+
return Hex.toHexString(utfEncoded);
61+
}
62+
63+
64+
private static byte getPaddingValue(NumericType numericType) {
65+
if (numericType.getValue().signum() == -1) {
66+
return (byte) 0xff;
67+
} else {
68+
return 0;
69+
}
70+
}
71+
72+
private static byte[] toByteArray(NumericType numericType) {
73+
BigInteger value = numericType.getValue();
74+
if (numericType instanceof Ufixed || numericType instanceof Uint) {
75+
if (value.bitLength() == MAX_BIT_LENGTH) {
76+
// As BigInteger is signed, if we have a 256 bit value, the resultant byte array
77+
// will contain a sign byte in it's MSB, which we should ignore for this unsigned
78+
// integer type.
79+
byte[] byteArray = new byte[MAX_BYTE_LENGTH];
80+
System.arraycopy(value.toByteArray(), 1, byteArray, 0, MAX_BYTE_LENGTH);
81+
return byteArray;
82+
}
83+
}
84+
return value.toByteArray();
85+
}
86+
}

abi/src/main/java/org/web3j/abi/datatypes/IntType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
* Common integer properties.
77
*/
88
public abstract class IntType extends NumericType {
9+
10+
private int bitSize;
911

1012
public IntType(String typePrefix, int bitSize, BigInteger value) {
1113
super(typePrefix + bitSize, value);
1214
if (!valid(bitSize, value)) {
1315
throw new UnsupportedOperationException(
1416
"Bitsize must be 8 bit aligned, and in range 0 < bitSize <= 256");
1517
}
18+
this.bitSize = bitSize;
1619
}
1720

1821
boolean valid(int bitSize, BigInteger value) {
@@ -29,4 +32,8 @@ static boolean isValidBitSize(int bitSize) {
2932
private static boolean isValidBitCount(int bitSize, BigInteger value) {
3033
return value.bitLength() <= bitSize;
3134
}
35+
36+
public int getBitSize() {
37+
return bitSize;
38+
}
3239
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.web3j.abi;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.math.BigInteger;
7+
8+
import org.junit.Test;
9+
import org.web3j.abi.datatypes.Utf8String;
10+
import org.web3j.abi.datatypes.generated.Int64;
11+
import org.web3j.abi.datatypes.generated.Uint64;
12+
import org.web3j.utils.Numeric;
13+
14+
public class PlatOnTypeDecoderTest {
15+
16+
@Test
17+
public void testUintDecode() {
18+
19+
assertThat(PlatOnTypeDecoder.decode(
20+
Numeric.hexStringToByteArray("0000000000000000"),
21+
Uint64.class
22+
),
23+
is(new Uint64(BigInteger.ZERO)));
24+
25+
assertThat(PlatOnTypeDecoder.decode(
26+
Numeric.hexStringToByteArray("7fffffffffffffff"),
27+
Uint64.class
28+
),
29+
is(new Uint64(BigInteger.valueOf(Long.MAX_VALUE))));
30+
}
31+
32+
@Test
33+
public void testIntDecode() {
34+
assertThat(PlatOnTypeDecoder.decode(
35+
Numeric.hexStringToByteArray("0000000000000000"),
36+
Int64.class
37+
),
38+
is(new Int64(BigInteger.ZERO)));
39+
40+
assertThat(PlatOnTypeDecoder.decode(
41+
Numeric.hexStringToByteArray("7fffffffffffffff"),
42+
Int64.class
43+
),
44+
is(new Int64(BigInteger.valueOf(Long.MAX_VALUE))));
45+
46+
assertThat(PlatOnTypeDecoder.decode(
47+
Numeric.hexStringToByteArray("8000000000000000"),
48+
Int64.class
49+
),
50+
is(new Int64(BigInteger.valueOf(Long.MIN_VALUE))));
51+
52+
assertThat(PlatOnTypeDecoder.decode(
53+
Numeric.hexStringToByteArray("ffffffffffffffff"),
54+
Int64.class
55+
),
56+
is(new Int64(BigInteger.valueOf(-1))));
57+
}
58+
59+
60+
@Test
61+
public void testUtf8String() {
62+
assertThat(PlatOnTypeDecoder.decode(
63+
Numeric.hexStringToByteArray("48656c6c6f2c20776f726c6421"),
64+
Utf8String.class),
65+
is(new Utf8String("Hello, world!")));
66+
}
67+
68+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.web3j.abi;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.math.BigInteger;
7+
8+
import org.junit.Test;
9+
import org.web3j.abi.datatypes.Int;
10+
import org.web3j.abi.datatypes.Uint;
11+
import org.web3j.abi.datatypes.Utf8String;
12+
import org.web3j.abi.datatypes.generated.Int64;
13+
import org.web3j.abi.datatypes.generated.Uint64;
14+
15+
16+
public class PlatOnTypeEncoderTest {
17+
18+
19+
@Test
20+
public void testUintEncode() {
21+
Uint zero = new Uint64(BigInteger.ZERO);
22+
assertThat(PlatOnTypeEncoder.encode(zero),
23+
is("0000000000000000"));
24+
25+
Uint maxLong = new Uint64(BigInteger.valueOf(Long.MAX_VALUE));
26+
assertThat(PlatOnTypeEncoder.encode(maxLong),
27+
is("7fffffffffffffff"));
28+
}
29+
30+
@Test(expected = UnsupportedOperationException.class)
31+
public void testInvalidUintEncode() {
32+
new Uint64(BigInteger.valueOf(-1));
33+
}
34+
35+
36+
@Test
37+
public void testIntEncode() {
38+
Int zero = new Int64(BigInteger.ZERO);
39+
assertThat(PlatOnTypeEncoder.encode(zero),
40+
is("0000000000000000"));
41+
42+
Int maxLong = new Int64(BigInteger.valueOf(Long.MAX_VALUE));
43+
assertThat(PlatOnTypeEncoder.encode(maxLong),
44+
is("7fffffffffffffff"));
45+
46+
Int minLong = new Int64(BigInteger.valueOf(Long.MIN_VALUE));
47+
assertThat(PlatOnTypeEncoder.encode(minLong),
48+
is("8000000000000000"));
49+
50+
Int minusOne = new Int64(BigInteger.valueOf(-1));
51+
assertThat(PlatOnTypeEncoder.encode(minusOne),
52+
is("ffffffffffffffff"));
53+
}
54+
55+
@Test
56+
public void testUtf8String() {
57+
Utf8String string = new Utf8String("Hello, world!");
58+
assertThat(PlatOnTypeEncoder.encode(string),
59+
is("48656c6c6f2c20776f726c6421"));
60+
}
61+
}

abi/src/test/java/org/web3j/abi/UtilsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.hamcrest.MatcherAssert.assertThat;
2727
import static org.web3j.abi.Utils.typeMap;
2828

29+
@SuppressWarnings("unchecked")
2930
public class UtilsTest {
3031

3132
@Test

0 commit comments

Comments
 (0)