- Import Account

- Create Account (miss seed)
- Created bitshares faucet api generator
- Created GrapheneAccount constructor and info loader
- Added funcion to daos
master
henry 2017-10-17 22:55:42 -04:00
parent 454644fc38
commit 119f4ea12b
9 changed files with 172 additions and 45 deletions

View File

@ -4,5 +4,19 @@ package cy.agorise.crystalwallet.apigenerator;
* Created by henry on 15/10/2017. * Created by henry on 15/10/2017.
*/ */
public class BitsharesFaucetApiGenerator { public abstract class BitsharesFaucetApiGenerator {
/**
* Class to register a new Bitshares Account
*
* @param accountName The name of the Account to be register
* @param ownerKey The owner key public address
* @param activeKey The active key public address
* @param memoKey the memo key public address
* @return The bitshares id of the registered account, or null
*/
public static String registerBitsharesAccount(String accountName, String ownerKey, String activeKey, String memoKey){
//TODO faucet function
return null;
}
} }

View File

@ -1,5 +1,6 @@
package cy.agorise.crystalwallet.apigenerator; package cy.agorise.crystalwallet.apigenerator;
import android.app.Application;
import android.arch.lifecycle.LiveData; import android.arch.lifecycle.LiveData;
import android.content.Context; import android.content.Context;
@ -7,7 +8,9 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.manager.BitsharesAccountManager;
import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinBalance;
import cy.agorise.crystalwallet.models.CryptoNetBalance; import cy.agorise.crystalwallet.models.CryptoNetBalance;
@ -295,8 +298,10 @@ public abstract class GrapheneApiGenerator {
thread.start(); thread.start();
} }
public static void subscribeBitsharesAccount(long accountId, final String accountBitsharesId, Context context){ public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId, final Context context){
final LiveData<List<CryptoCoinBalance>> balances = CrystalDatabase.getAppDatabase(context).cryptoCoinBalanceDao().getBalancesFromAccount(accountId); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
final LiveData<List<CryptoCoinBalance>> balances = db.cryptoCoinBalanceDao().getBalancesFromAccount(accountId);
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
SubscriptionListener balanceListener = new SubscriptionListener() { SubscriptionListener balanceListener = new SubscriptionListener() {
@Override @Override
public ObjectType getInterestObjectType() { public ObjectType getInterestObjectType() {
@ -313,12 +318,19 @@ public abstract class GrapheneApiGenerator {
if(balanceUpdate.owner.equals(accountBitsharesId)){ if(balanceUpdate.owner.equals(accountBitsharesId)){
boolean find = false; boolean find = false;
for(CryptoCoinBalance balance : balances.getValue()){ for(CryptoCoinBalance balance : balances.getValue()){
}
if(!find){
CryptoCoinBalance balance = new CryptoCoinBalance();
balanceDao.insertCryptoCoinBalance(balance);
} }
//TODO balance function //TODO balance function
//TODO refresh transactions BitsharesAccountManager.refreshAccountTransactions(accountId,context);
} }
} }
} }
} }
} }
}; };
@ -337,8 +349,8 @@ public abstract class GrapheneApiGenerator {
} }
public static void getAccountBalance(final long accountId, final String accountGrapheneId, final Context context){ public static void getAccountBalance(final long accountId, final String accountGrapheneId, final Context context){
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), null, new WitnessResponseListener() { WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), null, new WitnessResponseListener() {
@Override @Override
public void onSuccess(WitnessResponse response) { public void onSuccess(WitnessResponse response) {
@ -347,9 +359,11 @@ public abstract class GrapheneApiGenerator {
CryptoCoinBalance ccBalance = new CryptoCoinBalance(); CryptoCoinBalance ccBalance = new CryptoCoinBalance();
ccBalance.setAccountId(accountId); ccBalance.setAccountId(accountId);
ccBalance.setBalance(balance.getAmount().longValue()); ccBalance.setBalance(balance.getAmount().longValue());
//TODO find asset
//ccBalance.setCryptoCurrency();
//TODO cryptocyrrency
CrystalDatabase.getAppDatabase(context).cryptoCoinBalanceDao().insertCryptoCoinBalance(ccBalance); balanceDao.insertCryptoCoinBalance(ccBalance);
} }
} }

View File

@ -23,6 +23,9 @@ public interface CryptoNetAccountDao {
@Query("SELECT * FROM crypto_net_account") @Query("SELECT * FROM crypto_net_account")
List<CryptoNetAccount> getAll(); List<CryptoNetAccount> getAll();
@Query("SELECT * FROM crypto_net_account WHERE id = :accountId")
LiveData<CryptoNetAccount> getById( long accountId);
@Query("SELECT * FROM crypto_net_account WHERE crypto_net = 'BITSHARES'") @Query("SELECT * FROM crypto_net_account WHERE crypto_net = 'BITSHARES'")
LiveData<List<CryptoNetAccount>> getBitsharesAccounts(); LiveData<List<CryptoNetAccount>> getBitsharesAccounts();

View File

@ -23,7 +23,7 @@ public interface GrapheneAccountInfoDao {
LiveData<List<GrapheneAccountInfo>> getAll(); LiveData<List<GrapheneAccountInfo>> getAll();
@Query("SELECT * FROM graphene_account WHERE crypto_net_account_id = :cryptoNetAccountId") @Query("SELECT * FROM graphene_account WHERE crypto_net_account_id = :cryptoNetAccountId")
LiveData<GrapheneAccountInfo> getGrapheneAccountInfo(int cryptoNetAccountId); LiveData<GrapheneAccountInfo> getGrapheneAccountInfo(long cryptoNetAccountId);
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertGrapheneAccountInfo(GrapheneAccountInfo... accounts); public long[] insertGrapheneAccountInfo(GrapheneAccountInfo... accounts);

View File

@ -13,6 +13,7 @@ import java.util.Objects;
import cy.agorise.crystalwallet.apigenerator.ApiRequest; import cy.agorise.crystalwallet.apigenerator.ApiRequest;
import cy.agorise.crystalwallet.apigenerator.ApiRequestListener; import cy.agorise.crystalwallet.apigenerator.ApiRequestListener;
import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator;
import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator;
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequest; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequest;
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestsListener; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestsListener;
@ -20,12 +21,14 @@ import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendReque
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest;
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.CryptoNetAccount;
import cy.agorise.crystalwallet.models.GrapheneAccount; import cy.agorise.crystalwallet.models.GrapheneAccount;
import cy.agorise.crystalwallet.models.GrapheneAccountInfo;
import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Address;
import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.AssetAmount; import cy.agorise.graphenej.AssetAmount;
@ -36,6 +39,7 @@ import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.models.AccountProperties; import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.HistoricalTransfer; import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperationBuilder; import cy.agorise.graphenej.operations.TransferOperationBuilder;
/** /**
@ -45,10 +49,24 @@ import cy.agorise.graphenej.operations.TransferOperationBuilder;
public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetInfoRequestsListener { public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetInfoRequestsListener {
@Override @Override
public CryptoNetAccount createAccountFromSeed(CryptoNetAccount account, Context context) { public CryptoNetAccount createAccountFromSeed(CryptoNetAccount account, Context context) {
//TODO generate account keys if(account instanceof GrapheneAccount) {
//TODO register account faucet api
//TODO save account on DB GrapheneAccount grapheneAccount = (GrapheneAccount) account;
//TODO subscribe account
BitsharesFaucetApiGenerator.registerBitsharesAccount(grapheneAccount.getName(),
new Address(grapheneAccount.getOwnerKey(),"BTS").toString(),
new Address(grapheneAccount.getActiveKey(),"BTS").toString(),
new Address(grapheneAccount.getMemoKey(),"BTS").toString());
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
this.refreshAccountTransactions(account.getId(), context);
GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
return grapheneAccount;
}
return null; return null;
} }
@ -63,10 +81,14 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
}else if(grapheneAccount.getName() == null){ }else if(grapheneAccount.getName() == null){
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId());
} }
if(grapheneAccount == null) {
//TODO grapaheneAccount null, error fetching //TODO grapaheneAccount null, error fetching
return null;
}
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
//TODO save account on DB db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
this.refreshAccountTransactions(account.getId(), context); this.refreshAccountTransactions(account.getId(), context);
@ -80,12 +102,18 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
public void loadAccountFromDB(CryptoNetAccount account, Context context) { public void loadAccountFromDB(CryptoNetAccount account, Context context) {
if(account instanceof GrapheneAccount){ if(account instanceof GrapheneAccount){
GrapheneAccount grapheneAccount = (GrapheneAccount) account; GrapheneAccount grapheneAccount = (GrapheneAccount) account;
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getGrapheneAccountInfo(account.getId()).getValue());
if(grapheneAccount.getAccountId() == null){ if(grapheneAccount.getAccountId() == null){
grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName());
}else if(grapheneAccount.getName() == null){ }else if(grapheneAccount.getName() == null){
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId());
} }
if(grapheneAccount == null) {
//TODO grapaheneAccount null, error fetching //TODO grapaheneAccount null, error fetching
return;
}
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
this.refreshAccountTransactions(account.getId(),context); this.refreshAccountTransactions(account.getId(),context);
@ -163,19 +191,21 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){ private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){
Asset feeAsset = new Asset(sendRequest.getFeeAsset()); Asset feeAsset = new Asset(sendRequest.getFeeAsset());
UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId());
UserAccount toUserAccount = new UserAccount(sendRequest.getToAccount());
TransferOperationBuilder builder = new TransferOperationBuilder() TransferOperationBuilder builder = new TransferOperationBuilder()
.setSource(new UserAccount(sendRequest.getSourceAccount().getAccountId())) .setSource(fromUserAccount)
.setDestination(new UserAccount(sendRequest.getToAccount())) .setDestination(toUserAccount)
.setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getBaseAmount()), new Asset(sendRequest.getBaseAsset()))) .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getBaseAmount()), new Asset(sendRequest.getBaseAsset())))
.setFee(new AssetAmount(UnsignedLong.valueOf(sendRequest.getFeeAmount()), feeAsset)); .setFee(new AssetAmount(UnsignedLong.valueOf(sendRequest.getFeeAmount()), feeAsset));
if(sendRequest.getMemo() != null) {
//builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes()));
//TODO memo //TODO memo
}
ArrayList<BaseOperation> operationList = new ArrayList(); ArrayList<BaseOperation> operationList = new ArrayList();
operationList.add(builder.build()); operationList.add(builder.build());
//TODO get privateKey with seed model ECKey privateKey = sendRequest.getSourceAccount().getActiveKey();
LiveData<AccountSeed> seed = CrystalDatabase.getAppDatabase(sendRequest.getContext()).accountSeedDao().findById(sendRequest.getSourceAccount().getSeedId());
ECKey privateKey = new BrainKey(seed.getValue().getMasterSeed(),0).getPrivateKey();
Transaction transaction = new Transaction(privateKey, null, operationList); Transaction transaction = new Transaction(privateKey, null, operationList);
ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() {
@ -237,22 +267,29 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
return listener.account; return listener.account;
} }
public void refreshAccountTransactions(final long idAccount, Context context){ public static void refreshAccountTransactions(long idAccount, Context context){
final CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
final LiveData<List<CryptoCoinTransaction>> transactions = db.transactionDao().getByIdAccount(idAccount); LiveData<List<CryptoCoinTransaction>> transactions = db.transactionDao().getByIdAccount(idAccount);
final LiveData<GrapheneAccount> account = null; LiveData<CryptoNetAccount> account = db.cryptoNetAccountDao().getById(idAccount);
//TODO find account if(account.getValue().getCryptoNet() == CryptoNet.BITSHARES) {
GrapheneAccount grapheneAccount = new GrapheneAccount(account.getValue());
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getGrapheneAccountInfo(idAccount).getValue());
int start = transactions.getValue().size(); int start = transactions.getValue().size();
int limit = 50; int limit = 50;
int stop = start + limit; int stop = start + limit;
ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start,stop,limit,account.getValue(),db)); ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db));
GrapheneApiGenerator.getAccountTransaction(account.getValue().getName(),start,stop,limit,transactionRequest); GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest);
}
} }
private class TransactionRequestListener implements ApiRequestListener{ private static class TransactionRequestListener implements ApiRequestListener{
int start; int start;
int stop; int stop;

View File

@ -34,8 +34,8 @@ public class CryptoCoinBalance {
@ColumnInfo(name="account_id") @ColumnInfo(name="account_id")
private long mAccountId; private long mAccountId;
@ColumnInfo(name = "coin") @ColumnInfo(name = "crypto_currency_id")
private CryptoCoin mCoin; private long mCryptoCurrencyId;
@ColumnInfo(name = "balance") @ColumnInfo(name = "balance")
private long mBalance; private long mBalance;
@ -56,12 +56,12 @@ public class CryptoCoinBalance {
this.mAccountId = accountId; this.mAccountId = accountId;
} }
public CryptoCoin getCoin() { public long getCryptoCurrency() {
return mCoin; return mCryptoCurrencyId;
} }
public void setCoin(CryptoCoin coin) { public void setCryptoCurrency(long cryptoCurrencyId) {
this.mCoin = coin; this.mCryptoCurrencyId = cryptoCurrencyId;
} }
public long getBalance() { public long getBalance() {
@ -76,7 +76,7 @@ public class CryptoCoinBalance {
@Override @Override
public boolean areItemsTheSame( public boolean areItemsTheSame(
@NonNull CryptoCoinBalance oldBalance, @NonNull CryptoCoinBalance newBalance) { @NonNull CryptoCoinBalance oldBalance, @NonNull CryptoCoinBalance newBalance) {
return oldBalance.getCoin() == newBalance.getCoin(); return oldBalance.getCryptoCurrency() == newBalance.getCryptoCurrency();
} }
@Override @Override
public boolean areContentsTheSame( public boolean areContentsTheSame(
@ -94,7 +94,7 @@ public class CryptoCoinBalance {
if (mAccountId != that.mAccountId) return false; if (mAccountId != that.mAccountId) return false;
if (mBalance != that.mBalance) return false; if (mBalance != that.mBalance) return false;
return mCoin == that.mCoin; return mCryptoCurrencyId == that.mCryptoCurrencyId;
} }
} }

View File

@ -47,6 +47,16 @@ public class CryptoNetAccount {
@ColumnInfo(name = "crypto_net") @ColumnInfo(name = "crypto_net")
private CryptoNet mCryptoNet; private CryptoNet mCryptoNet;
public CryptoNetAccount() {
}
public CryptoNetAccount(long mId, long mSeedId, int mAccountIndex, CryptoNet mCryptoNet) {
this.mId = mId;
this.mSeedId = mSeedId;
this.mAccountIndex = mAccountIndex;
this.mCryptoNet = mCryptoNet;
}
public long getId() { public long getId() {
return mId; return mId;
} }

View File

@ -1,6 +1,9 @@
package cy.agorise.crystalwallet.models; package cy.agorise.crystalwallet.models;
import org.bitcoinj.core.ECKey;
/** /**
*
* Created by henry on 24/9/2017. * Created by henry on 24/9/2017.
*/ */
@ -10,6 +13,18 @@ public class GrapheneAccount extends CryptoNetAccount {
protected String name; protected String name;
protected String accountId; protected String accountId;
public GrapheneAccount() {
}
public GrapheneAccount(CryptoNetAccount account) {
super(account.getId(),account.getSeedId(),account.getAccountIndex(),account.getCryptoNet());
}
public void loadInfo(GrapheneAccountInfo info){
this.name = info.getName();
this.accountId = info.getAccountId();
}
public String getName() { public String getName() {
return name; return name;
} }
@ -25,4 +40,28 @@ public class GrapheneAccount extends CryptoNetAccount {
public void setAccountId(String accountId) { public void setAccountId(String accountId) {
this.accountId = accountId; this.accountId = accountId;
} }
/**
* Return the owner key, generates from the seed if it has not been generated. null if it can't be generated
*/
public ECKey getOwnerKey(){
//TODO implement
return null;
}
/**
* Return the active key, generates from the seed if it has not been generated. null if it can't be generated
*/
public ECKey getActiveKey(){
//TODO implement
return null;
}
/**
* Return the memo key, generates from the seed if it has not been generated. null if it can't be generated
*/
public ECKey getMemoKey(){
//TODO implement
return null;
}
} }

View File

@ -17,7 +17,7 @@ import android.arch.persistence.room.Index;
public class GrapheneAccountInfo { public class GrapheneAccountInfo {
@ColumnInfo(name = "crypto_net_account_id") @ColumnInfo(name = "crypto_net_account_id")
protected String cryptoNetAccountId; protected long cryptoNetAccountId;
@ColumnInfo(name = "account_name") @ColumnInfo(name = "account_name")
protected String name; protected String name;
@ -25,11 +25,21 @@ public class GrapheneAccountInfo {
@ColumnInfo(name = "account_id") @ColumnInfo(name = "account_id")
protected String accountId; protected String accountId;
public String getCryptoNetAccountId() { public GrapheneAccountInfo(long cryptoNetAccountId) {
this.cryptoNetAccountId = cryptoNetAccountId;
}
public GrapheneAccountInfo(GrapheneAccount account) {
this.cryptoNetAccountId = account.getId();
this.name = account.getName();
this.accountId = account.getAccountId();
}
public long getCryptoNetAccountId() {
return cryptoNetAccountId; return cryptoNetAccountId;
} }
public void setCryptoNetAccountId(String cryptoNetAccountId) { public void setCryptoNetAccountId(long cryptoNetAccountId) {
this.cryptoNetAccountId = cryptoNetAccountId; this.cryptoNetAccountId = cryptoNetAccountId;
} }