Skip to content

Commit

Permalink
added privateTx EIP1559
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <[email protected]>
  • Loading branch information
NickSneo committed Nov 6, 2023
1 parent bbd4847 commit d47d0a5
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 67 deletions.
76 changes: 38 additions & 38 deletions besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.eea.crypto.PrivateTxSignServiceImpl;
import org.web3j.protocol.eea.crypto.transaction.type.LegacyPrivateTransaction;
import org.web3j.service.TxSignService;
import org.web3j.protocol.eea.crypto.transaction.type.PrivateTransaction1559;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.utils.Base64String;
import org.web3j.utils.Numeric;
Expand All @@ -37,7 +37,7 @@ public class PrivateTransactionManager extends TransactionManager {

private final Besu besu;

private final TxSignService txSignService;
private final PrivateTxSignServiceImpl privateTxSignService;
private final long chainId;

private final Base64String privateFrom;
Expand Down Expand Up @@ -74,15 +74,15 @@ public PrivateTransactionManager(
final Base64String privateFrom,
final Base64String privacyGroupId,
final Restriction restriction,
final TxSignService txSignService) {
final PrivateTxSignServiceImpl privateTxSignService) {
super(transactionReceiptProcessor, credentials.getAddress());
this.besu = besu;
this.chainId = chainId;
this.privateFrom = privateFrom;
this.privateFor = null;
this.privacyGroupId = privacyGroupId;
this.restriction = restriction;
this.txSignService = txSignService;
this.privateTxSignService = privateTxSignService;
}

public PrivateTransactionManager(
Expand Down Expand Up @@ -112,15 +112,15 @@ public PrivateTransactionManager(
final Base64String privateFrom,
final List<Base64String> privateFor,
final Restriction restriction,
final TxSignService txSignService) {
final PrivateTxSignServiceImpl privateTxSignService) {
super(transactionReceiptProcessor, credentials.getAddress());
this.besu = besu;
this.chainId = chainId;
this.privateFrom = privateFrom;
this.privateFor = privateFor;
this.privacyGroupId = PrivacyGroupUtils.generateLegacyGroup(privateFrom, privateFor);
this.restriction = restriction;
this.txSignService = txSignService;
this.privateTxSignService = privateTxSignService;

Check warning on line 123 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#L123

Added line #L123 was not covered by tests
}

@Override
Expand All @@ -134,7 +134,7 @@ public EthSendTransaction sendTransaction(
throws IOException {

final BigInteger nonce =
besu.privGetTransactionCount(txSignService.getAddress(), privacyGroupId)
besu.privGetTransactionCount(privateTxSignService.getAddress(), privacyGroupId)

Check warning on line 137 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#L137

Added line #L137 was not covered by tests
.send()
.getTransactionCount();

Expand Down Expand Up @@ -179,40 +179,40 @@ public EthSendTransaction sendEIP1559Transaction(
boolean constructor)
throws IOException {
final BigInteger nonce =
besu.privGetTransactionCount(txSignService.getAddress(), privacyGroupId)
besu.privGetTransactionCount(privateTxSignService.getAddress(), privacyGroupId)

Check warning on line 182 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#L182

Added line #L182 was not covered by tests
.send()
.getTransactionCount();

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);
// }
final PrivateTransaction1559 transaction;
if (privateFor != null) {
transaction =
new PrivateTransaction1559(
chainId,
nonce,
gasLimit,
to,
data,
maxPriorityFeePerGas,
maxFeePerGas,
privateFrom,
privateFor,
restriction);
} else {
transaction =
new PrivateTransaction1559(
chainId,
nonce,
gasLimit,
to,
data,
maxPriorityFeePerGas,
maxFeePerGas,
privateFrom,
privacyGroupId,
restriction);
}

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

@Override
Expand Down Expand Up @@ -241,7 +241,7 @@ public EthGetCode getCode(

public String sign(final LegacyPrivateTransaction rawTransaction) {

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

Check warning on line 244 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#L244

Added line #L244 was not covered by tests

return Numeric.toHexString(signedMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.web3j.protocol.besu.Besu;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.tx.exceptions.ContractCallException;
import org.web3j.tx.response.PollingPrivateTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
Expand Down Expand Up @@ -51,6 +52,8 @@ class PrivateTransactionManagerTest {
DefaultBlockParameter defaultBlockParameter = mock(DefaultBlockParameter.class);
EthCall response = mock(EthCall.class);

EthSendTransaction sendTransaction = mock(EthSendTransaction.class);

@Test
public void sendPrivCallTest() throws IOException {
when(response.getValue()).thenReturn("test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
package org.web3j.protocol.eea.crypto;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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.PrivateTransaction1559;
import org.web3j.protocol.eea.crypto.transaction.type.PrivateTransactionType;
import org.web3j.rlp.RlpDecoder;
import org.web3j.rlp.RlpList;
Expand All @@ -36,22 +39,85 @@ 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.");
if (transactionType == PrivateTransactionType.PRIVATE_1559) {
return decodePrivateTransaction1559(transaction);
}
return decodeLegacyPrivateTransaction(transaction);
}

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;
if (firstByte == PrivateTransactionType.PRIVATE_1559.getRlpType())
return PrivateTransactionType.PRIVATE_1559;
else return PrivateTransactionType.PRIVATE_LEGACY;
}

private static LegacyPrivateTransaction decodePrivateTransaction1559(final byte[] transaction) {
final byte[] encodedTx = Arrays.copyOfRange(transaction, 1, transaction.length);
final RlpList rlpList = RlpDecoder.decode(encodedTx);
final RlpList temp = (RlpList) rlpList.getValues().get(0);
final List<RlpType> values = temp.getValues();

System.out.println(rlpList);
System.out.println(temp);
System.out.println(values);

final long chainId =
((RlpString) temp.getValues().get(0)).asPositiveBigInteger().longValue();
System.out.println("chainId " + chainId);
final BigInteger nonce = ((RlpString) temp.getValues().get(1)).asPositiveBigInteger();
System.out.println("nonce " + nonce);

// Return LEGACY as default, or throw an exception depending on your needs
return PrivateTransactionType.PRIVATE_LEGACY;
final BigInteger maxPriorityFeePerGas =
((RlpString) temp.getValues().get(2)).asPositiveBigInteger();
System.out.println("maxPriorityFeePerGas " + maxPriorityFeePerGas);
final BigInteger maxFeePerGas =
((RlpString) temp.getValues().get(3)).asPositiveBigInteger();
System.out.println("maxFeePerGas " + maxFeePerGas);

final BigInteger gasLimit = ((RlpString) temp.getValues().get(4)).asPositiveBigInteger();
System.out.println("gasLimit " + gasLimit);
final String to = ((RlpString) temp.getValues().get(5)).asString();
System.out.println("to " + to);

final String data = ((RlpString) temp.getValues().get(7)).asString();
System.out.println("data " + data);

final Base64String privateFrom = extractBase64(values.get(8));
System.out.println("privateFrom " + privateFrom);
final Restriction restriction = extractRestriction(values.get(10));
System.out.println("restriction " + restriction);

if (values.get(9) instanceof RlpList) {
List<Base64String> privateForList = extractBase64List(values.get(9));
return new PrivateTransaction1559(
chainId,
nonce,
gasLimit,
to,
data,
maxPriorityFeePerGas,
maxFeePerGas,
privateFrom,
privateForList,
null,
restriction);
} else {
Base64String privacyGroupId = extractBase64(values.get(9));
return new PrivateTransaction1559(

Check warning on line 108 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#L107-L108

Added lines #L107 - L108 were not covered by tests
chainId,
nonce,
gasLimit,
to,
data,
maxPriorityFeePerGas,
maxFeePerGas,
privateFrom,
null,
privacyGroupId,
restriction);
}
}

private static LegacyPrivateTransaction decodeLegacyPrivateTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
public class PrivateTransactionEncoder {

public static byte[] signMessage(
final LegacyPrivateTransaction rawTransaction, final Credentials credentials) {
final byte[] encodedTransaction = encode(rawTransaction);
final LegacyPrivateTransaction privateTransaction, final Credentials credentials) {
final byte[] encodedTransaction = encode(privateTransaction);
final Sign.SignatureData signatureData =
Sign.signMessage(encodedTransaction, credentials.getEcKeyPair());

return encode(rawTransaction, signatureData);
return encode(privateTransaction, signatureData);
}

public static byte[] signMessage(
Expand All @@ -59,11 +59,20 @@ public static byte[] encode(final LegacyPrivateTransaction rawTransaction, final
}

private static byte[] encode(
final LegacyPrivateTransaction rawTransaction, final Sign.SignatureData signatureData) {
final LegacyPrivateTransaction privateTransaction,
final Sign.SignatureData signatureData) {
// final List<RlpType> values = asRlpValues(rawTransaction, signatureData);
final List<RlpType> values = rawTransaction.asRlpValues(signatureData);
final List<RlpType> values = privateTransaction.asRlpValues(signatureData);
final RlpList rlpList = new RlpList(values);
return RlpEncoder.encode(rlpList);
byte[] encoded = RlpEncoder.encode(rlpList);

if (privateTransaction.getTransactionType().isPrivateEip1559()) {
return ByteBuffer.allocate(encoded.length + 1)
.put(privateTransaction.getTransactionType().getRlpType())
.put(encoded)
.array();
}
return encoded;
}

private static byte[] longToBytes(long x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.web3j.crypto.Sign;
import org.web3j.crypto.transaction.type.ITransaction;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpString;
import org.web3j.rlp.RlpType;
import org.web3j.utils.Base64String;
Expand All @@ -32,7 +31,7 @@ public class PrivateTransaction1559 extends LegacyPrivateTransaction implements
private BigInteger maxPriorityFeePerGas;
private BigInteger maxFeePerGas;

protected PrivateTransaction1559(
public PrivateTransaction1559(
long chainId,
BigInteger nonce,
BigInteger gasLimit,
Expand All @@ -59,7 +58,7 @@ protected PrivateTransaction1559(
this.maxFeePerGas = maxFeePerGas;
}

protected PrivateTransaction1559(
public PrivateTransaction1559(
long chainId,
BigInteger nonce,
BigInteger gasLimit,
Expand All @@ -84,7 +83,7 @@ protected PrivateTransaction1559(
restriction);
}

Check warning on line 84 in eea/src/main/java/org/web3j/protocol/eea/crypto/transaction/type/PrivateTransaction1559.java

View check run for this annotation

Codecov / codecov/patch

eea/src/main/java/org/web3j/protocol/eea/crypto/transaction/type/PrivateTransaction1559.java#L84

Added line #L84 was not covered by tests

protected PrivateTransaction1559(
public PrivateTransaction1559(
long chainId,
BigInteger nonce,
BigInteger gasLimit,
Expand Down Expand Up @@ -140,9 +139,6 @@ public List<RlpType> asRlpValues(final Sign.SignatureData signatureData) {
byte[] data = Numeric.hexStringToByteArray(getData());
result.add(RlpString.create(data));

// access list
result.add(new RlpList());

if (signatureData != null) {
result.add(RlpString.create(Sign.getRecId(signatureData, getChainId())));
result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getR())));
Expand Down Expand Up @@ -177,4 +173,9 @@ public BigInteger getMaxPriorityFeePerGas() {
public BigInteger getMaxFeePerGas() {
return maxFeePerGas;
}

@Override
public PrivateTransactionType getTransactionType() {
return PrivateTransactionType.PRIVATE_1559;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@
package org.web3j.protocol.eea.crypto.transaction.type;

public enum PrivateTransactionType {
PRIVATE_LEGACY("Legacy"),
PRIVATE_1559("EIP-1559");
PRIVATE_LEGACY(null),
PRIVATE_1559(((byte) 0x01));

private String description;
Byte type;

PrivateTransactionType(String description) {
this.description = description;
PrivateTransactionType(final Byte type) {
this.type = type;
}

public String getDescription() {
return description;
public Byte getRlpType() {
return type;
}

public boolean isPrivateLegacy() {
return this.equals(PrivateTransactionType.PRIVATE_LEGACY);

Check warning on line 30 in eea/src/main/java/org/web3j/protocol/eea/crypto/transaction/type/PrivateTransactionType.java

View check run for this annotation

Codecov / codecov/patch

eea/src/main/java/org/web3j/protocol/eea/crypto/transaction/type/PrivateTransactionType.java#L30

Added line #L30 was not covered by tests
}

public boolean isPrivateEip1559() {
return this.equals(PrivateTransactionType.PRIVATE_1559);
}
}
Loading

0 comments on commit d47d0a5

Please sign in to comment.