Skip to content

Commit

Permalink
Merge branch 'refactor/evm-builder' of github.com:ArkEcosystem/java-c…
Browse files Browse the repository at this point in the history
…rypto into refactor/evm-builder
  • Loading branch information
alfonsobries committed Nov 18, 2024
2 parents 6c5013c + ebb1220 commit 14571c6
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ 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 {
buffer.put((byte) 0);
}

// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class AbstractTransactionBuilder<

public AbstractTransactionBuilder() {
this.transaction = getTransactionInstance();

initializeTransactionDefaults();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import org.arkecosystem.crypto.transactions.types.AbstractTransaction;
import org.arkecosystem.crypto.transactions.types.ValidatorRegistration;

public class ValidatorRegistrationBuilder extends AbstractTransactionBuilder<ValidatorRegistrationBuilder> {
public class ValidatorRegistrationBuilder
extends AbstractTransactionBuilder<ValidatorRegistrationBuilder> {
public ValidatorRegistrationBuilder validatorPublicKey(String validatorPublicKey) {
this.transaction.validatorPublicKey = validatorPublicKey;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import org.arkecosystem.crypto.transactions.types.AbstractTransaction;
import org.arkecosystem.crypto.transactions.types.ValidatorResignation;

public class ValidatorResignationBuilder extends AbstractTransactionBuilder<ValidatorResignationBuilder> {
public class ValidatorResignationBuilder
extends AbstractTransactionBuilder<ValidatorResignationBuilder> {

@Override
protected AbstractTransaction getTransactionInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -132,7 +131,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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/arkecosystem/crypto/utils/AbiBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AbiBase() throws IOException {
String abiFilePath = "Abi.Consensus.json";

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");
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/arkecosystem/crypto/utils/AbiEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public String encodeFunctionCall(String functionName, List<Object> args) throws
return encodeFunctionData(parameters);
}


private String encodeFunctionData(Map<String, Object> parameters) throws Exception {
List<Object> args = (List<Object>) parameters.getOrDefault("args", new ArrayList<>());

Expand Down Expand Up @@ -232,13 +231,12 @@ private Map<String, Object> 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<String, Object> encodeBool(Boolean value) {
String encoded = String.format("%064x", value ? 1 : 0);
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/arkecosystem/crypto/utils/TransactionHasher.java
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand All @@ -32,12 +31,19 @@ public static byte[] toHash(Map<String, Object> transaction, boolean skipSignatu
List<Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 14571c6

Please sign in to comment.