diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java index 9e78385..5240ea1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -52,7 +52,8 @@ public class CrystalApplication extends Application { public static final String BITCOIN_SERVER_URLS[] ={ - "https://insight.bitpay.com/" + "https://insight.bitpay.com/", + "https://testnet.blockexplorer.com/" }; public static final CryptoCurrency BITCOIN_CURRENCY = new CryptoCurrency("BTC",CryptoNet.BITCOIN,8); diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java index f987a69..638075d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java @@ -29,6 +29,9 @@ public interface CryptoNetAccountDao { @Query("SELECT cna.* FROM crypto_net_account cna WHERE seed_id = :seedId") List getAllCryptoNetAccountBySeed( long seedId); + @Query("SELECT cna.* FROM crypto_net_account cna WHERE crypto_net == 'BITCOIN'") + LiveData> getAllBitcoins(); + @Query("SELECT * FROM crypto_net_account WHERE id = :accountId") LiveData getByIdLiveData( long accountId); diff --git a/app/src/main/java/cy/agorise/crystalwallet/enums/CryptoCoin.java b/app/src/main/java/cy/agorise/crystalwallet/enums/CryptoCoin.java index e7bed2a..328f8a4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/enums/CryptoCoin.java +++ b/app/src/main/java/cy/agorise/crystalwallet/enums/CryptoCoin.java @@ -13,7 +13,7 @@ import java.util.List; */ public enum CryptoCoin implements Serializable { - BITCOIN(CryptoNet.BITCOIN,"BTC",8,0,NetworkParameters.fromID(NetworkParameters.ID_MAINNET)), + BITCOIN(CryptoNet.BITCOIN,"BTC",8,0,NetworkParameters.fromID(NetworkParameters.ID_TESTNET)), BITCOIN_TEST(CryptoNet.BITCOIN_TEST,"BTC",8,1,NetworkParameters.fromID(NetworkParameters.ID_TESTNET)), LITECOIN(CryptoNet.LITECOIN,"LTC",8,2,null), DASH(CryptoNet.DASH,"DASH",8,5,null), diff --git a/app/src/main/java/cy/agorise/crystalwallet/enums/SeedType.java b/app/src/main/java/cy/agorise/crystalwallet/enums/SeedType.java index 505a13f..557085c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/enums/SeedType.java +++ b/app/src/main/java/cy/agorise/crystalwallet/enums/SeedType.java @@ -6,5 +6,6 @@ package cy.agorise.crystalwallet.enums; public enum SeedType { BIP39, - BRAINKEY + BRAINKEY, + WIF } diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java index bc94748..8a78d5a 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java @@ -402,7 +402,7 @@ public class ReceiveTransactionFragment extends DialogFragment implements UIVali // @Override // public void onCarryOut() { // if (addressRequest.getStatus() == NextBitcoinAccountAddressRequest.StatusCode.SUCCEEDED){ - final CalculateBitcoinUriRequest uriRequest = new CalculateBitcoinUriRequest(cryptoCoin, cryptoNetAccount, getContext()); + final CalculateBitcoinUriRequest uriRequest = new CalculateBitcoinUriRequest(cryptoCoin, cryptoNetAccount, getContext(), amount); uriRequest.setListener(new CryptoNetInfoRequestListener(){ @Override diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java index be671e9..d48b28d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java @@ -810,8 +810,8 @@ public class SendTransactionFragment extends DialogFragment implements UIValidat } //Is not a bitshares QR - CryptoCoin cryptoCoin = CryptoCoin.getByCryptoNet(this.cryptoNetAccount.getCryptoNet()).get(0); - final BitcoinUriParseRequest bitcoinUriParseRequest = new BitcoinUriParseRequest(result.getText(), cryptoCoin); + //CryptoCoin cryptoCoin = CryptoCoin.getByCryptoNet(this.cryptoNetAccount.getCryptoNet()).get(0); + final BitcoinUriParseRequest bitcoinUriParseRequest = new BitcoinUriParseRequest(result.getText(), CryptoCoin.BITCOIN); bitcoinUriParseRequest.setListener(new CryptoNetInfoRequestListener() { @Override diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java index 6b437c1..6af2174 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -527,9 +527,9 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf uri.append(address.getAddress()); } - if(request.getCurrency()!= null){ + if(request.getAmount() > 0 ){ uri.append("?amount="); - uri.append(request.getAmount()); + uri.append(Double.toString(request.getAmount())); } System.out.println("GeneralAccountMAnager uri calculated : " + uri.toString()); @@ -542,14 +542,15 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf String uri = request.getUri(); if(uri.indexOf(":")>0){ String cryptoNet = uri.substring(0,uri.indexOf(":")); - if(cryptoNet.equalsIgnoreCase(this.cryptoCoin.getLabel())){ + if(cryptoNet.equalsIgnoreCase(this.cryptoCoin.name().toLowerCase())){ try{ - Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); + int parameterIndex =uri.indexOf("?"); + Address address = Address.fromBase58(this.cryptoCoin.getParameters(), uri.substring(uri.indexOf(":")+1,parameterIndex>0?parameterIndex:uri.length())); request.setAddress(address.toString()); request.setStatus(BitcoinUriParseRequest.StatusCode.VALID); - if(uri.indexOf("?")>0){ + if(parameterIndex>0){ try { - String[] parameters = uri.substring(uri.indexOf("?") + 1).split("&"); + String[] parameters = uri.substring(parameterIndex + 1).split("&"); for (String parameter : parameters) { int idx = parameter.indexOf("="); if (idx > 0 && parameter.substring(0, idx).equalsIgnoreCase("amount")) { @@ -566,13 +567,14 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf request.setStatus(BitcoinUriParseRequest.StatusCode.NOT_VALID); } }else{ - if(uri.indexOf("?")>0){ + int parameterIndex =uri.indexOf("?"); + if(parameterIndex>0){ try{ - Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); + Address address = Address.fromBase58(this.cryptoCoin.getParameters(), uri.substring(uri.indexOf(":")+1,parameterIndex>0?parameterIndex:uri.length())); request.setAddress(address.toString()); request.setStatus(BitcoinUriParseRequest.StatusCode.VALID); try{ - String[] parameters = uri.substring(uri.indexOf("?")+1).split("&"); + String[] parameters = uri.substring(parameterIndex+1).split("&"); for(String parameter : parameters){ int idx = parameter.indexOf("="); if(idx > 0 && parameter.substring(0,idx).equalsIgnoreCase("amount")){ @@ -586,7 +588,7 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf } }else{ try{ - Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); + Address address = Address.fromBase58(this.cryptoCoin.getParameters(), uri); request.setAddress(address.toString()); request.setStatus(BitcoinUriParseRequest.StatusCode.VALID); diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java b/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java index 9485654..326e167 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java @@ -9,12 +9,14 @@ import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.util.DiffUtil; +import org.bitcoinj.core.Base58; import org.bitcoinj.core.ECKey; import org.bitcoinj.crypto.HDKeyDerivation; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Arrays; import cy.agorise.crystalwallet.enums.SeedType; import cy.agorise.crystalwallet.models.seed.BIP39; @@ -115,8 +117,6 @@ public class AccountSeed { BufferedReader reader = null; switch (type) { case BRAINKEY: - - try { reader = new BufferedReader(new InputStreamReader(context.getAssets().open("brainkeydict.txt"), "UTF-8")); @@ -130,6 +130,7 @@ public class AccountSeed { } catch (IOException e) { e.printStackTrace(); } + break; case BIP39: try { reader = new BufferedReader(new InputStreamReader(context.getAssets().open("bip39dict.txt"), "UTF-8")); @@ -139,6 +140,7 @@ public class AccountSeed { } catch (IOException e) { e.printStackTrace(); } + break; } return null; } @@ -149,6 +151,15 @@ public class AccountSeed { return new BrainKey(this.mMasterSeed,0).getPrivateKey(); case BIP39: return HDKeyDerivation.createMasterPrivateKey(new BIP39(mId, mMasterSeed).getSeed()); + case WIF: + byte[] decoded = Base58.decode(this.mMasterSeed); + byte[] privKey = Arrays.copyOfRange(decoded, 1, decoded.length - 4); + byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length); + //TODO calculate chekcsum + while(privKey.length>32){ + privKey = Arrays.copyOfRange(privKey,0,privKey.length-1); + } + return ECKey.fromPrivate(privKey); } return null; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/BitcoinCryptoNetVerifier.java b/app/src/main/java/cy/agorise/crystalwallet/network/BitcoinCryptoNetVerifier.java index f29ea0d..d38bcfb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/network/BitcoinCryptoNetVerifier.java +++ b/app/src/main/java/cy/agorise/crystalwallet/network/BitcoinCryptoNetVerifier.java @@ -22,6 +22,8 @@ public class BitcoinCryptoNetVerifier extends CryptoNetVerifier{ if(value.equals(cryptoCoin.getParameters().getGenesisBlock().getHashAsString())){ CryptoNetManager.verifiedCryptoNetURL(cryptoCoin.getCryptoNet(), url, System.currentTimeMillis() - startTime); + }else{ + System.out.println("BitcoinCryptoNetVerifier bad genesis block " + value); } //TODO bad genesis block }else{ diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java index 7388e78..f664751 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java @@ -27,6 +27,13 @@ public class CalculateBitcoinUriRequest extends CryptoNetInfoRequest { this.context = context; } + public CalculateBitcoinUriRequest(CryptoCoin coin, CryptoNetAccount account, Context context, double amount) { + super(coin); + this.account = account; + this.context = context; + this.amount = amount; + } + public CalculateBitcoinUriRequest(CryptoCoin coin, CryptoNetAccount account, CryptoCurrency currency, double amount, Context context) { super(coin); this.account = account; diff --git a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java index 6e1bbce..8d3b05c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java @@ -170,7 +170,15 @@ public class CrystalWalletService extends LifecycleService { } }); - + final LiveData> cryptoNetAccountList = db.cryptoNetAccountDao().getAllBitcoins(); + cryptoNetAccountList.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List cryptoNetAccounts) { + for(CryptoNetAccount nextCryptoNetAccount : cryptoNetAccounts) { + generalAccountManager.loadAccountFromDB(nextCryptoNetAccount,thisService); + } + } + }); /*while(this.keepLoadingAccountTransactions){ try{ diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetAccountAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetAccountAdapter.java index 1917046..e08d0ae 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetAccountAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetAccountAdapter.java @@ -2,6 +2,7 @@ package cy.agorise.crystalwallet.views; import android.arch.lifecycle.ViewModelProviders; import android.content.Context; +import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -30,6 +31,11 @@ public class CryptoNetAccountAdapter extends ArrayAdapter { this.data = objects; } + @Override + public CryptoNetAccount getItem(int position) { + return data.get(position); + } + @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getView(position, convertView, parent);