3 import com.google.gson.annotations.SerializedName;
4 import io.bytom.common.ParameterizedTypeImpl;
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;
11 import java.util.HashMap;
12 import java.util.List;
23 @SerializedName("alias")
26 @SerializedName("xpub")
29 @SerializedName("file")
32 private static Logger logger = Logger.getLogger(Key.class);
34 public String toJson() {
35 return Utils.serializer.toJson(this);
41 * @param client client object that makes requests to the core
42 * @param builder Key.Builder object that make parameters
43 * @return Key a key object
44 * @throws BytomException BytomException
46 public static Key create(Client client, Builder builder) throws BytomException {
47 Key key = client.request("create-key", builder, Key.class);
52 * List all key objects
54 * @param client client object that makes requests to the core
55 * @return a list of key object
56 * @throws BytomException BytomException
58 public static List<Key> list(Client client) throws BytomException {
59 Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Key.class});
60 List<Key> keyList = client.request("list-keys", null, listType);
62 logger.info("list-key:");
63 logger.info("size of key:"+keyList.size());
72 * @param client client object that makes requests to the core
73 * @param xpub the xpub is given when creates key
74 * @param password the password is given when creates key
75 * @throws BytomException BytomException
77 public static void delete(Client client, String xpub, String password) throws BytomException {
78 Map<String, String> req = new HashMap<String, String>();
79 req.put("xpub", xpub);
80 req.put("password", password);
81 client.request("delete-key", req);
82 logger.info("delete-key successfully.");
88 * @param client client object that makes requests to the core
89 * @param xpub the xpub is given when creates key
90 * @param oldPwd the old password is given when creates key
91 * @param newPwd new password used to set
92 * @throws BytomException BytomException
94 public static void resetPassword(Client client, String xpub, String oldPwd, String newPwd) throws BytomException {
95 Map<String, String> req = new HashMap<>();
96 req.put("xpub", xpub);
97 req.put("old_password", oldPwd);
98 req.put("new_password", newPwd);
99 client.request("reset-key-password", req);
103 * <h1>Key.Builder Class</h1>
105 public static class Builder {
107 * User specified, unique identifier.
114 public String password;
117 * Sets the alias on the builder object.
120 * @return updated builder object
122 public Builder setAlias(String alias) {
128 * Sets the alias on the builder object.
130 * @param password password
131 * @return updated builder object
133 public Builder setPassword(String password) {
134 this.password = password;
141 public String toString() {
143 "alias='" + alias + '\'' +
144 ", xpub='" + xpub + '\'' +
145 ", file='" + file + '\'' +