Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ENS and UD resolver #779

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ dependencies {
implementation 'dnsjava:dnsjava:2.1.9'
implementation 'org.jitsi:dnssecjava:1.2.0'
implementation 'org.slf4j:slf4j-nop:1.7.30'
implementation 'com.unstoppabledomains:resolution:3.0.0'
implementation 'com.github.brnunes:swipeablerecyclerview:1.0.2'

//noinspection GradleDependency
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/assets/licenses.html
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ <h3>zxcvbn4j (https://github.com/nulab/zxcvbn4j)</h3>
Copyright (c) 2014 Nulab Inc
<h3>slfj-nop - Simple Logging Facade for Java no-operation binding (https://www.slf4j.org/)</h3>
Copyright (c) 2004-2017 QOS.ch
<h3>resolution-java - Java library to resolve ENS & Unstoppable Domains</h3>
Copyright (c) 2020 Unstoppable Domains
<h3>The MIT License</h3>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/m2049r/xmrwallet/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ private void processNfcIntent(Intent intent) {
if (uri == null) {
Toast.makeText(this, getString(R.string.nfc_tag_read_undef), Toast.LENGTH_LONG).show();
} else {
BarcodeData bc = BarcodeData.fromString(uri.toString());
if (bc == null)
Toast.makeText(this, getString(R.string.nfc_tag_read_undef), Toast.LENGTH_LONG).show();
else
onUriScanned(bc);
BarcodeData.fromString(uri.toString(), data -> runOnUiThread(() -> {
if (data == null)
Toast.makeText(BaseActivity.this, getString(R.string.nfc_tag_read_undef), Toast.LENGTH_LONG).show();
else
onUriScanned(data);
}));
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions app/src/main/java/com/m2049r/xmrwallet/ScannerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.m2049r.xmrwallet.data.BarcodeData;

import me.dm7.barcodescanner.zxing.ZXingScannerView;
import timber.log.Timber;
Expand All @@ -36,7 +37,11 @@ public class ScannerFragment extends Fragment implements ZXingScannerView.Result
private OnScannedListener onScannedListener;

public interface OnScannedListener {
boolean onScanned(String qrCode);
void onScanned(String qrCode, ScannedCallbackListener listener);
}

public interface ScannedCallbackListener {
void onScanned(boolean success);
}

private ZXingScannerView mScannerView;
Expand All @@ -59,11 +64,11 @@ public void onResume() {
@Override
public void handleResult(Result rawResult) {
if ((rawResult.getBarcodeFormat() == BarcodeFormat.QR_CODE)) {
if (onScannedListener.onScanned(rawResult.getText())) {
return;
} else {
Toast.makeText(getActivity(), getString(R.string.send_qr_address_invalid), Toast.LENGTH_SHORT).show();
}
onScannedListener.onScanned(rawResult.getText(), (success) -> requireActivity().runOnUiThread(() -> {
if (!success) {
Toast.makeText(getActivity(), getString(R.string.send_qr_address_invalid), Toast.LENGTH_SHORT).show();
}
}));
} else {
Toast.makeText(getActivity(), getString(R.string.send_qr_invalid), Toast.LENGTH_SHORT).show();
}
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,17 +905,18 @@ public void onScan() {
}

@Override
public boolean onScanned(String qrCode) {
public void onScanned(String qrCode, ScannerFragment.ScannedCallbackListener listener) {
// #gurke
BarcodeData bcData = BarcodeData.fromString(qrCode);
if (bcData != null) {
popFragmentStack(null);
Timber.d("AAA");
onUriScanned(bcData);
return true;
} else {
return false;
}
BarcodeData.fromString(qrCode, (bcData) -> {
if (bcData != null) {
popFragmentStack(null);
Timber.d("AAA");
onUriScanned(bcData);
listener.onScanned(true);
} else {
listener.onScanned(false);
}
});
}

OnUriScannedListener onUriScannedListener = null;
Expand All @@ -937,6 +938,7 @@ void onUriScanned(BarcodeData barcodeData) {
}
}

@SuppressLint("MissingSuperCall")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Timber.d("onRequestPermissionsResult()");
Expand Down
49 changes: 47 additions & 2 deletions app/src/main/java/com/m2049r/xmrwallet/data/BarcodeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import android.net.Uri;

import com.m2049r.xmrwallet.util.OpenAliasHelper;
import com.unstoppabledomains.exceptions.ns.NamingServiceException;
import com.unstoppabledomains.resolution.DomainResolution;
import com.unstoppabledomains.resolution.Resolution;
import com.unstoppabledomains.resolution.naming.service.NamingServiceType;

import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -38,6 +42,10 @@ public enum Security {
OA_DNSSEC
}

public interface Listener {
void onParseComplete(BarcodeData data);
}

final public Crypto asset;
final public List<Crypto> ambiguousAssets;
final public String address;
Expand Down Expand Up @@ -169,7 +177,7 @@ static public BarcodeData parseUri(String uriString) {
}


static public BarcodeData fromString(String qrCode) {
static public void fromString(String qrCode, Listener listener) {
BarcodeData bcData = parseUri(qrCode);
if (bcData == null) {
// maybe it's naked?
Expand All @@ -179,7 +187,44 @@ static public BarcodeData fromString(String qrCode) {
// check for OpenAlias
bcData = parseOpenAlias(qrCode, false);
}
return bcData;
if (bcData == null) {
// Check for UD domain (async)
parseUD(qrCode, listener);
} else {
listener.onParseComplete(bcData);
}
}

static public void parseUD(String udString, Listener listener) {
Timber.d("parseUD=%s", udString);
if (udString == null) listener.onParseComplete(null);

new Thread(() -> {
DomainResolution resolution = Resolution.builder()
.providerUrl(NamingServiceType.ENS, "https://cloudflare-eth.com")
.build();

String address = null;
Crypto crypto = null;
for (Crypto currentCrypto: Crypto.values()) {
try {
address = resolution.getAddress(udString, currentCrypto.getSymbol().toLowerCase());
crypto = currentCrypto;
break;
} catch (NamingServiceException e) {
Timber.d(e.getLocalizedMessage());
}
}
if (crypto == null) {
Timber.d("Unsupported UD address %s", udString);
listener.onParseComplete(null);
} else if (!crypto.validate(address)) {
Timber.d("%s address invalid", crypto);
listener.onParseComplete(null);
} else {
listener.onParseComplete(new BarcodeData(crypto, address, udString, null, null, Security.NORMAL));
}
}).start();
}

static public BarcodeData parseOpenAlias(String oaString, boolean dnssec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
import com.m2049r.xmrwallet.util.validator.BitcoinAddressType;
import com.m2049r.xmrwallet.util.validator.BitcoinAddressValidator;
import com.m2049r.xmrwallet.util.validator.EthAddressValidator;
import com.unstoppabledomains.exceptions.ns.NamingServiceException;
import com.unstoppabledomains.resolution.DomainResolution;
import com.unstoppabledomains.resolution.Resolution;
import com.unstoppabledomains.resolution.naming.service.NamingServiceType;

import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -166,11 +170,7 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
etAddress.getEditText().setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
String enteredAddress = etAddress.getEditText().getText().toString().trim();
String dnsOA = dnsFromOpenAlias(enteredAddress);
Timber.d("OpenAlias is %s", dnsOA);
if (dnsOA != null) {
processOpenAlias(dnsOA);
}
goToOpenAlias(enteredAddress);
}
});
etAddress.getEditText().addTextChangedListener(new TextWatcher() {
Expand Down Expand Up @@ -216,6 +216,8 @@ public void afterTextChanged(Editable editable) {
}
if (possibleCryptos.isEmpty()) {
Timber.d("other");
// Makes the height of tvXmrTo consistent when invisible
tvXmrTo.setText(Html.fromHtml(getString(R.string.info_xmrto_help_xmr)));
tvXmrTo.setVisibility(View.INVISIBLE);
sendListener.setMode(SendFragment.Mode.XMR);
}
Expand All @@ -237,21 +239,21 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
if (clip == null) return;
// clean it up
final String address = clip.replaceAll("( +)|(\\r?\\n?)", "");
BarcodeData bc = BarcodeData.fromString(address);
if (bc != null) {
processScannedData(bc);
final EditText et = etAddress.getEditText();
et.setSelection(et.getText().length());
etAddress.requestFocus();
} else {
Toast.makeText(getActivity(), getString(R.string.send_address_invalid), Toast.LENGTH_SHORT).show();
}
BarcodeData.fromString(address, (bc) -> requireActivity().runOnUiThread(() -> {
if (bc != null) {
processScannedData(bc);
final EditText et = etAddress.getEditText();
et.setSelection(et.getText().length());
etAddress.requestFocus();
} else {
Toast.makeText(getActivity(), getString(R.string.send_address_invalid), Toast.LENGTH_SHORT).show();
}
}));
});

etNotes = view.findViewById(R.id.etNotes);
etNotes.getEditText().setRawInputType(InputType.TYPE_CLASS_TEXT);
etNotes.getEditText().

setOnEditorActionListener((v, actionId, event) -> {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN))
|| (actionId == EditorInfo.IME_ACTION_DONE)) {
Expand Down Expand Up @@ -315,42 +317,104 @@ private void updateCryptoButtons(boolean noAddress) {
tvXmrTo.setText(Html.fromHtml(getString(R.string.info_xmrto_ambiguous)));
tvXmrTo.setVisibility(View.VISIBLE);
} else {
// Makes the height of tvXmrTo consistent when invisible
tvXmrTo.setText(Html.fromHtml(getString(R.string.info_xmrto_help_xmr)));
tvXmrTo.setVisibility(View.INVISIBLE);
}
if (noAddress) {
selectedCrypto(Crypto.XMR);
}
}

private void goToOpenAlias(String enteredAddress) {
String dnsOA = dnsFromOpenAlias(enteredAddress);
Timber.d("OpenAlias is %s", dnsOA);
if (dnsOA != null) {
processOpenAlias(dnsOA);
} else if (enteredAddress.length() == 0 || checkAddressNoError()) {
etAddress.setErrorEnabled(false);
} else {
// Not all UD address match Patterns.DOMAIN_NAME (eg. .888, .x)
processUD(enteredAddress);
}
}

private void processOpenAlias(String dnsOA) {
if (resolvingOA) return; // already resolving - just wait
sendListener.popBarcodeData();
if (dnsOA != null) {
resolvingOA = true;
etAddress.setError(getString(R.string.send_address_resolve_openalias));
OpenAliasHelper.resolve(dnsOA, new OpenAliasHelper.OnResolvedListener() {
@Override
public void onResolved(Map<Crypto, BarcodeData> dataMap) {
resolvingOA = false;
BarcodeData barcodeData = dataMap.get(Crypto.XMR);
if (barcodeData == null) barcodeData = dataMap.get(Crypto.BTC);
if (barcodeData != null) {
Timber.d("Security=%s, %s", barcodeData.security.toString(), barcodeData.address);
processScannedData(barcodeData);
} else {
etAddress.setError(getString(R.string.send_address_not_openalias));
Timber.d("NO XMR OPENALIAS TXT FOUND");
resolvingOA = true;
etAddress.setError(getString(R.string.send_address_resolve_openalias));
OpenAliasHelper.resolve(dnsOA, new OpenAliasHelper.OnResolvedListener() {
@Override
public void onResolved(Map<Crypto, BarcodeData> dataMap) {
resolvingOA = false;
BarcodeData barcodeData = dataMap.get(Crypto.XMR);
if (barcodeData == null) barcodeData = dataMap.get(Crypto.BTC);
if (barcodeData != null) {
Timber.d("Security=%s, %s", barcodeData.security.toString(), barcodeData.address);
processScannedData(barcodeData);
} else {
Timber.d("NO XMR OPENALIAS TXT FOUND");
processUD(dnsOA);
}
}

@Override
public void onFailure() {
resolvingOA = false;
Timber.e("OA FAILED");
processUD(dnsOA);
}
});
}

private void processUD(String udString) {
sendListener.popBarcodeData();

DomainResolution resolution = Resolution.builder()
.providerUrl(NamingServiceType.ENS, "https://cloudflare-eth.com")
.build();
final boolean[] domainIsUD = {false};
final String[] address = {null};
final Crypto[] crypto = {null};
etAddress.setError(getString(R.string.send_address_resolve_ud));
new Thread(() -> {
for (Crypto currentCrypto: Crypto.values()) {
try {
address[0] = resolution.getAddress(udString, currentCrypto.getSymbol().toLowerCase());
crypto[0] = currentCrypto;
domainIsUD[0] = true;
break;
} catch (NamingServiceException e) {
Timber.d(e.getLocalizedMessage());
switch (e.getCode()) {
case UnknownCurrency:
case RecordNotFound:
domainIsUD[0] = true;
break;
default:
domainIsUD[0] = false;
break;
}
}
}

@Override
public void onFailure() {
resolvingOA = false;
requireActivity().runOnUiThread(() -> {
if (domainIsUD[0]) {
if (crypto[0] == null) {
Timber.d("Unsupported UD address %s", udString);
etAddress.setError(getString(R.string.send_address_no_ud_records));
} else {
BarcodeData barcodeData = new BarcodeData(crypto[0], address[0], udString,
null, null, BarcodeData.Security.NORMAL);
processScannedData(barcodeData);
}
} else {
Timber.d("Non ENS / UD address %s", udString);
etAddress.setError(getString(R.string.send_address_not_openalias));
Timber.e("OA FAILED");
}
});
} // else ignore
}).start();
}

private boolean checkAddressNoError() {
Expand Down Expand Up @@ -405,11 +469,7 @@ public boolean onValidateFields() {
if (!checkAddressNoError()) {
shakeAddress();
String enteredAddress = etAddress.getEditText().getText().toString().trim();
String dnsOA = dnsFromOpenAlias(enteredAddress);
Timber.d("OpenAlias is %s", dnsOA);
if (dnsOA != null) {
processOpenAlias(dnsOA);
}
goToOpenAlias(enteredAddress);
return false;
}

Expand Down
Loading