package qora.transaction; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import ntp.NTP; import org.json.simple.JSONObject; import com.google.common.primitives.Bytes; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; import database.DBSet; import qora.account.Account; import qora.account.PrivateKeyAccount; import qora.account.PublicKeyAccount; import qora.assets.Asset; import qora.crypto.Crypto; public class IssueAssetTransaction extends Transaction { private static final int ISSUER_LENGTH = 32; private static final int REFERENCE_LENGTH = 64; private static final int FEE_LENGTH = 8; private static final int SIGNATURE_LENGTH = 64; private static final int BASE_LENGTH = TIMESTAMP_LENGTH + REFERENCE_LENGTH + ISSUER_LENGTH + FEE_LENGTH + SIGNATURE_LENGTH; private PublicKeyAccount issuer; private Asset asset; public IssueAssetTransaction(PublicKeyAccount issuer, Asset asset, BigDecimal fee, long timestamp, byte[] reference, byte[] signature) { super(ISSUE_ASSET_TRANSACTION, fee, timestamp, reference, signature); this.issuer = issuer; this.asset = asset; } //GETTERS/SETTERS public PublicKeyAccount getIssuer() { return this.issuer; } public Asset getAsset() { return this.asset; } //PARSE CONVERT public static Transaction Parse(byte[] data) throws Exception { //CHECK IF WE MATCH BLOCK LENGTH if(data.length < BASE_LENGTH) { throw new Exception("Data does not match block length"); } int position = 0; //READ TIMESTAMP byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH); long timestamp = Longs.fromByteArray(timestampBytes); position += TIMESTAMP_LENGTH; //READ REFERENCE byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH); position += REFERENCE_LENGTH; //READ ISSUER byte[] issuerBytes = Arrays.copyOfRange(data, position, position + ISSUER_LENGTH); PublicKeyAccount issuer = new PublicKeyAccount(issuerBytes); position += ISSUER_LENGTH; //READ ASSET Asset asset = Asset.parse(Arrays.copyOfRange(data, position, data.length)); position += asset.getDataLength(); //READ FEE byte[] feeBytes = Arrays.copyOfRange(data, position, position + FEE_LENGTH); BigDecimal fee = new BigDecimal(new BigInteger(feeBytes), 8); position += FEE_LENGTH; //READ SIGNATURE byte[] signatureBytes = Arrays.copyOfRange(data, position, position + SIGNATURE_LENGTH); return new IssueAssetTransaction(issuer, asset, fee, timestamp, reference, signatureBytes); } @SuppressWarnings("unchecked") @Override public JSONObject toJson() { //GET BASE JSONObject transaction = this.getJsonBase(); //ADD CREATOR/NAME/DISCRIPTION/QUANTITY/DIVISIBLE transaction.put("creator", this.getAsset().getOwner().getAddress()); transaction.put("name", this.getAsset().getName()); transaction.put("description", this.getAsset().getDescription()); transaction.put("quantity", this.getAsset().getQuantity()); transaction.put("divisible", this.getAsset().isDivisible()); return transaction; } @Override public byte[] toBytes() { byte[] data = new byte[0]; //WRITE TYPE byte[] typeBytes = Ints.toByteArray(ISSUE_ASSET_TRANSACTION); typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0); data = Bytes.concat(data, typeBytes); //WRITE TIMESTAMP byte[] timestampBytes = Longs.toByteArray(this.timestamp); timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0); data = Bytes.concat(data, timestampBytes); //WRITE REFERENCE data = Bytes.concat(data, this.reference); //WRITE ISSUER data = Bytes.concat(data, this.issuer.getPublicKey()); //WRITE ASSET data = Bytes.concat(data , this.asset.toBytes(true)); //WRITE FEE byte[] feeBytes = this.fee.unscaledValue().toByteArray(); byte[] fill = new byte[FEE_LENGTH - feeBytes.length]; feeBytes = Bytes.concat(fill, feeBytes); data = Bytes.concat(data, feeBytes); //SIGNATURE data = Bytes.concat(data, this.signature); return data; } @Override public int getDataLength() { return TYPE_LENGTH + BASE_LENGTH + this.asset.getDataLength(); } //VALIDATE public boolean isSignatureValid() { byte[] data = new byte[0]; //WRITE TYPE byte[] typeBytes = Ints.toByteArray(ISSUE_ASSET_TRANSACTION); typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0); data = Bytes.concat(data, typeBytes); //WRITE TIMESTAMP byte[] timestampBytes = Longs.toByteArray(this.timestamp); timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0); data = Bytes.concat(data, timestampBytes); //WRITE REFERENCE data = Bytes.concat(data, this.reference); //WRITE ISSUER data = Bytes.concat(data, this.issuer.getPublicKey()); //WRITE ASSET data = Bytes.concat(data , this.asset.toBytes(false)); //WRITE FEE byte[] feeBytes = this.fee.unscaledValue().toByteArray(); byte[] fill = new byte[FEE_LENGTH - feeBytes.length]; feeBytes = Bytes.concat(fill, feeBytes); data = Bytes.concat(data, feeBytes); return Crypto.getInstance().verify(this.issuer.getPublicKey(), this.signature, data); } @Override public int isValid(DBSet db) { //CHECK IF RELEASED if(NTP.getTime() < ASSETS_RELEASE) { return NOT_YET_RELEASED; } //CHECK NAME LENGTH int nameLength = this.asset.getName().getBytes(StandardCharsets.UTF_8).length; if(nameLength > 400 || nameLength < 1) { return INVALID_NAME_LENGTH; } //CHECK DESCRIPTION LENGTH int descriptionLength = this.asset.getDescription().getBytes(StandardCharsets.UTF_8).length; if(descriptionLength > 4000 || descriptionLength < 1) { return INVALID_DESCRIPTION_LENGTH; } //CHECK QUANTITY long maxQuantity = this.asset.isDivisible() ? 10000000000L : 1000000000000000000L; if(this.asset.getQuantity() < 1 || this.asset.getQuantity() > maxQuantity) { return INVALID_QUANTITY; } //CHECK ISSUER if(!Crypto.getInstance().isValidAddress(this.asset.getOwner().getAddress())) { return INVALID_ADDRESS; } //CHECK IF ISSUER HAS ENOUGH MONEY if(this.issuer.getBalance(1, db).compareTo(this.fee) == -1) { return NO_BALANCE; } //CHECK IF REFERENCE IS OKE if(!Arrays.equals(this.issuer.getLastReference(db), this.reference)) { return INVALID_REFERENCE; } //CHECK IF FEE IS POSITIVE if(this.fee.compareTo(BigDecimal.ZERO) <= 0) { return NEGATIVE_FEE; } return VALIDATE_OKE; } //PROCESS/ORPHAN @Override public void process(DBSet db) { //UPDATE ISSUER this.issuer.setConfirmedBalance(this.issuer.getConfirmedBalance(db).subtract(this.fee), db); //UPDATE REFERENCE OF ISSUER this.issuer.setLastReference(this.signature, db); //INSERT INTO DATABASE long key = db.getAssetMap().add(this.asset); //ADD ASSETS TO OWNER this.asset.getOwner().setConfirmedBalance(key, new BigDecimal(this.asset.getQuantity()).setScale(8), db); //SET ORPHAN DATA db.getIssueAssetMap().set(this, key); } @Override public void orphan(DBSet db) { //UPDATE ISSUER this.issuer.setConfirmedBalance(this.issuer.getConfirmedBalance(db).add(this.fee), db); //UPDATE REFERENCE OF ISSUER this.issuer.setLastReference(this.reference, db); //DELETE FROM DATABASE long key = db.getIssueAssetMap().get(this); db.getAssetMap().delete(key); //REMOVE ASSETS FROM OWNER this.asset.getOwner().setConfirmedBalance(key, BigDecimal.ZERO.setScale(8), db); //DELETE ORPHAN DATA db.getIssueAssetMap().delete(this); } @Override public Account getCreator() { return this.issuer; } @Override public List<Account> getInvolvedAccounts() { List<Account> accounts = new ArrayList<Account>(); accounts.add(this.issuer); accounts.add(this.asset.getOwner()); return accounts; } @Override public boolean isInvolved(Account account) { String address = account.getAddress(); if(address.equals(this.issuer.getAddress()) || address.equals(this.asset.getOwner().getAddress())) { return true; } return false; } @Override public BigDecimal getAmount(Account account) { if(account.getAddress().equals(this.issuer.getAddress())) { return BigDecimal.ZERO.setScale(8).subtract(this.fee); } return BigDecimal.ZERO; } public static byte[] generateSignature(DBSet db, PrivateKeyAccount creator, Asset asset, BigDecimal fee, long timestamp) { byte[] data = new byte[0]; //WRITE TYPE byte[] typeBytes = Ints.toByteArray(ISSUE_ASSET_TRANSACTION); typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0); data = Bytes.concat(data, typeBytes); //WRITE TIMESTAMP byte[] timestampBytes = Longs.toByteArray(timestamp); timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0); data = Bytes.concat(data, timestampBytes); //WRITE REFERENCE data = Bytes.concat(data, creator.getLastReference(db)); //WRITE CREATOR data = Bytes.concat(data, creator.getPublicKey()); //WRITE ASSET data = Bytes.concat(data , asset.toBytes(false)); //WRITE FEE byte[] feeBytes = fee.unscaledValue().toByteArray(); byte[] fill = new byte[FEE_LENGTH - feeBytes.length]; feeBytes = Bytes.concat(fill, feeBytes); data = Bytes.concat(data, feeBytes); return Crypto.getInstance().sign(creator, data); } }