Skip to content

Commit

Permalink
Merge pull request #39 from kubacech/bugfix/BPUB-680-non-existent-etc…
Browse files Browse the repository at this point in the history
…-classifier

BPUB-680 using ETH classifier also for ETC
  • Loading branch information
generalbytes authored Feb 18, 2021
2 parents e4db0d6 + 5f82a1c commit 0e053ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public WalletTools() {

classificators.put(IClient.XMR, xmrwt);

WalletToolsETH wETH = new WalletToolsETH();
WalletToolsETH wETH = new WalletToolsETH(IClient.ETH);
WalletToolsETH wETC = new WalletToolsETH(IClient.ETC);
tools.put(IClient.ETH, wETH);
tools.put(IClient.ETC, wETH);
tools.put(IClient.ETC, wETC);
classificators.put(IClient.ETH, wETH);
classificators.put(IClient.ETC, wETC);
}

private IWalletTools getDefaultWalletTools() {
Expand Down Expand Up @@ -141,17 +143,17 @@ public Classification classify(String input, String cryptoCurrencyHint) {
Classification result = new Classification(Classification.TYPE_UNKNOWN);
if (IClient.BTC.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.BTC).classify(input);
}else if (IClient.LTC.equalsIgnoreCase(cryptoCurrencyHint)) {
} else if (IClient.LTC.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.LTC).classify(input);
}else if (IClient.DASH.equalsIgnoreCase(cryptoCurrencyHint)) {
} else if (IClient.DASH.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.DASH).classify(input);
}else if (IClient.XMR.equalsIgnoreCase(cryptoCurrencyHint)) {
} else if (IClient.XMR.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.XMR).classify(input);
}else if (IClient.ETH.equalsIgnoreCase(cryptoCurrencyHint)) {
} else if (IClient.ETH.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.ETH).classify(input);
}else if (IClient.ETC.equalsIgnoreCase(cryptoCurrencyHint)) {
} else if (IClient.ETC.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.ETC).classify(input);
}else if (IClient.BCH.equalsIgnoreCase(cryptoCurrencyHint)) {
} else if (IClient.BCH.equalsIgnoreCase(cryptoCurrencyHint)) {
result = classificators.get(IClient.BCH).classify(input);
}

Expand Down Expand Up @@ -216,13 +218,9 @@ public Classification classify(String input) {
return classificators.get(IClient.BCH).classify(input);
}


return result;
}




@Override
public Set<String> supportedCryptoCurrencies() {
final HashSet<String> result = new HashSet<String>();
Expand Down Expand Up @@ -252,5 +250,4 @@ public static int getCoinTypeByCryptoCurrency(String cryptoCurrency) {
}
return COIN_TYPE_BITCOIN;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public class WalletToolsETH implements IWalletTools {
public static final int XPRV = 0x0488ade4;
private static final String HEX_CHARS = "0123456789abcdef";

private String cryptoCurrency;

public WalletToolsETH() {
this(IClient.ETH);
}

public WalletToolsETH(String cryptoCurrency) {
this.cryptoCurrency = cryptoCurrency;
}

@Override
public String generateSeedMnemonicSeparatedBySpaces() {
Expand Down Expand Up @@ -139,7 +148,7 @@ public String getWalletAddressFromAccountPUB(String accountPUB, String cryptoCur
break;
}

if(standard == -1) {
if (standard == -1) {
throw new IllegalArgumentException("Unknown header bytes in pub: " + accountPUB);
} else {
if (isPub) {
Expand All @@ -153,10 +162,10 @@ public String getWalletAddressFromAccountPUB(String accountPUB, String cryptoCur
}
if (standard == STANDARD_BIP44) {
return Address.fromKey(MainNetParams.get(), walletKey, Script.ScriptType.P2PKH).toString();
}else if (standard == STANDARD_BIP84) {
} else if (standard == STANDARD_BIP84) {
return null; //TODO
}
}else {
} else {
return null;
}
}
Expand Down Expand Up @@ -249,7 +258,6 @@ public String getAccountPUB(IMasterPrivateKey master, String cryptoCurrency, int
return MasterPrivateKeyETH.serializePUB(masterKey,master.getStandard(), accountIndex, cryptoCurrency);
}


@Override
public String getWalletAddressFromPrivateKey(String privateKey, String cryptoCurrency) {
return null; //not supported yet
Expand All @@ -260,20 +268,17 @@ public ISignature sign(String privateKey, byte[] hashToSign, String cryptoCurren
return null;//not supported yet
}


public boolean isAddressValid(String address, String cryptoCurrency) {
if (address == null) {
return false;
}else{
} else {
if (!address.startsWith("0x") && !address.startsWith("XE")){
return false;
}
}

return isAddressValidInternal(address);
}


@Override
public Classification classify(String input, String cryptoCurrencyHint) {
return classify(input);
Expand Down Expand Up @@ -304,25 +309,24 @@ public Classification classify(String input) {
//most likely address lets check it
try {
if (isAddressValidInternal(input)) {
return new Classification(Classification.TYPE_ADDRESS, IClient.ETH,input);
return new Classification(Classification.TYPE_ADDRESS, cryptoCurrency, input);
}
} catch (AddressFormatException e) {
e.printStackTrace();
}
}else if (input.startsWith("XE")) {
} else if (input.startsWith("XE")) {
try {
if (isAddressValidInternal(input)) {
return new Classification(Classification.TYPE_ADDRESS,IClient.ETH,input);
return new Classification(Classification.TYPE_ADDRESS, cryptoCurrency, input);
}
} catch (AddressFormatException e) {
e.printStackTrace();
}
}else if (input.startsWith("xpub")) {
return new Classification(Classification.TYPE_PUB,IClient.ETH,input);
} else if (input.startsWith("xpub")) {
return new Classification(Classification.TYPE_PUB, cryptoCurrency, input);
}

return new Classification(Classification.TYPE_UNKNOWN);

}

private static boolean isAddressValidInternal(String address) {
Expand Down Expand Up @@ -357,7 +361,6 @@ public Set<String> supportedCryptoCurrencies() {
return result;
}


private static byte[] decodeAddressAsBytes(String address) {
if (address == null) {
return null;
Expand Down Expand Up @@ -422,7 +425,7 @@ private static String decodeAddressFromIBAN(String iban) {
String base36 = iban.substring(4);
BigInteger asBn = new BigInteger(base36, 36);
return encodeAddressToChecksummedAddress(padLeft(asBn.toString(16), 20));
}else{
} else {
return null;
}
}
Expand Down Expand Up @@ -471,11 +474,9 @@ private static int calculateModulus(String code) throws CheckDigitException {
total %= 97L;
}
}

return (int)(total % 97L);
}


private static String padLeft(String number, int bytes) {
String result = number;
while (result.length() < bytes * 2) {
Expand Down Expand Up @@ -514,7 +515,7 @@ private static String encodeAddressToChecksummedAddress(String address) {
checksumAddress += (addrChars[i] +"").toLowerCase();
}
}
return "0x"+checksumAddress;
return "0x" + checksumAddress;
}

private static String bytesToHexString(byte[] bytes) {
Expand Down Expand Up @@ -576,5 +577,4 @@ private String getAddress(ECKey ecKey) {
System.arraycopy(result,result.length - address.length, address,0, address.length);
return bytesToHexString(address);
}

}

0 comments on commit 0e053ec

Please sign in to comment.