Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 38 cryptocurrency web3j command line tools These tools provide several useful utility commands for working with Ethereum To transact on either the mainnet or testnet you need to have some Ether cryptocurrency available The transaction process relies on public key cryptography in which users have a public private key pair They use the private key which only they know to sign a transaction which is published with the associated public key The public key can then be used by other network participants to verify that the transaction is authentic based on the transaction signature Fortunately for the users of Ethereum and web3j the public key cryptography is abstracted away so you need to work with only an Ethereum wallet a digital file containing account credentials for transacting with Ethereum A user can have any number of wallet files It contains a passwordencrypted private key and an address that is derived from the public key note that the public key and address can both be derived from the private key All transactions on the network are associated with such an address You can create a new secure wallet file for Ethereum using the web3j command line tools as follows web3j 107 bin web3j wallet create _ _____ _ _ ____ _ _ __ _____ __ _ _ ___ _ _ _ V V __ _ ___ _ _ __ ______ ____ __ ___ _ __ Please enter a wallet file password Please re enter the password Please enter a destination directory location ethereum testnet keystore Wallet file timestamp UUID json created in ethereum testnet keystore Ensure that you select a secure password and that you dont forget it There is no way of recovering your funds if you forget your password or if someone steals your wallet file and figures out your password Once you have an address for your wallet you can head over to Etherscan to view the details of the current balance and all transactions associated with this address Figure 2 shows a wallet address balance on Etherscan Testnet has been configured to allow easy mining of Ether so you can start up a client in mining mode and in a matter of minutes have enough Ether to start creating your own transactions on the network Details are available for running a miner with Geth and with Parity Getting Started with web3j With the Ethereum client running youre now ready to write some Java code to connect to it using web3j Note that complete listings of all the code are available on GitHub The Ethereum clients expose several methods over JSONRPC which is a stateless remote procedure call RPC protocol using JSON web3j supports all of the Ethereum JSON RPC API However there is a lot more plumbing behind the scenes that web3j takes care of than just providing JSON RPC protocol wrappers The JSON RPC API is available over HTTP and interprocess communication IPC and there is a WebSocket implementation in Geth However the most common implementation employs HTTP which I use in the following examples web3j releases are published to both Maven Central and JFrogs JCenter At the time of this writing the current release is 109 however Id recommend you check the web3j main project page to use the most current release in your project
You must have JavaScript enabled to view digital editions.