準備工作 已經安裝好以太坊用戶端或是以太坊叢集 安裝jdk8開發環境,web3j需要在java 8的環境才能運行
引入jar包
<dependency> <groupId>org.web3j</groupId> <artifactId>core</artifactId> <version>2.2.1</version></dependency>
1 2 3 4 5 1 2 3 4 5
串連以太坊用戶端
使用web3j的api串連
public class Web3JClient { private static String ip = "http://ip地址:8545/"; private Web3JClient(){} private volatile static Web3j web3j; public static Web3j getClient(){ if(web3j==null){ synchronized (Web3JClient.class){ if(web3j==null){ web3j = Web3j.build(new HttpService(ip)); } } } return web3j; }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
使用Parity的api串連
public class ParityClient { private static String ip = "http://ip地址:8545/"; private ParityClient(){} private static class ClientHolder{ private static final Parity parity = Parity.build(new HttpService(ip)); } public static final Parity getParity(){ return ClientHolder.parity; }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
賬戶操作
1.建立賬戶POJO
public class AccountInfo { private String userName; private String phone; private String address; private String school; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
2.賬戶相關操作
public class Account { private static Parity parity = ParityClient.getParity(); private static Web3j web3j = Web3JClient.getClient(); /** * Life * Like this * Like that * Also * It's not the same with you think * @Author lzh * */ public List<String> getAccountlist(){ try{ return parity.personalListAccounts().send().getAccountIds(); }catch (Exception e){ e.printStackTrace(); } return null; } public String createAccount(String accountName,String password,AccountInfo accountInfo){ try { NewAccountIdentifier newAccountIdentifier = parity.personalNewAccount(password).send(); if(newAccountIdentifier!=null){ String accountId = newAccountIdentifier.getAccountId(); parity.personalSetAccountName(accountId,accountName); Map<String,Object> account = new HashMap<String,Object>(); account.put(accountId,accountInfo); parity.personalSetAccountMeta(accountId,account); return accountId; } } catch (Exception e) { e.printStackTrace(); } return null; } public PersonalAccountsInfo.AccountsInfo getAccountInfo(String accountId){ try{ PersonalAccountsInfo personalAccountsInfo = parity.personalAccountsInfo().send(); return personalAccountsInfo.getAccountsInfo().get(accountId); }catch (Exception e){ e.printStackTrace(); } return null; } public BigInteger getBalance(String accountId){ try { DefaultBlockParameter defaultBlockParameter = new DefaultBlockParameterNumber(58); EthGetBalance ethGetBalance = parity.ethGetBalance(accountId,defaultBlockParameter).send(); if(ethGetBalance!=null){ return ethGetBalance.getBalance(); } }catch (Exception e){ e.printStackTrace(); } return null; }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
賬戶測試
public class AccountTest { public static void main(String args[]) { getBalance(); } public static void getBalance(){ Account account = new Account(); BigInteger ba = account.getBalance("0xcee1086eabd4cac10f6658eeffcdc66ad7565450"); System.out.print(ba); } public static void queryAccount(){ Account account = new Account(); List<String> accounts = account.getAccountlist(); for(String accountId:accounts){ System.out.println(accountId); } } public void createAccount(){ Account account =