Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

11.9. 账号管理

11.9.1. 获得账号列表

			
	public List<String> getAccountlist() {

		try {
			return web3j.ethAccounts().send().getAccounts();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}				
			
			

11.9.2. 获得账号信息

			
	public String getAccount(int index) {
		String account = null;

		try {
			account = web3j.ethAccounts().send().getAccounts().get(index);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return account;
	}				
			

			

11.9.3. 创建账号

			
package cn.netkiller.example.ethereum.account;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.admin.methods.response.PersonalListAccounts;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
//import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.ipc.UnixIpcService;

public class AccountTest {
	private static Admin admin;

	public AccountTest() {
		// TODO Auto-generated constructor stub
		// admin = Admin.build(new HttpService("http://127.0.0.1:8545"));
		admin = Admin.build(new UnixIpcService("/Users/neo/Library/Ethereum/geth.ipc"));

	}

	private void createAccount() throws IOException {
		String password = "12345678";
		NewAccountIdentifier newAccountIdentifier = admin.personalNewAccount(password).send();
		String address = newAccountIdentifier.getAccountId();
		System.out.println("New account address: " + address);
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		AccountTest account = new AccountTest();
		account.createAccount();
	}

}
			
			

11.9.4. 解锁账号

			
Admin web3j = Admin.build(new HttpService());  // defaults to http://localhost:8545/
PersonalUnlockAccount personalUnlockAccount = web3j.personalUnlockAccount("0x000...", "a password").sendAsync().get();
if (personalUnlockAccount.accountUnlocked()) {
    // send a transaction
}				
			
			
			
package cn.netkiller.example.ethereum.account;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.admin.methods.response.PersonalListAccounts;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
//import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.ipc.UnixIpcService;

public class AccountTest {
	private static Admin admin;

	public AccountTest() {
		// TODO Auto-generated constructor stub
		// admin = Admin.build(new HttpService("http://127.0.0.1:8545"));
		admin = Admin.build(new UnixIpcService("/Users/neo/Library/Ethereum/geth.ipc"));

	}

	private void unlockAccount() {
		String address = "0xf56b81a2bcb964D2806071e9Be4289A5559BB0fA";
		String password = "12345678";
		// 账号解锁持续时间 单位秒 缺省值300秒
		BigInteger unlockDuration = BigInteger.valueOf(60L);
		try {
			PersonalUnlockAccount personalUnlockAccount = admin.personalUnlockAccount(address, password, unlockDuration).send();
			Boolean isUnlocked = personalUnlockAccount.accountUnlocked();
			System.out.println("Account unlock " + isUnlocked);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		AccountTest account = new AccountTest();
		account.unlockAccount();
	}

}