[Ethereum] How to sign transaction for calling sendRawTransaction JSON RPC method

javajson-rpctestnets

I have a private ethereum network and a java process with communicates with a ethereum node with JSON RPC.

I need to add a transaction from java process by calling the sendRawTransaction RPC method.

How can I sign the transaction to be passed to a sendRawTransaction call?

Best Answer

You might find this answer helpful: How to transfer ether from one account to another using EthereumJ

It gives an example of using the EthereumJ java library to create a transaction and sign it with a private key. It uses the Transaction sign() method. The example goes on to submit the transaction via the library, but for your case you could serialize it and submit it to the sendRawTransaction RPC call.

Something like:

Transaction tx = new Transaction( ... );
tx.sign(senderKey);
String hex =  Hex.toHexString(tx.getEncoded())

See the example here: https://github.com/ethereum/ethereumj/blob/develop/ethereumj-core/src/main/java/org/ethereum/samples/SendTransaction.java