Skip to content

Commit

Permalink
add chain id and signature checks
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Dec 9, 2023
1 parent faba4ef commit a243c82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import static org.hyperledger.besu.cli.subcommands.TxParseSubCommand.COMMAND_NAME;

import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
Expand All @@ -37,10 +39,9 @@
*/
@CommandLine.Command(
name = COMMAND_NAME,
description = "Execute an Eth ereum State Test.",
description = "Execute an Ethereum State Test.",
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class,
subcommands = {})
versionProvider = VersionProvider.class)
public class TxParseSubCommand implements Runnable {

/** The constant COMMAND_NAME. */
Expand All @@ -52,6 +53,8 @@ public class TxParseSubCommand implements Runnable {
arity = "1..1",
description = "file to read transaction data lines from, otherwise defaults to stdin")
private String corpusFile = null;
static final BigInteger halfCurveOrder = SignatureAlgorithmFactory.getInstance().getHalfCurveOrder();
static final BigInteger chainId = new BigInteger("1", 10);

private final PrintWriter out;

Expand Down Expand Up @@ -100,7 +103,19 @@ public void run() {

void dump(final Bytes tx) {
try {
out.println(TransactionDecoder.decodeOpaqueBytes(tx, EncodingContext.BLOCK_BODY).getSender());
var transaction = TransactionDecoder.decodeOpaqueBytes(tx, EncodingContext.BLOCK_BODY);

// https://github.com/hyperledger/besu/blob/5fe49c60b30fe2954c7967e8475c3b3e9afecf35/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java#L252
if (transaction.getChainId().isPresent() && !transaction.getChainId().get().equals(chainId) ){
throw new Exception("wrong chain id");
}

// https://github.com/hyperledger/besu/blob/5fe49c60b30fe2954c7967e8475c3b3e9afecf35/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java#L270
if (transaction.getS().compareTo(halfCurveOrder) > 0) {
throw new Exception("signature s out of range");
}
out.println(transaction.getSender());

} catch (Exception ex) {
err(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ public void assertValidRlp() {
String output = baos.toString(UTF_8);
assertThat(output).isEqualTo("0x730aa72228b55236de378f5267dfc77723b1380c\n");
}

@Test
public void assertInvalidChainId(){
txParseSubCommand.dump(
Bytes.fromHexString(
"0xf901fc303083303030803030b901ae30303030303030303030433030303030303030303030303030303030303030303030303030303030203030413030303030303030303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038390030000000002800380000413159000021000000002b0000000000003800000000003800000000000000000000002b633279315a00633200303041374222610063416200325832325a002543323861795930314130383058435931317a633043304239623900310031254363384361000023433158253041380037000027285839005a007937000000623700002761002b5a003937622e000000006300007863410000002a2e320000000000005a0000000000000037242778002039006120412e6362002138300000002a313030303030303030303030373030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030a03030303030303030303030303030303030303030303030303030303030303030a03030303030303030303030303030303030303030303030303030303030303030"));
String output = baos.toString(UTF_8);
assertThat(output).isEqualTo("err: wrong chain id\n");
}
}

0 comments on commit a243c82

Please sign in to comment.