OSDN Git Service

merge tx signer
[bytom/bytom-java-sdk.git] / tx-signer / src / main / java / io / bytom / api / Transaction.java
1 package io.bytom.api;\r
2 \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
10 \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
16 \r
17 /**\r
18  * Created by liqiang on 2018/10/24.\r
19  */\r
20 \r
21 public class Transaction {\r
22 \r
23     @SerializedName("tx_id")\r
24     public String txID;\r
25     /**\r
26      * version\r
27      */\r
28     public Integer version;\r
29     /**\r
30      * size\r
31      */\r
32     public Integer size;\r
33     /**\r
34      * time_range\r
35      */\r
36     @SerializedName("time_range")\r
37     public Integer timeRange;\r
38 \r
39     /**\r
40      * status\r
41      */\r
42     public Integer fee;\r
43 \r
44     /**\r
45      * List of specified inputs for a transaction.\r
46      */\r
47     public List<AnnotatedInput> inputs;\r
48 \r
49     /**\r
50      * List of specified outputs for a transaction.\r
51      */\r
52     public List<AnnotatedOutput> outputs;\r
53 \r
54     //    public InputWitnessComponent inputWitnessComponent;\r
55     private static Logger logger = Logger.getLogger(Transaction.class);\r
56 \r
57     public String toJson() {\r
58         return Utils.serializer.toJson(this);\r
59     }\r
60 \r
61     public static Transaction fromJson(String json) {\r
62         return Utils.serializer.fromJson(json, Transaction.class);\r
63     }\r
64 \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
69     }\r
70 \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
76 \r
77         logger.info("decode-raw-transaction:");\r
78         logger.info(Transaction.toJson());\r
79 \r
80         return Transaction;\r
81     }\r
82 \r
83     public static class Builder {\r
84         @SerializedName("tx_id")\r
85         public String txID;\r
86         public Integer version;\r
87         public Integer size;\r
88         @SerializedName("time_range")\r
89         public Integer timeRange;\r
90 \r
91         Transaction tx;\r
92         List<AnnotatedInput> inputs;\r
93         List<AnnotatedOutput> outputs;\r
94 \r
95         public Builder() {\r
96             this.inputs = new ArrayList<>();\r
97             this.outputs = new ArrayList<>();\r
98         }\r
99 \r
100         public Builder addInput(AnnotatedInput input) {\r
101             this.inputs.add(input);\r
102             return this;\r
103         }\r
104 \r
105         public Builder addOutput(AnnotatedOutput output) {\r
106             this.outputs.add(output);\r
107             return this;\r
108         }\r
109 \r
110 \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
117             tx.size = size;\r
118             return tx;\r
119         }\r
120 \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
125             tx.version = 1;\r
126             tx.size = 0;\r
127             tx.timeRange = timeRange;\r
128             return tx;\r
129         }\r
130     }\r
131 \r
132     public static class AnnotatedInput {\r
133 \r
134         @SerializedName("input_id")\r
135         public String inputID;\r
136         /**\r
137          * address\r
138          */\r
139         public String address;\r
140 \r
141         /**\r
142          * The number of units of the asset being issued or spent.\r
143          */\r
144         public long amount;\r
145 \r
146         //        /**\r
147 //         * The definition of the asset being issued or spent (possibly null).\r
148 //         */\r
149 //        @SerializedName("asset_definition")\r
150 //        private Map<String, Object> assetDefinition;\r
151         @SerializedName("asset_definition")\r
152         public String assetDefinition;\r
153 \r
154         /**\r
155          * The id of the asset being issued or spent.\r
156          */\r
157         @SerializedName("asset_id")\r
158         public String assetId;\r
159 \r
160         /**\r
161          * The control program which must be satisfied to transfer this output.\r
162          */\r
163         @SerializedName("control_program")\r
164         public String controlProgram;\r
165 \r
166         /**\r
167          * The id of the output consumed by this input. Null if the input is an\r
168          * issuance.\r
169          */\r
170         @SerializedName("spent_output_id")\r
171         public String spentOutputId;\r
172 \r
173         /**\r
174          * The type of the input.<br>\r
175          * Possible values are "issue" and "spend".\r
176          */\r
177         public int type;\r
178 \r
179         public String sourceId;\r
180 \r
181         public long sourcePosition;\r
182 \r
183         public String nonce;\r
184 \r
185         private int controlProgramIndex;\r
186         private boolean change;\r
187 \r
188         public int keyIndex;\r
189         public String chainPath;\r
190 \r
191         @SerializedName("witness_component")\r
192         public InputWitnessComponent witnessComponent;\r
193 \r
194         @Override\r
195         public String toString() {\r
196             return Utils.serializer.toJson(this);\r
197         }\r
198 \r
199         public int getControlProgramIndex() {\r
200             return controlProgramIndex;\r
201         }\r
202 \r
203         public AnnotatedInput setControlProgramIndex(int controlProgramIndex) {\r
204             this.controlProgramIndex = controlProgramIndex;\r
205             return this;\r
206         }\r
207 \r
208         public boolean isChange() {\r
209             return change;\r
210         }\r
211 \r
212         public AnnotatedInput setChange(boolean change) {\r
213             this.change = change;\r
214             return this;\r
215         }\r
216 \r
217         public AnnotatedInput setAmount(long amount) {\r
218             this.amount = amount;\r
219             return this;\r
220         }\r
221 \r
222         public AnnotatedInput setAssetId(String assetId) {\r
223             this.assetId = assetId;\r
224             return this;\r
225         }\r
226 \r
227         public AnnotatedInput setControlProgram(String controlProgram) {\r
228             this.controlProgram = controlProgram;\r
229             return this;\r
230         }\r
231 \r
232         public AnnotatedInput setType(int type) {\r
233             this.type = type;\r
234             return this;\r
235         }\r
236 \r
237         public AnnotatedInput setSourceId(String sourceId) {\r
238             this.sourceId = sourceId;\r
239             return this;\r
240         }\r
241 \r
242         public AnnotatedInput setSourcePosition(long sourcePosition) {\r
243             this.sourcePosition = sourcePosition;\r
244             return this;\r
245         }\r
246 \r
247         public AnnotatedInput setNonce(String nonce) {\r
248             this.nonce = nonce;\r
249             return this;\r
250         }\r
251 \r
252         public AnnotatedInput setAssetDefinition(String assetDefinition) {\r
253             this.assetDefinition = assetDefinition;\r
254             return this;\r
255         }\r
256 \r
257         public int getKeyIndex() {\r
258             return keyIndex;\r
259         }\r
260 \r
261         public AnnotatedInput setKeyIndex(int keyIndex) {\r
262             this.keyIndex = keyIndex;\r
263             return this;\r
264         }\r
265 \r
266     }\r
267 \r
268     public static class AnnotatedOutput {\r
269 \r
270         /**\r
271          * address\r
272          */\r
273         public String address;\r
274 \r
275         /**\r
276          * The number of units of the asset being controlled.\r
277          */\r
278         public long amount;\r
279 \r
280         /**\r
281          * The definition of the asset being controlled (possibly null).\r
282          */\r
283         @SerializedName("asset_definition")\r
284         public Map<String, Object> assetDefinition;\r
285 \r
286         /**\r
287          * The id of the asset being controlled.\r
288          */\r
289         @SerializedName("asset_id")\r
290         public String assetId;\r
291 \r
292         /**\r
293          * The control program which must be satisfied to transfer this output.\r
294          */\r
295         @SerializedName("control_program")\r
296         public String controlProgram;\r
297 \r
298         /**\r
299          * The id of the output.\r
300          */\r
301         @SerializedName("id")\r
302         public String id;\r
303 \r
304         /**\r
305          * The output's position in a transaction's list of outputs.\r
306          */\r
307         public Integer position;\r
308 \r
309         /**\r
310          * The type the output.<br>\r
311          * Possible values are "control" and "retire".\r
312          */\r
313         public String type;\r
314 \r
315         public AnnotatedOutput setAddress(String address) {\r
316             this.address = address;\r
317             return this;\r
318         }\r
319 \r
320         public AnnotatedOutput setAmount(long amount) {\r
321             this.amount = amount;\r
322             return this;\r
323         }\r
324 \r
325         public AnnotatedOutput setAssetId(String assetId) {\r
326             this.assetId = assetId;\r
327             return this;\r
328         }\r
329 \r
330         public AnnotatedOutput setControlProgram(String controlProgram) {\r
331             this.controlProgram = controlProgram;\r
332             return this;\r
333         }\r
334 \r
335         public AnnotatedOutput setPosition(Integer position) {\r
336             this.position = position;\r
337             return this;\r
338         }\r
339     }\r
340 \r
341     /**\r
342      * A single witness component, holding information that will become the input\r
343      * witness.\r
344      */\r
345     public static class InputWitnessComponent {\r
346 \r
347         /**\r
348          * The list of signatures made with the specified keys (null unless type is\r
349          * "signature").\r
350          */\r
351         public String[] signatures;\r
352     }\r
353 }\r