-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathAccountManager.java
More file actions
77 lines (68 loc) · 2.15 KB
/
AccountManager.java
File metadata and controls
77 lines (68 loc) · 2.15 KB
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
70
71
72
73
74
75
76
77
package com.ethjava;
import com.ethjava.utils.Environment;
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 java.io.IOException;
import java.math.BigInteger;
import java.util.List;
/**
* 账号管理相关
*/
public class AccountManager {
private static Admin admin;
public static void main(String[] args) {
admin = Admin.build(new HttpService(Environment.RPC_URL));
createNewAccount();
getAccountList();
unlockAccount();
// admin.personalSendTransaction(); 该方法与web3j.sendTransaction相同 不在此写例子。
}
/**
* 创建账号
*/
private static void createNewAccount() {
String password = "123456789";
try {
NewAccountIdentifier newAccountIdentifier = admin.personalNewAccount(password).send();
String address = newAccountIdentifier.getAccountId();
System.out.println("new account address " + address);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取账号列表
*/
private static void getAccountList() {
try {
PersonalListAccounts personalListAccounts = admin.personalListAccounts().send();
List<String> addressList;
addressList = personalListAccounts.getAccountIds();
System.out.println("account size " + addressList.size());
for (String address : addressList) {
System.out.println(address);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 账号解锁
*/
private static void unlockAccount() {
String address = "0x05f50cd5a97d9b3fec35df3d0c6c8234e6793bdf";
String password = "123456789";
//账号解锁持续时间 单位秒 缺省值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();
}
}
}