From 76a53c12e1d488ba0c6721cc9a500e6dfe9f7038 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 17 Oct 2018 21:56:40 -0400 Subject: [PATCH] - Added bitcoin generated addresses table --- .../crystalwallet/dao/BitcoinAddressDao.java | 27 ++++++ .../crystalwallet/dao/CrystalDatabase.java | 21 ++++- .../crystalwallet/models/BitcoinAddress.java | 90 +++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinAddressDao.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/models/BitcoinAddress.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinAddressDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinAddressDao.java new file mode 100644 index 0000000..f814f56 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinAddressDao.java @@ -0,0 +1,27 @@ +package cy.agorise.crystalwallet.dao; + +import android.arch.lifecycle.LiveData; +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; +import android.arch.persistence.room.Query; + +import cy.agorise.crystalwallet.models.BitcoinAddress; +import cy.agorise.crystalwallet.models.BitcoinTransaction; +import cy.agorise.crystalwallet.models.BitcoinTransactionExtended; + +/** + * Created by Henry Varona on 10/17/2018. + */ +@Dao +public interface BitcoinAddressDao { + + @Query("SELECT * FROM bitcoin_address") + LiveData getAll(); + + @Query("SELECT COUNT(*) FROM bitcoin_address ba WHERE ba.address = :address") + Boolean addressExists(String address); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertBitcoinAddresses(BitcoinAddress... addresses); +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java index 7dc40c0..8ee1f5d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java @@ -10,6 +10,7 @@ import android.content.Context; import cy.agorise.crystalwallet.dao.converters.Converters; import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.BitcoinAddress; import cy.agorise.crystalwallet.models.BitcoinTransaction; import cy.agorise.crystalwallet.models.BitcoinTransactionGTxIO; import cy.agorise.crystalwallet.models.BitsharesAccountNameCache; @@ -43,8 +44,9 @@ import cy.agorise.crystalwallet.models.GrapheneAccountInfo; CryptoCurrencyEquivalence.class, GeneralSetting.class, BitcoinTransaction.class, - BitcoinTransactionGTxIO.class -}, version = 5, exportSchema = false) + BitcoinTransactionGTxIO.class, + BitcoinAddress.class +}, version = 6, exportSchema = false) @TypeConverters({Converters.class}) public abstract class CrystalDatabase extends RoomDatabase { @@ -62,6 +64,7 @@ public abstract class CrystalDatabase extends RoomDatabase { public abstract CryptoCurrencyEquivalenceDao cryptoCurrencyEquivalenceDao(); public abstract GeneralSettingDao generalSettingDao(); public abstract BitcoinTransactionDao bitcoinTransactionDao(); + public abstract BitcoinAddressDao bitcoinAddressDao(); public static CrystalDatabase getAppDatabase(Context context) { if (instance == null) { @@ -72,6 +75,7 @@ public abstract class CrystalDatabase extends RoomDatabase { .addMigrations(MIGRATION_2_3) .addMigrations(MIGRATION_3_4) .addMigrations(MIGRATION_4_5) + .addMigrations(MIGRATION_5_6) .build(); } return instance; @@ -118,4 +122,17 @@ public abstract class CrystalDatabase extends RoomDatabase { +"FOREIGN KEY (bitcoin_transaction_id) REFERENCES bitcoin_transaction(crypto_coin_transaction_id) ON DELETE CASCADE)"); } }; + + static final Migration MIGRATION_5_6 = new Migration(5, 6) { + @Override + public void migrate(SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE bitcoin_address (" + +"account_id INTEGER NOT NULL," + +"address_index INTEGER NOT NULL," + +"is_change INTEGER NOT NULL," + +"address TEXT NOT NULL," + +"PRIMARY KEY (account_id, address_index)," + +"FOREIGN KEY (account_id) REFERENCES crypto_net_account(id) ON DELETE CASCADE)"); + } + }; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinAddress.java b/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinAddress.java new file mode 100644 index 0000000..2ecb704 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/models/BitcoinAddress.java @@ -0,0 +1,90 @@ +package cy.agorise.crystalwallet.models; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.ForeignKey; +import android.support.annotation.NonNull; + +/** + * Represents a Bitcoin derivated address + * + * Created by Henry Varona on 10/17/2018. + */ +@Entity( + tableName="bitcoin_address", + primaryKeys = {"account_id","address_index"}, + foreignKeys = { + @ForeignKey( + entity = CryptoNetAccount.class, + parentColumns = "id", + childColumns = "account_id", + onDelete = ForeignKey.CASCADE + ) + } +) +public class BitcoinAddress { + + /** + * The id of the account associated + */ + @ColumnInfo(name="account_id") + protected long accountId; + + /** + * The index of this address + */ + @ColumnInfo(name="address_index") + @NonNull protected long index; + + /** + * Whether or not this address is a change one + */ + @ColumnInfo(name="is_change") + @NonNull protected boolean isChange; + + /** + * Address + */ + @ColumnInfo(name="address") + @NonNull protected String address; + + public BitcoinAddress(long accountId, @NonNull long index, boolean isChange, String address) { + this.accountId = accountId; + this.index = index; + this.isChange = isChange; + this.address = address; + } + + public long getAccountId() { + return accountId; + } + + public void setAccountId(long accountId) { + this.accountId = accountId; + } + + @NonNull + public long getIndex() { + return index; + } + + public void setIndex(@NonNull long index) { + this.index = index; + } + + public boolean isChange() { + return isChange; + } + + public void setChange(boolean change) { + isChange = change; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +}