1 package io.bytom.offline.api;
3 import io.bytom.offline.common.Utils;
4 import io.bytom.offline.types.*;
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
10 public abstract class BaseInput {
12 static final int ISSUANCE_INPUT_TYPE = 0;
13 static final int SPEND_INPUT_TYPE = 1;
15 static final byte AssetKeySpace = 0;
16 static final byte AccountKeySpace = 1;
18 private String inputID;
21 * The number of units of the asset being issued or spent.
26 * The id of the asset being issued or spent.
28 private String assetId;
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
34 private String program;
36 private int vmVersion = 1;
39 * used for bip32 derived path.
40 * it represents accountIndex when is SpendInput, it represents assetIndex when is IssuanceInput
42 private Integer keyIndex;
44 WitnessComponent witnessComponent = new WitnessComponent();
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;
53 public byte[] serializeInput() throws IOException {
54 ByteArrayOutputStream stream = new ByteArrayOutputStream();
56 Utils.writeVarint(1, stream);
57 Utils.writeExtensibleString(serializeInputCommitment(), stream);
58 Utils.writeExtensibleString(serializeInputWitness(), stream);
59 return stream.toByteArray();
63 public void validate() {
64 if (assetId == null) {
65 throw new IllegalArgumentException("the asset id of input must be specified.");
68 throw new IllegalArgumentException("the amount id of input must be specified.");
70 if (program == null) {
71 throw new IllegalArgumentException("the program id of input must be specified.");
73 if (witnessComponent.getRootPrivateKey() == null) {
74 throw new IllegalArgumentException("the root private key of input must be specified.");
78 public AssetAmount getAssetAmount() {
79 return new AssetAmount(new AssetID(this.assetId), this.amount);
82 public void setRootPrivateKey(String prvKey) {
83 witnessComponent.setRootPrivateKey(prvKey);
86 public String getInputID() {
90 public void setInputID(String inputID) {
91 this.inputID = inputID;
94 public long getAmount() {
98 public void setAmount(long amount) {
102 public String getAssetId() {
106 public void setAssetId(String assetId) {
107 this.assetId = assetId;
110 public String getProgram() {
114 public void setProgram(String program) {
115 this.program = program;
118 public int getVmVersion() {
122 public void setVmVersion(int vmVersion) {
123 this.vmVersion = vmVersion;
126 public int getKeyIndex() {
130 public void setKeyIndex(int keyIndex) {
131 this.keyIndex = keyIndex;