From 91ef2d0e9aa3be2ed182128bd5e7ce1d3bf3e15f Mon Sep 17 00:00:00 2001 From: hvarona Date: Tue, 13 Nov 2018 22:02:22 -0400 Subject: [PATCH] Added parseUri to BitocinManager --- .../manager/GeneralAccountManager.java | 64 ++++++++++++++++++- .../BitcoinUriParseRequest.java | 13 +++- 2 files changed, 74 insertions(+), 3 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 08c2e90..6b437c1 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.BitcoinUriParseRequest; import cy.agorise.crystalwallet.requestmanagers.CalculateBitcoinUriRequest; import cy.agorise.crystalwallet.requestmanagers.CreateBitcoinAccountRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; @@ -156,12 +157,13 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf this.validateAddress((ValidateBitcoinAddressRequest) request); }else if(request instanceof CalculateBitcoinUriRequest){ this.calculateUri((CalculateBitcoinUriRequest) request); + }else if(request instanceof BitcoinUriParseRequest){ + this.parseUri((BitcoinUriParseRequest) request); }else{ System.out.println("Invalid " +this.cryptoCoin.getLabel() + " request "); } } - } /** @@ -536,6 +538,66 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf request.validate(); } + private void parseUri(BitcoinUriParseRequest request){ + String uri = request.getUri(); + if(uri.indexOf(":")>0){ + String cryptoNet = uri.substring(0,uri.indexOf(":")); + if(cryptoNet.equalsIgnoreCase(this.cryptoCoin.getLabel())){ + try{ + Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); + request.setAddress(address.toString()); + request.setStatus(BitcoinUriParseRequest.StatusCode.VALID); + if(uri.indexOf("?")>0){ + try { + String[] parameters = uri.substring(uri.indexOf("?") + 1).split("&"); + for (String parameter : parameters) { + int idx = parameter.indexOf("="); + if (idx > 0 && parameter.substring(0, idx).equalsIgnoreCase("amount")) { + request.setAmount(Double.parseDouble(parameter.substring(idx + 1))); + } + } + }catch(Exception ignored){} + } + }catch(AddressFormatException ex){ + request.setStatus(BitcoinUriParseRequest.StatusCode.NOT_VALID); + } + + }else{ + request.setStatus(BitcoinUriParseRequest.StatusCode.NOT_VALID); + } + }else{ + if(uri.indexOf("?")>0){ + try{ + Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); + request.setAddress(address.toString()); + request.setStatus(BitcoinUriParseRequest.StatusCode.VALID); + try{ + String[] parameters = uri.substring(uri.indexOf("?")+1).split("&"); + for(String parameter : parameters){ + int idx = parameter.indexOf("="); + if(idx > 0 && parameter.substring(0,idx).equalsIgnoreCase("amount")){ + request.setAmount(Double.parseDouble(parameter.substring(idx+1))); + } + } + }catch(Exception ignored){} + + }catch(AddressFormatException ex){ + request.setStatus(BitcoinUriParseRequest.StatusCode.NOT_VALID); + } + }else{ + try{ + Address address = Address.fromBase58(this.cryptoCoin.getParameters(), request.getAddress()); + request.setAddress(address.toString()); + request.setStatus(BitcoinUriParseRequest.StatusCode.VALID); + + }catch(AddressFormatException ex){ + request.setStatus(BitcoinUriParseRequest.StatusCode.NOT_VALID); + } + } + } + request.validate(); + } + private List getUtxos(long accountId, CrystalDatabase db){ List answer = new ArrayList<>(); List bTGTxI = new ArrayList<>(); diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/BitcoinUriParseRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/BitcoinUriParseRequest.java index ad2a5b9..1c2e7be 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/BitcoinUriParseRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/BitcoinUriParseRequest.java @@ -22,17 +22,22 @@ public class BitcoinUriParseRequest extends CryptoNetInfoRequest { NOT_VALID } + private String uri; + private String address; private Double amount; private String memo; + + private StatusCode status = StatusCode.NOT_STARTED; - public BitcoinUriParseRequest(String uri) { - super(CryptoCoin.BITCOIN); + public BitcoinUriParseRequest(String uri, CryptoCoin cryptoCoin) { + super(cryptoCoin); this.address = ""; this.amount = -1.0; this.memo = ""; + this.uri = uri; } public String getAddress() { @@ -73,4 +78,8 @@ public class BitcoinUriParseRequest extends CryptoNetInfoRequest { public StatusCode getStatus() { return status; } + + public String getUri() { + return uri; + } }