Creating and working with wallet files¶
In order to sign transactions offline, you need to have either your Ethereum wallet file or the public and private keys associated with an Ethereum wallet/account.
web3j is able to both generate a new secure Ethereum wallet file for you, or work with an existing wallet file.
To create a new wallet file:
String fileName = WalletUtils.generateNewWalletFile( "your password", new File("/path/to/destination"));
To load the credentials from a wallet file:
Credentials credentials = WalletUtils.loadCredentials( "your password", "/path/to/walletfile");
These credentials are then used to sign transactions.
Please refer to the Web3 Secret Storage Definition for the full wallet file specification.
Transactions to be used in an offline signing capacity, should use the RawTransaction type for this purpose. The RawTransaction is similar to the previously mentioned Transaction type, however it does not require a from address, as this can be inferred from the signature.
In order to create and sign a raw transaction, the sequence of events is as follows:
- Identify the next available nonce for the sender account
- Create the RawTransaction object
- Encode the RawTransaction object using Recursive Length Prefix encoding
- Sign the RawTransaction object
- Send the RawTransaction object to a node for processing
The nonce is an increasing numeric value which is used to uniquely identify transactions. A nonce can only be used once and until a transaction is mined, it is possible to send multiple versions of a transaction with the same nonce, however, once mined, any subsequent submissions will be rejected.
Once you have obtained the next available nonce, the value can then be used to create your transaction object:
RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, <gas price>, <gas limit>, <toAddress>, <value>);
The transaction can then be signed and encoded:
byte signedMessage = TransactionEncoder.signMessage(rawTransaction, <credentials>); String hexValue = Numeric.toHexString(signedMessage);
Where the credentials are those loaded as per Creating and working with wallet files
The transaction is then sent using eth_sendRawTransaction:
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); // poll for transaction response via org.web3j.protocol.Web3j.ethGetTransactionReceipt(<txHash>)
Please refer to the integration test CreateRawTransactionIT for a full example of creating and sending a raw transaction.