OSDN Git Service

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