1 package io.bytom.api;
\r
3 import com.google.gson.annotations.SerializedName;
\r
4 import io.bytom.common.ParameterizedTypeImpl;
\r
5 import io.bytom.common.SuccessRespon;
\r
6 import io.bytom.common.Utils;
\r
7 import io.bytom.exception.BytomException;
\r
8 import io.bytom.http.Client;
\r
9 import org.apache.log4j.Logger;
\r
11 import java.lang.reflect.Type;
\r
12 import java.util.ArrayList;
\r
13 import java.util.HashMap;
\r
14 import java.util.List;
\r
15 import java.util.Map;
\r
18 * Created by liqiang on 2018/10/24.
\r
21 public class Transaction {
\r
23 @SerializedName("tx_id")
\r
28 public Integer version;
\r
32 public Integer size;
\r
36 @SerializedName("time_range")
\r
37 public Integer timeRange;
\r
45 * List of specified inputs for a transaction.
\r
47 public List<AnnotatedInput> inputs;
\r
50 * List of specified outputs for a transaction.
\r
52 public List<AnnotatedOutput> outputs;
\r
54 // public InputWitnessComponent inputWitnessComponent;
\r
55 private static Logger logger = Logger.getLogger(Transaction.class);
\r
57 public String toJson() {
\r
58 return Utils.serializer.toJson(this);
\r
61 public static Transaction fromJson(String json) {
\r
62 return Utils.serializer.fromJson(json, Transaction.class);
\r
65 public static Transaction fromSuccessRespon(String json) {
\r
66 Type responType = new ParameterizedTypeImpl(SuccessRespon.class, new Class[]{Transaction.class});
\r
67 SuccessRespon<Transaction> result = Utils.serializer.fromJson(json, responType);
\r
68 return result.dataObject;
\r
71 public static Transaction decode(Client client, String txId) throws BytomException {
\r
72 Map<String, Object> req = new HashMap<String, Object>();
\r
73 req.put("raw_transaction", txId);
\r
74 Transaction Transaction =
\r
75 client.request("decode-raw-transaction", req, Transaction.class);
\r
77 logger.info("decode-raw-transaction:");
\r
78 logger.info(Transaction.toJson());
\r
83 public static class Builder {
\r
84 @SerializedName("tx_id")
\r
86 public Integer version;
\r
87 public Integer size;
\r
88 @SerializedName("time_range")
\r
89 public Integer timeRange;
\r
92 List<AnnotatedInput> inputs;
\r
93 List<AnnotatedOutput> outputs;
\r
96 this.inputs = new ArrayList<>();
\r
97 this.outputs = new ArrayList<>();
\r
100 public Builder addInput(AnnotatedInput input) {
\r
101 this.inputs.add(input);
\r
105 public Builder addOutput(AnnotatedOutput output) {
\r
106 this.outputs.add(output);
\r
111 public Transaction build(Integer version, Integer timeRange, Integer size) {
\r
112 tx = new Transaction();
\r
113 tx.inputs = this.inputs;
\r
114 tx.outputs = this.outputs;
\r
115 tx.version = version;
\r
116 tx.timeRange = timeRange;
\r
121 public Transaction build(int timeRange) {
\r
122 tx = new Transaction();
\r
123 tx.inputs = this.inputs;
\r
124 tx.outputs = this.outputs;
\r
127 tx.timeRange = timeRange;
\r
132 public static class AnnotatedInput {
\r
134 @SerializedName("input_id")
\r
135 public String inputID;
\r
139 public String address;
\r
142 * The number of units of the asset being issued or spent.
\r
144 public long amount;
\r
147 // * The definition of the asset being issued or spent (possibly null).
\r
149 // @SerializedName("asset_definition")
\r
150 // private Map<String, Object> assetDefinition;
\r
151 @SerializedName("asset_definition")
\r
152 public String assetDefinition;
\r
155 * The id of the asset being issued or spent.
\r
157 @SerializedName("asset_id")
\r
158 public String assetId;
\r
161 * The control program which must be satisfied to transfer this output.
\r
163 @SerializedName("control_program")
\r
164 public String controlProgram;
\r
167 * The id of the output consumed by this input. Null if the input is an
\r
170 @SerializedName("spent_output_id")
\r
171 public String spentOutputId;
\r
174 * The type of the input.<br>
\r
175 * Possible values are "issue" and "spend".
\r
179 public String sourceId;
\r
181 public long sourcePosition;
\r
183 public String nonce;
\r
185 private int controlProgramIndex;
\r
186 private boolean change;
\r
188 public int keyIndex;
\r
189 public String chainPath;
\r
191 @SerializedName("witness_component")
\r
192 public InputWitnessComponent witnessComponent;
\r
195 public String toString() {
\r
196 return Utils.serializer.toJson(this);
\r
199 public int getControlProgramIndex() {
\r
200 return controlProgramIndex;
\r
203 public AnnotatedInput setControlProgramIndex(int controlProgramIndex) {
\r
204 this.controlProgramIndex = controlProgramIndex;
\r
208 public boolean isChange() {
\r
212 public AnnotatedInput setChange(boolean change) {
\r
213 this.change = change;
\r
217 public AnnotatedInput setAmount(long amount) {
\r
218 this.amount = amount;
\r
222 public AnnotatedInput setAssetId(String assetId) {
\r
223 this.assetId = assetId;
\r
227 public AnnotatedInput setControlProgram(String controlProgram) {
\r
228 this.controlProgram = controlProgram;
\r
232 public AnnotatedInput setType(int type) {
\r
237 public AnnotatedInput setSourceId(String sourceId) {
\r
238 this.sourceId = sourceId;
\r
242 public AnnotatedInput setSourcePosition(long sourcePosition) {
\r
243 this.sourcePosition = sourcePosition;
\r
247 public AnnotatedInput setNonce(String nonce) {
\r
248 this.nonce = nonce;
\r
252 public AnnotatedInput setAssetDefinition(String assetDefinition) {
\r
253 this.assetDefinition = assetDefinition;
\r
257 public int getKeyIndex() {
\r
261 public AnnotatedInput setKeyIndex(int keyIndex) {
\r
262 this.keyIndex = keyIndex;
\r
268 public static class AnnotatedOutput {
\r
273 public String address;
\r
276 * The number of units of the asset being controlled.
\r
278 public long amount;
\r
281 * The definition of the asset being controlled (possibly null).
\r
283 @SerializedName("asset_definition")
\r
284 public Map<String, Object> assetDefinition;
\r
287 * The id of the asset being controlled.
\r
289 @SerializedName("asset_id")
\r
290 public String assetId;
\r
293 * The control program which must be satisfied to transfer this output.
\r
295 @SerializedName("control_program")
\r
296 public String controlProgram;
\r
299 * The id of the output.
\r
301 @SerializedName("id")
\r
305 * The output's position in a transaction's list of outputs.
\r
307 public Integer position;
\r
310 * The type the output.<br>
\r
311 * Possible values are "control" and "retire".
\r
313 public String type;
\r
315 public AnnotatedOutput setAddress(String address) {
\r
316 this.address = address;
\r
320 public AnnotatedOutput setAmount(long amount) {
\r
321 this.amount = amount;
\r
325 public AnnotatedOutput setAssetId(String assetId) {
\r
326 this.assetId = assetId;
\r
330 public AnnotatedOutput setControlProgram(String controlProgram) {
\r
331 this.controlProgram = controlProgram;
\r
335 public AnnotatedOutput setPosition(Integer position) {
\r
336 this.position = position;
\r
342 * A single witness component, holding information that will become the input
\r
345 public static class InputWitnessComponent {
\r
348 * The list of signatures made with the specified keys (null unless type is
\r
351 public String[] signatures;
\r