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

Add deserializers #2093

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -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<BlockHashParam> {
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<HexIndexParam> {

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);
}


}
}
Original file line number Diff line number Diff line change
@@ -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<TransactionHashParam> {
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;
}
}