From 5aa717501623c0dce7bac0249621c430791b8de3 Mon Sep 17 00:00:00 2001 From: hvarona Date: Sun, 18 Nov 2018 11:44:30 -0400 Subject: [PATCH 1/8] Implemented WIF Account Seed --- .../cy/agorise/crystalwallet/enums/SeedType.java | 3 ++- .../agorise/crystalwallet/models/AccountSeed.java | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) 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/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; } From 2e2ee010527ff582487b144d51fbc9044810841a Mon Sep 17 00:00:00 2001 From: hvarona Date: Sun, 18 Nov 2018 21:24:15 -0400 Subject: [PATCH 2/8] Change parseUri to compares with lowercase cryptocoin --- .../cy/agorise/crystalwallet/manager/GeneralAccountManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..a03fe92 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -542,7 +542,7 @@ 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.getLabel().toLowerCase())){ try{ Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); request.setAddress(address.toString()); From ce1df325d8eb136db52ba050eee43599bb942aa2 Mon Sep 17 00:00:00 2001 From: hvarona Date: Sun, 18 Nov 2018 21:30:42 -0400 Subject: [PATCH 3/8] Change parseUri to compares with lowercase cryptocoin --- .../cy/agorise/crystalwallet/manager/GeneralAccountManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a03fe92..a0f3911 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -542,7 +542,7 @@ 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().toLowerCase())){ + if(cryptoNet.equalsIgnoreCase(this.cryptoCoin.name().toLowerCase())){ try{ Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); request.setAddress(address.toString()); From 73ba419b66ddd75533bf67dd5e96e45b074e2a07 Mon Sep 17 00:00:00 2001 From: hvarona Date: Sun, 18 Nov 2018 21:41:29 -0400 Subject: [PATCH 4/8] Change parseUri to compares with lowercase cryptocoin --- .../manager/GeneralAccountManager.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 a0f3911..205baa8 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -544,12 +544,13 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf String cryptoNet = uri.substring(0,uri.indexOf(":")); 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); From dfca4b8c53583e58f170dbb2f63614e0e9429e67 Mon Sep 17 00:00:00 2001 From: hvarona Date: Sun, 18 Nov 2018 21:55:32 -0400 Subject: [PATCH 5/8] Change parseUri to compares with lowercase cryptocoin --- .../agorise/crystalwallet/manager/GeneralAccountManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 205baa8..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()); From ecf5335ea36368d4430a73cccd8a6ef9b07fd169 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 18 Nov 2018 22:26:05 -0400 Subject: [PATCH 6/8] - Fixing amount in receive fragment - Fixing send fragment to read bitcoin uris qr code --- .../fragments/ReceiveTransactionFragment.java | 2 +- .../crystalwallet/fragments/SendTransactionFragment.java | 4 ++-- .../requestmanagers/CalculateBitcoinUriRequest.java | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) 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/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; From 3ac651c0a4fc0bcc6c6789ecc685110c25b9f6d0 Mon Sep 17 00:00:00 2001 From: hvarona Date: Mon, 19 Nov 2018 22:25:34 -0400 Subject: [PATCH 7/8] change to testnet --- .../agorise/crystalwallet/application/CrystalApplication.java | 3 ++- .../main/java/cy/agorise/crystalwallet/enums/CryptoCoin.java | 2 +- .../crystalwallet/network/BitcoinCryptoNetVerifier.java | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) 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/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/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{ From 62541e1863c65e0065ab6f4aeec057e17f0bd9d5 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Mon, 19 Nov 2018 23:19:23 -0400 Subject: [PATCH 8/8] - Now the bitcoin accounts get their transactions loaded --- .../agorise/crystalwallet/dao/CryptoNetAccountDao.java | 3 +++ .../crystalwallet/service/CrystalWalletService.java | 10 +++++++++- .../crystalwallet/views/CryptoNetAccountAdapter.java | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) 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/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);