-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAccount.java
More file actions
336 lines (286 loc) · 10.3 KB
/
Account.java
File metadata and controls
336 lines (286 loc) · 10.3 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package io.bytom.api;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import io.bytom.common.ParameterizedTypeImpl;
import io.bytom.common.Utils;
import io.bytom.exception.*;
import io.bytom.http.Client;
import org.apache.log4j.Logger;
import java.lang.reflect.Type;
import java.util.*;
/**
* <h1>Account Class</h1>
*/
public class Account {
@SerializedName("id")
public String id;
@SerializedName("alias")
public String alias;
@SerializedName("key_index")
public Integer key_index;
@SerializedName("quorum")
public Integer quorum;
@SerializedName("xpubs")
public List<String> xpubs;
private static Logger logger = Logger.getLogger(Account.class);
public String toJson() {
return Utils.serializer.toJson(this);
}
/**
* create-account
*
* @param client client object that makes requests to the core
* @param builder Account.Builder to make parameters
* @return Account return a account object
* @throws BytomException BytomException
*/
public static Account create(Client client, Builder builder) throws BytomException {
Account account = client.request("create-account", builder, Account.class);
logger.info("create-account");
logger.info(account.toString());
return account;
}
/**
* list-accounts
*
* @param client client object that makes requests to the core
* @return return a list of account object
* @throws BytomException BytomException
*/
public static List<Account> list(Client client) throws BytomException {
Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Account.class});
List<Account> accountList = client.request("list-accounts", null, listType);
logger.info("list-accounts:");
logger.info("size of accountList:"+accountList.size());
logger.info(accountList);
return accountList;
}
/**
* delete-account
* @param client client object that makes requests to the core
* @param account_info account_info
* @throws BytomException BytomException
*/
public static void delete(Client client, String account_info) throws BytomException {
Map<String, String> req = new HashMap<>();
req.put("account_info", account_info);
client.request("delete-account", req);
}
public static class Builder {
public List<String> root_xpubs;
public String alias;
public Integer quorum;
/**
* add a xpub to root_xpubs
*
* @param xpub xpub
* @return this Builder object
*/
public Builder addRootXpub(String xpub) {
this.root_xpubs.add(xpub);
return this;
}
/**
* set xpubs to root_xpubs
*
* @param xpubs xpubs
* @return this Builder object
*/
public Builder setRootXpub(List<String> xpubs) {
this.root_xpubs = new ArrayList<>(xpubs);
return this;
}
/**
* set alias to alias
* @param alias alias
* @return this Builder object
*/
public Builder setAlias(String alias) {
this.alias = alias;
return this;
}
/**
* set quorum to quorum
*
* @param quorum quorum
* @return this Builder object
*/
public Builder setQuorum(Integer quorum) {
this.quorum = quorum;
return this;
}
}
/**
* Use this class to create a {@link Receiver} under an account.
*/
public static class ReceiverBuilder {
@SerializedName("account_alias")
public String accountAlias;
@SerializedName("account_id")
public String accountId;
/**
* Specifies the account under which the receiver is created. You must use
* this method or @{link ReceiverBuilder#setAccountId}, but not both.
*
* @param alias the unique alias of the account
* @return this ReceiverBuilder object
*/
public ReceiverBuilder setAccountAlias(String alias) {
this.accountAlias = alias;
return this;
}
/**
* Specifies the account under which the receiver is created. You must use
* this method or @{link ReceiverBuilder#setAccountAlias}, but not both.
*
* @param id the unique ID of the account
* @return this ReceiverBuilder object
*/
public ReceiverBuilder setAccountId(String id) {
this.accountId = id;
return this;
}
/**
* Creates a single Receiver object under an account.
*
* @param client the client object providing access to an instance of Chain Core
* @return a new Receiver object
* @throws APIException This exception is raised if the api returns errors while creating the control programs.
* @throws BadURLException This exception wraps java.net.MalformedURLException.
* @throws ConnectivityException This exception is raised if there are connectivity issues with the server.
* @throws HTTPException This exception is raised when errors occur making http requests.
* @throws JSONException This exception is raised due to malformed json requests or responses.
*/
public Receiver create(Client client) throws BytomException {
Gson gson = new Gson();
Receiver receiver = client.request(
"create-account-receiver", this, Receiver.class);
logger.info("create-account-receiver:");
logger.info(receiver.toJson());
return receiver;
}
@Override
public String toString() {
return "ReceiverBuilder{" +
"accountAlias='" + accountAlias + '\'' +
", accountId='" + accountId + '\'' +
'}';
}
}
/**
* Address Class
*/
public static class Address {
@SerializedName("account_alias")
public String accountAlias;
@SerializedName("account_id")
public String accountId;
@SerializedName("address")
public String address;
@SerializedName("change")
public Boolean change;
@SerializedName("valid")
public Boolean valid;
@SerializedName("is_local")
public Boolean is_local;
/**
* Serializes the Address into a form that is safe to transfer over the wire.
*
* @return the JSON-serialized representation of the Receiver object
*/
public String toJson() {
return Utils.serializer.toJson(this);
}
/**
* Deserializes a Address from JSON.
*
* @param json a JSON-serialized Receiver object
* @return the deserialized Receiver object
* @throws JSONException Raised if the provided string is not valid JSON.
*/
public static Address fromJson(String json) throws JSONException {
try {
return Utils.serializer.fromJson(json, Address.class);
} catch (IllegalStateException e) {
throw new JSONException("Unable to parse serialized receiver: " + e.getMessage());
}
}
}
/**
* Use this class to create a {@link Address} under an account.
*/
public static class AddressBuilder {
@SerializedName("account_alias")
public String accountAlias;
@SerializedName("account_id")
public String accountId;
/**
* Specifies the account under which the address is created. You must use
* this method or @{link AddressBuilder#setAccountId}, but not both.
*
* @param alias the unique alias of the account
* @return this AddressBuilder object
*/
public AddressBuilder setAccountAlias(String alias) {
this.accountAlias = alias;
return this;
}
/**
* Specifies the account under which the address is created. You must use
* this method or @{link AddressBuilder#setAccountAlias}, but not both.
*
* @param id the unique ID of the account
* @return this AddressBuilder object
*/
public AddressBuilder setAccountId(String id) {
this.accountId = id;
return this;
}
/**
* list-addresses
* @param client client object that makes requests to the core
* @return list of address object
* @throws BytomException BytomException
*/
public List<Address> list(Client client) throws BytomException {
Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Address.class});
List<Address> addressList = client.request("list-addresses", this, listType);
logger.info("list-addresses:");
logger.info("size of addressList:" + addressList.size());
logger.info(addressList.get(0).toJson());
return addressList;
}
/**
* validate-address
* @param client client object that makes requests to the core
* @param address an address string
* @return an address object
* @throws BytomException BytomException
*/
public Address validate(Client client, String address) throws BytomException {
Map<String, Object> req = new HashMap<>();
req.put("address", address);
Address addressResult = client.request("validate-address", req, Address.class);
logger.info("validate-address:");
logger.info(addressResult.toJson());
return addressResult;
}
@Override
public String toString() {
return "AddressBuilder{" +
"accountAlias='" + accountAlias + '\'' +
", accountId='" + accountId + '\'' +
'}';
}
}
@Override
public String toString() {
return "Account{" +
"id='" + id + '\'' +
", alias='" + alias + '\'' +
", key_index=" + key_index +
", quorum=" + quorum +
", xpubs=" + xpubs +
'}';
}
}