Added parseUri to BitocinManager

This commit is contained in:
hvarona 2018-11-13 22:02:22 -04:00
parent 193f426739
commit 91ef2d0e9a
2 changed files with 74 additions and 3 deletions

View file

@ -38,6 +38,7 @@ 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.requestmanagers.BitcoinSendRequest; import cy.agorise.crystalwallet.requestmanagers.BitcoinSendRequest;
import cy.agorise.crystalwallet.requestmanagers.BitcoinUriParseRequest;
import cy.agorise.crystalwallet.requestmanagers.CalculateBitcoinUriRequest; import cy.agorise.crystalwallet.requestmanagers.CalculateBitcoinUriRequest;
import cy.agorise.crystalwallet.requestmanagers.CreateBitcoinAccountRequest; import cy.agorise.crystalwallet.requestmanagers.CreateBitcoinAccountRequest;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest;
@ -156,12 +157,13 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf
this.validateAddress((ValidateBitcoinAddressRequest) request); this.validateAddress((ValidateBitcoinAddressRequest) request);
}else if(request instanceof CalculateBitcoinUriRequest){ }else if(request instanceof CalculateBitcoinUriRequest){
this.calculateUri((CalculateBitcoinUriRequest) request); this.calculateUri((CalculateBitcoinUriRequest) request);
}else if(request instanceof BitcoinUriParseRequest){
this.parseUri((BitcoinUriParseRequest) request);
}else{ }else{
System.out.println("Invalid " +this.cryptoCoin.getLabel() + " request "); System.out.println("Invalid " +this.cryptoCoin.getLabel() + " request ");
} }
} }
} }
/** /**
@ -536,6 +538,66 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf
request.validate(); 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<BitcoinTransactionGTxIO> getUtxos(long accountId, CrystalDatabase db){ private List<BitcoinTransactionGTxIO> getUtxos(long accountId, CrystalDatabase db){
List<BitcoinTransactionGTxIO> answer = new ArrayList<>(); List<BitcoinTransactionGTxIO> answer = new ArrayList<>();
List<BitcoinTransactionGTxIO> bTGTxI = new ArrayList<>(); List<BitcoinTransactionGTxIO> bTGTxI = new ArrayList<>();

View file

@ -22,17 +22,22 @@ public class BitcoinUriParseRequest extends CryptoNetInfoRequest {
NOT_VALID NOT_VALID
} }
private String uri;
private String address; private String address;
private Double amount; private Double amount;
private String memo; private String memo;
private StatusCode status = StatusCode.NOT_STARTED; private StatusCode status = StatusCode.NOT_STARTED;
public BitcoinUriParseRequest(String uri) { public BitcoinUriParseRequest(String uri, CryptoCoin cryptoCoin) {
super(CryptoCoin.BITCOIN); super(cryptoCoin);
this.address = ""; this.address = "";
this.amount = -1.0; this.amount = -1.0;
this.memo = ""; this.memo = "";
this.uri = uri;
} }
public String getAddress() { public String getAddress() {
@ -73,4 +78,8 @@ public class BitcoinUriParseRequest extends CryptoNetInfoRequest {
public StatusCode getStatus() { public StatusCode getStatus() {
return status; return status;
} }
public String getUri() {
return uri;
}
} }