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;
11 import java.lang.reflect.Type;
15 * <h1>Account Class</h1>
17 public class Account {
22 @SerializedName("alias")
25 @SerializedName("key_index")
26 public Integer key_index;
28 @SerializedName("quorum")
29 public Integer quorum;
31 @SerializedName("xpubs")
32 public List<String> xpubs;
34 private static Logger logger = Logger.getLogger(Account.class);
36 public String toJson() {
37 return Utils.serializer.toJson(this);
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
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());
58 * @param client client object that makes requests to the core
59 * @return return a list of account object
60 * @throws BytomException BytomException
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);
73 * @param client client object that makes requests to the core
74 * @param account_info account_info
75 * @throws BytomException BytomException
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);
83 public static class Builder {
85 public List<String> root_xpubs;
89 public Integer quorum;
92 * add a xpub to root_xpubs
95 * @return this Builder object
97 public Builder addRootXpub(String xpub) {
98 this.root_xpubs.add(xpub);
103 * set xpubs to root_xpubs
106 * @return this Builder object
108 public Builder setRootXpub(List<String> xpubs) {
109 this.root_xpubs = new ArrayList<>(xpubs);
116 * @return this Builder object
118 public Builder setAlias(String alias) {
124 * set quorum to quorum
126 * @param quorum quorum
127 * @return this Builder object
129 public Builder setQuorum(Integer quorum) {
130 this.quorum = quorum;
137 * Use this class to create a {@link Receiver} under an account.
139 public static class ReceiverBuilder {
141 @SerializedName("account_alias")
142 public String accountAlias;
144 @SerializedName("account_id")
145 public String accountId;
148 * Specifies the account under which the receiver is created. You must use
149 * this method or @{link ReceiverBuilder#setAccountId}, but not both.
151 * @param alias the unique alias of the account
152 * @return this ReceiverBuilder object
154 public ReceiverBuilder setAccountAlias(String alias) {
155 this.accountAlias = alias;
160 * Specifies the account under which the receiver is created. You must use
161 * this method or @{link ReceiverBuilder#setAccountAlias}, but not both.
163 * @param id the unique ID of the account
164 * @return this ReceiverBuilder object
166 public ReceiverBuilder setAccountId(String id) {
172 * Creates a single Receiver object under an account.
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.
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());
193 public String toString() {
194 return "ReceiverBuilder{" +
195 "accountAlias='" + accountAlias + '\'' +
196 ", accountId='" + accountId + '\'' +
204 public static class Address {
205 @SerializedName("account_alias")
206 public String accountAlias;
208 @SerializedName("account_id")
209 public String accountId;
211 @SerializedName("address")
212 public String address;
214 @SerializedName("change")
215 public Boolean change;
217 @SerializedName("vaild")
218 public Boolean vaild;
220 @SerializedName("is_local")
221 public Boolean is_local;
224 * Serializes the Address into a form that is safe to transfer over the wire.
226 * @return the JSON-serialized representation of the Receiver object
228 public String toJson() {
229 return Utils.serializer.toJson(this);
233 * Deserializes a Address from JSON.
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.
239 public static Address fromJson(String json) throws JSONException {
241 return Utils.serializer.fromJson(json, Address.class);
242 } catch (IllegalStateException e) {
243 throw new JSONException("Unable to parse serialized receiver: " + e.getMessage());
250 * Use this class to create a {@link Address} under an account.
252 public static class AddressBuilder {
254 @SerializedName("account_alias")
255 public String accountAlias;
257 @SerializedName("account_id")
258 public String accountId;
261 * Specifies the account under which the address is created. You must use
262 * this method or @{link AddressBuilder#setAccountId}, but not both.
264 * @param alias the unique alias of the account
265 * @return this AddressBuilder object
267 public AddressBuilder setAccountAlias(String alias) {
268 this.accountAlias = alias;
273 * Specifies the account under which the address is created. You must use
274 * this method or @{link AddressBuilder#setAccountAlias}, but not both.
276 * @param id the unique ID of the account
277 * @return this AddressBuilder object
279 public AddressBuilder setAccountId(String id) {
286 * @param client client object that makes requests to the core
287 * @return list of address object
288 * @throws BytomException BytomException
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());
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
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());
314 return addressResult;
318 public String toString() {
319 return "AddressBuilder{" +
320 "accountAlias='" + accountAlias + '\'' +
321 ", accountId='" + accountId + '\'' +
327 public String toString() {
330 ", alias='" + alias + '\'' +
331 ", key_index=" + key_index +
332 ", quorum=" + quorum +