From c4158b73014a98066a5fc61476aaa0792379f2be Mon Sep 17 00:00:00 2001 From: Chiziaruhoma Ogbonda Date: Tue, 29 Mar 2022 17:32:39 +0100 Subject: [PATCH 1/6] created base env file --- starport_template/lib/base_env.dart | 43 ++++++++++ starport_template/lib/main.dart | 6 +- starport_template/lib/starport_app.dart | 4 +- .../lib/stores/accounts_store.dart | 4 +- .../lib/stores/settings_store.dart | 4 +- .../lib/stores/transactions_store.dart | 4 +- starport_template/lib/utils/base_env.dart | 63 -------------- .../lib/utils/cosmos_balances.dart | 4 +- .../cosmos_transaction_history_loader.dart | 4 +- starport_template/lib/utils/env_util.dart | 82 +++++++++++++++++++ .../lib/utils/node_info_loader.dart | 4 +- 11 files changed, 143 insertions(+), 79 deletions(-) create mode 100644 starport_template/lib/base_env.dart delete mode 100644 starport_template/lib/utils/base_env.dart create mode 100644 starport_template/lib/utils/env_util.dart diff --git a/starport_template/lib/base_env.dart b/starport_template/lib/base_env.dart new file mode 100644 index 00000000..aadc1e2a --- /dev/null +++ b/starport_template/lib/base_env.dart @@ -0,0 +1,43 @@ +class BaseEnv { + BaseEnv({ + this.envLcdUrl = 'http://10.0.2.2', + this.envGrpcUrl = 'http://localhost', + this.envLcdPort = '1317', + this.envGrpcPort = '9090', + this.prefixAddress = 'cosmos', + }); + + /// Configure the LCD Url + final String envLcdUrl; + + /// Configure the GRPC Url + final String envGrpcUrl; + + /// Configure the LCD Port + final String envLcdPort; + + /// Configure the GRPC Port + final String envGrpcPort; + + /// Configure the address prefix + final String prefixAddress; + + /// Configure the base api url + String get envBaseApiUrl => '$envLcdUrl:$envLcdPort'; + + BaseEnv copyWith({ + String? envLcdUrl, + String? envGrpcUrl, + String? envLcdPort, + String? envGrpcPort, + String? prefixAddress, + }) { + return BaseEnv( + envLcdUrl: envLcdUrl ?? this.envLcdUrl, + envGrpcUrl: envGrpcUrl ?? this.envGrpcUrl, + envLcdPort: envLcdPort ?? this.envLcdPort, + envGrpcPort: envGrpcPort ?? this.envGrpcPort, + prefixAddress: prefixAddress ?? this.prefixAddress, + ); + } +} diff --git a/starport_template/lib/main.dart b/starport_template/lib/main.dart index 50731486..14b458cf 100644 --- a/starport_template/lib/main.dart +++ b/starport_template/lib/main.dart @@ -2,11 +2,12 @@ import 'package:alan/alan.dart'; import 'package:cosmos_auth/cosmos_auth.dart'; import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:flutter/material.dart'; +import 'package:starport_template/base_env.dart'; import 'package:starport_template/starport_app.dart'; import 'package:starport_template/stores/accounts_store.dart'; import 'package:starport_template/stores/settings_store.dart'; import 'package:starport_template/stores/transactions_store.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; import 'package:transaction_signing_gateway/mobile/no_op_transaction_summary_ui.dart'; import 'package:transaction_signing_gateway/transaction_signing_gateway.dart'; @@ -16,7 +17,8 @@ void main() { } void _buildDependencies() { - StarportApp.baseEnv = BaseEnv(); + final env = BaseEnv(); + StarportApp.baseEnv = BaseEnvUtil(env); StarportApp.networkInfo = StarportApp.baseEnv.networkInfo; _logBackendInfo(StarportApp.networkInfo); StarportApp.secureDataStore = FlutterSecureStorageDataStore(); diff --git a/starport_template/lib/starport_app.dart b/starport_template/lib/starport_app.dart index 9bda7735..7f770bf0 100644 --- a/starport_template/lib/starport_app.dart +++ b/starport_template/lib/starport_app.dart @@ -7,7 +7,7 @@ import 'package:starport_template/pages/routing_page.dart'; import 'package:starport_template/stores/accounts_store.dart'; import 'package:starport_template/stores/settings_store.dart'; import 'package:starport_template/stores/transactions_store.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; import 'package:transaction_signing_gateway/gateway/transaction_signing_gateway.dart'; class StarportApp extends StatelessWidget { @@ -18,7 +18,7 @@ class StarportApp extends StatelessWidget { static late TransactionSigningGateway signingGateway; static late AccountsStore accountsStore; static late TransactionsStore transactionsStore; - static late BaseEnv baseEnv; + static late BaseEnvUtil baseEnv; static late NetworkInfo networkInfo; static late SecureDataStore secureDataStore; static late SettingsStore settingsStore; diff --git a/starport_template/lib/stores/accounts_store.dart b/starport_template/lib/stores/accounts_store.dart index 3c16ad1a..6518f3a1 100644 --- a/starport_template/lib/stores/accounts_store.dart +++ b/starport_template/lib/stores/accounts_store.dart @@ -4,7 +4,7 @@ import 'package:mobx/mobx.dart'; import 'package:starport_template/entities/account_additional_data.dart'; import 'package:starport_template/entities/balance.dart'; import 'package:starport_template/entities/import_account_form_data.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/cosmos_balances.dart'; import 'package:starport_template/utils/token_sender.dart'; import 'package:transaction_signing_gateway/alan/alan_account_derivation_info.dart'; @@ -19,7 +19,7 @@ class AccountsStore { static const chainId = 'my_starport_chain'; final TransactionSigningGateway _transactionSigningGateway; - final BaseEnv baseEnv; + final BaseEnvUtil baseEnv; final Observable _areAccountsLoading = Observable(false); final Observable _isSendMoneyLoading = Observable(false); diff --git a/starport_template/lib/stores/settings_store.dart b/starport_template/lib/stores/settings_store.dart index 3dcf5711..b4f8ad5d 100644 --- a/starport_template/lib/stores/settings_store.dart +++ b/starport_template/lib/stores/settings_store.dart @@ -3,7 +3,7 @@ import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:mobx/mobx.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:starport_template/pages/passcode_prompt_page.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/node_info_loader.dart'; class SettingsStore { @@ -18,7 +18,7 @@ class SettingsStore { static const _nodeNetworkKey = 'node_network'; final CosmosAuth _cosmosAuth; - final BaseEnv _baseEnv; + final BaseEnvUtil _baseEnv; final SecureDataStore _secureDataStore; final Observable _appLockEnabled = Observable(false); final Observable _biometricsEnabled = Observable(false); diff --git a/starport_template/lib/stores/transactions_store.dart b/starport_template/lib/stores/transactions_store.dart index fa4eadf0..d2c9c2f2 100644 --- a/starport_template/lib/stores/transactions_store.dart +++ b/starport_template/lib/stores/transactions_store.dart @@ -1,13 +1,13 @@ import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:mobx/mobx.dart'; import 'package:starport_template/entities/transaction_history_item.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/cosmos_transaction_history_loader.dart'; class TransactionsStore { TransactionsStore(this.baseEnv); - final BaseEnv baseEnv; + final BaseEnvUtil baseEnv; final Observable _isTransactionHistoryLoading = Observable(false); final Observable _isTransactionHistoryError = Observable(false); diff --git a/starport_template/lib/utils/base_env.dart b/starport_template/lib/utils/base_env.dart deleted file mode 100644 index 5eeec61b..00000000 --- a/starport_template/lib/utils/base_env.dart +++ /dev/null @@ -1,63 +0,0 @@ -// ignore_for_file: do_not_use_environment - -import 'dart:io'; - -import 'package:alan/alan.dart'; -import 'package:grpc/grpc.dart'; - -class BaseEnv { - BaseEnv({ - String? lcdUrl, - String? grpcUrl, - String? lcdPort, - String? grpcPort, - }) : networkInfo = NetworkInfo( - bech32Hrp: 'cosmos', - lcdInfo: LCDInfo( - host: lcdUrl ?? envLcdUrl, - port: int.parse(lcdPort ?? envLcdPort), - ), - grpcInfo: GRPCInfo( - host: grpcUrl ?? envGrpcUrl, - port: _parseGrpcPort(grpcPort), - credentials: _parseGrpcPort(grpcPort) == 443 - ? const ChannelCredentials.secure() - : const ChannelCredentials.insecure(), - ), - ), - baseApiUrl = '${lcdUrl ?? envLcdUrl}:${lcdPort ?? envLcdPort}'; - - static int _parseGrpcPort(String? grpcPort) => int.parse(grpcPort ?? envGrpcPort); - - final NetworkInfo networkInfo; - - final String baseApiUrl; -} - -// DO NOT USE String.fromEnvironment without 'const'!!! -// https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 -String get envLcdPort => const String.fromEnvironment('LCD_PORT', defaultValue: '1317'); - -// DO NOT USE String.fromEnvironment without 'const'!!! -// https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 -String get envGrpcPort => const String.fromEnvironment('GRPC_PORT', defaultValue: '9090'); - -String get envLcdUrl { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('LCD_URL'); - if (result.isEmpty) { - return Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'; - } - return result; -} - -String get envGrpcUrl { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('GRPC_URL'); - if (result.isEmpty) { - return Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'; - } - return result; -} diff --git a/starport_template/lib/utils/cosmos_balances.dart b/starport_template/lib/utils/cosmos_balances.dart index a9c84b12..e363bdb9 100644 --- a/starport_template/lib/utils/cosmos_balances.dart +++ b/starport_template/lib/utils/cosmos_balances.dart @@ -5,12 +5,12 @@ import 'package:starport_template/entities/amount.dart'; import 'package:starport_template/entities/balance.dart'; import 'package:starport_template/entities/denom.dart'; import 'package:starport_template/model/balance_json.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; class CosmosBalances { CosmosBalances(this.baseEnv); - BaseEnv baseEnv; + BaseEnvUtil baseEnv; Future> getBalances(String accountAddress) async { final uri = '${baseEnv.baseApiUrl}/cosmos/bank/v1beta1/balances/$accountAddress'; diff --git a/starport_template/lib/utils/cosmos_transaction_history_loader.dart b/starport_template/lib/utils/cosmos_transaction_history_loader.dart index 1b7271bf..4eb4e73a 100644 --- a/starport_template/lib/utils/cosmos_transaction_history_loader.dart +++ b/starport_template/lib/utils/cosmos_transaction_history_loader.dart @@ -5,12 +5,12 @@ import 'package:starport_template/entities/amount.dart'; import 'package:starport_template/entities/denom.dart'; import 'package:starport_template/entities/transaction_history_item.dart'; import 'package:starport_template/model/tx_response_json.dart'; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; class CosmosTransactionHistoryLoader { CosmosTransactionHistoryLoader(this.baseEnv); - BaseEnv baseEnv; + BaseEnvUtil baseEnv; Future> getTransactionHistory(String accountAddress) async { final outGoingTransactions = await _getTransactionResponses(accountAddress, TransactionType.Send); diff --git a/starport_template/lib/utils/env_util.dart b/starport_template/lib/utils/env_util.dart new file mode 100644 index 00000000..712afa64 --- /dev/null +++ b/starport_template/lib/utils/env_util.dart @@ -0,0 +1,82 @@ +// ignore_for_file: do_not_use_environment + +import 'dart:io'; + +import 'package:alan/alan.dart'; +import 'package:grpc/grpc.dart'; +import 'package:starport_template/base_env.dart'; + +class BaseEnvUtil { + BaseEnvUtil(BaseEnv env) + : networkInfo = NetworkInfo( + bech32Hrp: prefixAddress ?? env.prefixAddress, + lcdInfo: LCDInfo( + host: lcdUrl ?? env.envLcdUrl, + port: int.parse(lcdPort ?? env.envLcdPort), + ), + grpcInfo: GRPCInfo( + host: grpcUrl ?? env.envGrpcUrl, + port: int.parse(grpcPort ?? env.envGrpcPort), + credentials: int.parse(grpcPort ?? env.envGrpcPort) == 443 + ? const ChannelCredentials.secure() + : const ChannelCredentials.insecure(), + ), + ), + baseApiUrl = lcdUrl != null && lcdPort != null ? '$lcdUrl:$lcdPort' : env.envBaseApiUrl; + + final NetworkInfo networkInfo; + + final String baseApiUrl; +} + +String? get prefixAddress { + // DO NOT USE String.fromEnvironment without 'const'!!! + // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 + const result = String.fromEnvironment('PREFIX_ADDR'); + if (result.isNotEmpty) { + return result; + } + return null; +} + +String? get lcdPort { + // DO NOT USE String.fromEnvironment without 'const'!!! + // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 + const result = String.fromEnvironment('LCD_PORT'); + if (result.isNotEmpty) { + return result; + } + return null; +} + +String? get grpcPort { + // DO NOT USE String.fromEnvironment without 'const'!!! + // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 + const result = String.fromEnvironment('GRPC_PORT'); + + if (result.isNotEmpty) { + return result; + } + return null; +} + +String? get lcdUrl { + // DO NOT USE String.fromEnvironment without 'const'!!! + // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 + const result = String.fromEnvironment('LCD_URL'); + + if (result.isNotEmpty) { + return Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'; + } + return null; +} + +String? get grpcUrl { + // DO NOT USE String.fromEnvironment without 'const'!!! + // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 + const result = String.fromEnvironment('GRPC_URL'); + if (result.isNotEmpty) { + return Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'; + } + return null; +} diff --git a/starport_template/lib/utils/node_info_loader.dart b/starport_template/lib/utils/node_info_loader.dart index d1948fba..7104130d 100644 --- a/starport_template/lib/utils/node_info_loader.dart +++ b/starport_template/lib/utils/node_info_loader.dart @@ -1,12 +1,12 @@ import 'dart:convert'; import 'package:http/http.dart' as http; -import 'package:starport_template/utils/base_env.dart'; +import 'package:starport_template/utils/env_util.dart'; class NodeInfoLoader { NodeInfoLoader(this.baseEnv); - BaseEnv baseEnv; + BaseEnvUtil baseEnv; Future getChainId() async { final uri = '${baseEnv.baseApiUrl}/node_info'; From 590fb12ed0f146110e3ad8bc245336059b9f5288 Mon Sep 17 00:00:00 2001 From: Chiziaruhoma Date: Wed, 30 Mar 2022 08:33:16 +0100 Subject: [PATCH 2/6] minor lint fixes --- starport_template/lib/stores/accounts_store.dart | 2 +- starport_template/lib/stores/transactions_store.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/starport_template/lib/stores/accounts_store.dart b/starport_template/lib/stores/accounts_store.dart index 6518f3a1..cd6bcec5 100644 --- a/starport_template/lib/stores/accounts_store.dart +++ b/starport_template/lib/stores/accounts_store.dart @@ -4,8 +4,8 @@ import 'package:mobx/mobx.dart'; import 'package:starport_template/entities/account_additional_data.dart'; import 'package:starport_template/entities/balance.dart'; import 'package:starport_template/entities/import_account_form_data.dart'; -import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/cosmos_balances.dart'; +import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/token_sender.dart'; import 'package:transaction_signing_gateway/alan/alan_account_derivation_info.dart'; import 'package:transaction_signing_gateway/transaction_signing_gateway.dart'; diff --git a/starport_template/lib/stores/transactions_store.dart b/starport_template/lib/stores/transactions_store.dart index d2c9c2f2..1ee4389f 100644 --- a/starport_template/lib/stores/transactions_store.dart +++ b/starport_template/lib/stores/transactions_store.dart @@ -1,8 +1,8 @@ import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:mobx/mobx.dart'; import 'package:starport_template/entities/transaction_history_item.dart'; -import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/cosmos_transaction_history_loader.dart'; +import 'package:starport_template/utils/env_util.dart'; class TransactionsStore { TransactionsStore(this.baseEnv); From b1a46d63baf5f7237ef734666bb9a91efb223070 Mon Sep 17 00:00:00 2001 From: Chiziaruhoma Date: Fri, 1 Apr 2022 13:37:48 +0100 Subject: [PATCH 3/6] refactor baseEnv to appConfig --- Makefile | 5 ++ packages/cosmos_auth/pubspec.lock | 14 ++-- packages/cosmos_ui_components/pubspec.lock | 2 +- .../transaction_signing_gateway/pubspec.lock | 6 +- starport_template/lib/app_config.dart | 61 ++++++++++++++ starport_template/lib/base_env.dart | 43 ---------- starport_template/lib/main.dart | 14 ++-- starport_template/lib/starport_app.dart | 4 +- .../lib/stores/accounts_store.dart | 10 +-- .../lib/stores/settings_store.dart | 4 +- .../lib/stores/transactions_store.dart | 8 +- .../lib/utils/cosmos_balances.dart | 8 +- .../cosmos_transaction_history_loader.dart | 8 +- starport_template/lib/utils/env_util.dart | 82 ------------------- .../lib/utils/node_info_loader.dart | 8 +- starport_template/pubspec.lock | 12 +-- 16 files changed, 114 insertions(+), 175 deletions(-) create mode 100644 starport_template/lib/app_config.dart delete mode 100644 starport_template/lib/base_env.dart delete mode 100644 starport_template/lib/utils/env_util.dart diff --git a/Makefile b/Makefile index 743c7689..1cf2f1c4 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,10 @@ install-cosmos-ui-components: install-cosmos-utils: $(info Running flutter pub upgrade on `cosmos_utils`) cd packages/cosmos_utils && fvm install && fvm flutter pub upgrade + +install-cosmos-auth: + $(info Running flutter pub upgrade on `cosmos_auth`) + cd packages/cosmos_auth && fvm install && fvm flutter pub upgrade install-transaction-signing-gateway: $(info Running flutter pub upgrade on `transaction_signing_gateway`) @@ -17,6 +21,7 @@ install-starport-template: # Ensures proper flutter channel is in use, runs pub upgrade. install: + $(MAKE) install-cosmos-auth $(MAKE) install-cosmos-ui-components $(MAKE) install-cosmos-utils $(MAKE) install-transaction-signing-gateway diff --git a/packages/cosmos_auth/pubspec.lock b/packages/cosmos_auth/pubspec.lock index 02490347..f95db12a 100644 --- a/packages/cosmos_auth/pubspec.lock +++ b/packages/cosmos_auth/pubspec.lock @@ -97,7 +97,7 @@ packages: description: path: "packages/cosmos_utils" ref: main - resolved-ref: "79f8b8ebb452ffbe4e3fa092e43824f0d2a79282" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" @@ -215,7 +215,7 @@ packages: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.3" + version: "0.6.4" lint: dependency: "direct dev" description: @@ -229,7 +229,7 @@ packages: name: local_auth url: "https://pub.dartlang.org" source: hosted - version: "1.1.10" + version: "1.1.11" logging: dependency: transitive description: @@ -313,7 +313,7 @@ packages: name: pointycastle url: "https://pub.dartlang.org" source: hosted - version: "3.5.1" + version: "3.5.2" pool: dependency: transitive description: @@ -327,14 +327,14 @@ packages: name: pub_semver url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" shelf: dependency: transitive description: name: shelf url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" shelf_packages_handler: dependency: transitive description: @@ -481,5 +481,5 @@ packages: source: hosted version: "3.1.0" sdks: - dart: ">=2.15.0-7.0.dev <3.0.0" + dart: ">=2.16.0 <3.0.0" flutter: ">=2.5.0" diff --git a/packages/cosmos_ui_components/pubspec.lock b/packages/cosmos_ui_components/pubspec.lock index 0f0da5e8..4da64cf0 100644 --- a/packages/cosmos_ui_components/pubspec.lock +++ b/packages/cosmos_ui_components/pubspec.lock @@ -69,7 +69,7 @@ packages: description: path: "packages/cosmos_utils" ref: main - resolved-ref: "99035c7be78a9959591220c8efb3e1e122274c03" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" diff --git a/packages/transaction_signing_gateway/pubspec.lock b/packages/transaction_signing_gateway/pubspec.lock index 9189a095..42fa8fd9 100644 --- a/packages/transaction_signing_gateway/pubspec.lock +++ b/packages/transaction_signing_gateway/pubspec.lock @@ -28,7 +28,7 @@ packages: name: archive url: "https://pub.dartlang.org" source: hosted - version: "3.2.2" + version: "3.3.0" args: dependency: transitive description: @@ -167,7 +167,7 @@ packages: description: path: "packages/cosmos_utils" ref: main - resolved-ref: "99035c7be78a9959591220c8efb3e1e122274c03" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" @@ -673,7 +673,7 @@ packages: name: win32 url: "https://pub.dartlang.org" source: hosted - version: "2.4.4" + version: "2.5.1" xdg_directories: dependency: transitive description: diff --git a/starport_template/lib/app_config.dart b/starport_template/lib/app_config.dart new file mode 100644 index 00000000..4a97edaf --- /dev/null +++ b/starport_template/lib/app_config.dart @@ -0,0 +1,61 @@ +import 'package:alan/alan.dart'; +import 'package:grpc/grpc.dart'; + +class AppConfig { + AppConfig({ + this.lcdUrl = 'http://10.0.2.2', + this.grpcUrl = 'http://localhost', + this.lcdPort = '1317', + this.grpcPort = '9090', + this.prefixAddress = 'cosmos', + }); + + /// Configure the LCD Url + final String lcdUrl; + + /// Configure the GRPC Url + final String grpcUrl; + + /// Configure the LCD Port + final String lcdPort; + + /// Configure the GRPC Port + final String grpcPort; + + /// Configure the address prefix + final String prefixAddress; + + /// Configure the base api url + String get baseApiUrl => '$lcdUrl:$lcdPort'; + + /// Network information of the Cosmos-based network. + NetworkInfo get networkInfo => NetworkInfo( + bech32Hrp: prefixAddress, + lcdInfo: LCDInfo( + host: lcdUrl, + port: int.parse(lcdPort), + ), + grpcInfo: GRPCInfo( + host: grpcUrl, + port: int.parse(grpcPort), + credentials: + int.parse(grpcPort) == 443 ? const ChannelCredentials.secure() : const ChannelCredentials.insecure(), + ), + ); + + AppConfig copyWith({ + String? lcdUrl, + String? grpcUrl, + String? lcdPort, + String? grpcPort, + String? prefixAddress, + }) { + return AppConfig( + lcdUrl: lcdUrl ?? this.lcdUrl, + grpcUrl: grpcUrl ?? this.grpcUrl, + lcdPort: lcdPort ?? this.lcdPort, + grpcPort: grpcPort ?? this.grpcPort, + prefixAddress: prefixAddress ?? this.prefixAddress, + ); + } +} diff --git a/starport_template/lib/base_env.dart b/starport_template/lib/base_env.dart deleted file mode 100644 index aadc1e2a..00000000 --- a/starport_template/lib/base_env.dart +++ /dev/null @@ -1,43 +0,0 @@ -class BaseEnv { - BaseEnv({ - this.envLcdUrl = 'http://10.0.2.2', - this.envGrpcUrl = 'http://localhost', - this.envLcdPort = '1317', - this.envGrpcPort = '9090', - this.prefixAddress = 'cosmos', - }); - - /// Configure the LCD Url - final String envLcdUrl; - - /// Configure the GRPC Url - final String envGrpcUrl; - - /// Configure the LCD Port - final String envLcdPort; - - /// Configure the GRPC Port - final String envGrpcPort; - - /// Configure the address prefix - final String prefixAddress; - - /// Configure the base api url - String get envBaseApiUrl => '$envLcdUrl:$envLcdPort'; - - BaseEnv copyWith({ - String? envLcdUrl, - String? envGrpcUrl, - String? envLcdPort, - String? envGrpcPort, - String? prefixAddress, - }) { - return BaseEnv( - envLcdUrl: envLcdUrl ?? this.envLcdUrl, - envGrpcUrl: envGrpcUrl ?? this.envGrpcUrl, - envLcdPort: envLcdPort ?? this.envLcdPort, - envGrpcPort: envGrpcPort ?? this.envGrpcPort, - prefixAddress: prefixAddress ?? this.prefixAddress, - ); - } -} diff --git a/starport_template/lib/main.dart b/starport_template/lib/main.dart index 14b458cf..12a3504f 100644 --- a/starport_template/lib/main.dart +++ b/starport_template/lib/main.dart @@ -2,12 +2,11 @@ import 'package:alan/alan.dart'; import 'package:cosmos_auth/cosmos_auth.dart'; import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:flutter/material.dart'; -import 'package:starport_template/base_env.dart'; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/starport_app.dart'; import 'package:starport_template/stores/accounts_store.dart'; import 'package:starport_template/stores/settings_store.dart'; import 'package:starport_template/stores/transactions_store.dart'; -import 'package:starport_template/utils/env_util.dart'; import 'package:transaction_signing_gateway/mobile/no_op_transaction_summary_ui.dart'; import 'package:transaction_signing_gateway/transaction_signing_gateway.dart'; @@ -17,9 +16,8 @@ void main() { } void _buildDependencies() { - final env = BaseEnv(); - StarportApp.baseEnv = BaseEnvUtil(env); - StarportApp.networkInfo = StarportApp.baseEnv.networkInfo; + StarportApp.appConfig = AppConfig(); + StarportApp.networkInfo = StarportApp.appConfig.networkInfo; _logBackendInfo(StarportApp.networkInfo); StarportApp.secureDataStore = FlutterSecureStorageDataStore(); @@ -39,9 +37,9 @@ void _buildDependencies() { ); StarportApp.cosmosAuth = CosmosAuth(); - StarportApp.accountsStore = AccountsStore(StarportApp.signingGateway, StarportApp.baseEnv); - StarportApp.settingsStore = SettingsStore(StarportApp.cosmosAuth, StarportApp.secureDataStore, StarportApp.baseEnv); - StarportApp.transactionsStore = TransactionsStore(StarportApp.baseEnv); + StarportApp.accountsStore = AccountsStore(StarportApp.signingGateway, StarportApp.appConfig); + StarportApp.settingsStore = SettingsStore(StarportApp.cosmosAuth, StarportApp.secureDataStore, StarportApp.appConfig); + StarportApp.transactionsStore = TransactionsStore(StarportApp.appConfig); } void _logBackendInfo(NetworkInfo networkInfo) => debugLog( diff --git a/starport_template/lib/starport_app.dart b/starport_template/lib/starport_app.dart index 7f770bf0..1d135def 100644 --- a/starport_template/lib/starport_app.dart +++ b/starport_template/lib/starport_app.dart @@ -3,11 +3,11 @@ import 'package:cosmos_auth/auth/cosmos_auth.dart'; import 'package:cosmos_ui_components/cosmos_theme.dart'; import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:flutter/material.dart'; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/pages/routing_page.dart'; import 'package:starport_template/stores/accounts_store.dart'; import 'package:starport_template/stores/settings_store.dart'; import 'package:starport_template/stores/transactions_store.dart'; -import 'package:starport_template/utils/env_util.dart'; import 'package:transaction_signing_gateway/gateway/transaction_signing_gateway.dart'; class StarportApp extends StatelessWidget { @@ -18,7 +18,7 @@ class StarportApp extends StatelessWidget { static late TransactionSigningGateway signingGateway; static late AccountsStore accountsStore; static late TransactionsStore transactionsStore; - static late BaseEnvUtil baseEnv; + static late AppConfig appConfig; static late NetworkInfo networkInfo; static late SecureDataStore secureDataStore; static late SettingsStore settingsStore; diff --git a/starport_template/lib/stores/accounts_store.dart b/starport_template/lib/stores/accounts_store.dart index cd6bcec5..270732fb 100644 --- a/starport_template/lib/stores/accounts_store.dart +++ b/starport_template/lib/stores/accounts_store.dart @@ -1,11 +1,11 @@ import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:flutter/foundation.dart'; import 'package:mobx/mobx.dart'; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/entities/account_additional_data.dart'; import 'package:starport_template/entities/balance.dart'; import 'package:starport_template/entities/import_account_form_data.dart'; import 'package:starport_template/utils/cosmos_balances.dart'; -import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/token_sender.dart'; import 'package:transaction_signing_gateway/alan/alan_account_derivation_info.dart'; import 'package:transaction_signing_gateway/transaction_signing_gateway.dart'; @@ -13,13 +13,13 @@ import 'package:transaction_signing_gateway/transaction_signing_gateway.dart'; class AccountsStore { AccountsStore( this._transactionSigningGateway, - this.baseEnv, + this.appConfig, ); static const chainId = 'my_starport_chain'; final TransactionSigningGateway _transactionSigningGateway; - final BaseEnvUtil baseEnv; + final AppConfig appConfig; final Observable _areAccountsLoading = Observable(false); final Observable _isSendMoneyLoading = Observable(false); @@ -149,7 +149,7 @@ class AccountsStore { isBalancesLoadingError = false; isBalancesLoading = true; try { - final newBalances = await CosmosBalances(baseEnv).getBalances(accountAddress); + final newBalances = await CosmosBalances(appConfig).getBalances(accountAddress); balancesList ..clear() ..addAll(newBalances); @@ -170,7 +170,7 @@ class AccountsStore { .deriveAccount( accountDerivationInfo: AlanAccountDerivationInfo( accountAlias: data.name, - networkInfo: baseEnv.networkInfo, + networkInfo: appConfig.networkInfo, mnemonic: data.mnemonic, chainId: chainId, ), diff --git a/starport_template/lib/stores/settings_store.dart b/starport_template/lib/stores/settings_store.dart index b4f8ad5d..771e8963 100644 --- a/starport_template/lib/stores/settings_store.dart +++ b/starport_template/lib/stores/settings_store.dart @@ -2,8 +2,8 @@ import 'package:cosmos_auth/auth/cosmos_auth.dart'; import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:mobx/mobx.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/pages/passcode_prompt_page.dart'; -import 'package:starport_template/utils/env_util.dart'; import 'package:starport_template/utils/node_info_loader.dart'; class SettingsStore { @@ -18,7 +18,7 @@ class SettingsStore { static const _nodeNetworkKey = 'node_network'; final CosmosAuth _cosmosAuth; - final BaseEnvUtil _baseEnv; + final AppConfig _baseEnv; final SecureDataStore _secureDataStore; final Observable _appLockEnabled = Observable(false); final Observable _biometricsEnabled = Observable(false); diff --git a/starport_template/lib/stores/transactions_store.dart b/starport_template/lib/stores/transactions_store.dart index 1ee4389f..94b91160 100644 --- a/starport_template/lib/stores/transactions_store.dart +++ b/starport_template/lib/stores/transactions_store.dart @@ -1,13 +1,13 @@ import 'package:cosmos_utils/cosmos_utils.dart'; import 'package:mobx/mobx.dart'; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/entities/transaction_history_item.dart'; import 'package:starport_template/utils/cosmos_transaction_history_loader.dart'; -import 'package:starport_template/utils/env_util.dart'; class TransactionsStore { - TransactionsStore(this.baseEnv); + TransactionsStore(this.appConfig); - final BaseEnvUtil baseEnv; + final AppConfig appConfig; final Observable _isTransactionHistoryLoading = Observable(false); final Observable _isTransactionHistoryError = Observable(false); @@ -24,7 +24,7 @@ class TransactionsStore { Future getTransactionHistory(String accountAddress) async { isTransactionHistoryLoading = true; try { - final list = await CosmosTransactionHistoryLoader(baseEnv).getTransactionHistory(accountAddress); + final list = await CosmosTransactionHistoryLoader(appConfig).getTransactionHistory(accountAddress); transactionsList ..clear() ..addAll(list); diff --git a/starport_template/lib/utils/cosmos_balances.dart b/starport_template/lib/utils/cosmos_balances.dart index e363bdb9..2973fe22 100644 --- a/starport_template/lib/utils/cosmos_balances.dart +++ b/starport_template/lib/utils/cosmos_balances.dart @@ -1,19 +1,19 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/entities/amount.dart'; import 'package:starport_template/entities/balance.dart'; import 'package:starport_template/entities/denom.dart'; import 'package:starport_template/model/balance_json.dart'; -import 'package:starport_template/utils/env_util.dart'; class CosmosBalances { - CosmosBalances(this.baseEnv); + CosmosBalances(this.appConfig); - BaseEnvUtil baseEnv; + AppConfig appConfig; Future> getBalances(String accountAddress) async { - final uri = '${baseEnv.baseApiUrl}/cosmos/bank/v1beta1/balances/$accountAddress'; + final uri = '${appConfig.baseApiUrl}/cosmos/bank/v1beta1/balances/$accountAddress'; final response = await http.get(Uri.parse(uri)); final map = jsonDecode(response.body) as Map; if (map['balances'] == null) { diff --git a/starport_template/lib/utils/cosmos_transaction_history_loader.dart b/starport_template/lib/utils/cosmos_transaction_history_loader.dart index 4eb4e73a..f325474a 100644 --- a/starport_template/lib/utils/cosmos_transaction_history_loader.dart +++ b/starport_template/lib/utils/cosmos_transaction_history_loader.dart @@ -1,16 +1,16 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:starport_template/app_config.dart'; import 'package:starport_template/entities/amount.dart'; import 'package:starport_template/entities/denom.dart'; import 'package:starport_template/entities/transaction_history_item.dart'; import 'package:starport_template/model/tx_response_json.dart'; -import 'package:starport_template/utils/env_util.dart'; class CosmosTransactionHistoryLoader { - CosmosTransactionHistoryLoader(this.baseEnv); + CosmosTransactionHistoryLoader(this.appConfig); - BaseEnvUtil baseEnv; + AppConfig appConfig; Future> getTransactionHistory(String accountAddress) async { final outGoingTransactions = await _getTransactionResponses(accountAddress, TransactionType.Send); @@ -24,7 +24,7 @@ class CosmosTransactionHistoryLoader { Future> _getTransactionResponses(String accountAddress, TransactionType type) async { final uri = - '${baseEnv.baseApiUrl}/cosmos/tx/v1beta1/txs?events=transfer.${type == TransactionType.Send ? 'sender' : 'recipient'}%3D%27$accountAddress%27'; + '${appConfig.baseApiUrl}/cosmos/tx/v1beta1/txs?events=transfer.${type == TransactionType.Send ? 'sender' : 'recipient'}%3D%27$accountAddress%27'; final response = await http.get(Uri.parse(uri)); final map = jsonDecode(response.body) as Map; if (map['tx_responses'] == null) { diff --git a/starport_template/lib/utils/env_util.dart b/starport_template/lib/utils/env_util.dart deleted file mode 100644 index 712afa64..00000000 --- a/starport_template/lib/utils/env_util.dart +++ /dev/null @@ -1,82 +0,0 @@ -// ignore_for_file: do_not_use_environment - -import 'dart:io'; - -import 'package:alan/alan.dart'; -import 'package:grpc/grpc.dart'; -import 'package:starport_template/base_env.dart'; - -class BaseEnvUtil { - BaseEnvUtil(BaseEnv env) - : networkInfo = NetworkInfo( - bech32Hrp: prefixAddress ?? env.prefixAddress, - lcdInfo: LCDInfo( - host: lcdUrl ?? env.envLcdUrl, - port: int.parse(lcdPort ?? env.envLcdPort), - ), - grpcInfo: GRPCInfo( - host: grpcUrl ?? env.envGrpcUrl, - port: int.parse(grpcPort ?? env.envGrpcPort), - credentials: int.parse(grpcPort ?? env.envGrpcPort) == 443 - ? const ChannelCredentials.secure() - : const ChannelCredentials.insecure(), - ), - ), - baseApiUrl = lcdUrl != null && lcdPort != null ? '$lcdUrl:$lcdPort' : env.envBaseApiUrl; - - final NetworkInfo networkInfo; - - final String baseApiUrl; -} - -String? get prefixAddress { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('PREFIX_ADDR'); - if (result.isNotEmpty) { - return result; - } - return null; -} - -String? get lcdPort { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('LCD_PORT'); - if (result.isNotEmpty) { - return result; - } - return null; -} - -String? get grpcPort { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('GRPC_PORT'); - - if (result.isNotEmpty) { - return result; - } - return null; -} - -String? get lcdUrl { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('LCD_URL'); - - if (result.isNotEmpty) { - return Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'; - } - return null; -} - -String? get grpcUrl { - // DO NOT USE String.fromEnvironment without 'const'!!! - // https://github.com/flutter/flutter/issues/55870#issuecomment-620776138 - const result = String.fromEnvironment('GRPC_URL'); - if (result.isNotEmpty) { - return Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'; - } - return null; -} diff --git a/starport_template/lib/utils/node_info_loader.dart b/starport_template/lib/utils/node_info_loader.dart index 7104130d..23e425c5 100644 --- a/starport_template/lib/utils/node_info_loader.dart +++ b/starport_template/lib/utils/node_info_loader.dart @@ -1,15 +1,15 @@ import 'dart:convert'; import 'package:http/http.dart' as http; -import 'package:starport_template/utils/env_util.dart'; +import 'package:starport_template/app_config.dart'; class NodeInfoLoader { - NodeInfoLoader(this.baseEnv); + NodeInfoLoader(this.appConfig); - BaseEnvUtil baseEnv; + AppConfig appConfig; Future getChainId() async { - final uri = '${baseEnv.baseApiUrl}/node_info'; + final uri = '${appConfig.baseApiUrl}/node_info'; final response = await http.get(Uri.parse(uri)); final map = jsonDecode(response.body) as Map; if (map['node_info'] == null) { diff --git a/starport_template/pubspec.lock b/starport_template/pubspec.lock index 23b34c2a..a57064cf 100644 --- a/starport_template/pubspec.lock +++ b/starport_template/pubspec.lock @@ -28,7 +28,7 @@ packages: name: archive url: "https://pub.dartlang.org" source: hosted - version: "3.2.2" + version: "3.3.0" args: dependency: transitive description: @@ -153,7 +153,7 @@ packages: description: path: "packages/cosmos_auth" ref: main - resolved-ref: "99035c7be78a9959591220c8efb3e1e122274c03" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" @@ -162,7 +162,7 @@ packages: description: path: "packages/cosmos_ui_components" ref: main - resolved-ref: "99035c7be78a9959591220c8efb3e1e122274c03" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" @@ -171,7 +171,7 @@ packages: description: path: "packages/cosmos_utils" ref: main - resolved-ref: "99035c7be78a9959591220c8efb3e1e122274c03" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" @@ -788,7 +788,7 @@ packages: description: path: "packages/transaction_signing_gateway" ref: main - resolved-ref: "99035c7be78a9959591220c8efb3e1e122274c03" + resolved-ref: "6d6a66c11aec98326728f99eab2672a88a9de8a3" url: "https://github.com/tendermint/flutter.git" source: git version: "0.0.1" @@ -882,7 +882,7 @@ packages: name: win32 url: "https://pub.dartlang.org" source: hosted - version: "2.4.4" + version: "2.5.1" xdg_directories: dependency: transitive description: From a3c623fecd5c9a6eb26325350c40636cd808aebd Mon Sep 17 00:00:00 2001 From: Chiziaruhoma Date: Fri, 1 Apr 2022 13:47:14 +0100 Subject: [PATCH 4/6] fix lint problems --- packages/cosmos_auth/test/mocks/mocks.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/cosmos_auth/test/mocks/mocks.dart b/packages/cosmos_auth/test/mocks/mocks.dart index 0cd1ed61..2f064619 100644 --- a/packages/cosmos_auth/test/mocks/mocks.dart +++ b/packages/cosmos_auth/test/mocks/mocks.dart @@ -6,6 +6,12 @@ import 'package:mocktail/mocktail.dart'; class StubSecureDataStore implements SecureDataStore { Map map = {}; + @override + Future> clearAllData() async { + map.clear(); + return right(true); + } + @override Future> readSecureText({ required String key, From b0b5ea9cad837343ee14589bef368887c2dd309e Mon Sep 17 00:00:00 2001 From: Chiziaruhoma Date: Fri, 1 Apr 2022 14:37:03 +0100 Subject: [PATCH 5/6] updated the docs --- starport_template/README.md | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/starport_template/README.md b/starport_template/README.md index fa0518b5..22a240b0 100644 --- a/starport_template/README.md +++ b/starport_template/README.md @@ -1,32 +1,46 @@ ## Setup and run Flutter app - [Setup Flutter based on your operating system](https://flutter.dev/docs/get-started/install) + - Make sure you follow all the steps above and have an all-green output of `flutter doctor` command. + - [Install and run Anroid Studio once](https://developer.android.com/studio/install) + - (For iOS) Install XCode from AppStore + - You can use Android Studio, [IntelliJ Idea](https://www.jetbrains.com/idea/download), or [VSCode](https://code.visualstudio.com/download) for Flutter development + - Install Flutter plugin in your chosen IDE + - Clone this repository using `git clone https://github.com/tendermint/flutter.git` + - To run it on your `localhost` environment, you will just have to run `main.dart` file in your project. That can be done directly by clicking on the play button in your IDE, or by running `flutter run` from the root of your project. This will run the app on any device or simulator connected to your system + - If you're running the app on real device instead of emulator/simulator or want to taget a specific blockchain run on a - remote machine, make sure to specify proper urls and ports when running the app. Here is an example for running the - app on cosmos hub testnet: + remote machine, make sure to specify proper urls and ports when running the app. + +- Edit and update your environment variables in the [`app_config.dart`](https://github.com/tendermint/flutter/blob/main/starport_template/app_config.dart) + +- Run the `starport_template` app using `fvm flutter run` or `make run-dev` commands: -``` -flutter run --dart-define=LCD_URL=https://api.testnet.cosmos.network --dart-define=LCD_PORT=443 --dart-define=GRPC_URL=https://grpc.testnet.cosmos.network --dart-define=GRPC_PORT=443 -``` ## Using template from Starport's scaffolded chain - Clone the [Starport](https://github.com/tendermint/starport) repository from `develop` branch + - Make sure to install [GoLang](https://go.dev/dl/) for your platform + - Run `make install` in Starport's root directory + - Run `starport scaffold chain github.com/cosmonaut/mars` to create a new chain + - Run `cd mars` + - Run `starport scaffold flutter` to create a new Flutter app (this clones the `starport_template` repository - the current repo) and puts in under `flutter` subdirectory + - Run `starport generate dart` to generate dart files from proto files to be used by the template to interact with the blockchain \ No newline at end of file From d3969633c4eccf443b9501833aa647cd1e703b17 Mon Sep 17 00:00:00 2001 From: Chiziaruhoma Date: Tue, 5 Apr 2022 08:45:24 +0100 Subject: [PATCH 6/6] updated lcdurl --- starport_template/lib/app_config.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starport_template/lib/app_config.dart b/starport_template/lib/app_config.dart index 4a97edaf..5adf32ce 100644 --- a/starport_template/lib/app_config.dart +++ b/starport_template/lib/app_config.dart @@ -3,7 +3,7 @@ import 'package:grpc/grpc.dart'; class AppConfig { AppConfig({ - this.lcdUrl = 'http://10.0.2.2', + this.lcdUrl = 'http://localhost', this.grpcUrl = 'http://localhost', this.lcdPort = '1317', this.grpcPort = '9090',