OSDN Git Service

558b6a7aa6e3c4047a92b962b69ae65dc34ff227
[bytom/bytom-java-sdk.git] / java-sdk / src / main / java / io / bytom / api / Key.java
1 package io.bytom.api;
2
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;
9
10 import java.lang.reflect.Type;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 /**
16  * <h1>Key Class</h1>
17  *
18  * @version 1.0
19  * @since   2018-05-18
20  */
21 public class Key {
22
23     @SerializedName("alias")
24     public String alias;
25
26     @SerializedName("xpub")
27     public String xpub;
28
29     @SerializedName("file")
30     public String file;
31
32     private static Logger logger = Logger.getLogger(Key.class);
33
34     public String toJson() {
35         return Utils.serializer.toJson(this);
36     }
37
38     /**
39      * Create a key object
40      *
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
45      */
46     public static Key create(Client client, Builder builder) throws BytomException {
47         Key key = client.request("create-key", builder, Key.class);
48         return key;
49     }
50
51     /**
52      * List all key objects
53      *
54      * @param client client object that makes requests to the core
55      * @return a list of key object
56      * @throws BytomException BytomException
57      */
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);
61
62         logger.info("list-key:");
63         logger.info("size of key:"+keyList.size());
64         logger.info(keyList);
65
66         return keyList;
67     }
68
69     /**
70      * delete a key
71      *
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
76      */
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.");
83     }
84
85     /**
86      * reset password
87      *
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
93      */
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);
100     }
101
102     /**
103      * <h1>Key.Builder Class</h1>
104      */
105     public static class Builder {
106         /**
107          * User specified, unique identifier.
108          */
109         public String alias;
110
111         /**
112          * User specified.
113          */
114         public String password;
115
116         /**
117          * Sets the alias on the builder object.
118          *
119          * @param alias alias
120          * @return updated builder object
121          */
122         public Builder setAlias(String alias) {
123             this.alias = alias;
124             return this;
125         }
126
127         /**
128          * Sets the alias on the builder object.
129          *
130          * @param password password
131          * @return updated builder object
132          */
133         public Builder setPassword(String password) {
134             this.password = password;
135             return this;
136         }
137
138     }
139
140     @Override
141     public String toString() {
142         return "Key{" +
143                 "alias='" + alias + '\'' +
144                 ", xpub='" + xpub + '\'' +
145                 ", file='" + file + '\'' +
146                 '}';
147     }
148 }