Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
- remove IDE warnings
- optimize imports
- fix incorrect usage of Java Generics
- fix incorrect Copyright notice
  • Loading branch information
sink772 committed Jul 23, 2020
1 parent 789d654 commit 05bd8b2
Show file tree
Hide file tree
Showing 41 changed files with 102 additions and 126 deletions.
6 changes: 2 additions & 4 deletions library/src/main/java/foundation/icon/icx/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand All @@ -33,15 +32,14 @@
*/
public final class Call<T> {

private RpcObject properties;
private Class<T> responseType;
private final RpcObject properties;
private final Class<T> responseType;

private Call(RpcObject properties, Class<T> responseType) {
this.properties = properties;
this.responseType = responseType;
}


RpcObject getProperties() {
return properties;
}
Expand Down
1 change: 0 additions & 1 deletion library/src/main/java/foundation/icon/icx/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand Down
26 changes: 18 additions & 8 deletions library/src/main/java/foundation/icon/icx/IconService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;

import foundation.icon.icx.crypto.IconKeys;
import foundation.icon.icx.data.*;
import foundation.icon.icx.transport.jsonrpc.*;
import foundation.icon.icx.data.Address;
import foundation.icon.icx.data.Block;
import foundation.icon.icx.data.Bytes;
import foundation.icon.icx.data.ConfirmedTransaction;
import foundation.icon.icx.data.Converters;
import foundation.icon.icx.data.ScoreApi;
import foundation.icon.icx.data.TransactionResult;
import foundation.icon.icx.transport.jsonrpc.AnnotatedConverterFactory;
import foundation.icon.icx.transport.jsonrpc.AnnotationConverter;
import foundation.icon.icx.transport.jsonrpc.RpcConverter;
import foundation.icon.icx.transport.jsonrpc.RpcConverter.RpcConverterFactory;
import foundation.icon.icx.transport.jsonrpc.RpcItem;
import foundation.icon.icx.transport.jsonrpc.RpcObject;
import foundation.icon.icx.transport.jsonrpc.RpcValue;

import java.math.BigInteger;
import java.util.ArrayList;
Expand All @@ -34,9 +44,9 @@
@SuppressWarnings("WeakerAccess")
public class IconService {

private Provider provider;
private List<RpcConverter.RpcConverterFactory> converterFactories = new ArrayList<>();
private Map<Class, RpcConverter<?>> converterMap = new HashMap<>();
private final Provider provider;
private final List<RpcConverter.RpcConverterFactory> converterFactories = new ArrayList<>();
private final Map<Class<?>, RpcConverter<?>> converterMap = new HashMap<>();

/**
* Creates an IconService instance
Expand Down Expand Up @@ -170,7 +180,7 @@ public Request<ConfirmedTransaction> getTransaction(Bytes hash) {
}

/**
* Gets the result of a transaction by transaction hash
* Gets the result of a transaction specified by the transaction hash
*
* @param hash a transaction hash
* @return a {@code TransactionResult} object
Expand Down Expand Up @@ -229,7 +239,7 @@ public Request<BigInteger> estimateStep(Transaction transaction) {

@SuppressWarnings("unchecked")
private <T> RpcConverter<T> findConverter(Class<T> type) {
RpcConverter converter = converterMap.get(type);
RpcConverter<T> converter = (RpcConverter<T>) converterMap.get(type);
if (converter != null) return converter;

for (RpcConverterFactory factory : converterFactories) {
Expand Down
13 changes: 8 additions & 5 deletions library/src/main/java/foundation/icon/icx/KeyWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;

import foundation.icon.icx.crypto.*;
import foundation.icon.icx.crypto.ECDSASignature;
import foundation.icon.icx.crypto.IconKeys;
import foundation.icon.icx.crypto.KeyStoreUtils;
import foundation.icon.icx.crypto.Keystore;
import foundation.icon.icx.crypto.KeystoreException;
import foundation.icon.icx.crypto.KeystoreFile;
import foundation.icon.icx.data.Address;
import foundation.icon.icx.data.Bytes;

Expand All @@ -36,8 +40,8 @@
@SuppressWarnings({"WeakerAccess", "unused"})
public class KeyWallet implements Wallet {

private Bytes privateKey;
private Bytes publicKey;
private final Bytes privateKey;
private final Bytes publicKey;

private KeyWallet(Bytes privateKey, Bytes publicKey) {
this.privateKey = privateKey;
Expand Down Expand Up @@ -125,5 +129,4 @@ public Bytes getPrivateKey() {
public Bytes getPublicKey() {
return publicKey;
}

}
1 change: 0 additions & 1 deletion library/src/main/java/foundation/icon/icx/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand Down
1 change: 0 additions & 1 deletion library/src/main/java/foundation/icon/icx/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand All @@ -36,8 +35,8 @@
*/
public class SignedTransaction {

private Transaction transaction;
private Wallet wallet;
private final Transaction transaction;
private final Wallet wallet;
private RpcObject properties;

/**
Expand Down
1 change: 0 additions & 1 deletion library/src/main/java/foundation/icon/icx/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand Down
33 changes: 16 additions & 17 deletions library/src/main/java/foundation/icon/icx/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand Down Expand Up @@ -77,7 +76,7 @@ public static Builder newBuilder() {
*/
public static final class Builder {

private TransactionData transactionData;
private final TransactionData transactionData;

private Builder() {
this.transactionData = new TransactionData();
Expand Down Expand Up @@ -227,8 +226,8 @@ public Transaction build() {
*/
public static final class CallBuilder {

private TransactionData transactionData;
private RpcObject.Builder dataBuilder;
private final TransactionData transactionData;
private final RpcObject.Builder dataBuilder;

private CallBuilder(TransactionData transactionData, String method) {
this.transactionData = transactionData;
Expand Down Expand Up @@ -277,7 +276,7 @@ public Transaction build() {
* A Builder for the message transaction.
*/
public static final class MessageBuilder {
private TransactionData transactionData;
private final TransactionData transactionData;

private MessageBuilder(TransactionData transactionData, String message) {
this.transactionData = transactionData;
Expand All @@ -300,8 +299,8 @@ public Transaction build() {
*/
public static final class DeployBuilder {

private TransactionData transactionData;
private RpcObject.Builder dataBuilder;
private final TransactionData transactionData;
private final RpcObject.Builder dataBuilder;

private DeployBuilder(TransactionData transactionData, String contentType, byte[] content) {
this.transactionData = transactionData;
Expand Down Expand Up @@ -394,7 +393,7 @@ public Transaction build() {
}

private static class TransactionData {
private BigInteger version = new BigInteger("3");
private final BigInteger version = new BigInteger("3");
private Address from;
private Address to;
private BigInteger value;
Expand All @@ -421,16 +420,16 @@ void checkAddress(Address address, String message) {
}

private static class RawTransaction implements Transaction {
private BigInteger version;
private Address from;
private Address to;
private BigInteger value;
private BigInteger stepLimit;
private final BigInteger version;
private final Address from;
private final Address to;
private final BigInteger value;
private final BigInteger stepLimit;
private BigInteger timestamp;
private BigInteger nid;
private BigInteger nonce;
private String dataType;
private RpcItem data;
private final BigInteger nid;
private final BigInteger nonce;
private final String dataType;
private final RpcItem data;

private RawTransaction(TransactionData transactionData) {
version = transactionData.version;
Expand Down
2 changes: 0 additions & 2 deletions library/src/main/java/foundation/icon/icx/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package foundation.icon.icx;
Expand All @@ -39,5 +38,4 @@ public interface Wallet {
* @return signature
*/
byte[] sign(byte[] data);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
* Original Code
Expand All @@ -33,7 +32,7 @@ public class ECDSASignature {
private final ECDomainParameters curve = new ECDomainParameters(
curveParams.getCurve(), curveParams.getG(), curveParams.getN(), curveParams.getH());

private Bytes privateKey;
private final Bytes privateKey;

public ECDSASignature(Bytes privateKey) {
this.privateKey = privateKey;
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/java/foundation/icon/icx/crypto/IconKeys.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package foundation.icon.icx.crypto;


import foundation.icon.icx.data.Address;
import foundation.icon.icx.data.Bytes;
import org.bouncycastle.crypto.RuntimeCryptoException;
Expand All @@ -14,11 +13,17 @@
import org.bouncycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.security.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import java.util.Arrays;


/**
* Implementation from
* https://github.com/web3j/web3j/blob/master/crypto/src/main/java/org/web3j/crypto/Keys.java
Expand Down Expand Up @@ -153,5 +158,4 @@ public static boolean isAndroidRuntime() {
}
return isAndroid == 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.InputMismatchException;
import java.util.TimeZone;


/**
* Original Code
* https://github.com/web3j/web3j/blob/master/crypto/src/main/java/org/web3j/crypto/WalletUtils.java
Expand All @@ -29,7 +28,6 @@ private KeyStoreUtils() { }
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}


public static String generateWalletFile(
KeystoreFile file, File destinationDirectory) throws IOException {

Expand All @@ -53,5 +51,4 @@ private static String getWalletFileName(KeystoreFile keystoreFile) {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(new Date()) + keystoreFile.getAddress() + ".json";
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package foundation.icon.icx.crypto;


import foundation.icon.icx.data.Bytes;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
Expand All @@ -24,7 +23,6 @@

import static foundation.icon.icx.crypto.IconKeys.secureRandom;


/**
* Original Code
* https://github.com/web3j/web3j/blob/master/crypto/src/main/java/org/web3j/crypto/Wallet.java
Expand Down Expand Up @@ -79,10 +77,8 @@ public static KeystoreFile create(String password, Bytes privateKey, int n, int
byte[] encryptKey = Arrays.copyOfRange(derivedKey, 0, 16);
byte[] iv = generateRandomBytes(16);


byte[] privateKeyBytes = privateKey.toByteArray(IconKeys.PRIVATE_KEY_SIZE);


byte[] cipherText = performCipherOperation(
Cipher.ENCRYPT_MODE, iv, encryptKey, privateKeyBytes);

Expand All @@ -91,7 +87,6 @@ public static KeystoreFile create(String password, Bytes privateKey, int n, int
return createWalletFile(privateKey, cipherText, iv, salt, mac, n, p);
}


private static KeystoreFile createWalletFile(
Bytes privateKey, byte[] cipherText, byte[] iv, byte[] salt, byte[] mac,
int n, int p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,4 @@ public int hashCode() {
return result;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package foundation.icon.icx.crypto;


import java.io.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.Provider;
import java.security.SecureRandomSpi;
import java.security.Security;
Expand Down
Loading

0 comments on commit 05bd8b2

Please sign in to comment.