Skip to content

Commit

Permalink
Refactor code fore blockchain network management (#1691)
Browse files Browse the repository at this point in the history
* [settings] remove unused `NetworkState` class

* [config] introduce network enum for different networks (it is not used yet)

* remove obsolete fields of endpoint data and settings store

* [settingsStore] remove obsolete endpointList observable

* [settingsStore] switch from EndpointData to the new Network enum

* remove obsolete hardcoded encointer endpoints

* [consts] rename `ipfs_gateway_local` to `ipfsGatewayLocal`

* [consts] remove obsolete `faucetAmount`

* fix: endpoint getters

* [settings] fix: remove obsolete EndpointData class

* [encointer_store_test] fix: serialization roundtrip

* [LocalStorage] disambiguate between removeObject and removeKV

* fmt

* [settings] re-introduce default endpoint test

* [config/networks] proper enum ordering and make clear that we are not referring to the relaychains

* [config/networks] rename info to id

* [config/networks] add comment about removing `value`

* [mockData] remove obsolete `unitTestEndpointInfo`

* [settings] rename misnomer `endpoint` to `currentNetwork`
  • Loading branch information
clangenb authored Jul 16, 2024
1 parent 708606e commit 68e3ec7
Show file tree
Hide file tree
Showing 31 changed files with 247 additions and 469 deletions.
2 changes: 1 addition & 1 deletion app/lib/common/components/account_select_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AccountSelectList extends StatelessWidget {
Widget build(BuildContext context) {
return ListView(
children: accounts.map((account) {
final address = AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: store.settings.endpoint.ss58!);
final address = AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: store.settings.currentNetwork.ss58());

return ListTile(
leading: AddressIcon(address, account.pubKey),
Expand Down
8 changes: 5 additions & 3 deletions app/lib/common/components/address_input_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AddressInputField extends StatefulWidget {
class _AddressInputFieldState extends State<AddressInputField> {
/// Returns true if the [account]'s name or address starts with [nameOrAddress].
bool filterByAddressOrName(AccountData account, String nameOrAddress) {
final ss58 = widget.store.settings.endpoint.ss58!;
final ss58 = widget.store.settings.currentNetwork.ss58();
// we can't just use account.address unfortunately, see #1019.
return account.name.startsWith(nameOrAddress.trim()) ||
AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: ss58).startsWith(nameOrAddress.trim());
Expand All @@ -44,7 +44,8 @@ class _AddressInputFieldState extends State<AddressInputField> {
return Container();
}

final address = AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: widget.store.settings.endpoint.ss58!);
final address =
AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: widget.store.settings.currentNetwork.ss58());

return Container(
padding: const EdgeInsets.only(top: 8),
Expand All @@ -71,7 +72,8 @@ class _AddressInputFieldState extends State<AddressInputField> {
}

Widget _listItemBuilder(BuildContext context, AccountData account, bool isSelected) {
final address = AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: widget.store.settings.endpoint.ss58!);
final address =
AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: widget.store.settings.currentNetwork.ss58());

return Container(
margin: const EdgeInsets.symmetric(horizontal: 8),
Expand Down
68 changes: 1 addition & 67 deletions app/lib/config/consts.dart
Original file line number Diff line number Diff line change
@@ -1,80 +1,16 @@
import 'dart:io';

import 'package:encointer_wallet/config/prod_community.dart';
import 'package:encointer_wallet/store/settings.dart';

const String androidLocalHost = '10.0.2.2';
const String iosLocalHost = 'localhost';

EndpointData networkEndpointEncointerGesell = EndpointData.fromJson({
'info': 'nctr-gsl',
'ss58': 42,
'text': 'Encointer Gesell (Hosted by Encointer Association)',
'value': 'wss://gesell.encointer.org',
'ipfsGateway': ipfsGatewayEncointer
});

EndpointData networkEndpointEncointerLietaer = EndpointData.fromJson({
'info': 'nctr-r',
'ss58': 42,
'text': 'Encointer Lietaer on Rococo (Hosted by Encointer Association)',
'value': 'wss://rococo.api.encointer.org',
'ipfsGateway': ipfsGatewayEncointer
});

EndpointData networkEndpointEncointerMainnet = EndpointData.fromJson({
'info': 'nctr-k',
'ss58': 2,
'text': 'Encointer Network on Kusama (Hosted by Encointer Association)',
'value': 'wss://kusama.api.encointer.org',
'ipfsGateway': ipfsGatewayEncointer
});

EndpointData networkEndpointEncointerGesellDev = EndpointData.fromJson({
'info': 'nctr-gsl-dev',
'ss58': 42,
'text': 'Encointer Gesell Local Devnet',
'value':
'ws://${Platform.isAndroid ? androidLocalHost : iosLocalHost}:9944', // do not use the docker's address, use the host's
'ipfsGateway': ipfs_gateway_local
});

EndpointData networkEndpointEncointerCantillon = EndpointData.fromJson({
'info': 'nctr-cln',
'ss58': 42,
'text': 'Encointer Cantillon (Hosted by Encointer Association)',
'value': 'wss://cantillon.encointer.org',
'worker': 'wss://substratee03.scs.ch',
'mrenclave': 'CbE3fPWjeYVo9LSNKgPPiCXThFBjfhP1GK6Y9S7t5WVe',
'ipfsGateway': ipfsGatewayEncointer
});

EndpointData networkEndpointEncointerCantillonDev = EndpointData.fromJson({
'info': 'nctr-cln-dev',
'ss58': 42,
'text': 'Encointer Cantillon (Hosted by Encointer Association)',
'value': 'ws://10.0.0.134:9979', // do not use the docker's address, use the host's
'worker': 'ws:/10.0.0.134:2079',
'mrenclave': '4SkU25tusVChcrUprW8X22QoEgamCgj3HKQeje7j8Z4E',
'ipfsGateway': ipfsGatewayEncointer
});

List<EndpointData> networkEndpoints = [
networkEndpointEncointerGesell,
networkEndpointEncointerLietaer,
networkEndpointEncointerMainnet,
networkEndpointEncointerGesellDev,
// networkEndpointEncointerCantillon,
// networkEndpointEncointerCantillonDev,
];

const fallBackCommunityIcon = 'assets/nctr_logo_faces_only_thick.svg';
const communityIconName = 'community_icon.svg';

// AVD: ${Platform.isAndroid ? androidLocalHost : iosLocalHost} = 127.0.0.1
const String ipfsGatewayEncointer = 'http://ipfs.encointer.org:8080';
// ignore: non_constant_identifier_names
final String ipfs_gateway_local = 'http://${Platform.isAndroid ? androidLocalHost : iosLocalHost}:8080';
final String ipfsGatewayLocal = 'http://${Platform.isAndroid ? androidLocalHost : iosLocalHost}:8080';

const encointerFeed = 'https://encointer.github.io/feed';
const communityMessagesPath = 'community_messages/$localePlaceHolder/cm.json';
Expand All @@ -87,8 +23,6 @@ String getEncointerFeedLink({bool devMode = false}) {
const int ertDecimals = 12;
const int encointerCurrenciesDecimals = 18;

const double faucetAmount = 0.1;

// links
const localePlaceHolder = 'LOCALE_PLACEHOLDER';
const ceremonyInfoLinkBase = 'https://leu.zuerich/$localePlaceHolder/#zeremonien';
Expand Down
128 changes: 128 additions & 0 deletions app/lib/config/networks/networks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'dart:io';

import 'package:encointer_wallet/config/consts.dart';

class NetworkEndpoint {
NetworkEndpoint({required this.name, required this.address});

final String name;
final String address;
}

const String gesellId = 'nctr-gsl';
const String gesellDevId = 'nctr-gsl-dev';
const String rococoId = 'nctr-r';
const String kusamaId = 'nctr-k';

/// Enum representing the different networks.
///
/// NOTE: We shouldn't do `_` wildcard matching in the switch statement so that
/// we get guaranteed type safety when we extend the enum variant due to the
/// compiler check for exhaustive matching.
enum Network {
encointerKusama,
encointerRococo,
gesell,
gesellDev;

factory Network.fromInfoOrDefault(String info) {
return switch (info) {
kusamaId => Network.encointerKusama,
rococoId => Network.encointerRococo,
gesellId => Network.gesell,
gesellDevId => Network.gesellDev,
_ => Network.encointerKusama,
};
}

factory Network.tryFromInfo(String info) {
return switch (info) {
kusamaId => Network.encointerKusama,
rococoId => Network.encointerRococo,
gesellId => Network.gesell,
gesellDevId => Network.gesellDev,
_ => throw Exception(['Invalid network $info']),
};
}

String id() {
return switch (this) {
encointerKusama => kusamaId,
encointerRococo => rococoId,
gesell => gesellId,
gesellDev => gesellDevId,
};
}

int ss58() {
return switch (this) {
encointerKusama => 2,
encointerRococo => 42,
gesell => 42,
gesellDev => 42,
};
}

/// After #1603 is implemented, we can also replace this with multiple endpoints,
/// such that we have fallback endpoints.
String ipfsGateway() {
return switch (this) {
encointerKusama => ipfsGatewayEncointer,
encointerRococo => ipfsGatewayEncointer,
gesell => ipfsGatewayEncointer,
// only dev network refers to the local one
gesellDev => ipfsGatewayLocal,
};
}

/// Exists for simple reverse compatibility.
/// Will be remove in the course of https://github.com/encointer/encointer-wallet-flutter/issues/1603.
String value() {
return switch (this) {
encointerKusama => networkEndpoints().first.address,
encointerRococo => networkEndpoints().first.address,
gesell => networkEndpoints().first.address,
// only dev network refers to the local one
gesellDev => networkEndpoints().first.address,
};
}

List<NetworkEndpoint> networkEndpoints() {
return switch (this) {
encointerKusama => kusamaEndpoints(),
encointerRococo => rococoEndpoints(),
gesell => gesellEndpoints(),
gesellDev => gesellDevEndpoints(),
};
}
}

List<NetworkEndpoint> gesellEndpoints() {
return [
NetworkEndpoint(name: 'Encointer Gesell (Hosted by Encointer Association)', address: 'wss://gesell.encointer.org')
];
}

List<NetworkEndpoint> gesellDevEndpoints() {
return [
NetworkEndpoint(
name: 'Encointer Gesell Local DevNet',
address: 'ws://${Platform.isAndroid ? androidLocalHost : iosLocalHost}:9944')
];
}

List<NetworkEndpoint> rococoEndpoints() {
return [
NetworkEndpoint(
name: 'Encointer Lietaer on Rococo (Hosted by Encointer Association)',
address: 'wss://rococo.api.encointer.org')
];
}

List<NetworkEndpoint> kusamaEndpoints() {
return [
NetworkEndpoint(
name: 'Encointer Network on Kusama (Hosted by Encointer Association)',
address: 'wss://kusama.api.encointer.org')
];
}
2 changes: 1 addition & 1 deletion app/lib/modules/account/view/import_account_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ImportAccountForm extends StatelessWidget with HandleNewAccountResultMixin

await AppAlert.showDialog<void>(
context,
title: Text(Fmt.address(acc.address(prefix: store.settings.endpoint.ss58 ?? 42).encode())!),
title: Text(Fmt.address(acc.address(prefix: store.settings.currentNetwork.ss58()).encode())!),
content: Text(l10n.importDuplicate),
actions: [
CupertinoButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class _TransferHistoryViewStoreBase with Store {
try {
final address = AddressUtils.pubKeyHexToAddress(
pubKey,
prefix: appStore.settings.endpoint.ss58 ?? 42,
prefix: appStore.settings.currentNetwork.ss58(),
);

final response = await ewHttp.getTypeList<Transaction>(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/page/assets/receive/receive_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _ReceivePageState extends State<ReceivePage> {
super.initState();
_appStore = context.read<AppStore>();
final address = AddressUtils.pubKeyHexToAddress(_appStore.account.currentAccountPubKey!,
prefix: _appStore.settings.endpoint.ss58!);
prefix: _appStore.settings.currentNetwork.ss58());
invoice = InvoiceQrCode(
account: address,
cid: _appStore.encointer.chosenCid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class PaymentOverview extends StatelessWidget {
@override
Widget build(BuildContext context) {
final recipientLabel = recipientAccount!.name;
final recipientAddress =
Fmt.address(AddressUtils.pubKeyHexToAddress(recipientAccount!.pubKey, prefix: store.settings.endpoint.ss58!))!;
final recipientAddress = Fmt.address(
AddressUtils.pubKeyHexToAddress(recipientAccount!.pubKey, prefix: store.settings.currentNetwork.ss58()))!;

return IntrinsicHeight(
child: Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _PaymentConfirmationPageState extends State<PaymentConfirmationPage> {
final amount = params.amount;
final recipientAddress = Address(
pubkey: AddressUtils.pubKeyHexToPubKey(recipientAccount.pubKey),
prefix: store.settings.endpoint.ss58!,
prefix: store.settings.currentNetwork.ss58(),
);

return Scaffold(
Expand Down
Loading

0 comments on commit 68e3ec7

Please sign in to comment.