Add ETH send address checksum validation

This commit is contained in:
wchen342
2022-01-04 20:20:29 +02:00
parent 824853ed32
commit 5b99b86628
5 changed files with 61 additions and 18 deletions
@@ -928,8 +928,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
if (mActivityType == ActivityType.SEND) {
EditText toSendValueText = findViewById(R.id.to_send_value_text);
String to = toSendValueText.getText().toString();
TextView sendToValidation = findViewById(R.id.to_send_error_text);
if (to.isEmpty()) {
// TODO(sergz): some address validation
return;
}
if (mCurrentErcToken == null || mCurrentErcToken.contractAddress.isEmpty()) {
@@ -1221,10 +1221,10 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
String fromAccountAddress = mCustomAccountAdapter.getTitleAtPosition(
mAccountSpinner.getSelectedItemPosition());
mValidator.validate(mCurrentChainId, getErcTokenRegistry(), getBraveWalletService(),
fromAccountAddress, s.toString(), (String validationResult) -> {
setSendToValidationResult(validationResult, true);
mValidator.validate(mCurrentChainId, getKeyringController(), getErcTokenRegistry(),
getBraveWalletService(), fromAccountAddress, s.toString(),
(String validationResult, Boolean disableButton) -> {
setSendToValidationResult(validationResult, disableButton);
});
}
@@ -525,6 +525,25 @@ public class Utils {
return res;
}
public static String maybeHexStrToUpperCase(String value) {
if (value == null) {
return "";
}
String prefix = "0x";
boolean hasPrefix = false;
if (value.startsWith(prefix)) {
hasPrefix = true;
value = value.substring(2);
}
value = value.toUpperCase(Locale.getDefault());
if (hasPrefix)
return prefix + value;
else
return value;
}
public static TxData getTxData(
String nonce, String gasPrice, String gasLimit, String to, String value, byte[] data) {
TxData res = new TxData();
@@ -11,6 +11,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.ErcToken;
import org.chromium.brave_wallet.mojom.ErcTokenRegistry;
import org.chromium.brave_wallet.mojom.KeyringController;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.crypto_wallet.util.TokenUtils;
import org.chromium.mojo.bindings.Callbacks;
@@ -22,37 +23,43 @@ public class Validations {
public static class SendToAccountAddress {
private HashSet<String> mKnownContractAddresses;
private static int VALID_ACCOUNT_ADDRESS_BYTE_LENGTH = 20;
private boolean mIsKnowContracts;
public SendToAccountAddress() {}
public void validate(String chainId, ErcTokenRegistry ercTokenRegistry,
BraveWalletService braveWalletService, String senderAccountAddress,
String receiverAccountAddress, Callbacks.Callback1<String> callback) {
public void validate(String chainId, KeyringController keyringController,
ErcTokenRegistry ercTokenRegistry, BraveWalletService braveWalletService,
String senderAccountAddress, String receiverAccountAddress,
Callbacks.Callback2<String, Boolean> callback) {
// Steps to validate:
// 1. Valid hex string
// 0x <20 bytes | 40 chars>
// 2. Not equal to ours address
// 3. Not one of token's contract addresses
// 4. Has valid checksum
String senderAccountAddressLower =
senderAccountAddress.toLowerCase(Locale.getDefault());
String receiverAccountAddressLower =
receiverAccountAddress.toLowerCase(Locale.getDefault());
String receiverAccountAddressUpper =
Utils.maybeHexStrToUpperCase(receiverAccountAddress);
Resources resources = ContextUtils.getApplicationContext().getResources();
byte[] bytesReceiverAccountAddress = Utils.hexStrToNumberArray(receiverAccountAddress);
if (bytesReceiverAccountAddress.length != VALID_ACCOUNT_ADDRESS_BYTE_LENGTH) {
callback.call(resources.getString(R.string.wallet_not_valid_eth_address));
callback.call(resources.getString(R.string.wallet_not_valid_eth_address), true);
return;
}
if (senderAccountAddressLower.equals(receiverAccountAddressLower)) {
callback.call(resources.getString(R.string.wallet_same_address_error));
callback.call(resources.getString(R.string.wallet_same_address_error), true);
return;
}
mIsKnowContracts = false;
if (mKnownContractAddresses == null) {
assert braveWalletService != null;
assert ercTokenRegistry != null;
@@ -67,15 +74,31 @@ public class Validations {
} else {
checkForKnowContracts(receiverAccountAddressLower, callback, resources);
}
if (mIsKnowContracts) return;
keyringController.getChecksumEthAddress(receiverAccountAddress, checksum_address -> {
if (receiverAccountAddress.equals(checksum_address)) {
callback.call("", false);
} else if (receiverAccountAddressLower.equals(receiverAccountAddress)
|| receiverAccountAddressUpper.equals(receiverAccountAddress)) {
callback.call(
resources.getString(R.string.address_missing_checksum_warning), false);
} else {
callback.call(
resources.getString(R.string.address_not_valid_checksum_error), true);
}
});
}
private void checkForKnowContracts(String receiverAccountAddressLower,
Callbacks.Callback1<String> callback, Resources resources) {
Callbacks.Callback2<String, Boolean> callback, Resources resources) {
assert mKnownContractAddresses != null;
if (mKnownContractAddresses.contains(receiverAccountAddressLower)) {
callback.call(resources.getString(R.string.wallet_contract_address_error));
callback.call(resources.getString(R.string.wallet_contract_address_error), true);
mIsKnowContracts = true;
} else {
callback.call("");
callback.call("", false);
mIsKnowContracts = false;
}
}
@@ -69,11 +69,6 @@ public class BraveWalletResetPreference
return true;
}
@Override
public void onDetached() {
super.onDetached();
}
private void showBraveWalletResetDialog() {
LayoutInflater inflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@@ -2061,6 +2061,12 @@ Are you sure you want to do this?
<message name="IDS_TO_ADDRESS_EDIT" desc="Brave Wallet buy-send-swap section for address(hint)">
Address or URL
</message>
<message name="IDS_ADDRESS_MISSING_CHECKSUM_WARNING" desc="Brave Wallet send address missing checksum warning text">
Missing checksum information
</message>
<message name="IDS_ADDRESS_NOT_VALID_CHECKSUM_ERROR" desc="Brave Wallet send address invalid checksum error text">
Invalid checksum information
</message>
<!-- <message name="IDS_LIMIT" desc="Brave Wallet buy-send-swap section text">
Limit
</message>