Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Java and the
Blockchain
Introducing web3j
@conors10
Decentralised
Immutable data structure
Blockchain Technologies
2008
2013
2014
2015+
Ethereum
• The world computer
• Turing-complete virtual machine
• Public blockchain (mainnet & testnet)
Ether
• The fuel of the Ethereum blockchain
• Pay miners to process transactions
• Market capitalisation ~$1bn USD (Bitcoi...
Obtaining Ether
• Buy it
• Find someone
• Kraken
• BTC Markets
• Mine it
• mainnet => requires dedicated GPUs
• testnet =>...
Smart Contracts
• Computerised contract
• Code + data that lives on the blockchain at an
address
• Transactions call funct...
Transactions
• Transfer Ether
• Deploy a smart contract
• Call a smart contract
Transactions
Integration with Ethereum
web3j
• Complete Ethereum JSON-RPC implementation
• Ethereum wallet support
• Smart contract wrappers
• Command line tools...
web3j artefacts
• Maven’s Nexus & Bintray's JFrog repositories
• Java 8: org.web3j:core
• Android: org.web3j:core-android
...
web3j transactions
Getting started with
Ethereum
Free cloud clients @ https://infura.io/
Run a local client (to generate Ether):
$ geth --rpc...
Create a wallet
$ ./web3j-1.0.6/bin/web3j wallet create
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
  / / / _  ...
Wallet file{
"address":"a929d0fe936c719c4e4d1194ae64e415c7e9e8fe",
"id":"c2fbffdd-f588-43a8-9b0c-facb6fd84dfe",
"version":3...
View transactions
Using web3j
• Create client
Web3j web3 = Web3j.build(new HttpService());
// defaults to http://localhost:8545/
• Call meth...
Display client version
Web3j web3 = Web3j.build(new HttpService());
Web3ClientVersion clientVersion =
web3.web3ClientVersi...
Sending Ether
Web3j web3 = Web3j.build(new HttpService());
Credentials credentials = WalletUtils.loadCredentials(
"passwor...
Block #1840479
Ethereum Smart Contracts
• Usually written in Solidity
• Statically typed high level language
• Compiled to Ethereum Virtu...
Greeter.sol
contract mortal {
address owner;
function mortal() { owner = msg.sender; }
function kill() { if (msg.sender ==...
Smart Contract Wrappers
• Compile
$ solc Greeter.sol --bin --abi --optimize
-o build/
• Generate wrappers
$ ./web3j-1.0.6/...
Greeter.java
public final class Greeter extends Contract {

private static final String BINARY = “6060604052604....";

......
Hello Blockchain World!
Web3j web3 = Web3j.build(new HttpService());
Credentials credentials =
WalletUtils.loadCredentials...
Smarter Contracts
Smarter Contracts
• Asset tokenisation
• Hold Ether
• EIP-20 smart contract token standard
• See web3j examples
web3j + Ethereum
• web3j simplifies working with Ethereum
• Plenty of documentation
• Smart contract integration tests
Further Information
• Project home http://web3j.io
• Mining http://docs.web3j.io/
transactions.html#obtaining-ether
• Usef...
Java and the blockchain - introducing web3j
Java and the blockchain - introducing web3j
Upcoming SlideShare
Loading in …5
×

Java and the blockchain - introducing web3j

37,558 views

Published on

An overview of what blockchain is, how the Ethereum blockchain works, and how you can deploy and interact with smart contracts on Ethereum, all from within the JVM, thanks to web3j.

Published in: Technology
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • great presentation!! thanks for the awesome work you have been doing!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Java and the blockchain - introducing web3j

  1. 1. Java and the Blockchain Introducing web3j @conors10
  2. 2. Decentralised
  3. 3. Immutable data structure
  4. 4. Blockchain Technologies 2008 2013 2014 2015+
  5. 5. Ethereum • The world computer • Turing-complete virtual machine • Public blockchain (mainnet & testnet)
  6. 6. Ether • The fuel of the Ethereum blockchain • Pay miners to process transactions • Market capitalisation ~$1bn USD (Bitcoin ~$10bn) • Associated with an address + wallet file 0x19e03255f667bdfd50a32722df860b1eeaf4d635
  7. 7. Obtaining Ether • Buy it • Find someone • Kraken • BTC Markets • Mine it • mainnet => requires dedicated GPUs • testnet => quick using your CPU • Refer to Geth/Parity mining docs
  8. 8. Smart Contracts • Computerised contract • Code + data that lives on the blockchain at an address • Transactions call functions => state transition
  9. 9. Transactions • Transfer Ether • Deploy a smart contract • Call a smart contract
  10. 10. Transactions
  11. 11. Integration with Ethereum
  12. 12. web3j • Complete Ethereum JSON-RPC implementation • Ethereum wallet support • Smart contract wrappers • Command line tools • Android compatible (v1.0.5+)
  13. 13. web3j artefacts • Maven’s Nexus & Bintray's JFrog repositories • Java 8: org.web3j:core • Android: org.web3j:core-android • web3j releases page: • Command line tools: web3j-<version>.zip
  14. 14. web3j transactions
  15. 15. Getting started with Ethereum Free cloud clients @ https://infura.io/ Run a local client (to generate Ether): $ geth --rpcapi personal,db,eth,net,web3 --rpc --testnet $ parity --chain testnet
  16. 16. Create a wallet $ ./web3j-1.0.6/bin/web3j wallet create _ _____ _ _ | | |____ (_) (_) __ _____| |__ / /_ _ ___ / / / _ '_ | | | / _ V V / __/ |_) |.___/ / | _ | || (_) | _/_/ ___|_.__/ ____/| |(_)|_| ___/ _/ | |__/ Please enter a wallet file password: Please re-enter the password: Please enter a destination directory location [/Users/Conor/ Library/Ethereum/testnet/keystore]: ~/testnet-keystore Wallet file UTC--2016-11-10T22-52-35.722000000Z-- a929d0fe936c719c4e4d1194ae64e415c7e9e8fe.json successfully created in: /Users/Conor/testnet-keystore
  17. 17. Wallet file{ "address":"a929d0fe936c719c4e4d1194ae64e415c7e9e8fe", "id":"c2fbffdd-f588-43a8-9b0c-facb6fd84dfe", "version":3, "crypto":{ "cipher":"aes-128-ctr", "ciphertext":"27be0c93939fc8262977c4454a6b7c261c931dfd8c030b2d3e60ef76f99bfdc6", "cipherparams":{ "iv":"5aa4fdc64eef6bd82621c6036a323c41" }, "kdf":"scrypt", "kdfparams":{ "dklen":32, "n":262144, "p":1, "r":8, "salt":"6ebc76f30ee21c9a05f907a1ad1df7cca06dd594cf6c537c5e6c79fa88c9b9d1" }, "mac":"178eace46da9acbf259e94141fbcb7d3d43041e2ec546cd4fe24958e55a49446" } }
  18. 18. View transactions
  19. 19. Using web3j • Create client Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/ • Call method web3.<method name>([param1, …, paramN). [send()|sendAsync()]
  20. 20. Display client version Web3j web3 = Web3j.build(new HttpService()); Web3ClientVersion clientVersion = web3.web3ClientVersion() .sendAsync().get(); System.out.println(“Client version: “ + clientVersion.getWeb3ClientVersion()); Client version: Geth/v1.4.18-stable-c72f5459/ darwin/go1.7.3
  21. 21. Sending Ether Web3j web3 = Web3j.build(new HttpService()); Credentials credentials = WalletUtils.loadCredentials( "password", "/path/to/walletfile"); TransactionReceipt transactionReceipt = Transfer.sendFundsAsync( web3, credentials, “0x<to address>", BigDecimal.valueOf(0.2), Convert.Unit.ETHER).get(); System.out.println(“Funds transfer completed…” + …); Funds transfer completed, transaction hash: 0x16e41aa9d97d1c3374a4cb9599febdb24d4d5648b607c99e01a8 e79e3eab2c34, block number: 1840479
  22. 22. Block #1840479
  23. 23. Ethereum Smart Contracts • Usually written in Solidity • Statically typed high level language • Compiled to Ethereum Virtual Machine (EVM) byte code • Create Java wrappers with web3j
  24. 24. Greeter.sol contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) suicide(owner); } } contract greeter is mortal { string greeting; // constructor function greeter(string _greeting) public { greeting = _greeting; } // getter function greet() constant returns (string) { return greeting; } }
  25. 25. Smart Contract Wrappers • Compile $ solc Greeter.sol --bin --abi --optimize -o build/ • Generate wrappers $ ./web3j-1.0.6/bin/web3j solidity generate build/greeter.bin build/ greeter.abi -p org.web3j.example.generated -o src/main/ java/
  26. 26. Greeter.java public final class Greeter extends Contract {
 private static final String BINARY = “6060604052604....";
 ...
 
 public Future<Utf8String> greet() {
 Function function = new Function<Utf8String>("greet", 
 Arrays.<Type>asList(), 
 Arrays.<TypeReference<Utf8String>>asList(new TypeReference<Utf8String>() {}));
 return executeCallSingleValueReturnAsync(function);
 }
 
 public static Future<Greeter> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialValue, Utf8String _greeting) {
 String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(_greeting));
 return deployAsync(Greeter.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialValue);
 } ...
  27. 27. Hello Blockchain World! Web3j web3 = Web3j.build(new HttpService()); Credentials credentials = WalletUtils.loadCredentials( "my password", "/path/to/walletfile"); Greeter contract = Greeter.deploy( web3, credentials, BigInteger.ZERO, new Utf8String("Hello blockchain world!")) .get(); Utf8String greeting = contract.greet().get(); System.out.println(greeting.getTypeAsString()); Hello blockchain world!
  28. 28. Smarter Contracts
  29. 29. Smarter Contracts • Asset tokenisation • Hold Ether • EIP-20 smart contract token standard • See web3j examples
  30. 30. web3j + Ethereum • web3j simplifies working with Ethereum • Plenty of documentation • Smart contract integration tests
  31. 31. Further Information • Project home http://web3j.io • Mining http://docs.web3j.io/ transactions.html#obtaining-ether • Useful resources http://docs.web3j.io/links.html • Chat https://gitter.im/web3j/web3j • Blog http://conorsvensson.com/

×