I'd like to generate and sign a transaction offline and then send that transaction with a different account. I've found a few web3js sources and I've read the web3 documentation, but I haven't found any clear solutions for doing this in Java with web3j. Thanks for the help.
-
what do you mean by "different account"? If you sign the transaction offline by one account, that's the account that will send the transaction, not another account– natewelch_Apr 5, 2019 at 19:10
-
I want to create a signed raw transaction that can be broadcast by a third-party (someone other than the user who created the transaction). I misspoke when I said "different account."– BenjaminiusApr 5, 2019 at 20:34
2 Answers
I think something like this should work
// Create the Transaction
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(<nonce>, <gasPrice>,
<gasLimit>, <to>, <value>);
// Sign the Transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, <your-Credentials>);
String hexValue = Numeric.toHexString(signedMessage);
and then give the hexValue to the third-party to broadcast it to the network
Update: now you need the chain ID to the method singMessage, like this:
long chainId = <the id of the used chain>
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId , <you Credentials>);
You probably need to dig around SignedRawTransaction
and TransactionEncoder
classes.