From ebb1220fb635a3910f7d5b03fb0961b8dd02c0cc Mon Sep 17 00:00:00 2001 From: alfonsobries Date: Mon, 18 Nov 2024 18:18:14 +0000 Subject: [PATCH] style: resolve style guide violations --- .../crypto/transactions/Deserializer.java | 9 ++++---- .../crypto/transactions/Serializer.java | 6 ++++-- .../builder/AbstractTransactionBuilder.java | 2 +- .../builder/ValidatorRegistrationBuilder.java | 3 ++- .../builder/ValidatorResignationBuilder.java | 3 ++- .../types/AbstractTransaction.java | 14 ++++++------- .../types/ValidatorRegistration.java | 8 +++---- .../types/ValidatorResignation.java | 3 ++- .../crypto/transactions/types/Vote.java | 5 ++--- .../arkecosystem/crypto/utils/AbiBase.java | 2 +- .../arkecosystem/crypto/utils/AbiEncoder.java | 6 ++---- .../crypto/utils/TransactionHasher.java | 20 +++++++++++------- .../crypto/transactions/FixtureLoader.java | 5 ++++- .../builder/TransferBuilderTest.java | 21 +++++++++---------- 14 files changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/arkecosystem/crypto/transactions/Deserializer.java b/src/main/java/org/arkecosystem/crypto/transactions/Deserializer.java index e6d945f..9d79dc9 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/Deserializer.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/Deserializer.java @@ -1,14 +1,13 @@ package org.arkecosystem.crypto.transactions; -import org.arkecosystem.crypto.encoding.Hex; -import org.arkecosystem.crypto.transactions.types.*; -import org.arkecosystem.crypto.enums.AbiFunction; -import org.arkecosystem.crypto.utils.AbiDecoder; - import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Map; +import org.arkecosystem.crypto.encoding.Hex; +import org.arkecosystem.crypto.enums.AbiFunction; +import org.arkecosystem.crypto.transactions.types.*; +import org.arkecosystem.crypto.utils.AbiDecoder; public class Deserializer { private static final int SIGNATURE_SIZE = 64; diff --git a/src/main/java/org/arkecosystem/crypto/transactions/Serializer.java b/src/main/java/org/arkecosystem/crypto/transactions/Serializer.java index 004c5fc..a3bfd62 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/Serializer.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/Serializer.java @@ -54,7 +54,8 @@ private void serializeData(ByteBuffer buffer) { // Write recipient marker and address if (transaction.recipientAddress != null && !transaction.recipientAddress.isEmpty()) { buffer.put((byte) 1); - byte[] recipientBytes = Hex.decode(transaction.recipientAddress.replaceFirst("^0x", "").toLowerCase()); + byte[] recipientBytes = + Hex.decode(transaction.recipientAddress.replaceFirst("^0x", "").toLowerCase()); buffer.put(recipientBytes); } else { @@ -62,7 +63,8 @@ private void serializeData(ByteBuffer buffer) { } // Write payload length as UInt32 and the payload itself if present - String payloadHex = transaction.data != null ? transaction.data.replaceFirst("^0x", "") : ""; + String payloadHex = + transaction.data != null ? transaction.data.replaceFirst("^0x", "") : ""; int payloadLength = payloadHex.length() / 2; buffer.putInt(payloadLength); if (payloadLength > 0) { diff --git a/src/main/java/org/arkecosystem/crypto/transactions/builder/AbstractTransactionBuilder.java b/src/main/java/org/arkecosystem/crypto/transactions/builder/AbstractTransactionBuilder.java index d71b659..d4e19d4 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/builder/AbstractTransactionBuilder.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/builder/AbstractTransactionBuilder.java @@ -9,7 +9,7 @@ public abstract class AbstractTransactionBuilder< public AbstractTransactionBuilder() { this.transaction = getTransactionInstance(); - + initializeTransactionDefaults(); } diff --git a/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorRegistrationBuilder.java b/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorRegistrationBuilder.java index ec56d7b..0104945 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorRegistrationBuilder.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorRegistrationBuilder.java @@ -3,7 +3,8 @@ import org.arkecosystem.crypto.transactions.types.AbstractTransaction; import org.arkecosystem.crypto.transactions.types.ValidatorRegistration; -public class ValidatorRegistrationBuilder extends AbstractTransactionBuilder { +public class ValidatorRegistrationBuilder + extends AbstractTransactionBuilder { public ValidatorRegistrationBuilder validatorPublicKey(String validatorPublicKey) { this.transaction.validatorPublicKey = validatorPublicKey; diff --git a/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorResignationBuilder.java b/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorResignationBuilder.java index 400ea09..72d8499 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorResignationBuilder.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/builder/ValidatorResignationBuilder.java @@ -3,7 +3,8 @@ import org.arkecosystem.crypto.transactions.types.AbstractTransaction; import org.arkecosystem.crypto.transactions.types.ValidatorResignation; -public class ValidatorResignationBuilder extends AbstractTransactionBuilder { +public class ValidatorResignationBuilder + extends AbstractTransactionBuilder { @Override protected AbstractTransaction getTransactionInstance() { diff --git a/src/main/java/org/arkecosystem/crypto/transactions/types/AbstractTransaction.java b/src/main/java/org/arkecosystem/crypto/transactions/types/AbstractTransaction.java index af57edd..76dc102 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/types/AbstractTransaction.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/types/AbstractTransaction.java @@ -2,6 +2,11 @@ package org.arkecosystem.crypto.transactions.types; import com.google.gson.GsonBuilder; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.arkecosystem.crypto.encoding.Hex; import org.arkecosystem.crypto.identities.PrivateKey; import org.arkecosystem.crypto.transactions.Serializer; @@ -10,12 +15,6 @@ import org.bitcoinj.core.ECKey; import org.bitcoinj.core.Sha256Hash; -import java.math.BigInteger; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - public abstract class AbstractTransaction { public int network; public long nonce; @@ -134,7 +133,8 @@ public boolean verify() { byte[] hash = this.hash(true); - ECKey recoveredKey = ECKey.recoverFromSignature(recId, signature, Sha256Hash.wrap(hash), true); + ECKey recoveredKey = + ECKey.recoverFromSignature(recId, signature, Sha256Hash.wrap(hash), true); if (recoveredKey == null) { return false; } diff --git a/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorRegistration.java b/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorRegistration.java index f950807..30ad6c8 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorRegistration.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorRegistration.java @@ -1,11 +1,10 @@ package org.arkecosystem.crypto.transactions.types; -import org.arkecosystem.crypto.enums.AbiFunction; -import org.arkecosystem.crypto.utils.AbiEncoder; - import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.arkecosystem.crypto.enums.AbiFunction; +import org.arkecosystem.crypto.utils.AbiEncoder; public class ValidatorRegistration extends AbstractTransaction { public ValidatorRegistration() { @@ -35,7 +34,8 @@ public String getPayload() { args.add(validatorPublicKeyHex); try { - return new AbiEncoder().encodeFunctionCall(AbiFunction.VALIDATOR_REGISTRATION.toString(), args); + return new AbiEncoder() + .encodeFunctionCall(AbiFunction.VALIDATOR_REGISTRATION.toString(), args); } catch (Exception e) { throw new RuntimeException("Error encoding function call", e); } diff --git a/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorResignation.java b/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorResignation.java index e0ada97..c10f3bd 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorResignation.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/types/ValidatorResignation.java @@ -7,7 +7,8 @@ public class ValidatorResignation extends AbstractTransaction { @Override public String getPayload() { try { - return new AbiEncoder().encodeFunctionCall(AbiFunction.VALIDATOR_RESIGNATION.toString()); + return new AbiEncoder() + .encodeFunctionCall(AbiFunction.VALIDATOR_RESIGNATION.toString()); } catch (Exception e) { throw new RuntimeException("Error encoding function call", e); } diff --git a/src/main/java/org/arkecosystem/crypto/transactions/types/Vote.java b/src/main/java/org/arkecosystem/crypto/transactions/types/Vote.java index 90675e6..407778d 100644 --- a/src/main/java/org/arkecosystem/crypto/transactions/types/Vote.java +++ b/src/main/java/org/arkecosystem/crypto/transactions/types/Vote.java @@ -1,11 +1,10 @@ package org.arkecosystem.crypto.transactions.types; -import org.arkecosystem.crypto.enums.AbiFunction; -import org.arkecosystem.crypto.utils.AbiEncoder; - import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.arkecosystem.crypto.enums.AbiFunction; +import org.arkecosystem.crypto.utils.AbiEncoder; public class Vote extends AbstractTransaction { public Vote() { diff --git a/src/main/java/org/arkecosystem/crypto/utils/AbiBase.java b/src/main/java/org/arkecosystem/crypto/utils/AbiBase.java index b011a12..33d0b4c 100644 --- a/src/main/java/org/arkecosystem/crypto/utils/AbiBase.java +++ b/src/main/java/org/arkecosystem/crypto/utils/AbiBase.java @@ -17,7 +17,7 @@ public AbiBase() throws IOException { String abiFilePath = "Abi.Consensus.json"; InputStream abiInputStream = getClass().getClassLoader().getResourceAsStream(abiFilePath); - + ObjectMapper mapper = new ObjectMapper(); Map abiJson = mapper.readValue(abiInputStream, Map.class); this.abi = (List>) abiJson.get("abi"); diff --git a/src/main/java/org/arkecosystem/crypto/utils/AbiEncoder.java b/src/main/java/org/arkecosystem/crypto/utils/AbiEncoder.java index d07ca6a..e53b2eb 100644 --- a/src/main/java/org/arkecosystem/crypto/utils/AbiEncoder.java +++ b/src/main/java/org/arkecosystem/crypto/utils/AbiEncoder.java @@ -24,7 +24,6 @@ public String encodeFunctionCall(String functionName, List args) throws return encodeFunctionData(parameters); } - private String encodeFunctionData(Map parameters) throws Exception { List args = (List) parameters.getOrDefault("args", new ArrayList<>()); @@ -232,13 +231,12 @@ private Map encodeAddress(String value) throws Exception { throw new Exception("Invalid address: " + value); } value = stripHexPrefix(value).toLowerCase(); - + // Pad the string to 64 characters with leading zeros String paddedValue = String.format("%64s", value).replace(' ', '0'); - + return Map.of("dynamic", false, "encoded", "0x" + paddedValue); } - private Map encodeBool(Boolean value) { String encoded = String.format("%064x", value ? 1 : 0); diff --git a/src/main/java/org/arkecosystem/crypto/utils/TransactionHasher.java b/src/main/java/org/arkecosystem/crypto/utils/TransactionHasher.java index e3782b7..68d18a9 100644 --- a/src/main/java/org/arkecosystem/crypto/utils/TransactionHasher.java +++ b/src/main/java/org/arkecosystem/crypto/utils/TransactionHasher.java @@ -1,20 +1,19 @@ package org.arkecosystem.crypto.utils; -import org.arkecosystem.crypto.encoding.Hex; -import org.bitcoinj.core.Sha256Hash; - import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; +import org.arkecosystem.crypto.encoding.Hex; +import org.bitcoinj.core.Sha256Hash; public class TransactionHasher { /** * Generates the transaction hash. * - * @param transaction The transaction data. + * @param transaction The transaction data. * @param skipSignature Whether to skip the signature fields. * @return The hash of the transaction. */ @@ -32,12 +31,19 @@ public static byte[] toHash(Map transaction, boolean skipSignatu List fields = new ArrayList<>(); fields.add(toBeArray(new BigInteger(transaction.get("network").toString()))); fields.add(toBeArray(new BigInteger(transaction.get("nonce").toString()))); - fields.add(toBeArray(new BigInteger(transaction.get("gasPrice").toString()))); // maxPriorityFeePerGas - fields.add(toBeArray(new BigInteger(transaction.get("gasPrice").toString()))); // maxFeePerGas + fields.add( + toBeArray( + new BigInteger( + transaction.get("gasPrice").toString()))); // maxPriorityFeePerGas + fields.add( + toBeArray(new BigInteger(transaction.get("gasPrice").toString()))); // maxFeePerGas fields.add(toBeArray(new BigInteger(transaction.get("gasLimit").toString()))); fields.add(recipientAddress); fields.add(toBeArray(new BigInteger(transaction.get("value").toString()))); - String dataHex = transaction.get("data") != null ? ((String) transaction.get("data")).replaceFirst("^0x", "") : ""; + String dataHex = + transaction.get("data") != null + ? ((String) transaction.get("data")).replaceFirst("^0x", "") + : ""; byte[] data = Hex.decode(dataHex); fields.add(data); fields.add(new ArrayList<>()); // Access list is unused diff --git a/src/test/java/org/arkecosystem/crypto/transactions/FixtureLoader.java b/src/test/java/org/arkecosystem/crypto/transactions/FixtureLoader.java index e3d674b..d918380 100644 --- a/src/test/java/org/arkecosystem/crypto/transactions/FixtureLoader.java +++ b/src/test/java/org/arkecosystem/crypto/transactions/FixtureLoader.java @@ -9,7 +9,10 @@ public class FixtureLoader { private static String readFile(String path) throws IOException { - InputStream inputStream = FixtureLoader.class.getClassLoader().getResourceAsStream(String.format("%s.json", path)); + InputStream inputStream = + FixtureLoader.class + .getClassLoader() + .getResourceAsStream(String.format("%s.json", path)); if (inputStream == null) { throw new IOException("Resource not found: " + path); } diff --git a/src/test/java/org/arkecosystem/crypto/transactions/builder/TransferBuilderTest.java b/src/test/java/org/arkecosystem/crypto/transactions/builder/TransferBuilderTest.java index 3a9dc45..926ad18 100644 --- a/src/test/java/org/arkecosystem/crypto/transactions/builder/TransferBuilderTest.java +++ b/src/test/java/org/arkecosystem/crypto/transactions/builder/TransferBuilderTest.java @@ -4,10 +4,8 @@ import com.google.gson.Gson; import java.io.InputStream; -import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.Map; - import org.arkecosystem.crypto.encoding.Hex; import org.junit.jupiter.api.Test; @@ -20,18 +18,19 @@ public void it_should_sign_it_with_a_passphrase() throws Exception { Map data = (Map) fixture.get("data"); // Updated to use BigInteger for large values - TransferBuilder builder = new TransferBuilder() - .gasPrice(((Number) data.get("gasPrice")).intValue()) - .nonce(Long.parseLong(data.get("nonce").toString())) - .network(((Number) data.get("network")).intValue()) - .gasLimit(((Number) data.get("gasLimit")).intValue()) - .recipientAddress((String) data.get("recipientAddress")) - .value((String) data.get("value")) - .sign("my super secret passphrase"); + TransferBuilder builder = + new TransferBuilder() + .gasPrice(((Number) data.get("gasPrice")).intValue()) + .nonce(Long.parseLong(data.get("nonce").toString())) + .network(((Number) data.get("network")).intValue()) + .gasLimit(((Number) data.get("gasLimit")).intValue()) + .recipientAddress((String) data.get("recipientAddress")) + .value((String) data.get("value")) + .sign("my super secret passphrase"); byte[] serializedBytes = builder.transaction.serialize(false); String serializedHex = Hex.encode(serializedBytes); - + // Compare the serialized transaction assertEquals(fixture.get("serialized"), serializedHex); assertEquals(data.get("id"), builder.transaction.getId());