web3j管理APIs(Management APIs)

web3j除了实现最基本的标准JSON-RPC API,以太坊客户端,如GethParity,通过JSON-RPC提供额外的管理API。

它们提供的一个关键的通用功能是创建和解锁在网络上交易的以太坊帐户的能力。在GethParity中,这是在它们的私有模块中实现的,其中的细节可在下面得到:

在web3j中对这些私有模块的提供支持。这些方法对于GethParity都是通用的,它存在于web3j的管理模块Admin中。

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
package org.web3j.protocol.admin;

import java.math.BigInteger;
import java.util.concurrent.ScheduledExecutorService;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;
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.core.Request;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthSendTransaction;

/**
* JSON-RPC Request object building factory for common Parity and Geth.
*/
public interface Admin extends Web3j {

static Admin build(Web3jService web3jService) {
return new JsonRpc2_0Admin(web3jService);
}

static Admin build(
Web3jService web3jService, long pollingInterval,
ScheduledExecutorService scheduledExecutorService) {
return new JsonRpc2_0Admin(web3jService, pollingInterval, scheduledExecutorService);
}

public Request<?, PersonalListAccounts> personalListAccounts();

public Request<?, NewAccountIdentifier> personalNewAccount(String password);

public Request<?, PersonalUnlockAccount> personalUnlockAccount(
String address, String passphrase, BigInteger duration);

public Request<?, PersonalUnlockAccount> personalUnlockAccount(
String address, String passphrase);

public Request<?, EthSendTransaction> personalSendTransaction(
Transaction transaction, String password);

}

可以初始化一个新的web3j连接器,使用Admin模块的工厂方法:

1
2
3
4
5
Admin web3j = Admin.build(new HttpService());  // defaults to http://localhost:8545/
PersonalUnlockAccount personalUnlockAccount = admin.personalUnlockAccount("0x000...", "a password").send();
if (personalUnlockAccount.accountUnlocked()) {
// send a transaction
}

对于geth特定方法,可以使用geth连接器:

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
package org.web3j.protocol.geth;

import rx.Observable;

import org.web3j.protocol.Web3jService;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.BooleanResponse;
import org.web3j.protocol.admin.methods.response.PersonalSign;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.MinerStartResponse;
import org.web3j.protocol.geth.response.PersonalEcRecover;
import org.web3j.protocol.geth.response.PersonalImportRawKey;
import org.web3j.protocol.websocket.events.PendingTransactionNotification;
import org.web3j.protocol.websocket.events.SyncingNotfication;

/**
* JSON-RPC Request object building factory for Geth.
*/
public interface Geth extends Admin {
static Geth build(Web3jService web3jService) {
return new JsonRpc2_0Geth(web3jService);
}

Request<?, PersonalImportRawKey> personalImportRawKey(String keydata, String password);

Request<?, BooleanResponse> personalLockAccount(String accountId);

Request<?, PersonalSign> personalSign(String message, String accountId, String password);

Request<?, PersonalEcRecover> personalEcRecover(String message, String signiture);

Request<?, MinerStartResponse> minerStart(int threadCount);

Request<?, BooleanResponse> minerStop();

/**
* Creates an observable that emits a notification when a new transaction is added
* to the pending state and is signed with a key that is available in the node.
*
* @return Observable that emits a notification when a new transaction is added
* to the pending state
*/
Observable<PendingTransactionNotification> newPendingTransactionsNotifications();

/**
* Creates an observable that emits a notification when a node starts or stops syncing.
* @return Observalbe that emits changes to syncing status
*/
Observable<SyncingNotfication> syncingStatusNotifications();

}

而对于Parity,则可以使用相关的Parity 连接器。Parity连接器还提供对Parity的Trace模块的支持。这些连接器分别在web3j的gethparity模块中可用。

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.web3j.protocol.parity;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.web3j.crypto.WalletFile;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.BooleanResponse;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.admin.methods.response.PersonalSign;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.parity.methods.request.Derivation;
import org.web3j.protocol.parity.methods.response.ParityAddressesResponse;
import org.web3j.protocol.parity.methods.response.ParityAllAccountsInfo;
import org.web3j.protocol.parity.methods.response.ParityDefaultAddressResponse;
import org.web3j.protocol.parity.methods.response.ParityDeriveAddress;
import org.web3j.protocol.parity.methods.response.ParityExportAccount;
import org.web3j.protocol.parity.methods.response.ParityListRecentDapps;

/**
* JSON-RPC Request object building factory for Parity.
*/
public interface Parity extends Admin, Trace {
static Parity build(Web3jService web3jService) {
return new JsonRpc2_0Parity(web3jService);
}

Request<?, ParityAllAccountsInfo> parityAllAccountsInfo();

Request<?, BooleanResponse> parityChangePassword(
String accountId, String oldPassword, String newPassword);

Request<?, ParityDeriveAddress> parityDeriveAddressHash(
String accountId, String password, Derivation hashType, boolean toSave);

Request<?, ParityDeriveAddress> parityDeriveAddressIndex(
String accountId, String password, List<Derivation> indicesType, boolean toSave);

Request<?, ParityExportAccount> parityExportAccount(String accountId, String password);

Request<?, ParityAddressesResponse> parityGetDappAddresses(String dAppId);

Request<?, ParityDefaultAddressResponse> parityGetDappDefaultAddress(String dAppId);

Request<?, ParityAddressesResponse> parityGetNewDappsAddresses();

Request<?, ParityDefaultAddressResponse> parityGetNewDappsDefaultAddress();

Request<?, ParityAddressesResponse> parityImportGethAccounts(ArrayList<String> gethAddresses);

Request<?, BooleanResponse> parityKillAccount(String accountId, String password);

Request<?, ParityAddressesResponse> parityListAccounts(
BigInteger quantity, String accountId, DefaultBlockParameter blockParameter);

Request<?, ParityAddressesResponse> parityListGethAccounts();

Request<?, ParityListRecentDapps> parityListRecentDapps();

Request<?, NewAccountIdentifier> parityNewAccountFromPhrase(String phrase, String password);

Request<?, NewAccountIdentifier> parityNewAccountFromSecret(String secret, String password);

Request<?, NewAccountIdentifier> parityNewAccountFromWallet(
WalletFile walletFile, String password);

Request<?, BooleanResponse> parityRemoveAddress(String accountId);

Request<?, BooleanResponse> paritySetAccountMeta(
String accountId, Map<String, Object> metadata);

Request<?, BooleanResponse> paritySetAccountName(String address, String name);

Request<?, BooleanResponse> paritySetDappAddresses(
String dAppId, ArrayList<String> availableAccountIds);

Request<?, BooleanResponse> paritySetDappDefaultAddress(String dAppId, String defaultAddress);

Request<?, BooleanResponse> paritySetNewDappsAddresses(ArrayList<String> availableAccountIds);

Request<?, BooleanResponse> paritySetNewDappsDefaultAddress(String defaultAddress);

Request<?, BooleanResponse> parityTestPassword(String accountId, String password);

Request<?, PersonalSign> paritySignMessage(
String accountId, String password, String hexMessage);
}

你可以参考集成测试ParityIT,了解使用这些API的例子。

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
package org.web3j.protocol.parity;

import org.junit.Before;
import org.junit.Test;

import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.admin.methods.response.PersonalListAccounts;
import org.web3j.protocol.admin.methods.response.PersonalSign;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
import org.web3j.protocol.http.HttpService;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* JSON-RPC 2.0 Integration Tests.
*/
public class ParityIT {

private static String PASSWORD = "1n5ecur3P@55w0rd";
private Parity parity;

@Before
public void setUp() {
this.parity = Parity.build(new HttpService());
}

@Test
public void testPersonalListAccounts() throws Exception {
PersonalListAccounts personalListAccounts = parity.personalListAccounts().send();
assertNotNull(personalListAccounts.getAccountIds());
}

@Test
public void testPersonalNewAccount() throws Exception {
NewAccountIdentifier newAccountIdentifier = createAccount();
assertFalse(newAccountIdentifier.getAccountId().isEmpty());
}

@Test
public void testPersonalUnlockAccount() throws Exception {
NewAccountIdentifier newAccountIdentifier = createAccount();
PersonalUnlockAccount personalUnlockAccount = parity.personalUnlockAccount(
newAccountIdentifier.getAccountId(), PASSWORD).send();
assertTrue(personalUnlockAccount.accountUnlocked());
}

@Test
public void testPersonalSign() throws Exception {
PersonalListAccounts personalListAccounts = parity.personalListAccounts().send();
assertNotNull(personalListAccounts.getAccountIds());

PersonalSign personalSign = parity.paritySignMessage("0xdeadbeaf",
personalListAccounts.getAccountIds().get(0), "123").send();
// address : 0xadfc0262bbed8c1f4bd24a4a763ac616803a8c54
assertNotNull(personalSign.getSignedMessage());
// result : 0x80ab45a65bd5acce92eac60b52235a34eee647c8dbef8e62108be90a4ac9a22222f87dd8934f
// c71545cf2ea1b71d8b62146e6d741ac6ee12fd1d1d740adca9021b
}

private NewAccountIdentifier createAccount() throws Exception {
return parity.personalNewAccount(PASSWORD).send();
}
}
  • web3j教程,主要是针对java和android程序员进行区块链以太坊开发的web3j开发详解。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。

汇智网原创翻译,转载请标明出处。这里是原文