OSDN Git Service

a little modify
[bytom/bytom-java-sdk.git] / src / main / java / io / bytom / api / Asset.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.BytomException;
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>Asset Class</h1>
15  * <br>
16  * String - id, asset id.<br>
17  * String - alias, name of the asset.<br>
18  * String - issuance_program, control program of the issuance of asset.<br>
19  * Array of Object - keys, information of asset pubkey.<br>
20  * String - definition, definition of asset.<br>
21  * Integer - quorum, threshold of keys that must sign a transaction to spend asset units controlled by the account.<br>
22  */
23 public class Asset {
24
25     /**
26      * Globally unique identifier of the asset.<br>
27      * Asset version 1 specifies the asset id as the hash of:<br>
28      * - the asset version<br>
29      * - the asset's issuance program<br>
30      * - the core's VM version<br>
31      * - the hash of the network's initial block
32      */
33     public String id;
34
35     /**
36      * User specified, unique identifier.
37      */
38     public String alias;
39
40     /**
41      * A program specifying a predicate to be satisfied when issuing the asset.
42      */
43     @SerializedName(value = "issuance_program", alternate = {"issue_program"})
44     public String issuanceProgram;
45
46     /**
47      * The list of keys used to create the issuance program for the asset.<br>
48      * Signatures from these keys are required for issuing units of the asset.
49      */
50     public Key[] keys;
51
52     @SerializedName("key_index")
53     public Integer keyIndex;
54
55     @SerializedName("xpubs")
56     public List<String> xpubs;
57
58     /**
59      * The number of keys required to sign an issuance of the asset.
60      */
61     @SerializedName("quorum")
62     public int quorum;
63
64     /**
65      * User-specified, arbitrary/unstructured data visible across blockchain networks.<br>
66      * Version 1 assets specify the definition in their issuance programs, rendering the
67      * definition immutable.
68      */
69     @SerializedName("definition")
70     public Map<String, Object> definition;
71
72     /**
73      * version of VM.
74      */
75     @SerializedName("vm_version")
76     public int vmVersion;
77
78     /**
79      * type of asset.
80      */
81     @SerializedName("type")
82     public String type;
83
84     /**
85      * byte of asset definition.
86      */
87     @SerializedName("raw_definition_byte")
88     public String rawDefinitionByte;
89
90     public static Logger logger = Logger.getLogger(Asset.class);
91
92     public String toJson() {
93         return Utils.serializer.toJson(this);
94     }
95
96     public static class Key {
97         /**
98          * Hex-encoded representation of the root extended public key
99          */
100         @SerializedName("root_xpub")
101         public String rootXpub;
102
103         /**
104          * The derived public key, used in the asset's issuance program.
105          */
106         @SerializedName("asset_pubkey")
107         public String assetPubkey;
108
109         /**
110          * The derivation path of the derived key.
111          */
112         @SerializedName("asset_derivation_path")
113         public String[] assetDerivationPath;
114
115         @Override
116         public String toString() {
117             return "Key{" +
118                     "rootXpub='" + rootXpub + '\'' +
119                     ", assetPubkey='" + assetPubkey + '\'' +
120                     ", assetDerivationPath=" + Arrays.toString(assetDerivationPath) +
121                     '}';
122         }
123     }
124
125     /**
126      * <h2>Builder Class</h2>
127      */
128     public static class Builder {
129         /**
130          * User specified, unique identifier.
131          */
132         public String alias;
133
134         /**
135          * User-specified, arbitrary/unstructured data visible across blockchain networks.<br>
136          * Version 1 assets specify the definition in their issuance programs, rendering
137          * the definition immutable.
138          */
139         public Map<String, Object> definition;
140
141         /**
142          * The list of keys used to create the issuance program for the asset.<br>
143          * Signatures from these keys are required for issuing units of the asset.<br>
144          * <strong>Must set with {@link #addRootXpub(String)} or
145          * {@link #setRootXpubs(List)} before calling {@link #create(Client)}.</strong>
146          */
147         @SerializedName("root_xpubs")
148         public List<String> rootXpubs;
149
150         /**
151          * The number of keys required to sign an issuance of the asset.<br>
152          * <strong>Must set with {@link #setQuorum(int)} before calling
153          * {@link #create(Client)}.</strong>
154          */
155         public int quorum;
156
157         /**
158          * Unique identifier used for request idempotence.
159          */
160         @SerializedName("access_token")
161         private String access_token;
162
163         /**
164          * Default constructor initializes the list of keys.
165          */
166         public Builder() {
167             this.rootXpubs = new ArrayList<>();
168         }
169
170         /**
171          * Creates an asset object.
172          *
173          * @param client client object that makes request to the core
174          * @return an asset object
175          * @throws BytomException BytomException
176          */
177         public Asset create(Client client) throws BytomException {
178             Asset asset = client.request("create-asset", this, Asset.class);
179             logger.info("create-asset:");
180             logger.info(asset.toString());
181             return asset;
182         }
183
184         /**
185          * Sets the alias on the builder object.
186          * @param alias alias
187          * @return updated builder object
188          */
189         public Builder setAlias(String alias) {
190             this.alias = alias;
191             return this;
192         }
193
194         /**
195          * Adds a field to the existing definition object (initializing the object if it
196          * doesn't exist).
197          * @param key key of the definition field
198          * @param value value of the definition field
199          * @return updated builder object
200          */
201         public Builder addDefinitionField(String key, Object value) {
202             if (this.definition == null) {
203                 this.definition = new HashMap<>();
204             }
205             this.definition.put(key, value);
206             return this;
207         }
208
209         /**
210          * Sets the asset definition object.<br>
211          * <strong>Note:</strong> any existing asset definition fields will be replaced.
212          * @param definition asset definition object
213          * @return updated builder object
214          */
215         public Builder setDefinition(Map<String, Object> definition) {
216             this.definition = definition;
217             return this;
218         }
219
220         /**
221          * Sets the quorum of the issuance program. <strong>Must be called before
222          * {@link #create(Client)}.</strong>
223          * @param quorum proposed quorum
224          * @return updated builder object
225          */
226         public Builder setQuorum(int quorum) {
227             this.quorum = quorum;
228             return this;
229         }
230
231         /**
232          * Adds a key to the builder's list.<br>
233          * <strong>Either this or {@link #setRootXpubs(List)} must be called before
234          * {@link #create(Client)}.</strong>
235          * @param xpub key
236          * @return updated builder object.
237          */
238         public Builder addRootXpub(String xpub) {
239             this.rootXpubs.add(xpub);
240             return this;
241         }
242
243         /**
244          * Sets the builder's list of keys.<br>
245          * <strong>Note:</strong> any existing keys will be replaced.<br>
246          * <strong>Either this or {@link #addRootXpub(String)} must be called before
247          * {@link #create(Client)}.</strong>
248          * @param xpubs list of xpubs
249          * @return updated builder object
250          */
251         public Builder setRootXpubs(List<String> xpubs) {
252             this.rootXpubs = new ArrayList<>(xpubs);
253             return this;
254         }
255
256         @Override
257         public String toString() {
258             return "Builder{" +
259                     "alias='" + alias + '\'' +
260                     ", definition=" + definition +
261                     ", rootXpubs=" + rootXpubs +
262                     ", quorum=" + quorum +
263                     ", access_token='" + access_token + '\'' +
264                     '}';
265         }
266     }
267
268     /**
269      * <h2>QueryBuilder Class</h2>
270      */
271     public static class QueryBuilder {
272
273         @SerializedName("id")
274         public String id;
275
276         public QueryBuilder setId(String assetId) {
277             this.id = assetId;
278             return this;
279         }
280
281         /**
282          * get-asset from bytomd
283          *
284          * @param client client object that makes requests to the core
285          * @return The Asset Object
286          * @throws BytomException BytomException
287          */
288         public Asset get(Client client) throws BytomException {
289             Asset asset = client.request("get-asset", this, Asset.class);
290             logger.info("get-asset:");
291             logger.info(asset.toJson());
292             return asset;
293         }
294
295         /**
296          * get all assets from bytomd
297          *
298          * @param client client object that makes requests to the core
299          * @return return list of asset object
300          * @throws BytomException BytomException
301          */
302         public List<Asset> list(Client client) throws BytomException {
303             Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Asset.class});
304             List<Asset> assetList = client.request("list-assets", null, listType);
305             logger.info("list-assets:");
306             logger.info("size of assetList:"+assetList.size());
307             logger.info(assetList);
308             return assetList;
309         }
310
311     }
312
313     /**
314      * <h2>AliasUpdateBuilder Class</h2>
315      */
316     public static class AliasUpdateBuilder {
317         /**
318          * id of asset.
319          */
320         @SerializedName("id")
321         public String id;
322         /**
323          * new alias of asset
324          */
325         @SerializedName("alias")
326         public String alias;
327
328         public AliasUpdateBuilder setAssetId(String assetId) {
329             this.id = assetId;
330             return this;
331         }
332
333         public AliasUpdateBuilder setAlias(String alias) {
334             this.alias = alias;
335             return this;
336         }
337
338         /**
339          * update-asset-alias
340          *
341          * @param client client object that makes requests to the core
342          * @throws BytomException BytomException
343          */
344         public void update(Client client) throws BytomException {
345             client.request("update-asset-alias", this);
346             logger.info("update-asset-alias:");
347             logger.info("id:"+id);
348             logger.info("alias:"+alias);
349         }
350
351     }
352
353     @Override
354     public String toString() {
355         return "Asset{" +
356                 "id='" + id + '\'' +
357                 ", alias='" + alias + '\'' +
358                 ", issuanceProgram='" + issuanceProgram + '\'' +
359                 ", keys=" + Arrays.toString(keys) +
360                 ", keyIndex=" + keyIndex +
361                 ", xpubs=" + xpubs +
362                 ", quorum=" + quorum +
363                 ", definition=" + definition +
364                 ", vmVersion=" + vmVersion +
365                 ", type='" + type + '\'' +
366                 ", rawDefinitionByte='" + rawDefinitionByte + '\'' +
367                 '}';
368     }
369 }