返回介绍

11.9. 账号管理

发布于 2023-06-19 14:14:32 字数 3620 浏览 0 评论 0 收藏 0

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();
	}

}
			
			

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文