Merge pull request #1356 from AlphaWallet/Improve-URL-Tests

Improve URL parse tests
pull/1360/head
James Brown 5 years ago committed by GitHub
commit d7bbbc2296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/src/main/java/com/alphawallet/app/ui/DappBrowserFragment.java
  2. 67
      app/src/main/java/com/alphawallet/app/util/QRURLParser.java
  3. 13
      app/src/test/java/com/alphawallet/app/QRExtractorTest.java

@ -1288,10 +1288,12 @@ public class DappBrowserFragment extends Fragment implements OnSignTransactionLi
case FUNCTION_CALL:
//EIP681 function call. TODO: create function call confirmation. For now treat same way as tokenscript function call
break;
case OTHER: //treat as URL
case URL:
loadUrlRemote(qrCode);
break;
case OTHER:
qrCode = null;
break;
}
}
break;

@ -5,10 +5,9 @@ import com.alphawallet.app.entity.EthereumProtocolParser;
import com.alphawallet.app.entity.QrUrlResult;
import com.alphawallet.app.ui.widget.entity.ENSHandler;
import java.util.regex.Matcher;
import java.net.URL;
import java.util.regex.Pattern;
import static com.alphawallet.app.entity.EthereumProtocolParser.ADDRESS_LENGTH;
import static org.web3j.crypto.WalletUtils.isValidAddress;
/**
@ -41,28 +40,34 @@ public class QRURLParser {
private QRURLParser() { }
private static String extractAddress(String str) {
private static String extractAddress(String str)
{
String address;
try {
if (!isValidAddress(str))
try
{
if (isValidAddress(str))
{
Matcher matcher = findAddress.matcher(str);
if (matcher.find())
{
str = matcher.group(1) + matcher.group(2);
}
return str;
}
if (isValidAddress(str))
String[] split = str.split("[/&@?=]");
address = split.length > 0 ? split[0] : null;
//is it a valid ethereum Address?
if (address == null || isValidAddress(address))
return address;
if (couldBeENS(address))
{
address = str.substring(0, ADDRESS_LENGTH);
return address;
}
else
{
String[] split = str.split("[/&@?=]");
address = split.length > 0 ? split[0] : null;
address = "";
}
} catch (StringIndexOutOfBoundsException ex) {
}
catch (StringIndexOutOfBoundsException ex)
{
return null;
}
@ -98,7 +103,16 @@ public class QRURLParser {
}
else
{
result = new QrUrlResult(url, EIP681Type.URL); //resolve to URL
try
{
//looks like a URL?
new URL(url);
result = new QrUrlResult(url, EIP681Type.URL); //resolve to URL
}
catch (Exception e)
{
result = new QrUrlResult(url, EIP681Type.OTHER);
}
}
}
@ -109,10 +123,29 @@ public class QRURLParser {
QrUrlResult result = parse(url);
if (result == null) {
if (result == null || result.type == EIP681Type.OTHER) {
return null;
}
return result.getAddress();
}
private static boolean couldBeENS(String address)
{
String[] split = address.split("[.]");
if (split.length > 1)
{
String extension = split[split.length - 1];
switch (extension)
{
case "eth":
case "xyz":
return true;
default:
break;
}
}
return false;
}
}

@ -138,7 +138,7 @@ public class QRExtractorTest {
// Too many ':'
result = parser.parse("something:coin:0x0000000000000000000000000000000000000XyZ?k1=v1&k2=v2");
assertTrue(result == null);
assertTrue(result.type == EIP681Type.OTHER);
//Test EIP681
result = parser.parse("ethereum:0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359?value=2.014e18");
@ -171,5 +171,16 @@ public class QRExtractorTest {
assertTrue(result.getGasPrice().equals(BigInteger.valueOf(700000)));
assertTrue(result.getGasLimit().equals(BigInteger.valueOf(27500)));
assertTrue(new BigInteger("1000",10).equals(result.getValue()));
result = parser.parse("www.duckduckgo.com"); //should't parse URL without protocol
assertTrue(result.type == EIP681Type.OTHER);
result = parser.parse("https://www.alphawallet.com");
System.out.println(result.getAddress());
assertTrue(result.type == EIP681Type.URL);
result = parser.parse("http://www.alphawallet.com");
System.out.println(result.getAddress());
assertTrue(result.type == EIP681Type.URL);
}
}
Loading…
Cancel
Save