From 8ff53de0ae07fd288f3111581f8a83c18f1fefdc Mon Sep 17 00:00:00 2001 From: hvarona Date: Sat, 10 Nov 2018 23:21:49 -0400 Subject: [PATCH] Added BitcoinUriRequest --- .../manager/GeneralAccountManager.java | 91 ++++++++++++++----- .../manager/SteemAccountManager.java | 4 +- .../CalculateBitcoinUriRequest.java | 69 ++++++++++++++ 3 files changed, 141 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java 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 9b49076..acf34f2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -38,6 +38,7 @@ import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.requestmanagers.BitcoinSendRequest; +import cy.agorise.crystalwallet.requestmanagers.CalculateBitcoinUriRequest; import cy.agorise.crystalwallet.requestmanagers.CreateBitcoinAccountRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequestsListener; @@ -153,6 +154,8 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf this.getNextAddress((NextBitcoinAccountAddressRequest) request); }else if(request instanceof ValidateBitcoinAddressRequest){ this.validateAddress((ValidateBitcoinAddressRequest) request); + }else if(request instanceof CalculateBitcoinUriRequest){ + this.calculateUri((CalculateBitcoinUriRequest) request); }else{ System.out.println("Invalid " +this.cryptoCoin.getLabel() + " request "); } @@ -461,28 +464,74 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf private void getNextAddress(NextBitcoinAccountAddressRequest request){ CrystalDatabase db = CrystalDatabase.getAppDatabase(request.getContext()); long index = db.bitcoinAddressDao().getLastExternalAddress(request.getAccount().getId()); - index++; - AccountSeed seed = db.accountSeedDao().findById(request.getAccount().getSeedId()); - DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey((DeterministicKey) seed.getPrivateKey(), - new ChildNumber(44, true)); - DeterministicKey coinKey = HDKeyDerivation.deriveChildKey(purposeKey, - new ChildNumber(cryptoCoin.getCoinNumber(), true)); - DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinKey, - new ChildNumber(request.getAccount().getAccountIndex(), true)); - DeterministicKey externalKey = HDKeyDerivation.deriveChildKey(accountKey, - new ChildNumber(0, false)); - ECKey addrKey = HDKeyDerivation.deriveChildKey(externalKey, new ChildNumber((int) index, true)); - BitcoinAddress address = new BitcoinAddress(); - address.setChange(false); - address.setAccountId(request.getAccount().getId()); - address.setIndex(index); - String addressString =addrKey.toAddress(this.cryptoCoin.getParameters()).toString(); - address.setAddress(addressString); - db.bitcoinAddressDao().insertBitcoinAddresses(address); - InsightApiGenerator.getTransactionFromAddress(this.cryptoCoin,addressString,true, null); + BitcoinAddress address = db.bitcoinAddressDao().getExternalByIndex(index); + if(db.bitcoinTransactionDao().getGtxIOByAddress(address.getAddress()).size()<=0){ + request.setAddress(address.getAddress()); + request.setStatus(NextBitcoinAccountAddressRequest.StatusCode.SUCCEEDED); + }else { + index++; + AccountSeed seed = db.accountSeedDao().findById(request.getAccount().getSeedId()); + DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey((DeterministicKey) seed.getPrivateKey(), + new ChildNumber(44, true)); + DeterministicKey coinKey = HDKeyDerivation.deriveChildKey(purposeKey, + new ChildNumber(cryptoCoin.getCoinNumber(), true)); + DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinKey, + new ChildNumber(request.getAccount().getAccountIndex(), true)); + DeterministicKey externalKey = HDKeyDerivation.deriveChildKey(accountKey, + new ChildNumber(0, false)); + ECKey addrKey = HDKeyDerivation.deriveChildKey(externalKey, new ChildNumber((int) index, true)); + address = new BitcoinAddress(); + address.setChange(false); + address.setAccountId(request.getAccount().getId()); + address.setIndex(index); + String addressString = addrKey.toAddress(this.cryptoCoin.getParameters()).toString(); + address.setAddress(addressString); + db.bitcoinAddressDao().insertBitcoinAddresses(address); + InsightApiGenerator.getTransactionFromAddress(this.cryptoCoin, addressString, true, null); - request.setAddress(addressString); - request.setStatus(NextBitcoinAccountAddressRequest.StatusCode.SUCCEEDED); + request.setAddress(addressString); + request.setStatus(NextBitcoinAccountAddressRequest.StatusCode.SUCCEEDED); + } + } + + private void calculateUri(CalculateBitcoinUriRequest request) { + StringBuilder uri = new StringBuilder(this.cryptoCoin.getLabel()+":"); + + CrystalDatabase db = CrystalDatabase.getAppDatabase(request.getContext()); + long index = db.bitcoinAddressDao().getLastExternalAddress(request.getAccount().getId()); + BitcoinAddress address = db.bitcoinAddressDao().getExternalByIndex(index); + if(db.bitcoinTransactionDao().getGtxIOByAddress(address.getAddress()).size()<=0){ + uri.append(address.getAddress()); + }else { + index++; + AccountSeed seed = db.accountSeedDao().findById(request.getAccount().getSeedId()); + DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey((DeterministicKey) seed.getPrivateKey(), + new ChildNumber(44, true)); + DeterministicKey coinKey = HDKeyDerivation.deriveChildKey(purposeKey, + new ChildNumber(cryptoCoin.getCoinNumber(), true)); + DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinKey, + new ChildNumber(request.getAccount().getAccountIndex(), true)); + DeterministicKey externalKey = HDKeyDerivation.deriveChildKey(accountKey, + new ChildNumber(0, false)); + ECKey addrKey = HDKeyDerivation.deriveChildKey(externalKey, new ChildNumber((int) index, true)); + address = new BitcoinAddress(); + address.setChange(false); + address.setAccountId(request.getAccount().getId()); + address.setIndex(index); + String addressString = addrKey.toAddress(this.cryptoCoin.getParameters()).toString(); + address.setAddress(addressString); + db.bitcoinAddressDao().insertBitcoinAddresses(address); + InsightApiGenerator.getTransactionFromAddress(this.cryptoCoin, addressString, true, null); + + uri.append(address.getAddress()); + } + if(request.getCurrency()!= null){ + uri.append("?amount="); + uri.append(request.getAmount()); + } + + request.setUri(uri.toString()); + request.validate(); } private List getUtxos(long accountId, CrystalDatabase db){ diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/SteemAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/SteemAccountManager.java index 997b1ce..ecef586 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/SteemAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/SteemAccountManager.java @@ -215,7 +215,7 @@ public class SteemAccountManager implements CryptoAccountManager, CryptoNetInfoR long idSeed = accountSeedDao.insertAccountSeed(seed); if (idSeed >= 0) { GrapheneAccount account = new GrapheneAccount(); - account.setCryptoNet(CryptoNet.BITSHARES); + account.setCryptoNet(CryptoNet.STEEM); account.setAccountIndex(0); account.setSeedId(idSeed); account.setAccountId(userAccount.getObjectId()); @@ -242,7 +242,7 @@ public class SteemAccountManager implements CryptoAccountManager, CryptoNetInfoR long idSeed = accountSeedDao.insertAccountSeed(seed); if (idSeed >= 0) { GrapheneAccount account = new GrapheneAccount(); - account.setCryptoNet(CryptoNet.BITSHARES); + account.setCryptoNet(CryptoNet.STEEM); account.setAccountIndex(0); account.setSeedId(idSeed); account.setAccountId(userAccount.getObjectId()); diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java new file mode 100644 index 0000000..7388e78 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CalculateBitcoinUriRequest.java @@ -0,0 +1,69 @@ +package cy.agorise.crystalwallet.requestmanagers; + +import android.content.Context; + +import cy.agorise.crystalwallet.enums.CryptoCoin; +import cy.agorise.crystalwallet.models.CryptoCurrency; +import cy.agorise.crystalwallet.models.CryptoNetAccount; + +/** + * This class validates that an account name exist, this can be used to verified the existing accounts + * or to verified if the name is available to create an Account + * + * Created by henry on 8/10/2017. + */ + +public class CalculateBitcoinUriRequest extends CryptoNetInfoRequest { + private CryptoNetAccount account; + private CryptoCurrency currency; + private double amount; + private Context context; + + private String uri; + + public CalculateBitcoinUriRequest(CryptoCoin coin, CryptoNetAccount account, Context context) { + super(coin); + this.account = account; + this.context = context; + } + + public CalculateBitcoinUriRequest(CryptoCoin coin, CryptoNetAccount account, CryptoCurrency currency, double amount, Context context) { + super(coin); + this.account = account; + this.currency = currency; + this.amount = amount; + this.context = context; + } + + public CryptoNetAccount getAccount() { + return account; + } + + public CryptoCurrency getCurrency() { + return currency; + } + + public double getAmount() { + return amount; + } + + public Context getContext() { + return context; + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + this.validate(); + } + + public void validate(){ + if ((this.uri != null)){ + this._fireOnCarryOutEvent(); + } + } + +}