OSDN Git Service

fix signature
[bytom/bytom-java-sdk.git] / tx-signer / src / main / java / io / bytom / offline / api / BaseInput.java
1 package io.bytom.offline.api;
2
3 import io.bytom.offline.common.Utils;
4 import io.bytom.offline.types.*;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8 import java.util.Map;
9
10 public abstract class BaseInput {
11
12     static final int ISSUANCE_INPUT_TYPE = 0;
13     static final int SPEND_INPUT_TYPE = 1;
14
15     static final byte AssetKeySpace = 0;
16     static final byte AccountKeySpace = 1;
17
18     private String inputID;
19
20     /**
21      * The number of units of the asset being issued or spent.
22      */
23     private Long amount;
24
25     /**
26      * The id of the asset being issued or spent.
27      */
28     private String assetId;
29
30     /**
31      * The program which must be satisfied to transfer this output.
32      * it represents control program when is SpendInput, it represents issuance program when is IssuanceInput
33      */
34     private String program;
35
36     private int vmVersion = 1;
37
38     /**
39      * used for bip32 derived path.
40      * it represents accountIndex when is SpendInput, it represents assetIndex when is IssuanceInput
41      */
42     private Integer keyIndex;
43
44     WitnessComponent witnessComponent = new WitnessComponent();
45
46     public BaseInput() {}
47
48     public abstract InputEntry toInputEntry(Map<Hash, Entry> entryMap, int index);
49     public abstract void buildWitness(String txID) throws Exception;
50     public abstract byte[] serializeInputCommitment() throws IOException;
51     public abstract byte[] serializeInputWitness() throws IOException;
52
53     public byte[] serializeInput() throws IOException {
54         ByteArrayOutputStream stream = new ByteArrayOutputStream();
55         // assert version
56         Utils.writeVarint(1, stream);
57         Utils.writeExtensibleString(serializeInputCommitment(), stream);
58         Utils.writeExtensibleString(serializeInputWitness(), stream);
59         return stream.toByteArray();
60     }
61
62
63     public void validate() {
64         if (assetId == null) {
65             throw new IllegalArgumentException("the asset id of input must be specified.");
66         }
67         if (amount == null) {
68             throw new IllegalArgumentException("the amount id of input must be specified.");
69         }
70         if (program == null) {
71             throw new IllegalArgumentException("the program id of input must be specified.");
72         }
73         if (keyIndex == null) {
74             throw new IllegalArgumentException("the key index of input must be specified.");
75         }
76         if (witnessComponent.getRootPrivateKey() == null) {
77             throw new IllegalArgumentException("the root private key of input must be specified.");
78         }
79     }
80
81     public AssetAmount getAssetAmount() {
82         return new AssetAmount(new AssetID(this.assetId), this.amount);
83     }
84
85     public void setRootPrivateKey(String prvKey) {
86         witnessComponent.setRootPrivateKey(prvKey);
87     }
88
89     public String getInputID() {
90         return inputID;
91     }
92
93     public void setInputID(String inputID) {
94         this.inputID = inputID;
95     }
96
97     public long getAmount() {
98         return amount;
99     }
100
101     public void setAmount(long amount) {
102         this.amount = amount;
103     }
104
105     public String getAssetId() {
106         return assetId;
107     }
108
109     public void setAssetId(String assetId) {
110         this.assetId = assetId;
111     }
112
113     public String getProgram() {
114         return program;
115     }
116
117     public void setProgram(String program) {
118         this.program = program;
119     }
120
121     public int getVmVersion() {
122         return vmVersion;
123     }
124
125     public void setVmVersion(int vmVersion) {
126         this.vmVersion = vmVersion;
127     }
128
129     public int getKeyIndex() {
130         return keyIndex;
131     }
132
133     public void setKeyIndex(int keyIndex) {
134         this.keyIndex = keyIndex;
135     }
136 }