From cfb82dd02de624a0e8524d9eb3046a7a4f749550 Mon Sep 17 00:00:00 2001 From: M Date: Wed, 12 Jan 2022 15:20:43 +0200 Subject: [PATCH 1/2] Changed way for generation of change address for electrum wallets. Remove some yats call. --- cw_bitcoin/lib/electrum_wallet.dart | 17 +++++++- cw_bitcoin/lib/electrum_wallet_addresses.dart | 6 +++ ios/Podfile.lock | 2 +- lib/bitcoin/cw_bitcoin.dart | 6 +++ lib/main.dart | 26 ++++++------ lib/reactions/on_current_wallet_change.dart | 42 +++++++++---------- .../dashboard/widgets/address_page.dart | 19 +++++---- .../wallet_address_list_view_model.dart | 29 +++++++++---- 8 files changed, 93 insertions(+), 54 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index b4fd2289f..e55b706dc 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:math'; import 'package:cw_core/unspent_coins_info.dart'; import 'package:hive/hive.dart'; import 'package:cw_bitcoin/electrum_wallet_addresses.dart'; @@ -238,7 +239,7 @@ abstract class ElectrumWalletBase extends WalletBase addr.isHidden) + .toList(); + + if (addresses.length < minCountOfHiddenAddresses) { + addresses = walletAddresses.addresses.toList(); + } + + return addresses[random.nextInt(addresses.length)].address; + } } diff --git a/cw_bitcoin/lib/electrum_wallet_addresses.dart b/cw_bitcoin/lib/electrum_wallet_addresses.dart index 66511086f..a5e5b6e9b 100644 --- a/cw_bitcoin/lib/electrum_wallet_addresses.dart +++ b/cw_bitcoin/lib/electrum_wallet_addresses.dart @@ -4,6 +4,7 @@ import 'package:cw_core/wallet_addresses.dart'; import 'package:cw_core/wallet_info.dart'; import 'package:flutter/foundation.dart'; import 'package:mobx/mobx.dart'; +import 'dart:math'; part 'electrum_wallet_addresses.g.dart'; @@ -129,4 +130,9 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { print(e.toString()); } } + + void randomizeAddress() { + final random = Random(); + address = addresses[random.nextInt(addresses.length)].address; + } } \ No newline at end of file diff --git a/ios/Podfile.lock b/ios/Podfile.lock index e318cd4f8..9de19b391 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -196,4 +196,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: bc2591d23316907c9c90ca1cd2fce063fd866508 -COCOAPODS: 1.10.2 +COCOAPODS: 1.11.2 diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index 72a9a8a47..650b5771b 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -56,6 +56,12 @@ class CWBitcoin extends Bitcoin { final bitcoinWallet = wallet as BitcoinWallet; bitcoinWallet.walletAddresses.nextAddress(); } + + @override + Future randomAddress(Object wallet) { + final bitcoinWallet = wallet as BitcoinWallet; + bitcoinWallet.walletAddresses.randomizeAddress(); + } @override Object createBitcoinTransactionCredentials(List outputs, TransactionPriority priority) diff --git a/lib/main.dart b/lib/main.dart index 1647edf51..47e55de59 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -197,7 +197,7 @@ class AppState extends State with SingleTickerProviderStateMixin { void initState() { super.initState(); _handleIncomingLinks(); - _handleInitialUri(); + //_handleInitialUri(); } @override @@ -235,18 +235,18 @@ class AppState extends State with SingleTickerProviderStateMixin { } void _fetchEmojiFromUri(Uri uri) { - final queryParameters = uri.queryParameters; - if (queryParameters?.isEmpty ?? true) { - return; - } - final emoji = queryParameters['eid']; - final refreshToken = queryParameters['refresh_token']; - if ((emoji?.isEmpty ?? true)||(refreshToken?.isEmpty ?? true)) { - return; - } - yatStore.emoji = emoji; - yatStore.refreshToken = refreshToken; - yatStore.emojiIncommingSC.add(emoji); + //final queryParameters = uri.queryParameters; + //if (queryParameters?.isEmpty ?? true) { + // return; + //} + //final emoji = queryParameters['eid']; + //final refreshToken = queryParameters['refresh_token']; + //if ((emoji?.isEmpty ?? true)||(refreshToken?.isEmpty ?? true)) { + // return; + //} + //yatStore.emoji = emoji; + //yatStore.refreshToken = refreshToken; + //yatStore.emojiIncommingSC.add(emoji); } @override diff --git a/lib/reactions/on_current_wallet_change.dart b/lib/reactions/on_current_wallet_change.dart index 4a7f8b101..842bc761f 100644 --- a/lib/reactions/on_current_wallet_change.dart +++ b/lib/reactions/on_current_wallet_change.dart @@ -17,34 +17,34 @@ import 'package:cake_wallet/store/yat/yat_store.dart'; ReactionDisposer _onCurrentWalletChangeReaction; ReactionDisposer _onCurrentWalletChangeFiatRateUpdateReaction; -ReactionDisposer _onCurrentWalletAddressChangeReaction; +//ReactionDisposer _onCurrentWalletAddressChangeReaction; void startCurrentWalletChangeReaction(AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { _onCurrentWalletChangeReaction?.reaction?.dispose(); _onCurrentWalletChangeFiatRateUpdateReaction?.reaction?.dispose(); - _onCurrentWalletAddressChangeReaction?.reaction?.dispose(); + //_onCurrentWalletAddressChangeReaction?.reaction?.dispose(); - _onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address, - (String address) async { - if (address == appStore.wallet.walletInfo.yatLastUsedAddress) { - return; - } + //_onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address, + //(String address) async { + //if (address == appStore.wallet.walletInfo.yatLastUsedAddress) { + // return; + //} - try { - final yatStore = getIt.get(); - await updateEmojiIdAddress( - appStore.wallet.walletInfo.yatEmojiId, - appStore.wallet.walletAddresses.address, - yatStore.apiKey, - appStore.wallet.type - ); - appStore.wallet.walletInfo.yatLastUsedAddress = address; - await appStore.wallet.walletInfo.save(); - } catch (e) { - print(e.toString()); - } - }); + //try { + // final yatStore = getIt.get(); + // await updateEmojiIdAddress( + // appStore.wallet.walletInfo.yatEmojiId, + // appStore.wallet.walletAddresses.address, + // yatStore.apiKey, + // appStore.wallet.type + // ); + // appStore.wallet.walletInfo.yatLastUsedAddress = address; + // await appStore.wallet.walletInfo.save(); + //} catch (e) { + // print(e.toString()); + //} + //}); _onCurrentWalletChangeReaction = reaction((_) => appStore.wallet, (WalletBase< Balance, TransactionHistoryBase, TransactionInfo> diff --git a/lib/src/screens/dashboard/widgets/address_page.dart b/lib/src/screens/dashboard/widgets/address_page.dart index 51bb05a40..604725d33 100644 --- a/lib/src/screens/dashboard/widgets/address_page.dart +++ b/lib/src/screens/dashboard/widgets/address_page.dart @@ -27,6 +27,7 @@ class AddressPage extends StatelessWidget { @override Widget build(BuildContext context) { + addressListViewModel.generateRandomAddress(); autorun((_) async { if (!walletViewModel.isOutdatedElectrumWallet || !walletViewModel.settingsStore.shouldShowReceiveWarning) { @@ -124,15 +125,15 @@ class AddressPage extends StatelessWidget { ], ), ), - ) - : PrimaryButton( - onPressed: () => addressListViewModel.nextAddress(), - text: 'Next address', - color: Theme.of(context).buttonColor, - textColor: Theme.of(context) - .accentTextTheme - .display3 - .backgroundColor); + ) : Container(); + //: PrimaryButton( + // onPressed: () => addressListViewModel.nextAddress(), + // text: 'Next address', + // color: Theme.of(context).buttonColor, + // textColor: Theme.of(context) + // .accentTextTheme + // .display3 + // .backgroundColor); }) ], ), 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 6fb2cdcb4..47c135d29 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 @@ -77,16 +77,16 @@ abstract class WalletAddressListViewModelBase with Store { } }); - reaction((_) => yatStore.emoji, (String emojiId) => this.emoji = emojiId); + //reaction((_) => yatStore.emoji, (String emojiId) => this.emoji = emojiId); - _onLastUsedYatAddressSubscription = - _wallet.walletInfo.yatLastUsedAddressStream.listen((String yatAddress) { - if (yatAddress == _wallet.walletAddresses.address) { - emoji = yatStore.emoji; - } else { - emoji = ''; - } - }); + //_onLastUsedYatAddressSubscription = + // _wallet.walletInfo.yatLastUsedAddressStream.listen((String yatAddress) { + // if (yatAddress == _wallet.walletAddresses.address) { + // emoji = yatStore.emoji; + // } else { + // emoji = ''; + // } + //}); if (_wallet.walletAddresses.address == _wallet.walletInfo.yatLastUsedAddress) { emoji = yatStore.emoji; @@ -223,4 +223,15 @@ abstract class WalletAddressListViewModelBase with Store { wallet.save(); } } + + @action + void generateRandomAddress() { + final wallet = _wallet; + + if (wallet.type == WalletType.bitcoin + || wallet.type == WalletType.litecoin) { + bitcoin.randomAddress(wallet); + wallet.save(); + } + } } From f591f188c5a2be1f4d84a9dc348fb02359e73984 Mon Sep 17 00:00:00 2001 From: M Date: Wed, 12 Jan 2022 15:32:23 +0200 Subject: [PATCH 2/2] Temporary comment yat code --- lib/main.dart | 6 +- lib/store/yat/yat_store.dart | 229 ++++++++++++++++++----------------- 2 files changed, 120 insertions(+), 115 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 47e55de59..0c8da9b01 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -196,7 +196,7 @@ class AppState extends State with SingleTickerProviderStateMixin { @override void initState() { super.initState(); - _handleIncomingLinks(); + //_handleIncomingLinks(); //_handleInitialUri(); } @@ -214,7 +214,7 @@ class AppState extends State with SingleTickerProviderStateMixin { return; } if (!mounted) return; - _fetchEmojiFromUri(uri); + //_fetchEmojiFromUri(uri); } catch (e) { if (!mounted) return; print(e.toString()); @@ -226,7 +226,7 @@ class AppState extends State with SingleTickerProviderStateMixin { stream = getUriLinksStream().listen((Uri uri) { print('uri: $uri'); if (!mounted) return; - _fetchEmojiFromUri(uri); + //_fetchEmojiFromUri(uri); }, onError: (Object error) { if (!mounted) return; print('Error: $error'); diff --git a/lib/store/yat/yat_store.dart b/lib/store/yat/yat_store.dart index caee5b0fa..d6a655595 100644 --- a/lib/store/yat/yat_store.dart +++ b/lib/store/yat/yat_store.dart @@ -15,18 +15,18 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart'; part 'yat_store.g.dart'; class YatLink { - static const partnerId = 'CW'; - static const baseDevUrl = 'https://yat.fyi'; - static const baseReleaseUrl = 'https://y.at'; - static const signInSuffix = '/partner/$partnerId/link-email'; - static const createSuffix = '/create'; - static const managePath = '/partner/$partnerId/manage'; - static const queryParameter = '?address_json='; - static const apiDevUrl = 'https://a.yat.fyi'; - static const apiReleaseUrl = 'https://a.y.at'; - static const requestDevUrl = 'https://a.yat.fyi/emoji_id/'; - static const requestReleaseUrl = 'https://a.y.at/emoji_id/'; - static const startFlowUrl = 'https://www.y03btrk.com/4RQSJ/6JHXF/'; + static const partnerId = ''; // 'CW'; + static const baseDevUrl = ''; // 'https://yat.fyi'; + static const baseReleaseUrl = ''; // 'https://y.at'; + static const signInSuffix = ''; // '/partner/$partnerId/link-email'; + static const createSuffix = ''; // '/create'; + static const managePath = ''; // '/partner/$partnerId/manage'; + static const queryParameter = ''; // '?address_json='; + static const apiDevUrl = ''; // 'https://a.yat.fyi'; + static const apiReleaseUrl = ''; // 'https://a.y.at'; + static const requestDevUrl = ''; // 'https://a.yat.fyi/emoji_id/'; + static const requestReleaseUrl = ''; //'https://a.y.at/emoji_id/'; + static const startFlowUrl = ''; // 'https://www.y03btrk.com/4RQSJ/6JHXF/'; static const isDevMode = true; static const tags = >{"XMR" : ['0x1001', '0x1002'], "BTC" : ['0x1003'], "LTC" : ['0x3fff']}; @@ -43,118 +43,122 @@ class YatLink { } Future> fetchYatAddress(String emojiId, String ticker) async { - final url = YatLink.emojiIdUrl + emojiId + '/payment'; - final response = await get(url); + //final url = YatLink.emojiIdUrl + emojiId + '/payment'; + //final response = await get(url); - if (response.statusCode != 200) { - throw YatException(text: response.body.toString()); - } + //if (response.statusCode != 200) { + // throw YatException(text: response.body.toString()); + //} - final addresses = []; - final currency = ticker.toUpperCase(); - final responseJSON = json.decode(response.body) as Map; - final result = responseJSON['result'] as Map; - result.forEach((dynamic key, dynamic value) { - final tag = key as String ?? ''; - final record = value as Map; + //final addresses = []; + //final currency = ticker.toUpperCase(); + //final responseJSON = json.decode(response.body) as Map; + //final result = responseJSON['result'] as Map; + //result.forEach((dynamic key, dynamic value) { + // final tag = key as String ?? ''; + // final record = value as Map; - if (YatLink.tags[currency]?.contains(tag) ?? false) { - final address = record['address'] as String; - if (address?.isNotEmpty ?? false) { - addresses.add(address); - } - } - }); + // if (YatLink.tags[currency]?.contains(tag) ?? false) { + // final address = record['address'] as String; + // if (address?.isNotEmpty ?? false) { + // addresses.add(address); + // } + // } + //}); - return addresses; + //return addresses; + return []; } Future fetchYatAccessToken(String refreshToken) async { - try { - final url = YatLink.apiUrl + '/auth/token/refresh'; - final bodyJson = json.encode({'refresh_token': refreshToken}); - final response = await post( - url, - headers: { - 'Content-Type': 'application/json', - 'Accept': '*/*' - }, - body: bodyJson); + //try { + // final url = YatLink.apiUrl + '/auth/token/refresh'; + // final bodyJson = json.encode({'refresh_token': refreshToken}); + // final response = await post( + // url, + // headers: { + // 'Content-Type': 'application/json', + // 'Accept': '*/*' + // }, + // body: bodyJson); - if (response.statusCode != 200) { - throw YatException(text: response.body.toString()); - } + // if (response.statusCode != 200) { + // throw YatException(text: response.body.toString()); + // } - final responseJSON = json.decode(response.body) as Map; - return responseJSON['access_token'] as String; - }catch(_) { - return ''; - } + // final responseJSON = json.decode(response.body) as Map; + // return responseJSON['access_token'] as String; + //}catch(_) { + // return ''; + //} + return ''; } Future fetchYatApiKey(String accessKey) async { - try { - final url = YatLink.apiUrl + '/api_keys'; - final bodyJson = json.encode({'name': 'CW'}); - final response = await post( - url, - headers: { - 'Authorization': 'Bearer $accessKey', - 'Content-Type': 'application/json', - 'Accept': '*/*' - }, - body: bodyJson); + //try { + // final url = YatLink.apiUrl + '/api_keys'; + // final bodyJson = json.encode({'name': 'CW'}); + // final response = await post( + // url, + // headers: { + // 'Authorization': 'Bearer $accessKey', + // 'Content-Type': 'application/json', + // 'Accept': '*/*' + // }, + // body: bodyJson); - if (response.statusCode != 200) { - throw YatException(text: response.body.toString()); - } + // if (response.statusCode != 200) { + // throw YatException(text: response.body.toString()); + // } - final responseJSON = json.decode(response.body) as Map; - return responseJSON['api_key'] as String; - }catch(_) { - return ''; - } + // final responseJSON = json.decode(response.body) as Map; + // return responseJSON['api_key'] as String; + //}catch(_) { + // return ''; + //} + return ''; } Future updateEmojiIdAddress(String emojiId, String address, String apiKey, WalletType type) async { - final url = YatLink.emojiIdUrl + emojiId; - final cur = walletTypeToCryptoCurrency(type); - final curFormatted = cur.toString().toUpperCase(); - var tag = ''; + //final url = YatLink.emojiIdUrl + emojiId; + //final cur = walletTypeToCryptoCurrency(type); + //final curFormatted = cur.toString().toUpperCase(); + //var tag = ''; - if (type == WalletType.monero && !address.startsWith('4')) { - tag = YatLink.tags[curFormatted].last; - } else { - tag = YatLink.tags[curFormatted].first; - } + //if (type == WalletType.monero && !address.startsWith('4')) { + // tag = YatLink.tags[curFormatted].last; + //} else { + // tag = YatLink.tags[curFormatted].first; + //} - final bodyJson = json.encode({ - 'insert': [{ - 'data': address, - 'tag': tag - }] - }); - final response = await patch( - url, - headers: { - 'x-api-key': apiKey, - 'Content-Type': 'application/json', - 'Accept': '*/*' - }, - body: bodyJson); + //final bodyJson = json.encode({ + // 'insert': [{ + // 'data': address, + // 'tag': tag + // }] + //}); + //final response = await patch( + // url, + // headers: { + // 'x-api-key': apiKey, + // 'Content-Type': 'application/json', + // 'Accept': '*/*' + // }, + // body: bodyJson); - if (response.statusCode != 200) { - throw YatException(text: response.body.toString()); - } + //if (response.statusCode != 200) { + // throw YatException(text: response.body.toString()); + //} } Future visualisationForEmojiId(String emojiId) async { - final url = YatLink.emojiIdUrl + emojiId + '/json/VisualizerFileLocations'; - final response = await get(url); - final responseJSON = json.decode(response.body) as Map; - final data = responseJSON['data'] as Map; - final result = data['gif'] as String ?? ''; - return result; + //final url = YatLink.emojiIdUrl + emojiId + '/json/VisualizerFileLocations'; + //final response = await get(url); + //final responseJSON = json.decode(response.body) as Map; + //final data = responseJSON['data'] as Map; + //final result = data['gif'] as String ?? ''; + //return result; + return ''; } class YatStore = YatStoreBase with _$YatStore; @@ -254,18 +258,19 @@ abstract class YatStoreBase with Store { } String defineQueryParameters() { - final result = {}; - final tags = YatLink.tags[_wallet.currency.toString().toUpperCase()]; - String tag = tags.first; + //final result = {}; + //final tags = YatLink.tags[_wallet.currency.toString().toUpperCase()]; + //String tag = tags.first; - if (_wallet.type == WalletType.monero - && _wallet.walletAddresses.address.startsWith('4')) { - tag = tags.last; - } - result[tag] = '${_wallet.walletAddresses.address}|${_wallet.name}'; - final addressJson = json.encode([result]); - final addressJsonBytes = utf8.encode(addressJson); + //if (_wallet.type == WalletType.monero + // && _wallet.walletAddresses.address.startsWith('4')) { + // tag = tags.last; + //} + //result[tag] = '${_wallet.walletAddresses.address}|${_wallet.name}'; + //final addressJson = json.encode([result]); + //final addressJsonBytes = utf8.encode(addressJson); - return base64.encode(addressJsonBytes); + //return base64.encode(addressJsonBytes); + return ''; } } \ No newline at end of file