OSDN Git Service

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