SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
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 (Bitcoin ~$10bn)
• Associated with an address + wallet file
0x19e03255f667bdfd50a32722df860b1eeaf4d635
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
Smart Contracts
• Computerised contract
• Code + data that lives on the blockchain at an
address
• Transactions call functions => state transition
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
• Android compatible (v1.0.5+)
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
web3j transactions
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
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
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"
}
}
View transactions
Using web3j
• Create client
Web3j web3 = Web3j.build(new HttpService());
// defaults to http://localhost:8545/
• Call method
web3.<method name>([param1, …, paramN).
[send()|sendAsync()]
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
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
Block #1840479
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
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;
}
}
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/
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);

}
...
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!
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
• Useful resources http://docs.web3j.io/links.html
• Chat https://gitter.im/web3j/web3j
• Blog http://conorsvensson.com/

Contenu connexe

Tendances

Tendances (20)

Ethereum - MetaMask&Remix&Smartcontract
Ethereum - MetaMask&Remix&SmartcontractEthereum - MetaMask&Remix&Smartcontract
Ethereum - MetaMask&Remix&Smartcontract
 
Meteor and Bitcoin (Lightning Talk)
Meteor and Bitcoin (Lightning Talk)Meteor and Bitcoin (Lightning Talk)
Meteor and Bitcoin (Lightning Talk)
 
20180711 Metamask
20180711 Metamask 20180711 Metamask
20180711 Metamask
 
Libbitcoin slides
Libbitcoin slidesLibbitcoin slides
Libbitcoin slides
 
20180714 workshop - Ethereum decentralized application with truffle framework
20180714 workshop - Ethereum decentralized application with truffle framework20180714 workshop - Ethereum decentralized application with truffle framework
20180714 workshop - Ethereum decentralized application with truffle framework
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
 
Ingredients for creating dapps
Ingredients for creating dappsIngredients for creating dapps
Ingredients for creating dapps
 
WebSockets Jump Start
WebSockets Jump StartWebSockets Jump Start
WebSockets Jump Start
 
Building interactivity with websockets
Building interactivity with websocketsBuilding interactivity with websockets
Building interactivity with websockets
 
Blockchain for Developers
Blockchain for DevelopersBlockchain for Developers
Blockchain for Developers
 
Blockchain for creative content - What we do in LikeCoin
Blockchain for creative content - What we do in LikeCoinBlockchain for creative content - What we do in LikeCoin
Blockchain for creative content - What we do in LikeCoin
 
gething started - ethereum & using the geth golang client
gething started - ethereum & using the geth golang clientgething started - ethereum & using the geth golang client
gething started - ethereum & using the geth golang client
 
Web sockets Introduction
Web sockets IntroductionWeb sockets Introduction
Web sockets Introduction
 
Realtime web experience with signal r
Realtime web experience with signal rRealtime web experience with signal r
Realtime web experience with signal r
 
Write Smart Contracts with Truffle Framework
Write Smart Contracts with Truffle FrameworkWrite Smart Contracts with Truffle Framework
Write Smart Contracts with Truffle Framework
 
CBGTBT - Part 3 - Transactions 101
CBGTBT - Part 3 - Transactions 101CBGTBT - Part 3 - Transactions 101
CBGTBT - Part 3 - Transactions 101
 
Cryptography In Silverlight
Cryptography In SilverlightCryptography In Silverlight
Cryptography In Silverlight
 
Build dapps 1:3 dev tools
Build dapps 1:3 dev toolsBuild dapps 1:3 dev tools
Build dapps 1:3 dev tools
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Ethereum bxl
Ethereum bxlEthereum bxl
Ethereum bxl
 

Similaire à Java and the blockchain - introducing web3j

Ethereum Web3.js - Some tips for the developer
Ethereum Web3.js - Some  tips  for  the developer Ethereum Web3.js - Some  tips  for  the developer
Ethereum Web3.js - Some tips for the developer
炫成 林
 
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Srikanth Reddy Pallerla
 
Windows Phone 8 - 12 Network Communication
Windows Phone 8 - 12 Network CommunicationWindows Phone 8 - 12 Network Communication
Windows Phone 8 - 12 Network Communication
Oliver Scheer
 

Similaire à Java and the blockchain - introducing web3j (20)

Token platform based on sidechain
Token platform based on sidechainToken platform based on sidechain
Token platform based on sidechain
 
Windows 8 Apps and the Outside World
Windows 8 Apps and the Outside WorldWindows 8 Apps and the Outside World
Windows 8 Apps and the Outside World
 
Jugando con websockets en nodeJS
Jugando con websockets en nodeJSJugando con websockets en nodeJS
Jugando con websockets en nodeJS
 
Streaming Data with scalaz-stream
Streaming Data with scalaz-streamStreaming Data with scalaz-stream
Streaming Data with scalaz-stream
 
Csphtp1 22
Csphtp1 22Csphtp1 22
Csphtp1 22
 
Ethereum Web3.js - Some tips for the developer
Ethereum Web3.js - Some  tips  for  the developer Ethereum Web3.js - Some  tips  for  the developer
Ethereum Web3.js - Some tips for the developer
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
 
Securing your Pulsar Cluster with Vault_Chris Kellogg
Securing your Pulsar Cluster with Vault_Chris KelloggSecuring your Pulsar Cluster with Vault_Chris Kellogg
Securing your Pulsar Cluster with Vault_Chris Kellogg
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
 
WebSockets in JEE 7
WebSockets in JEE 7WebSockets in JEE 7
WebSockets in JEE 7
 
Web Real-time Communications
Web Real-time CommunicationsWeb Real-time Communications
Web Real-time Communications
 
signalr
signalrsignalr
signalr
 
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
 
Flowchain: A case study on building a Blockchain for the IoT
Flowchain: A case study on building a Blockchain for the IoTFlowchain: A case study on building a Blockchain for the IoT
Flowchain: A case study on building a Blockchain for the IoT
 
Windows Phone 8 - 12 Network Communication
Windows Phone 8 - 12 Network CommunicationWindows Phone 8 - 12 Network Communication
Windows Phone 8 - 12 Network Communication
 
Blockchain
BlockchainBlockchain
Blockchain
 
.NET Conf 2022 - Networking in .NET 7
.NET Conf 2022 - Networking in .NET 7.NET Conf 2022 - Networking in .NET 7
.NET Conf 2022 - Networking in .NET 7
 
11.Open Data Protocol(ODATA)
11.Open Data Protocol(ODATA) 11.Open Data Protocol(ODATA)
11.Open Data Protocol(ODATA)
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
 
Go 1.8 'new' networking features
Go 1.8 'new' networking featuresGo 1.8 'new' networking features
Go 1.8 'new' networking features
 

Plus de Conor Svensson

Plus de Conor Svensson (6)

Blockchain - Navigating this Game-Changing Technology
Blockchain - Navigating this Game-Changing TechnologyBlockchain - Navigating this Game-Changing Technology
Blockchain - Navigating this Game-Changing Technology
 
Ether mining 101 v2
Ether mining 101 v2Ether mining 101 v2
Ether mining 101 v2
 
Cloud Native Microservices with Spring Cloud
Cloud Native Microservices with Spring CloudCloud Native Microservices with Spring Cloud
Cloud Native Microservices with Spring Cloud
 
Ether Mining 101
Ether Mining 101Ether Mining 101
Ether Mining 101
 
Cloud Native Microservices with Spring Cloud
Cloud Native Microservices with Spring CloudCloud Native Microservices with Spring Cloud
Cloud Native Microservices with Spring Cloud
 
Java Microservices with Netflix OSS & Spring
Java Microservices with Netflix OSS & Spring Java Microservices with Netflix OSS & Spring
Java Microservices with Netflix OSS & Spring
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Java and the blockchain - introducing web3j