Skip to content

Commit

Permalink
code refractored and added eip1559 private tx class
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <[email protected]>
  • Loading branch information
NickSneo committed Nov 3, 2023
1 parent 6b6f97b commit bbd4847
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 300 deletions.
3 changes: 2 additions & 1 deletion abi/src/main/java/org/web3j/abi/TypeDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@ private static <T extends Type> T decodeDynamicParameterFromStruct(
value = decodeDynamicStruct(dynamicElementData, 0, TypeReference.create(declaredField));
} else if (DynamicArray.class.isAssignableFrom(declaredField)) {
if (parameter == null) {
throw new RuntimeException("parameter can not be null, try to use annotation @Parameterized to specify the parameter type");
throw new RuntimeException(

Check warning on line 568 in abi/src/main/java/org/web3j/abi/TypeDecoder.java

View check run for this annotation

Codecov / codecov/patch

abi/src/main/java/org/web3j/abi/TypeDecoder.java#L568

Added line #L568 was not covered by tests
"parameter can not be null, try to use annotation @Parameterized to specify the parameter type");
}
value =
(T)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.web3j.abi.datatypes.generated.Bytes32;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.eea.crypto.PrivateTransactionEncoder;
import org.web3j.protocol.eea.crypto.RawPrivateTransaction;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.tx.gas.BesuPrivacyGasProvider;
import org.web3j.utils.Base64String;
import org.web3j.utils.Numeric;
Expand Down Expand Up @@ -87,8 +87,8 @@ public String buildOnChainPrivateTransaction(
Base64String enclaveKey,
final BigInteger nonce,
String call) {
RawPrivateTransaction rawTransaction =
RawPrivateTransaction.createTransaction(
LegacyPrivateTransaction rawTransaction =
LegacyPrivateTransaction.createTransaction(

Check warning on line 91 in besu/src/main/java/org/web3j/protocol/besu/privacy/OnChainPrivacyTransactionBuilder.java

View check run for this annotation

Codecov / codecov/patch

besu/src/main/java/org/web3j/protocol/besu/privacy/OnChainPrivacyTransactionBuilder.java#L90-L91

Added lines #L90 - L91 were not covered by tests
nonce,
gasProvider.getGasPrice(),
gasProvider.getGasLimit(),
Expand Down
72 changes: 36 additions & 36 deletions besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.web3j.protocol.core.methods.response.EthGetCode;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.eea.crypto.PrivateTxSignServiceImpl;
import org.web3j.protocol.eea.crypto.RawPrivateTransaction;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.service.TxSignService;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.utils.Base64String;
Expand Down Expand Up @@ -138,11 +138,11 @@ public EthSendTransaction sendTransaction(
.send()
.getTransactionCount();

final RawPrivateTransaction transaction;
final LegacyPrivateTransaction transaction;

if (privateFor != null) {
transaction =
RawPrivateTransaction.createTransaction(
LegacyPrivateTransaction.createTransaction(

Check warning on line 145 in besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java

View check run for this annotation

Codecov / codecov/patch

besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java#L145

Added line #L145 was not covered by tests
nonce,
gasPrice,
gasLimit,
Expand All @@ -153,7 +153,7 @@ public EthSendTransaction sendTransaction(
restriction);
} else {
transaction =
RawPrivateTransaction.createTransaction(
LegacyPrivateTransaction.createTransaction(

Check warning on line 156 in besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java

View check run for this annotation

Codecov / codecov/patch

besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java#L156

Added line #L156 was not covered by tests
nonce,
gasPrice,
gasLimit,
Expand Down Expand Up @@ -183,36 +183,36 @@ public EthSendTransaction sendEIP1559Transaction(
.send()
.getTransactionCount();

final RawPrivateTransaction transaction;
if (privateFor != null) {
transaction =
RawPrivateTransaction.createTransaction(
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
data,
privateFrom,
privateFor,
restriction);
} else {
transaction =
RawPrivateTransaction.createTransaction(
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
data,
privateFrom,
privacyGroupId,
restriction);
}
final LegacyPrivateTransaction transaction;
// if (privateFor != null) {
// transaction =
// RawPrivateTransaction.createTransaction(
// chainId,
// nonce,
// maxPriorityFeePerGas,
// maxFeePerGas,
// gasLimit,
// to,
// data,
// privateFrom,
// privateFor,
// restriction);
// } else {
// transaction =
// RawPrivateTransaction.createTransaction(
// chainId,
// nonce,
// maxPriorityFeePerGas,
// maxFeePerGas,
// gasLimit,
// to,
// data,
// privateFrom,
// privacyGroupId,
// restriction);
// }

return signAndSend(transaction);
return signAndSend(null);

Check warning on line 215 in besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java

View check run for this annotation

Codecov / codecov/patch

besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java#L215

Added line #L215 was not covered by tests
}

@Override
Expand All @@ -239,20 +239,20 @@ public EthGetCode getCode(
.send();
}

public String sign(final RawPrivateTransaction rawTransaction) {
public String sign(final LegacyPrivateTransaction rawTransaction) {

final byte[] signedMessage = txSignService.sign(rawTransaction, chainId);

return Numeric.toHexString(signedMessage);
}

public EthSendTransaction signAndSend(final RawPrivateTransaction rawTransaction)
public EthSendTransaction signAndSend(final LegacyPrivateTransaction rawTransaction)
throws IOException {
final String hexValue = sign(rawTransaction);
return this.besu.eeaSendRawTransaction(hexValue).send();
}

public PrivateEnclaveKey signAndDistribute(RawPrivateTransaction rawTransaction)
public PrivateEnclaveKey signAndDistribute(LegacyPrivateTransaction rawTransaction)
throws IOException {
String hexValue = sign(rawTransaction);
return this.besu.privDistributeRawTransaction(hexValue).send();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.SignedRawTransaction;
import org.web3j.crypto.TransactionDecoder;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.protocol.eea.crypto.transaction.type.PrivateTransactionType;
import org.web3j.rlp.RlpDecoder;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpString;
Expand All @@ -30,25 +32,50 @@

public class PrivateTransactionDecoder {

public static RawPrivateTransaction decode(final String hexTransaction) {
public static LegacyPrivateTransaction decode(final String hexTransaction) {
final byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
final PrivateTransactionType transactionType = getPrivateTransactionType(transaction);

switch (transactionType) {
case PRIVATE_LEGACY:
return decodeLegacyPrivateTransaction(transaction);
default:
throw new IllegalArgumentException("Unsupported private transaction type.");

Check warning on line 43 in eea/src/main/java/org/web3j/protocol/eea/crypto/PrivateTransactionDecoder.java

View check run for this annotation

Codecov / codecov/patch

eea/src/main/java/org/web3j/protocol/eea/crypto/PrivateTransactionDecoder.java#L43

Added line #L43 was not covered by tests
}
}

private static PrivateTransactionType getPrivateTransactionType(final byte[] transaction) {
// Determine the type of the private transaction, similar to TransactionDecoder.
byte firstByte = transaction[0];
// if (firstByte == PrivateTransactionType.PRIVATE_LEGACY.getRlpType())
// return PrivateTransactionType.PRIVATE_LEGACY;

// Return LEGACY as default, or throw an exception depending on your needs
return PrivateTransactionType.PRIVATE_LEGACY;
}

private static LegacyPrivateTransaction decodeLegacyPrivateTransaction(
final byte[] transaction) {
final RlpList rlpList = RlpDecoder.decode(transaction);
final RlpList temp = (RlpList) rlpList.getValues().get(0);
final List<RlpType> values = temp.getValues();

final RawTransaction rawTransaction = TransactionDecoder.decode(hexTransaction);
final RawTransaction rawTransaction =
TransactionDecoder.decode(Numeric.toHexString(transaction));

if (values.size() == 9) {
final Base64String privateFrom = extractBase64(values.get(6));
final Restriction restriction = extractRestriction(values.get(8));

if (values.get(7) instanceof RlpList) {
return new RawPrivateTransaction(
rawTransaction, privateFrom, extractBase64List(values.get(7)), restriction);
List<Base64String> privateForList = extractBase64List(values.get(7));
return new LegacyPrivateTransaction(
rawTransaction, privateFrom, privateForList, null, restriction);
} else {
return new RawPrivateTransaction(
rawTransaction, privateFrom, extractBase64(values.get(7)), restriction);
Base64String privacyGroupId = extractBase64(values.get(7));
return new LegacyPrivateTransaction(
rawTransaction, privateFrom, null, privacyGroupId, restriction);
}

} else {
final Base64String privateFrom = extractBase64(values.get(9));
final Restriction restriction = extractRestriction(values.get(11));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@
package org.web3j.protocol.eea.crypto;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.Sign;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.rlp.RlpEncoder;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpString;
import org.web3j.rlp.RlpType;
import org.web3j.utils.Base64String;

/** Create signed RLP encoded private transaction. */
public class PrivateTransactionEncoder {

public static byte[] signMessage(
final RawPrivateTransaction rawTransaction, final Credentials credentials) {
final LegacyPrivateTransaction rawTransaction, final Credentials credentials) {
final byte[] encodedTransaction = encode(rawTransaction);
final Sign.SignatureData signatureData =
Sign.signMessage(encodedTransaction, credentials.getEcKeyPair());
Expand All @@ -38,7 +36,7 @@ public static byte[] signMessage(
}

public static byte[] signMessage(
final RawPrivateTransaction rawTransaction,
final LegacyPrivateTransaction rawTransaction,
final long chainId,
final Credentials credentials) {
final byte[] encodedTransaction = encode(rawTransaction, chainId);
Expand All @@ -50,19 +48,20 @@ public static byte[] signMessage(
return encode(rawTransaction, eip155SignatureData);
}

public static byte[] encode(final RawPrivateTransaction rawTransaction) {
public static byte[] encode(final LegacyPrivateTransaction rawTransaction) {
return encode(rawTransaction, null);
}

public static byte[] encode(final RawPrivateTransaction rawTransaction, final long chainId) {
public static byte[] encode(final LegacyPrivateTransaction rawTransaction, final long chainId) {
final Sign.SignatureData signatureData =
new Sign.SignatureData(longToBytes(chainId), new byte[] {}, new byte[] {});
return encode(rawTransaction, signatureData);
}

private static byte[] encode(
final RawPrivateTransaction rawTransaction, final Sign.SignatureData signatureData) {
final List<RlpType> values = asRlpValues(rawTransaction, signatureData);
final LegacyPrivateTransaction rawTransaction, final Sign.SignatureData signatureData) {
// final List<RlpType> values = asRlpValues(rawTransaction, signatureData);
final List<RlpType> values = rawTransaction.asRlpValues(signatureData);
final RlpList rlpList = new RlpList(values);
return RlpEncoder.encode(rlpList);
}
Expand All @@ -72,11 +71,4 @@ private static byte[] longToBytes(long x) {
buffer.putLong(x);
return buffer.array();
}

public static List<RlpType> asRlpValues(
final RawPrivateTransaction privateTransaction,
final Sign.SignatureData signatureData) {

return privateTransaction.asRlpValues(signatureData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.service.TxSignService;
import org.web3j.tx.ChainId;

Expand All @@ -26,27 +27,25 @@ public PrivateTxSignServiceImpl(Credentials credentials) {
this.credentials = credentials;
}

@Override
public byte[] sign(RawTransaction rawTransaction, long chainId) {
if (!(rawTransaction instanceof RawPrivateTransaction)) {
throw new RuntimeException("Can only sign RawPrivateTransaction");
public byte[] sign(RawTransaction privateTransaction, long chainId) {
if (!(privateTransaction instanceof LegacyPrivateTransaction)) {
throw new RuntimeException("Can only sign LegacyPrivateTransaction");

Check warning on line 32 in eea/src/main/java/org/web3j/protocol/eea/crypto/PrivateTxSignServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

eea/src/main/java/org/web3j/protocol/eea/crypto/PrivateTxSignServiceImpl.java#L32

Added line #L32 was not covered by tests
}

final byte[] signedMessage;

if (chainId > ChainId.NONE) {
signedMessage =
PrivateTransactionEncoder.signMessage(
(RawPrivateTransaction) rawTransaction, chainId, credentials);
(LegacyPrivateTransaction) privateTransaction, chainId, credentials);
} else {
signedMessage =
PrivateTransactionEncoder.signMessage(
(RawPrivateTransaction) rawTransaction, credentials);
(LegacyPrivateTransaction) privateTransaction, credentials);
}
return signedMessage;
}

@Override
public String getAddress() {
return credentials.getAddress();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import org.web3j.crypto.Sign;
import org.web3j.crypto.SignatureDataOperations;
import org.web3j.crypto.SignedRawTransaction;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.utils.Base64String;
import org.web3j.utils.Restriction;

public class SignedRawPrivateTransaction extends RawPrivateTransaction
public class SignedRawPrivateTransaction extends LegacyPrivateTransaction
implements SignatureDataOperations {

private final Sign.SignatureData signatureData;
Expand Down Expand Up @@ -55,23 +56,24 @@ public SignedRawPrivateTransaction(
final Base64String privateFrom,
final List<Base64String> privateFor,
final Restriction restriction) {
this(signedRawTransaction, privateFrom, privateFor, null, restriction);
this(
signedRawTransaction.getNonce(),
signedRawTransaction.getGasPrice(),
signedRawTransaction.getGasLimit(),
signedRawTransaction.getTo(),
signedRawTransaction.getData(),
signedRawTransaction.getSignatureData(),
privateFrom,
privateFor,
null,
restriction);
}

public SignedRawPrivateTransaction(
final SignedRawTransaction signedRawTransaction,
final Base64String privateFrom,
final Base64String privacyGroupId,
final Restriction restriction) {
this(signedRawTransaction, privateFrom, null, privacyGroupId, restriction);
}

private SignedRawPrivateTransaction(
final SignedRawTransaction signedRawTransaction,
final Base64String privateFrom,
final List<Base64String> privateFor,
final Base64String privacyGroupId,
final Restriction restriction) {
this(
signedRawTransaction.getNonce(),
signedRawTransaction.getGasPrice(),
Expand All @@ -80,7 +82,7 @@ private SignedRawPrivateTransaction(
signedRawTransaction.getData(),
signedRawTransaction.getSignatureData(),
privateFrom,
privateFor,
null,
privacyGroupId,
restriction);
}
Expand Down
Loading

0 comments on commit bbd4847

Please sign in to comment.