From ad7ffa6cdcd6da218ea958742d59cf6eee534b5f Mon Sep 17 00:00:00 2001 From: Reynold Morel Date: Fri, 4 Aug 2023 15:49:26 -0400 Subject: [PATCH] Add deserializers --- .../rpc/deserializers/BlockHashParam.java | 59 +++++++++++++++++++ .../rpc/deserializers/HexIndexParam.java | 46 +++++++++++++++ .../deserializers/TransactionHashParam.java | 58 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/deserializers/BlockHashParam.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/deserializers/HexIndexParam.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/deserializers/TransactionHashParam.java diff --git a/rskj-core/src/main/java/org/ethereum/rpc/deserializers/BlockHashParam.java b/rskj-core/src/main/java/org/ethereum/rpc/deserializers/BlockHashParam.java new file mode 100644 index 00000000000..0ab11b38516 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/deserializers/BlockHashParam.java @@ -0,0 +1,59 @@ +package org.ethereum.rpc.deserializers; + +import co.rsk.crypto.Keccak256; +import co.rsk.util.HexUtils; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import org.ethereum.rpc.exception.RskJsonRpcRequestException; + +import java.io.IOException; + +import static co.rsk.util.HexUtils.stringHexToByteArray; + +@JsonDeserialize(using = BlockHashParam.Deserializer.class) +public class BlockHashParam { + private final Keccak256 blockHash; + + public BlockHashParam(String hash) { + // TODO: validate + this.blockHash = new Keccak256(stringHexToByteArray(hash)); + } + + public static class Deserializer extends StdDeserializer { + private static final int BLOCK_HASH_BYTE_LENGTH = 32; + + public Deserializer() { + this(null); + } + + public Deserializer(Class vc) { + super(vc); + } + + @Override + public BlockHashParam deserialize(JsonParser jp, DeserializationContext ctxt) + throws IOException { + JsonNode node = jp.getCodec().readTree(jp); + String blockHashString = node.asText(); + byte[] blockHashBytes; + + try { + blockHashBytes = HexUtils.stringHexToByteArray(blockHashString); + } catch (Exception e) { + throw RskJsonRpcRequestException.invalidParamError("Invalid block hash format. " + e.getMessage()); + } + + if (BLOCK_HASH_BYTE_LENGTH != blockHashBytes.length) { + throw RskJsonRpcRequestException.invalidParamError("Invalid block hash: incorrect length."); + } + return new BlockHashParam(blockHashString); + } + } + + public Keccak256 getBlockHash() { + return blockHash; + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/deserializers/HexIndexParam.java b/rskj-core/src/main/java/org/ethereum/rpc/deserializers/HexIndexParam.java new file mode 100644 index 00000000000..3127f521384 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/deserializers/HexIndexParam.java @@ -0,0 +1,46 @@ +package org.ethereum.rpc.deserializers; + +import co.rsk.util.HexUtils; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import org.ethereum.rpc.exception.RskJsonRpcRequestException; + +import java.io.IOException; + +import static co.rsk.util.HexUtils.jsonHexToInt; + +public class HexIndexParam { + private final Integer index; + + public HexIndexParam(String index) { + this.index = jsonHexToInt(index); + } + + public static class Deserializer extends StdDeserializer { + + public Deserializer() { + this(null); + } + + public Deserializer(Class vc) { + super(vc); + } + + @Override + public HexIndexParam deserialize(JsonParser jp, DeserializationContext ctxt) + throws IOException { + JsonNode node = jp.getCodec().readTree(jp); + String indexString = node.asText(); + + if(!HexUtils.hasHexPrefix(indexString)) { + throw RskJsonRpcRequestException.invalidParamError("Invalid argument: " + indexString + ": param should be a hex value string."); + } + + return new HexIndexParam(indexString); + } + + + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/deserializers/TransactionHashParam.java b/rskj-core/src/main/java/org/ethereum/rpc/deserializers/TransactionHashParam.java new file mode 100644 index 00000000000..76b1ca91035 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/deserializers/TransactionHashParam.java @@ -0,0 +1,58 @@ +package org.ethereum.rpc.deserializers; + +import co.rsk.crypto.Keccak256; +import co.rsk.util.HexUtils; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import org.ethereum.rpc.exception.RskJsonRpcRequestException; + +import java.io.IOException; + +import static co.rsk.util.HexUtils.stringHexToByteArray; + +public class TransactionHashParam { + private final Keccak256 transactionHash; + + public TransactionHashParam(String hash) { + this.transactionHash = new Keccak256(stringHexToByteArray(hash)); + } + + public static class Deserializer extends StdDeserializer { + private static final int TRANSACTION_HASH_BYTE_LENGTH = 32; + + public Deserializer() { + this(null); + } + + public Deserializer(Class vc) { + super(vc); + } + + @Override + public TransactionHashParam deserialize(JsonParser jp, DeserializationContext ctxt) + throws IOException { + JsonNode node = jp.getCodec().readTree(jp); + String transactionHashString = node.asText(); + byte[] transactionHashBytes; + + try { + transactionHashBytes = HexUtils.stringHexToByteArray(transactionHashString); + } catch (Exception e) { + throw RskJsonRpcRequestException.invalidParamError("Invalid transaction hash format. " + e.getMessage()); + } + + if (TRANSACTION_HASH_BYTE_LENGTH != transactionHashBytes.length) { + throw RskJsonRpcRequestException.invalidParamError("Invalid transaction hash: incorrect length."); + } + return new TransactionHashParam(transactionHashString); + } + + + } + + public Keccak256 getTransactionHash() { + return transactionHash; + } +}