From 73680a96e289aa0caa785303c3e2a76f80168285 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Thu, 5 Jan 2023 20:03:55 +0200 Subject: [PATCH] Add primary receive address extracted from private key --- cw_ethereum/lib/ethereum_wallet.dart | 1 + lib/ethereum/cw_ethereum.dart | 7 +++- .../wallet_address_list_view_model.dart | 40 +++++++++++++++---- tool/configure.dart | 3 ++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index 53ab87e9e..57ff39e75 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -40,6 +40,7 @@ abstract class EthereumWalletBase super(walletInfo) { this.walletInfo = walletInfo; transactionHistory = EthereumTransactionHistory(); + walletAddresses.address = EthPrivateKey.fromHex(privateKey).address.toString(); } final String mnemonic; diff --git a/lib/ethereum/cw_ethereum.dart b/lib/ethereum/cw_ethereum.dart index f2765520a..65c13fe4c 100644 --- a/lib/ethereum/cw_ethereum.dart +++ b/lib/ethereum/cw_ethereum.dart @@ -11,6 +11,9 @@ class CWEthereum extends Ethereum { WalletCredentials createEthereumNewWalletCredentials({ required String name, WalletInfo? walletInfo, - }) => - EthereumNewWalletCredentials(name: name, walletInfo: walletInfo); + }) { + return EthereumNewWalletCredentials(name: name, walletInfo: walletInfo); + } + + String getAddress(WalletBase wallet) => (wallet as EthereumWallet).walletAddresses.address; } diff --git a/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart b/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart index b861261a6..c38690b43 100644 --- a/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart +++ b/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart @@ -1,5 +1,5 @@ +import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/store/yat/yat_store.dart'; -import 'package:flutter/foundation.dart'; import 'package:mobx/mobx.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cake_wallet/utils/list_item.dart'; @@ -11,9 +11,7 @@ import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cw_core/transaction_history.dart'; import 'package:cw_core/balance.dart'; import 'package:cw_core/transaction_info.dart'; -import 'package:cw_core/wallet_type.dart'; import 'package:cake_wallet/store/app_store.dart'; -import 'dart:async'; import 'package:cake_wallet/monero/monero.dart'; import 'package:cake_wallet/haven/haven.dart'; @@ -41,7 +39,7 @@ class MoneroURI extends PaymentURI { String toString() { var base = 'monero:' + address; - if (amount?.isNotEmpty ?? false) { + if (amount.isNotEmpty) { base += '?tx_amount=${amount.replaceAll(',', '.')}'; } @@ -59,7 +57,7 @@ class HavenURI extends PaymentURI { String toString() { var base = 'haven:' + address; - if (amount?.isNotEmpty ?? false) { + if (amount.isNotEmpty) { base += '?tx_amount=${amount.replaceAll(',', '.')}'; } @@ -77,7 +75,7 @@ class BitcoinURI extends PaymentURI { String toString() { var base = 'bitcoin:' + address; - if (amount?.isNotEmpty ?? false) { + if (amount.isNotEmpty) { base += '?amount=${amount.replaceAll(',', '.')}'; } @@ -95,7 +93,25 @@ class LitecoinURI extends PaymentURI { String toString() { var base = 'litecoin:' + address; - if (amount?.isNotEmpty ?? false) { + if (amount.isNotEmpty) { + base += '?amount=${amount.replaceAll(',', '.')}'; + } + + return base; + } +} + +class EthereumURI extends PaymentURI { + EthereumURI({ + required String amount, + required String address}) + : super(amount: amount, address: address); + + @override + String toString() { + var base = 'ethereum:' + address; + + if (amount.isNotEmpty) { base += '?amount=${amount.replaceAll(',', '.')}'; } @@ -152,6 +168,10 @@ abstract class WalletAddressListViewModelBase with Store { return LitecoinURI(amount: amount, address: address.address); } + if (_wallet.type == WalletType.ethereum) { + return EthereumURI(amount: amount, address: address.address); + } + throw Exception('Unexpected type: ${type.toString()}'); } @@ -209,6 +229,12 @@ abstract class WalletAddressListViewModelBase with Store { addressList.addAll(bitcoinAddresses); } + if (wallet.type == WalletType.ethereum) { + final primaryAddress = ethereum!.getAddress(wallet); + + addressList.add(WalletAddressListItem(isPrimary: true, name: null, address: primaryAddress)); + } + return addressList; } diff --git a/tool/configure.dart b/tool/configure.dart index c02edb7cc..e952a12ea 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -477,10 +477,12 @@ Future generateEthereum(bool hasImplementation) async { const ethereumCommonHeaders = """ """; const ethereumCWHeaders = """ +import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_credentials.dart'; import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_service.dart'; import 'package:cw_ethereum/ethereum_mnemonics.dart'; +import 'package:cw_ethereum/ethereum_wallet.dart'; import 'package:cw_ethereum/ethereum_wallet_creation_credentials.dart'; import 'package:cw_ethereum/ethereum_wallet_service.dart'; import 'package:hive/hive.dart'; @@ -491,6 +493,7 @@ abstract class Ethereum { List getEthereumWordList(String language); WalletService createEthereumWalletService(Box walletInfoSource); WalletCredentials createEthereumNewWalletCredentials({required String name, WalletInfo? walletInfo}); + String getAddress(WalletBase wallet); } """;