Files
hash-wallet/cw_base/lib/base_wallet.dart
T
2a74317aff Add Base Wallet (#2558)
* chore: Create Base package template

* chore: Update configuration scripts and dependencies to include 'base' coin support

* chore: Update pubspec.yaml, adjust SDK constraints, and add new dependencies

* feat: Add support for base wallet type and cryptocurrency in related configurations

* feat: Implement BaseWallet and DefaultBaseTokens classes for managing base cryptocurrency transactions

* feat: Add BaseTransactionHistory and BaseTransactionInfo classes for handling base cryptocurrency transaction data

* feat: Implement BaseClient, BaseWalletService, and BaseMnemonicIsIncorrectException

* feat: Setup base proxy for main package and adjust token class name

* feat: Setup initial base node configuration

* feat: Integrate support for base wallet type across various functions and services

* feat: Add base support to exchange and outputs

* feat: Add base wallet type integration to wallet creation and node management

* feat: Add base support to transaction details and transaction list item

* feat: Implement BaseURI class and add base support across wallet creation and keys view models

* feat: Add support for BaseScan preference in settings store

* feat: Integrate Base support into various workflows and documentation

* feat: Addingbase config to windows and secret key support

* feat: Add base support to settings and address handling

* chore: Move walletTypeToCurrency to another file and adjust across codebase

* feat: Add 'basescan_history' string localization

* feat: Remove Cryptocurrency.base, it doesn't have a base currency

* feat: Set eth as native currency

* feat: Enhance Base support across various sending, exchange, transaction details, fees, dashboard and balance

* feat: Integrate Base wallet support in advanced privacy settings and home settings view models

* feat: Add Base wallet support with icon and integration across providers, default settings migration, preferences, wallet connect, settings and token utilities

* feat: Update currency handling to use baseEth for Base wallet integration across various components

* fix: Update default token symbol to ETH in Base wallet transaction model

* refactor: Change default currency references from BASE to ETH across client, transaction info, and wallet components

* feat: Introduce normalizedIconPath for CryptoCurrency to standardize icon retrieval across wallet components

* fix: Include baseEth as a native token in EVMChainClient for currency handling

* chore: Update base_icon.png

* feat: Add more nodes to base_node_list.yml

* refactor: Update wallet icon retrieval to use normalizedIconPath

* chore: Remove unused cases and cleanup impl.

* feat: Add base proxy generation to configure

* refactor: Update base proxy and generation code in configure.dart

* feat: Add missing wallet and mono images

* fix: Wrong image path for pr and add missing base qr image

* feat: Enhance EVMTransactionErrorFeesHandler to parse new insufficient funds error format and calculate transaction fees in ETH and USD

* feat: Add gas price safety buffer for Base chain transactions to improve fee estimation accuracy, also fixes send all not calculating correctly

* Update model_generator.sh [skip ci]

* Update assets/base_node_list.yml [skip ci]

* Update cw_base/lib/default_base_erc20_tokens.dart [skip ci]

* Review fixes

* fixes

---------

Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
2025-10-11 04:24:32 +03:00

166 lines
5.5 KiB
Dart

import 'dart:convert';
import 'package:cw_base/base_client.dart';
import 'package:cw_base/base_transaction_history.dart';
import 'package:cw_base/base_transaction_info.dart';
import 'package:cw_base/default_base_erc20_tokens.dart';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/erc20_token.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_keys_file.dart';
import 'package:cw_evm/evm_chain_transaction_history.dart';
import 'package:cw_evm/evm_chain_transaction_info.dart';
import 'package:cw_evm/evm_chain_transaction_model.dart';
import 'package:cw_evm/evm_chain_wallet.dart';
import 'package:cw_evm/evm_erc20_balance.dart';
class BaseWallet extends EVMChainWallet {
BaseWallet({
required super.walletInfo,
required super.password,
super.mnemonic,
super.initialBalance,
super.privateKey,
required super.client,
required super.encryptionFileUtils,
super.passphrase,
}) : super(nativeCurrency: CryptoCurrency.baseEth);
@override
Future<void> initErc20TokensBox() async {
final boxName = "${walletInfo.name.replaceAll(" ", "_")}_ ${Erc20Token.baseBoxName}";
evmChainErc20TokensBox = await CakeHive.openBox<Erc20Token>(boxName);
}
@override
void addInitialTokens([bool isMigration = false]) {
final initialErc20Tokens = DefaultBaseErc20Tokens().initialBaseErc20Tokens;
for (final token in initialErc20Tokens) {
if (evmChainErc20TokensBox.containsKey(token.contractAddress)) {
final existingToken = evmChainErc20TokensBox.get(token.contractAddress);
if (existingToken?.tag != token.tag) {
evmChainErc20TokensBox.put(token.contractAddress, token);
}
} else {
if (isMigration) token.enabled = false;
evmChainErc20TokensBox.put(token.contractAddress, token);
}
}
}
@override
List<String> get getDefaultTokenContractAddresses =>
DefaultBaseErc20Tokens().initialBaseErc20Tokens.map((e) => e.contractAddress).toList();
@override
Future<bool> checkIfScanProviderIsEnabled() async {
return (await sharedPrefs.future).getBool("use_basescan") ?? true;
}
@override
String getTransactionHistoryFileName() => 'base_transactions.json';
@override
Erc20Token createNewErc20TokenObject(Erc20Token token, String? iconPath) {
return Erc20Token(
name: token.name,
symbol: token.symbol,
contractAddress: token.contractAddress,
decimal: token.decimal,
enabled: token.enabled,
tag: token.tag ?? 'ETH',
iconPath: iconPath,
isPotentialScam: token.isPotentialScam,
);
}
@override
EVMChainTransactionInfo getTransactionInfo(
EVMChainTransactionModel transactionModel, String address) {
final model = BaseTransactionInfo(
id: transactionModel.hash,
height: transactionModel.blockNumber,
ethAmount: transactionModel.amount,
direction: transactionModel.from == address
? TransactionDirection.outgoing
: TransactionDirection.incoming,
isPending: false,
date: transactionModel.date,
confirmations: transactionModel.confirmations,
ethFee: BigInt.from(transactionModel.gasUsed) * transactionModel.gasPrice,
exponent: transactionModel.tokenDecimal ?? 18,
tokenSymbol: transactionModel.tokenSymbol ?? "ETH",
to: transactionModel.to,
from: transactionModel.from,
contractAddress: transactionModel.contractAddress,
);
return model;
}
@override
EVMChainTransactionHistory setUpTransactionHistory(
WalletInfo walletInfo, String password, EncryptionFileUtils encryptionFileUtils) {
return BaseTransactionHistory(
walletInfo: walletInfo,
password: password,
encryptionFileUtils: encryptionFileUtils,
);
}
static Future<BaseWallet> open({
required String name,
required String password,
required WalletInfo walletInfo,
required EncryptionFileUtils encryptionFileUtils,
}) async {
final hasKeysFile = await WalletKeysFile.hasKeysFile(name, walletInfo.type);
final path = await pathForWallet(name: name, type: walletInfo.type);
Map<String, dynamic>? data;
try {
final jsonSource = await encryptionFileUtils.read(path: path, password: password);
data = json.decode(jsonSource) as Map<String, dynamic>;
} catch (e) {
if (!hasKeysFile) rethrow;
}
final balance = EVMChainERC20Balance.fromJSON(data?['balance'] as String?) ??
EVMChainERC20Balance(BigInt.zero);
final WalletKeysData keysData;
// Migrate wallet from the old scheme to then new .keys file scheme
if (!hasKeysFile) {
final mnemonic = data!['mnemonic'] as String?;
final privateKey = data['private_key'] as String?;
final passphrase = data['passphrase'] as String?;
keysData = WalletKeysData(mnemonic: mnemonic, privateKey: privateKey, passphrase: passphrase);
} else {
keysData = await WalletKeysFile.readKeysFile(
name,
walletInfo.type,
password,
encryptionFileUtils,
);
}
return BaseWallet(
walletInfo: walletInfo,
password: password,
mnemonic: keysData.mnemonic,
privateKey: keysData.privateKey,
passphrase: keysData.passphrase,
initialBalance: balance,
client: BaseClient(),
encryptionFileUtils: encryptionFileUtils,
);
}
}