Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement json param validation #2121

Merged
merged 6 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions rskj-core/src/main/java/co/rsk/rpc/Web3EthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,38 @@
package co.rsk.rpc;

import co.rsk.rpc.modules.eth.EthModule;
import org.ethereum.rpc.CallArguments;
import org.ethereum.rpc.dto.BlockResultDTO;
import org.ethereum.rpc.dto.CompilationResultDTO;
import org.ethereum.rpc.dto.TransactionReceiptDTO;
import org.ethereum.rpc.dto.TransactionResultDTO;
import org.ethereum.rpc.parameters.BlockHashParam;
import org.ethereum.rpc.parameters.FilterRequestParam;
import org.ethereum.rpc.parameters.BlockIdentifierParam;
import org.ethereum.rpc.parameters.BlockRefParam;
import org.ethereum.rpc.parameters.CallArgumentsParam;
import org.ethereum.rpc.parameters.HexAddressParam;
import org.ethereum.rpc.parameters.HexDataParam;
import org.ethereum.rpc.parameters.HexIndexParam;
import org.ethereum.rpc.parameters.TxHashParam;

import java.math.BigInteger;
import java.util.Map;

@SuppressWarnings({"java:S100", "java:S112"})
public interface Web3EthModule {
default String[] eth_accounts() {
return getEthModule().accounts();
}

default String eth_sign(String addr, String data) {
default String eth_sign(HexAddressParam addr, HexDataParam data) {
return getEthModule().sign(addr, data);
}

default String eth_call(CallArguments args, String bnOrId) {
default String eth_call(CallArgumentsParam args, BlockIdentifierParam bnOrId) {
return getEthModule().call(args, bnOrId);
}

default String eth_estimateGas(CallArguments args) {
default String eth_estimateGas(CallArgumentsParam args) {
return getEthModule().estimateGas(args);
}

Expand Down Expand Up @@ -73,21 +78,17 @@ default String eth_chainId() {

String eth_blockNumber();

String eth_call(CallArguments args, Map<String, String> blockRef) throws Exception; // NOSONAR
String eth_call(CallArgumentsParam args, Map<String, String> blockRef) throws Exception; // NOSONAR

String eth_getBalance(String address, String block) throws Exception;
String eth_getBalance(HexAddressParam address, BlockRefParam blockRefParam) throws Exception;

String eth_getBalance(String address) throws Exception;

String eth_getBalance(String address, Map<String, String> blockRef) throws Exception; // NOSONAR
String eth_getBalance(HexAddressParam address) throws Exception;

String eth_getStorageAt(String address, String storageIdx, Map<String, String> blockRef) throws Exception; // NOSONAR

String eth_getStorageAt(String address, String storageIdx, String blockId) throws Exception;

String eth_getTransactionCount(String address, Map<String, String> blockRef) throws Exception; // NOSONAR

String eth_getTransactionCount(String address, String blockId) throws Exception ;
String eth_getTransactionCount(HexAddressParam address, BlockRefParam blockRefParam) throws Exception;

String eth_getBlockTransactionCountByHash(BlockHashParam blockHash)throws Exception;

Expand All @@ -103,11 +104,11 @@ default String eth_getCode(String address, String blockId) {

String eth_getCode(String address, Map<String, String> blockRef) throws Exception; // NOSONAR

default String eth_sendRawTransaction(String rawData) {
default String eth_sendRawTransaction(HexDataParam rawData) {
return getEthModule().sendRawTransaction(rawData);
}

default String eth_sendTransaction(CallArguments args) {
default String eth_sendTransaction(CallArgumentsParam args) {
return getEthModule().sendTransaction(args);
}

Expand All @@ -121,7 +122,7 @@ default String eth_sendTransaction(CallArguments args) {

TransactionResultDTO eth_getTransactionByBlockNumberAndIndex(String bnOrId, String index) throws Exception;

TransactionReceiptDTO eth_getTransactionReceipt(String transactionHash) throws Exception;
TransactionReceiptDTO eth_getTransactionReceipt(TxHashParam transactionHash) throws Exception;

BlockResultDTO eth_getUncleByBlockHashAndIndex(BlockHashParam blockHash, HexIndexParam uncleIdx) throws Exception;

Expand Down
19 changes: 12 additions & 7 deletions rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
import org.ethereum.rpc.CallArguments;
import org.ethereum.rpc.converters.CallArgumentsToByteArray;
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
import org.ethereum.rpc.parameters.BlockIdentifierParam;
import org.ethereum.rpc.parameters.CallArgumentsParam;
import org.ethereum.rpc.parameters.HexAddressParam;
import org.ethereum.rpc.parameters.HexDataParam;
import org.ethereum.vm.GasCost;
import org.ethereum.vm.PrecompiledContracts;
import org.ethereum.vm.program.ProgramResult;
Expand Down Expand Up @@ -120,10 +124,11 @@ public Map<String, Object> bridgeState() throws IOException, BlockStoreException
return state.stateToMap();
}

public String call(CallArguments args, String bnOrId) {
public String call(CallArgumentsParam argsParam, BlockIdentifierParam bnOrId) {
String hReturn = null;
CallArguments args = argsParam.toCallArguments();
try {
ExecutionBlockRetriever.Result result = executionBlockRetriever.retrieveExecutionBlock(bnOrId);
ExecutionBlockRetriever.Result result = executionBlockRetriever.retrieveExecutionBlock(bnOrId.getIdentifier());
Block block = result.getBlock();
Trie finalState = result.getFinalState();
ProgramResult res;
Expand All @@ -150,11 +155,11 @@ public String call(CallArguments args, String bnOrId) {
}
}

public String estimateGas(CallArguments args) {
public String estimateGas(CallArgumentsParam args) {
String estimation = null;
Block bestBlock = blockchain.getBestBlock();
try {
CallArgumentsToByteArray hexArgs = new CallArgumentsToByteArray(args);
CallArgumentsToByteArray hexArgs = new CallArgumentsToByteArray(args.toCallArguments());

TransactionExecutor executor = reversibleTransactionExecutor.estimateGas(
bestBlock,
Expand Down Expand Up @@ -194,17 +199,17 @@ protected String internalEstimateGas(ProgramResult reversibleExecutionResult) {
}

@Override
public String sendTransaction(CallArguments args) {
public String sendTransaction(CallArgumentsParam args) {
return ethModuleTransaction.sendTransaction(args);
}

@Override
public String sendRawTransaction(String rawData) {
public String sendRawTransaction(HexDataParam rawData) {
return ethModuleTransaction.sendRawTransaction(rawData);
}

@Override
public String sign(String addr, String data) {
public String sign(HexAddressParam addr, HexDataParam data) {
return ethModuleWallet.sign(addr, data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

package co.rsk.rpc.modules.eth;

import org.ethereum.rpc.CallArguments;
import org.ethereum.rpc.parameters.CallArgumentsParam;
import org.ethereum.rpc.parameters.HexDataParam;

public interface EthModuleTransaction {
String sendTransaction(CallArguments args);
String sendTransaction(CallArgumentsParam args);

String sendRawTransaction(String rawData);
String sendRawTransaction(HexDataParam rawData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
import org.ethereum.core.TransactionPoolAddResult;
import org.ethereum.rpc.CallArguments;
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
import org.ethereum.rpc.parameters.CallArgumentsParam;
import org.ethereum.rpc.parameters.HexDataParam;
import org.ethereum.util.TransactionArgumentsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import co.rsk.core.RskAddress;
import co.rsk.core.Wallet;
import co.rsk.net.TransactionGateway;
import co.rsk.util.HexUtils;

public class EthModuleTransactionBase implements EthModuleTransaction {

Expand All @@ -56,7 +57,12 @@ public EthModuleTransactionBase(Constants constants, Wallet wallet, TransactionP
}

@Override
public synchronized String sendTransaction(CallArguments args) {
public synchronized String sendTransaction(CallArgumentsParam argsParam) {
CallArguments args = argsParam.toCallArguments();

if(args.getFrom() == null) {
throw invalidParamError("from is null");
}

Account senderAccount = this.wallet.getAccount(new RskAddress(args.getFrom()));

Expand Down Expand Up @@ -97,10 +103,10 @@ public synchronized String sendTransaction(CallArguments args) {
}

@Override
public String sendRawTransaction(String rawData) {
public String sendRawTransaction(HexDataParam rawData) {
String s = null;
try {
Transaction tx = new ImmutableTransaction(HexUtils.stringHexToByteArray(rawData));
Transaction tx = new ImmutableTransaction(rawData.getRawDataBytes());

if (null == tx.getGasLimit() || null == tx.getGasPrice() || null == tx.getValue()) {
throw invalidParamError("Missing parameter, gasPrice, gas or value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import co.rsk.net.TransactionGateway;
import org.ethereum.config.Constants;
import org.ethereum.core.TransactionPool;
import org.ethereum.rpc.CallArguments;
import org.ethereum.rpc.parameters.CallArgumentsParam;

import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError;

Expand All @@ -36,7 +36,7 @@ public EthModuleTransactionDisabled(Constants constants, TransactionPool transac
}

@Override
public String sendTransaction(CallArguments args) { // lgtm [java/non-sync-override]
public synchronized String sendTransaction(CallArgumentsParam args) { // lgtm [java/non-sync-override]
LOGGER.debug("eth_sendTransaction({}): {}", args, null);
throw invalidParamError("Local wallet is disabled in this node");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
import org.ethereum.core.Blockchain;
import org.ethereum.core.TransactionPool;
import org.ethereum.db.TransactionInfo;
import org.ethereum.rpc.CallArguments;
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
import org.ethereum.rpc.parameters.CallArgumentsParam;
import org.ethereum.rpc.parameters.HexDataParam;
import org.ethereum.vm.program.ProgramResult;

import co.rsk.core.Wallet;
Expand Down Expand Up @@ -64,7 +65,7 @@ public EthModuleTransactionInstant(
}

@Override
public synchronized String sendTransaction(CallArguments args) {
public synchronized String sendTransaction(CallArgumentsParam args) {
try {
this.blockExecutor.setRegisterProgramResults(true);

Expand All @@ -80,7 +81,7 @@ public synchronized String sendTransaction(CallArguments args) {
}

@Override
public String sendRawTransaction(String rawData) {
public String sendRawTransaction(HexDataParam rawData) {
try {
this.blockExecutor.setRegisterProgramResults(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

package co.rsk.rpc.modules.eth;

import org.ethereum.rpc.parameters.HexAddressParam;
import org.ethereum.rpc.parameters.HexDataParam;

public interface EthModuleWallet {

String[] accounts();

String sign(String addr, String data);
String sign(HexAddressParam addr, HexDataParam data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package co.rsk.rpc.modules.eth;

import org.ethereum.rpc.parameters.HexAddressParam;
import org.ethereum.rpc.parameters.HexDataParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,7 +39,7 @@ public String[] accounts() {
}

@Override
public String sign(String addr, String data) {
public String sign(HexAddressParam addr, HexDataParam data) {
LOGGER.debug("eth_sign({}, {}): {}", addr, data, null);
throw invalidParamError("Local wallet is disabled in this node");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import org.ethereum.crypto.signature.ECDSASignature;
import org.ethereum.rpc.parameters.HexAddressParam;
import org.ethereum.rpc.parameters.HexDataParam;
import org.ethereum.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import co.rsk.core.RskAddress;
import co.rsk.core.Wallet;
import co.rsk.util.HexUtils;

Expand All @@ -47,15 +48,17 @@ public EthModuleWalletEnabled(Wallet wallet) {
}

@Override
public String sign(String addr, String data) {
public String sign(HexAddressParam addr, HexDataParam data) {
String s = null;
try {
Account account = this.wallet.getAccount(new RskAddress(addr));
Account account = this.wallet.getAccount(addr.getAddress());
if (account == null) {
throw invalidParamError("Account not found");
}

return s = this.sign(data, account.getEcKey());
s = this.sign(data.getRawDataBytes(), account.getEcKey());

return s;
} finally {
LOGGER.debug("eth_sign({}, {}): {}", addr, data, s);
}
Expand All @@ -71,8 +74,7 @@ public String[] accounts() {
}
}

private String sign(String data, ECKey ecKey) {
byte[] dataHash = HexUtils.stringHexToByteArray(data);
private String sign(byte[] dataHash, ECKey ecKey) {
// 0x19 = 25, length should be an ascii decimals, message - original
String prefix = (char) 25 + "Ethereum Signed Message:\n" + dataHash.length;

Expand Down
Loading