[Ethereum] eth_sendRawTransaction:invalid argument 0: json: cannot unmarshal non-string into Go value of type hexutil.Bytes

go-ethereumweb3j

I call the eth_sign to get signature of RawTransaction, then call eth_sendRawTransaction,but got the error

"{"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"invalid
argument 0: json: cannot unmarshal non-string into Go value of type
hexutil.Bytes"}}"

I have no idea in where it is wrong.

I use some libs of web3j,this is my method:

public JSONObject eth_sendRawTransaction(String contract, String from, String to, double amount, long gasLimit) throws Exception {
    walletpassphrase(from) ;

    JSONObject json = null;
    try {
        BigInteger nNonce = getNonce(from);

        String gasPrice = eth_gasPrice();
        BigInteger nGasPrice = Numeric.decodeQuantity(gasPrice);
        BigInteger nGasLimit = BigInteger.valueOf(gasLimit);

        BigInteger nAmount = Convert.toWei(Double.toString(amount), Convert.Unit.ETHER).toBigInteger();
        Function function = new Function(
                "transfer",
                Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(to),
                        new org.web3j.abi.datatypes.generated.Uint256(nAmount)),
                Collections.emptyList());
        String data = FunctionEncoder.encode(function);

        RawTransaction rawTransaction = RawTransaction.createTransaction(
                nNonce,
                nGasPrice,
                nGasLimit,
                contract,
                BigInteger.ZERO,
                data);
        byte[] encodedTransaction = TransactionEncoder.encode(rawTransaction);
        String encoded = Numeric.toHexString(encodedTransaction);
        String signature = eth_sign(from, encoded);
        String signed = encoded + Numeric.cleanHexPrefix(signature);

        String s = main("eth_sendRawTransaction", "[{"+
                " \"data\": \""+signed+"\""+
                "}]");

        json = JSONObject.fromObject(s);
        System.out.println("eth_sendRawTransaction - " + json);
    } catch (Throwable e) {
        e.printStackTrace();
    }

    lockAccount(from) ;

    return json;

}

I think that this code is wrong in below,but i don't know how to fix.

String signed = encoded + Numeric.cleanHexPrefix(signature);

Best Answer

eth_sendRawTransaction takes a single parameter, and that parameter is expected to be a JSON string with the hex encoding of the signed transaction in it.

You're passing a JSON dictionary instead.

EDIT

eth_sign is also probably a problem. eth_sign prefixes the string it receives before signing it. See https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign.

eth_signTransaction though seemingly undocumented, seems to do what you want.

Related Topic