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 (keyIndex == null) {
74 throw new IllegalArgumentException("the key index of input must be specified.");
76 if (witnessComponent.getRootPrivateKey() == null) {
77 throw new IllegalArgumentException("the root private key of input must be specified.");
81 public AssetAmount getAssetAmount() {
82 return new AssetAmount(new AssetID(this.assetId), this.amount);
85 public void setRootPrivateKey(String prvKey) {
86 witnessComponent.setRootPrivateKey(prvKey);
89 public String getInputID() {
93 public void setInputID(String inputID) {
94 this.inputID = inputID;
97 public long getAmount() {
101 public void setAmount(long amount) {
102 this.amount = amount;
105 public String getAssetId() {
109 public void setAssetId(String assetId) {
110 this.assetId = assetId;
113 public String getProgram() {
117 public void setProgram(String program) {
118 this.program = program;
121 public int getVmVersion() {
125 public void setVmVersion(int vmVersion) {
126 this.vmVersion = vmVersion;
129 public int getKeyIndex() {
133 public void setKeyIndex(int keyIndex) {
134 this.keyIndex = keyIndex;