mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 12:29:51 +00:00
feat(token_validation): Improve flow for adding new tokens across wallets
This change: - Implements check to see if a token is already added, preventing duplicates - Triggers dialog warning if its a duplicate token - Takes EVM Chains contract adddress case-insensitivity when making checks for potential scams.
This commit is contained in:
parent
150becb679
commit
6fccd784ec
37 changed files with 124 additions and 3 deletions
|
@ -197,8 +197,8 @@ abstract class EVMChainWalletBase
|
|||
for (var token in erc20Currencies) {
|
||||
bool isPotentialScam = false;
|
||||
|
||||
bool isWhitelisted =
|
||||
getDefaultTokenContractAddresses.any((element) => element == token.contractAddress);
|
||||
bool isWhitelisted = getDefaultTokenContractAddresses
|
||||
.any((element) => element.toLowerCase() == token.contractAddress.toLowerCase());
|
||||
|
||||
final tokenSymbol = token.title.toUpperCase();
|
||||
|
||||
|
@ -213,6 +213,16 @@ abstract class EVMChainWalletBase
|
|||
token.iconPath = null;
|
||||
await token.save();
|
||||
}
|
||||
|
||||
// For fixing wrongly classified tokens
|
||||
if (!isPotentialScam && token.isPotentialScam) {
|
||||
token.isPotentialScam = false;
|
||||
final iconPath = CryptoCurrency.all
|
||||
.firstWhere((element) => element.title.toUpperCase() == token.symbol.toUpperCase())
|
||||
.iconPath;
|
||||
token.iconPath = iconPath;
|
||||
await token.save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -654,6 +664,11 @@ abstract class EVMChainWalletBase
|
|||
List<Erc20Token> get erc20Currencies => evmChainErc20TokensBox.values.toList();
|
||||
|
||||
Future<void> addErc20Token(Erc20Token token) async {
|
||||
if (evmChainErc20TokensBox.values.any((element) =>
|
||||
element.contractAddress.toLowerCase() == token.contractAddress.toLowerCase())) {
|
||||
throw Exception('Token already exists');
|
||||
}
|
||||
|
||||
String? iconPath;
|
||||
|
||||
if ((token.iconPath == null || token.iconPath!.isEmpty) && !token.isPotentialScam) {
|
||||
|
|
|
@ -212,4 +212,11 @@ class CWEthereum extends Ethereum {
|
|||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultEthereumErc20Tokens().initialErc20Tokens.map((e) => e.contractAddress).toList();
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final ethereumWallet = wallet as EthereumWallet;
|
||||
return ethereumWallet.erc20Currencies.any((element) => element.contractAddress.toLowerCase() == contractAddress.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -211,4 +211,10 @@ class CWPolygon extends Polygon {
|
|||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultPolygonErc20Tokens().initialPolygonErc20Tokens.map((e) => e.contractAddress).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final polygonWallet = wallet as PolygonWallet;
|
||||
return polygonWallet.erc20Currencies.any((element) => element.contractAddress.toLowerCase() == contractAddress.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -155,4 +155,10 @@ class CWSolana extends Solana {
|
|||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultSPLTokens().initialSPLTokens.map((e) => e.mintAddress).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final solanaWallet = wallet as SolanaWallet;
|
||||
return solanaWallet.splTokenCurrencies.any((element) => element.mintAddress == contractAddress);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -200,6 +200,23 @@ class _EditTokenPageBodyState extends State<EditTokenPageBody> {
|
|||
onPressed: () async {
|
||||
if (_formKey.currentState!.validate() &&
|
||||
(!_showDisclaimer || _disclaimerChecked)) {
|
||||
final isTokenAlreadyAdded = await widget.homeSettingsViewModel
|
||||
.checkIfTokenIsAlreadyAdded(_contractAddressController.text);
|
||||
if (isTokenAlreadyAdded) {
|
||||
showPopUp<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertWithOneAction(
|
||||
alertTitle: S.current.warning,
|
||||
alertContent: S.of(context).token_already_exists,
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () => Navigator.of(dialogContext).pop(),
|
||||
);
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final isWhitelisted = await widget.homeSettingsViewModel
|
||||
.checkIfTokenIsWhitelisted(_contractAddressController.text);
|
||||
|
||||
|
|
|
@ -138,4 +138,10 @@ class CWTron extends Tron {
|
|||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultTronTokens().initialTronTokens.map((e) => e.contractAddress).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final tronWallet = wallet as TronWallet;
|
||||
return tronWallet.tronTokenCurrencies.any((element) => element.contractAddress == contractAddress);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,6 +126,31 @@ abstract class HomeSettingsViewModelBase with Store {
|
|||
}
|
||||
}
|
||||
|
||||
@action
|
||||
Future<bool> checkIfTokenIsAlreadyAdded(String contractAddress) async {
|
||||
if (_balanceViewModel.wallet.type == WalletType.ethereum) {
|
||||
return ethereum!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.polygon) {
|
||||
return polygon!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.solana) {
|
||||
return solana!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.tron) {
|
||||
return tron!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.zano) {
|
||||
return zano!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> deleteToken(CryptoCurrency token) async {
|
||||
try {
|
||||
|
@ -297,7 +322,7 @@ abstract class HomeSettingsViewModelBase with Store {
|
|||
required bool isEthereum,
|
||||
}) async {
|
||||
final uri = Uri.https(
|
||||
"api.etherscan.io",
|
||||
"api.etherscan.io",
|
||||
"/v2/api",
|
||||
{
|
||||
"chainid": isEthereum ? "1" : "137",
|
||||
|
|
|
@ -136,4 +136,10 @@ class CWZano extends Zano {
|
|||
Map<String, List<int>> debugCallLength() {
|
||||
return api.debugCallLength();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final zanoWallet = wallet as ZanoWallet;
|
||||
return zanoWallet.zanoAssets.values.any((element) => element?.assetId == contractAddress);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "بقشيش:",
|
||||
"to": "ل",
|
||||
"today": "اليوم",
|
||||
"token_already_exists": "رمز موجود بالفعل",
|
||||
"token_contract_address": "عنوان عقد الرمز",
|
||||
"token_decimal": "رمز عشري",
|
||||
"token_name": "اسم الرمز ، على سبيل المثال: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Tip:",
|
||||
"to": "Да",
|
||||
"today": "Днес",
|
||||
"token_already_exists": "Токена вече съществува",
|
||||
"token_contract_address": "Адрес на токен договор",
|
||||
"token_decimal": "Токен десетичен",
|
||||
"token_name": "Име на токена, напр.: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Spropitné:",
|
||||
"to": "Na",
|
||||
"today": "Dnes",
|
||||
"token_already_exists": "Token již existuje",
|
||||
"token_contract_address": "Adresa tokenové smlouvy",
|
||||
"token_decimal": "Token v desítkové soustavě",
|
||||
"token_name": "Název tokenu např.: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Hinweis:",
|
||||
"to": "Zu",
|
||||
"today": "Heute",
|
||||
"token_already_exists": "Token existiert bereits",
|
||||
"token_contract_address": "Token-Contract-Adresse",
|
||||
"token_decimal": "Token-Dezimalzahl",
|
||||
"token_name": "Token-Name, z. B.: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Tip:",
|
||||
"to": "To",
|
||||
"today": "Today",
|
||||
"token_already_exists": "Token already exists",
|
||||
"token_contract_address": "Token contract address",
|
||||
"token_decimal": "Token decimal",
|
||||
"token_name": "Token name eg: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Consejo:",
|
||||
"to": "A",
|
||||
"today": "Hoy",
|
||||
"token_already_exists": "Token ya existe",
|
||||
"token_contract_address": "Dirección de contrato de token",
|
||||
"token_decimal": "Token decimal",
|
||||
"token_name": "Nombre del token, por ejemplo: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Pourboire:",
|
||||
"to": "À",
|
||||
"today": "Aujourd'hui",
|
||||
"token_already_exists": "Le jeton existe déjà",
|
||||
"token_contract_address": "Adresse du contrat de token",
|
||||
"token_decimal": "Décimales de token",
|
||||
"token_name": "Nom du token, par exemple : Tether",
|
||||
|
|
|
@ -901,6 +901,7 @@
|
|||
"tip": "Tukwici:",
|
||||
"to": "Zuwa",
|
||||
"today": "Yau",
|
||||
"token_already_exists": "Alama an riga an wanzu",
|
||||
"token_contract_address": "Adireshin kwangilar Token",
|
||||
"token_decimal": "Alamar ƙima",
|
||||
"token_name": "Alamar sunan misali: Tether",
|
||||
|
|
|
@ -901,6 +901,7 @@
|
|||
"tip": "टिप:",
|
||||
"to": "को",
|
||||
"today": "आज",
|
||||
"token_already_exists": "टोकन पहले से मौजूद है",
|
||||
"token_contract_address": "टोकन अनुबंध पता",
|
||||
"token_decimal": "सांकेतिक दशमलव",
|
||||
"token_name": "टोकन नाम जैसे: टीथर",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Savjet:",
|
||||
"to": "Do",
|
||||
"today": "Danas",
|
||||
"token_already_exists": "Token već postoji",
|
||||
"token_contract_address": "Adresa ugovora tokena",
|
||||
"token_decimal": "Token decimalni",
|
||||
"token_name": "Naziv tokena npr.: Tether",
|
||||
|
|
|
@ -897,6 +897,7 @@
|
|||
"tip": "Թեյավճար",
|
||||
"to": "Դեպի",
|
||||
"today": "Այսօր",
|
||||
"token_already_exists": "Նշանն արդեն գոյություն ունի",
|
||||
"token_contract_address": "Token-ի պայմանագրի հասցե",
|
||||
"token_decimal": "Token-ի տասանիշ",
|
||||
"token_name": "Token-ի անուն, օրինակ՝ Tether",
|
||||
|
|
|
@ -902,6 +902,7 @@
|
|||
"tip": "Tip:",
|
||||
"to": "Ke",
|
||||
"today": "Hari ini",
|
||||
"token_already_exists": "Token sudah ada",
|
||||
"token_contract_address": "Alamat kontrak token",
|
||||
"token_decimal": "Desimal token",
|
||||
"token_name": "Nama token misalnya: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Suggerimento:",
|
||||
"to": "A",
|
||||
"today": "Oggi",
|
||||
"token_already_exists": "Il token esiste già",
|
||||
"token_contract_address": "Indirizzo del contratto token",
|
||||
"token_decimal": "Decimale del token",
|
||||
"token_name": "Nome del token, ad esempio: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "ヒント: ",
|
||||
"to": "に",
|
||||
"today": "今日",
|
||||
"token_already_exists": "トークンはすでに存在します",
|
||||
"token_contract_address": "トークンコントラクトアドレス",
|
||||
"token_decimal": "トークン10進数",
|
||||
"token_name": "トークン名 例: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "팁:",
|
||||
"to": "받는 통화",
|
||||
"today": "오늘",
|
||||
"token_already_exists": "토큰이 이미 존재합니다",
|
||||
"token_contract_address": "토큰 계약 주소",
|
||||
"token_decimal": "토큰 소수 자릿수",
|
||||
"token_name": "토큰 이름 (예: Tether)",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "အကြံပြုချက်-",
|
||||
"to": "သို့",
|
||||
"today": "ဒီနေ့",
|
||||
"token_already_exists": "တိုကင်ရှိပြီးသား",
|
||||
"token_contract_address": "တိုကင်စာချုပ်လိပ်စာ",
|
||||
"token_decimal": "တိုကင်ဒဿမ",
|
||||
"token_name": "တိုကင်အမည် ဥပမာ- Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Tip:",
|
||||
"to": "Naar",
|
||||
"today": "Vandaag",
|
||||
"token_already_exists": "Token bestaat al",
|
||||
"token_contract_address": "Token contractadres",
|
||||
"token_decimal": "Token decimaal",
|
||||
"token_name": "Tokennaam bijv.: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "tip:",
|
||||
"to": "Do",
|
||||
"today": "Dzisiaj",
|
||||
"token_already_exists": "Token już istnieje",
|
||||
"token_contract_address": "Adres kontraktu tokena",
|
||||
"token_decimal": "Token dziesiętny",
|
||||
"token_name": "Nazwa tokena, np.: Tether",
|
||||
|
|
|
@ -901,6 +901,7 @@
|
|||
"tip": "Dica:",
|
||||
"to": "Para",
|
||||
"today": "Hoje",
|
||||
"token_already_exists": "Token já existe",
|
||||
"token_contract_address": "Endereço do contrato de token",
|
||||
"token_decimal": "Token decimal",
|
||||
"token_name": "Nome do token, por exemplo: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Совет:",
|
||||
"to": "К",
|
||||
"today": "Сегодня",
|
||||
"token_already_exists": "Токен уже существует",
|
||||
"token_contract_address": "Адрес контракта токена",
|
||||
"token_decimal": "Десятичный токен",
|
||||
"token_name": "Имя токена, например: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "เพิ่มค่าตอบแทน:",
|
||||
"to": "ถึง",
|
||||
"today": "วันนี้",
|
||||
"token_already_exists": "โทเค็นมีอยู่แล้ว",
|
||||
"token_contract_address": "ที่อยู่สัญญาโทเค็น",
|
||||
"token_decimal": "โทเค็นทศนิยม",
|
||||
"token_name": "ชื่อโทเค็น เช่น Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Tip:",
|
||||
"to": "Sa",
|
||||
"today": "Ngayon",
|
||||
"token_already_exists": "Mayroon nang token",
|
||||
"token_contract_address": "Address ng token contract",
|
||||
"token_decimal": "Token decimal",
|
||||
"token_name": "Pangalan ng token, halimbawa: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "Bahşiş:",
|
||||
"to": "İle",
|
||||
"today": "Bugün",
|
||||
"token_already_exists": "Token zaten var",
|
||||
"token_contract_address": "Token sözleşme adresi",
|
||||
"token_decimal": "Belirteç ondalık",
|
||||
"token_name": "Belirteç adı, örneğin: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Порада:",
|
||||
"to": "До",
|
||||
"today": "Сьогодні",
|
||||
"token_already_exists": "Маркер вже існує",
|
||||
"token_contract_address": "Адреса договору маркера",
|
||||
"token_decimal": "Токен десятковий",
|
||||
"token_name": "Назва токена, наприклад: Tether",
|
||||
|
|
|
@ -901,6 +901,7 @@
|
|||
"tip": "ٹپ:",
|
||||
"to": "to",
|
||||
"today": "آج",
|
||||
"token_already_exists": "ٹوکن پہلے ہی موجود ہے",
|
||||
"token_contract_address": "ٹوکن کنٹریکٹ ایڈریس",
|
||||
"token_decimal": "ٹوکن اعشاریہ",
|
||||
"token_name": "ٹوکن کا نام جیسے: Tether",
|
||||
|
|
|
@ -896,6 +896,7 @@
|
|||
"tip": "Mẹo:",
|
||||
"to": "ĐẾN",
|
||||
"today": "Hôm nay",
|
||||
"token_already_exists": "Mã thông báo đã tồn tại",
|
||||
"token_contract_address": "Địa chỉ hợp đồng token",
|
||||
"token_decimal": "Số thập phân của token",
|
||||
"token_name": "Tên token ví dụ: Tether",
|
||||
|
|
|
@ -900,6 +900,7 @@
|
|||
"tip": "Owó àfikún:",
|
||||
"to": "Si",
|
||||
"today": "Lénìí",
|
||||
"token_already_exists": "Token tẹlẹ wa",
|
||||
"token_contract_address": "Àmi guide adirẹsi",
|
||||
"token_decimal": "Àmi eleemewa",
|
||||
"token_name": "Orukọ àmi fun apẹẹrẹ: Tether",
|
||||
|
|
|
@ -899,6 +899,7 @@
|
|||
"tip": "提示:",
|
||||
"to": "到",
|
||||
"today": "今天",
|
||||
"token_already_exists": "令牌已经存在",
|
||||
"token_contract_address": "代币合约地址",
|
||||
"token_decimal": "令牌十进制",
|
||||
"token_name": "代币名称例如:Tether",
|
||||
|
|
|
@ -748,6 +748,7 @@ abstract class Ethereum {
|
|||
void setLedgerConnection(WalletBase wallet, ledger.LedgerConnection connection);
|
||||
Future<List<HardwareAccountData>> getHardwareWalletAccounts(LedgerViewModel ledgerVM, {int index = 0, int limit = 5});
|
||||
List<String> getDefaultTokenContractAddresses();
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress);
|
||||
}
|
||||
""";
|
||||
|
||||
|
@ -855,6 +856,7 @@ abstract class Polygon {
|
|||
void setLedgerConnection(WalletBase wallet, ledger.LedgerConnection connection);
|
||||
Future<List<HardwareAccountData>> getHardwareWalletAccounts(LedgerViewModel ledgerVM, {int index = 0, int limit = 5});
|
||||
List<String> getDefaultTokenContractAddresses();
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress);
|
||||
}
|
||||
""";
|
||||
|
||||
|
@ -1141,6 +1143,7 @@ abstract class Solana {
|
|||
List<int>? getValidationLength(CryptoCurrency type);
|
||||
double? getEstimateFees(WalletBase wallet);
|
||||
List<String> getDefaultTokenContractAddresses();
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress);
|
||||
}
|
||||
|
||||
""";
|
||||
|
@ -1219,6 +1222,7 @@ abstract class Tron {
|
|||
|
||||
void updateTronGridUsageState(WalletBase wallet, bool isEnabled);
|
||||
List<String> getDefaultTokenContractAddresses();
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress);
|
||||
}
|
||||
""";
|
||||
|
||||
|
@ -1293,6 +1297,7 @@ abstract class Zano {
|
|||
String getAddress(WalletBase wallet);
|
||||
bool validateAddress(String address);
|
||||
Map<String, List<int>> debugCallLength();
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress);
|
||||
}
|
||||
""";
|
||||
const zanoEmptyDefinition = 'Zano? zano;\n';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue