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;
10 import java.lang.reflect.Type;
14 * <h1>Account Class</h1>
16 public class Account {
21 @SerializedName("alias")
24 @SerializedName("key_index")
25 public Integer key_index;
27 @SerializedName("quorum")
28 public Integer quorum;
30 @SerializedName("xpubs")
31 public List<String> xpubs;
33 private static Logger logger = Logger.getLogger(Account.class);
35 public String toJson() {
36 return Utils.serializer.toJson(this);
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
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());
57 * @param client client object that makes requests to the core
58 * @return return a list of account object
59 * @throws BytomException BytomException
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);
72 * @param client client object that makes requests to the core
73 * @param account_info account_info
74 * @throws BytomException BytomException
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);
82 public static class Builder {
84 public List<String> root_xpubs;
88 public Integer quorum;
91 * add a xpub to root_xpubs
94 * @return this Builder object
96 public Builder addRootXpub(String xpub) {
97 this.root_xpubs.add(xpub);
102 * set xpubs to root_xpubs
105 * @return this Builder object
107 public Builder setRootXpub(List<String> xpubs) {
108 this.root_xpubs = new ArrayList<>(xpubs);
115 * @return this Builder object
117 public Builder setAlias(String alias) {
123 * set quorum to quorum
125 * @param quorum quorum
126 * @return this Builder object
128 public Builder setQuorum(Integer quorum) {
129 this.quorum = quorum;
136 * Use this class to create a {@link Receiver} under an account.
138 public static class ReceiverBuilder {
140 @SerializedName("account_alias")
141 public String accountAlias;
143 @SerializedName("account_id")
144 public String accountId;
147 * Specifies the account under which the receiver is created. You must use
148 * this method or @{link ReceiverBuilder#setAccountId}, but not both.
150 * @param alias the unique alias of the account
151 * @return this ReceiverBuilder object
153 public ReceiverBuilder setAccountAlias(String alias) {
154 this.accountAlias = alias;
159 * Specifies the account under which the receiver is created. You must use
160 * this method or @{link ReceiverBuilder#setAccountAlias}, but not both.
162 * @param id the unique ID of the account
163 * @return this ReceiverBuilder object
165 public ReceiverBuilder setAccountId(String id) {
171 * Creates a single Receiver object under an account.
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.
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());
192 public String toString() {
193 return "ReceiverBuilder{" +
194 "accountAlias='" + accountAlias + '\'' +
195 ", accountId='" + accountId + '\'' +
201 * Use this class to create a {@link Address} under an account.
203 public static class AddressBuilder {
205 @SerializedName("account_alias")
206 public String accountAlias;
208 @SerializedName("account_id")
209 public String accountId;
212 * Specifies the account under which the address is created. You must use
213 * this method or @{link AddressBuilder#setAccountId}, but not both.
215 * @param alias the unique alias of the account
216 * @return this AddressBuilder object
218 public AddressBuilder setAccountAlias(String alias) {
219 this.accountAlias = alias;
224 * Specifies the account under which the address is created. You must use
225 * this method or @{link AddressBuilder#setAccountAlias}, but not both.
227 * @param id the unique ID of the account
228 * @return this AddressBuilder object
230 public AddressBuilder setAccountId(String id) {
237 * @param client client object that makes requests to the core
238 * @return list of address object
239 * @throws BytomException BytomException
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());
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
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());
265 return addressResult;
269 public String toString() {
270 return "AddressBuilder{" +
271 "accountAlias='" + accountAlias + '\'' +
272 ", accountId='" + accountId + '\'' +
278 public String toString() {
281 ", alias='" + alias + '\'' +
282 ", key_index=" + key_index +
283 ", quorum=" + quorum +