使用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; } } 12345678910111213141516171819 12345678910111213141516171819使用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; } } 1234567891011121314 12345678910111213141.创建账户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; } } 12345678910111213141516171819202122232425262728293031323334353637383940414243 123456789101112131415161718192021222324252627282930313233343536373839404142432.账户相关操作
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; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869