|
| 1 | +/* |
| 2 | + * Copyright Hyperledger Besu Contributors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.cli.subcommands; |
| 16 | + |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | +import static org.hyperledger.besu.cli.subcommands.TxParseSubCommand.COMMAND_NAME; |
| 19 | + |
| 20 | +import org.hyperledger.besu.cli.util.VersionProvider; |
| 21 | +import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; |
| 22 | +import org.hyperledger.besu.ethereum.core.encoding.EncodingContext; |
| 23 | +import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder; |
| 24 | + |
| 25 | +import java.io.BufferedReader; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.io.PrintWriter; |
| 28 | +import java.math.BigInteger; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Paths; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import org.apache.tuweni.bytes.Bytes; |
| 34 | +import picocli.CommandLine; |
| 35 | + |
| 36 | +/** |
| 37 | + * txparse sub command implementing txparse spec from |
| 38 | + * https://github.com/holiman/txparse/tree/main/cmd/txparse |
| 39 | + */ |
| 40 | +@CommandLine.Command( |
| 41 | + name = COMMAND_NAME, |
| 42 | + description = "Parse input transactions and return the sender, or an error.", |
| 43 | + mixinStandardHelpOptions = true, |
| 44 | + versionProvider = VersionProvider.class) |
| 45 | +public class TxParseSubCommand implements Runnable { |
| 46 | + |
| 47 | + /** The constant COMMAND_NAME. */ |
| 48 | + public static final String COMMAND_NAME = "txparse"; |
| 49 | + |
| 50 | + @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. |
| 51 | + @CommandLine.Option( |
| 52 | + names = "--corpus-file", |
| 53 | + arity = "1..1", |
| 54 | + description = "file to read transaction data lines from, otherwise defaults to stdin") |
| 55 | + private String corpusFile = null; |
| 56 | + |
| 57 | + static final BigInteger halfCurveOrder = |
| 58 | + SignatureAlgorithmFactory.getInstance().getHalfCurveOrder(); |
| 59 | + static final BigInteger chainId = new BigInteger("1", 10); |
| 60 | + |
| 61 | + private final PrintWriter out; |
| 62 | + |
| 63 | + /** |
| 64 | + * Instantiates a new TxParse sub command. |
| 65 | + * |
| 66 | + * @param out the PrintWriter where validation results will be reported. |
| 67 | + */ |
| 68 | + public TxParseSubCommand(final PrintWriter out) { |
| 69 | + this.out = out; |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("StreamResourceLeak") |
| 73 | + Stream<String> fileStreamReader(final String filePath) { |
| 74 | + try { |
| 75 | + return Files.lines(Paths.get(filePath)) |
| 76 | + // skip comments |
| 77 | + .filter(line -> !line.startsWith("#")) |
| 78 | + // strip out non-alphanumeric characters |
| 79 | + .map(line -> line.replaceAll("[^a-zA-Z0-9]", "")); |
| 80 | + } catch (Exception ex) { |
| 81 | + throw new RuntimeException(ex); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void run() { |
| 87 | + |
| 88 | + Stream<String> txStream; |
| 89 | + if (corpusFile != null) { |
| 90 | + txStream = fileStreamReader(corpusFile); |
| 91 | + } else { |
| 92 | + txStream = new BufferedReader(new InputStreamReader(System.in, UTF_8)).lines(); |
| 93 | + } |
| 94 | + |
| 95 | + txStream.forEach( |
| 96 | + line -> { |
| 97 | + try { |
| 98 | + Bytes bytes = Bytes.fromHexStringLenient(line); |
| 99 | + dump(bytes); |
| 100 | + } catch (Exception ex) { |
| 101 | + err(ex.getMessage()); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + void dump(final Bytes tx) { |
| 107 | + try { |
| 108 | + var transaction = TransactionDecoder.decodeOpaqueBytes(tx, EncodingContext.BLOCK_BODY); |
| 109 | + |
| 110 | + // https://github.com/hyperledger/besu/blob/5fe49c60b30fe2954c7967e8475c3b3e9afecf35/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java#L252 |
| 111 | + if (transaction.getChainId().isPresent() && !transaction.getChainId().get().equals(chainId)) { |
| 112 | + throw new Exception("wrong chain id"); |
| 113 | + } |
| 114 | + |
| 115 | + // https://github.com/hyperledger/besu/blob/5fe49c60b30fe2954c7967e8475c3b3e9afecf35/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java#L270 |
| 116 | + if (transaction.getS().compareTo(halfCurveOrder) > 0) { |
| 117 | + throw new Exception("signature s out of range"); |
| 118 | + } |
| 119 | + out.println(transaction.getSender()); |
| 120 | + |
| 121 | + } catch (Exception ex) { |
| 122 | + err(ex.getMessage()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + void err(final String message) { |
| 127 | + out.println("err: " + message); |
| 128 | + } |
| 129 | +} |
0 commit comments