From 2ea6f12b3cebbc75ebae6941bdd65cecfe815ce1 Mon Sep 17 00:00:00 2001 From: catbref Date: Wed, 6 Jun 2018 21:16:50 +0100 Subject: [PATCH] First stub files for conversion to data access layer, etc. --- src/data/account/Account.java | 47 ++++++++++++ src/data/account/GenesisAccount.java | 9 +++ src/data/account/PublicKeyAccount.java | 27 +++++++ src/data/transaction/GenesisTransaction.java | 37 +++++++++ src/data/transaction/Transaction.java | 79 ++++++++++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 src/data/account/Account.java create mode 100644 src/data/account/GenesisAccount.java create mode 100644 src/data/account/PublicKeyAccount.java create mode 100644 src/data/transaction/GenesisTransaction.java create mode 100644 src/data/transaction/Transaction.java diff --git a/src/data/account/Account.java b/src/data/account/Account.java new file mode 100644 index 00000000..af480bea --- /dev/null +++ b/src/data/account/Account.java @@ -0,0 +1,47 @@ +package data.account; + +public class Account { + + // Properties + protected String address; + protected byte[] reference; + + // Constructors + + protected Account() { + } + + public Account(String address) { + this.address = address; + } + + // Getters/Setters + + public String getAddress() { + return this.address; + } + + public byte[] getReference() { + return this.reference; + } + + public void setReference(byte[] reference) { + this.reference = reference; + } + + // Comparison + + @Override + public boolean equals(Object b) { + if (!(b instanceof Account)) + return false; + + return this.getAddress().equals(((Account) b).getAddress()); + } + + @Override + public int hashCode() { + return this.getAddress().hashCode(); + } + +} diff --git a/src/data/account/GenesisAccount.java b/src/data/account/GenesisAccount.java new file mode 100644 index 00000000..fdd0c4bb --- /dev/null +++ b/src/data/account/GenesisAccount.java @@ -0,0 +1,9 @@ +package data.account; + +public final class GenesisAccount extends PublicKeyAccount { + + public GenesisAccount() { + super(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 }); + } + +} diff --git a/src/data/account/PublicKeyAccount.java b/src/data/account/PublicKeyAccount.java new file mode 100644 index 00000000..fd554d7e --- /dev/null +++ b/src/data/account/PublicKeyAccount.java @@ -0,0 +1,27 @@ +package data.account; + +import qora.crypto.Crypto; + +public class PublicKeyAccount extends Account { + + // Properties + protected byte[] publicKey; + + // Constructors + + public PublicKeyAccount(byte[] publicKey) { + super(Crypto.toAddress(publicKey)); + + this.publicKey = publicKey; + } + + protected PublicKeyAccount() { + } + + // Getters/Setters + + public byte[] getPublicKey() { + return this.publicKey; + } + +} diff --git a/src/data/transaction/GenesisTransaction.java b/src/data/transaction/GenesisTransaction.java new file mode 100644 index 00000000..0a6f5730 --- /dev/null +++ b/src/data/transaction/GenesisTransaction.java @@ -0,0 +1,37 @@ +package data.transaction; + +import java.math.BigDecimal; + +import data.account.Account; +import data.account.GenesisAccount; + +public class GenesisTransaction extends Transaction { + + // Properties + private Account recipient; + private BigDecimal amount; + + // Constructors + + public GenesisTransaction(String recipient, BigDecimal amount, long timestamp, byte[] signature) { + super(TransactionType.GENESIS, BigDecimal.ZERO, new GenesisAccount(), timestamp, signature); + + this.recipient = new Account(recipient); + this.amount = amount; + } + + public GenesisTransaction(String recipient, BigDecimal amount, long timestamp) { + this(recipient, amount, timestamp, null); + } + + // Getters/Setters + + public Account getRecipient() { + return this.recipient; + } + + public BigDecimal getAmount() { + return this.amount; + } + +} diff --git a/src/data/transaction/Transaction.java b/src/data/transaction/Transaction.java new file mode 100644 index 00000000..443456fd --- /dev/null +++ b/src/data/transaction/Transaction.java @@ -0,0 +1,79 @@ +package data.transaction; + +import java.math.BigDecimal; +import java.util.Map; +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.toMap; + +import data.account.PublicKeyAccount; + +public abstract class Transaction { + + // Transaction types + public enum TransactionType { + GENESIS(1), PAYMENT(2), REGISTER_NAME(3), UPDATE_NAME(4), SELL_NAME(5), CANCEL_SELL_NAME(6), BUY_NAME(7), CREATE_POLL(8), VOTE_ON_POLL(9), ARBITRARY( + 10), ISSUE_ASSET(11), TRANSFER_ASSET(12), CREATE_ASSET_ORDER(13), CANCEL_ASSET_ORDER(14), MULTIPAYMENT(15), DEPLOY_AT(16), MESSAGE(17); + + public final int value; + + private final static Map map = stream(TransactionType.values()).collect(toMap(type -> type.value, type -> type)); + + TransactionType(int value) { + this.value = value; + } + + public static TransactionType valueOf(int value) { + return map.get(value); + } + } + + // Properties shared with all transaction types + protected TransactionType type; + protected PublicKeyAccount creator; + protected long timestamp; + protected byte[] reference; + protected BigDecimal fee; + protected byte[] signature; + + // Constructors + + public Transaction(TransactionType type, BigDecimal fee, PublicKeyAccount creator, long timestamp, byte[] reference, byte[] signature) { + this.fee = fee; + this.type = type; + this.creator = creator; + this.timestamp = timestamp; + this.reference = reference; + this.signature = signature; + } + + public Transaction(TransactionType type, BigDecimal fee, PublicKeyAccount creator, long timestamp, byte[] reference) { + this(type, fee, creator, timestamp, reference, null); + } + + // Getters/setters + + public TransactionType getType() { + return this.type; + } + + public PublicKeyAccount getCreator() { + return this.creator; + } + + public long getTimestamp() { + return this.timestamp; + } + + public byte[] getReference() { + return this.reference; + } + + public BigDecimal getFee() { + return this.fee; + } + + public byte[] getSignature() { + return this.signature; + } + +}