Skip to content

Commit

Permalink
Add Filecoin address validation when sending
Browse files Browse the repository at this point in the history
  • Loading branch information
simoarpe committed Jun 19, 2023
1 parent 0f57e02 commit c9adf52
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

import android.content.res.Resources;

import androidx.annotation.NonNull;

import org.chromium.base.ContextUtils;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
import org.chromium.brave_wallet.mojom.BlockchainToken;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.KeyringService;
Expand All @@ -32,11 +35,9 @@ public void validate(NetworkInfo selectedNetwork, KeyringService keyringService,
BlockchainRegistry blockchainRegistry, BraveWalletService braveWalletService,
String senderAccountAddress, String receiverAccountAddress,
Callbacks.Callback2<String, Boolean> callback) {
String senderAccountAddressLower =
senderAccountAddress.toLowerCase(Locale.getDefault());
String senderAccountAddressLower = senderAccountAddress.toLowerCase(Locale.ENGLISH);

String receiverAccountAddressLower =
receiverAccountAddress.toLowerCase(Locale.getDefault());
String receiverAccountAddressLower = receiverAccountAddress.toLowerCase(Locale.ENGLISH);

Resources resources = ContextUtils.getApplicationContext().getResources();

Expand All @@ -55,6 +56,15 @@ public void validate(NetworkInfo selectedNetwork, KeyringService keyringService,
}
callback.call(responseMsg, success);
});
} else if (coinType == CoinType.FIL) {
if (receiverAccountAddress.isEmpty()) {
return;
}
final boolean showErrorMessage = !isValidFilAddress(receiverAccountAddress);
final String errorMessage = showErrorMessage
? resources.getString(R.string.invalid_fil_address)
: "";
callback.call(errorMessage, showErrorMessage);
} else {
// Steps to validate:
// 1. Valid hex string
Expand All @@ -71,10 +81,8 @@ public void validate(NetworkInfo selectedNetwork, KeyringService keyringService,
if (!receiverAccountAddress.isEmpty()
&& bytesReceiverAccountAddress.length
!= VALID_ACCOUNT_ADDRESS_BYTE_LENGTH) {
int invalidAddressId = coinType == CoinType.FIL
? R.string.invalid_fil_address
: R.string.wallet_not_valid_eth_address;
callback.call(resources.getString(invalidAddressId), true);
callback.call(
resources.getString(R.string.wallet_not_valid_eth_address), true);
return;
}

Expand Down Expand Up @@ -120,6 +128,20 @@ public void validate(NetworkInfo selectedNetwork, KeyringService keyringService,
});
}

/**
* Checks if the Filecoin contract address provided is valid.
* @param address Lowercase Filecoin contract address.
* @return {@code true} if the Filecoin contract address is valid, {@code false} otherwise.
*/
private boolean isValidFilAddress(@NonNull final String address) {
if (!address.startsWith(BraveWalletConstants.FILECOIN_MAINNET)
&& !address.startsWith(BraveWalletConstants.FILECOIN_TESTNET)) {
return false;
}
// Secp256k have 41 address length and BLS keys have 86.
return (address.length() == 41 || address.length() == 86);
}

private void checkForKnowContracts(String receiverAccountAddressLower,
Callbacks.Callback2<String, Boolean> callback, Resources resources) {
assert mKnownContractAddresses != null;
Expand Down

0 comments on commit c9adf52

Please sign in to comment.