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;
10 import java.lang.reflect.Type;
14 * <h1>Asset Class</h1>
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>
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
36 * User specified, unique identifier.
41 * A program specifying a predicate to be satisfied when issuing the asset.
43 @SerializedName(value = "issuance_program", alternate = {"issue_program"})
44 public String issuanceProgram;
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.
52 @SerializedName("key_index")
53 public Integer keyIndex;
55 @SerializedName("xpubs")
56 public List<String> xpubs;
59 * The number of keys required to sign an issuance of the asset.
61 @SerializedName("quorum")
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.
69 @SerializedName("definition")
70 public Map<String, Object> definition;
75 @SerializedName("vm_version")
81 @SerializedName("type")
85 * byte of asset definition.
87 @SerializedName("raw_definition_byte")
88 public String rawDefinitionByte;
90 public static Logger logger = Logger.getLogger(Asset.class);
92 public String toJson() {
93 return Utils.serializer.toJson(this);
96 public static class Key {
98 * Hex-encoded representation of the root extended public key
100 @SerializedName("root_xpub")
101 public String rootXpub;
104 * The derived public key, used in the asset's issuance program.
106 @SerializedName("asset_pubkey")
107 public String assetPubkey;
110 * The derivation path of the derived key.
112 @SerializedName("asset_derivation_path")
113 public String[] assetDerivationPath;
116 public String toString() {
118 "rootXpub='" + rootXpub + '\'' +
119 ", assetPubkey='" + assetPubkey + '\'' +
120 ", assetDerivationPath=" + Arrays.toString(assetDerivationPath) +
126 * <h2>Builder Class</h2>
128 public static class Builder {
130 * User specified, unique identifier.
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.
139 public Map<String, Object> definition;
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>
147 @SerializedName("root_xpubs")
148 public List<String> rootXpubs;
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>
158 * Unique identifier used for request idempotence.
160 @SerializedName("access_token")
161 private String access_token;
164 * Default constructor initializes the list of keys.
167 this.rootXpubs = new ArrayList<>();
171 * Creates an asset object.
173 * @param client client object that makes request to the core
174 * @return an asset object
175 * @throws BytomException BytomException
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());
185 * Sets the alias on the builder object.
187 * @return updated builder object
189 public Builder setAlias(String alias) {
195 * Adds a field to the existing definition object (initializing the object if it
197 * @param key key of the definition field
198 * @param value value of the definition field
199 * @return updated builder object
201 public Builder addDefinitionField(String key, Object value) {
202 if (this.definition == null) {
203 this.definition = new HashMap<>();
205 this.definition.put(key, value);
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
215 public Builder setDefinition(Map<String, Object> definition) {
216 this.definition = definition;
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
226 public Builder setQuorum(int quorum) {
227 this.quorum = quorum;
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>
236 * @return updated builder object.
238 public Builder addRootXpub(String xpub) {
239 this.rootXpubs.add(xpub);
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
251 public Builder setRootXpubs(List<String> xpubs) {
252 this.rootXpubs = new ArrayList<>(xpubs);
257 public String toString() {
259 "alias='" + alias + '\'' +
260 ", definition=" + definition +
261 ", rootXpubs=" + rootXpubs +
262 ", quorum=" + quorum +
263 ", access_token='" + access_token + '\'' +
269 * <h2>QueryBuilder Class</h2>
271 public static class QueryBuilder {
273 @SerializedName("id")
276 public QueryBuilder setId(String assetId) {
282 * get-asset from bytomd
284 * @param client client object that makes requests to the core
285 * @return The Asset Object
286 * @throws BytomException BytomException
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());
296 * get all assets from bytomd
298 * @param client client object that makes requests to the core
299 * @return return list of asset object
300 * @throws BytomException BytomException
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);
314 * <h2>AliasUpdateBuilder Class</h2>
316 public static class AliasUpdateBuilder {
320 @SerializedName("id")
325 @SerializedName("alias")
328 public AliasUpdateBuilder setAssetId(String assetId) {
333 public AliasUpdateBuilder setAlias(String alias) {
341 * @param client client object that makes requests to the core
342 * @throws BytomException BytomException
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);
354 public String toString() {
357 ", alias='" + alias + '\'' +
358 ", issuanceProgram='" + issuanceProgram + '\'' +
359 ", keys=" + Arrays.toString(keys) +
360 ", keyIndex=" + keyIndex +
362 ", quorum=" + quorum +
363 ", definition=" + definition +
364 ", vmVersion=" + vmVersion +
365 ", type='" + type + '\'' +
366 ", rawDefinitionByte='" + rawDefinitionByte + '\'' +