1

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

0

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.

  • main("eth_sendRawTransaction", "xxxxx") is my wrapped method,it is no problem,i'm sorry i didn't make that quite clear. – Lin Chen Jan 6 '18 at 6:05
  • I'm not sure what you mean by that. Do you mean that main is fixing things up and just sending a string? – smarx Jan 6 '18 at 6:06
  • The error message seems consistent with you passing a dictionary instead of a string. – smarx Jan 6 '18 at 6:06
  • this is finally sended to geth.>> {"jsonrpc":"2.0","method":"eth_sendRawTransaction", "params":[{ "data": "0xf8670e85051f4d5c0083419ce094ec0d1e2d07d3c81bfd22eeac06115385577696f480b844a9059cbb00000000000000000000000083e0e523b6cb2f9089b706c909a65a54c988e894000000000000000000000000000000000000000000000000002386f26fc10000fbde882364f9e55adcf2fe04e77ce082a5acd9c7e0a86afbab051c39caf6bdb25b09607737b8441bb0ccbd8311b6e61c384c7908ae2c875bc8d4f82cb7eb0e5a1c"}], "id": 1} – Lin Chen Jan 6 '18 at 6:14
  • Right. You're sending a dictionary with a "data" key instead of sending a string as required by that method – smarx Jan 6 '18 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.