From 3513b0440daf9b468c8205af200a431587c2a144 Mon Sep 17 00:00:00 2001 From: poka Date: Fri, 18 Feb 2022 00:20:21 +0100 Subject: [PATCH] Hello substrate sandbox: Successfully connected to local duniter v2s node; Best bloc realtime subscription --- android/app/build.gradle | 4 +- android/app/src/main/AndroidManifest.xml | 7 +- lib/main.dart | 8 +- lib/providers/substrate_sdk.dart | 45 +++++++++ lib/screens/home.dart | 22 ++++- lib/screens/substrate_sandbox.dart | 64 +++++++++++++ pubspec.lock | 112 +++++++++++++++++++++++ pubspec.yaml | 1 + 8 files changed, 255 insertions(+), 8 deletions(-) create mode 100644 lib/providers/substrate_sdk.dart create mode 100644 lib/screens/substrate_sandbox.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index 095df97..799e480 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -45,8 +45,8 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "gecko.axiomteam.fr" - minSdkVersion 16 - targetSdkVersion 30 + minSdkVersion 19 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index b6701ee..6ed9014 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -9,9 +9,12 @@ - + android:label="Ğecko" + android:usesCleartextTraffic="true"> + WalletOptionsProvider()), ChangeNotifierProvider(create: (_) => ChangePinProvider()), ChangeNotifierProvider(create: (_) => SearchProvider()), - ChangeNotifierProvider(create: (_) => CesiumPlusProvider()) + ChangeNotifierProvider(create: (_) => CesiumPlusProvider()), + ChangeNotifierProvider(create: (_) => SubstrateSdk()) ], child: GraphQLProvider( client: _client, @@ -178,8 +180,8 @@ class Gecko extends StatelessWidget { ), primaryColor: const Color(0xffFFD58D), textTheme: const TextTheme( - bodyText1: TextStyle(), - bodyText2: TextStyle(), + bodyText1: TextStyle(fontSize: 20), + bodyText2: TextStyle(fontSize: 18), ).apply( bodyColor: const Color(0xFF000000), ), diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart new file mode 100644 index 0000000..6fa3ca1 --- /dev/null +++ b/lib/providers/substrate_sdk.dart @@ -0,0 +1,45 @@ +import 'package:flutter/material.dart'; +import 'package:polkawallet_sdk/api/types/networkParams.dart'; +import 'package:polkawallet_sdk/polkawallet_sdk.dart'; +import 'package:polkawallet_sdk/storage/keyring.dart'; + +class SubstrateSdk with ChangeNotifier { + final String subNode = '192.168.1.85:9944'; + final bool isSsl = false; + + final WalletSDK sdk = WalletSDK(); + final Keyring keyring = Keyring(); + bool sdkReady = false; + bool nodeConnected = false; + int blocNumber = 0; + + Future initApi() async { + await keyring.init([0, 2]); + + await sdk.init(keyring); + sdkReady = true; + notifyListeners(); + } + + Future connectNode() async { + final String socketKind = isSsl ? 'wss' : 'ws'; + final node = NetworkParams(); + node.name = 'pokaniter'; + node.endpoint = '$socketKind://$subNode'; + node.ss58 = 42; + final res = await sdk.api.connectNode(keyring, [node]).timeout( + const Duration(seconds: 10), + onTimeout: () => null, + ); + if (res != null) { + nodeConnected = true; + notifyListeners(); + } + + // Subscribe bloc number + sdk.api.setting.subscribeBestNumber((res) { + blocNumber = int.parse(res.toString()); + notifyListeners(); + }); + } +} diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 23d4759..7be0ce5 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -12,6 +12,7 @@ import 'package:gecko/screens/onBoarding/1.dart'; import 'package:gecko/screens/search.dart'; import 'package:gecko/screens/settings.dart'; import 'package:flutter/services.dart'; +import 'package:gecko/screens/substrate_sandbox.dart'; import 'package:provider/provider.dart'; class HomeScreen extends StatelessWidget { @@ -176,9 +177,28 @@ Widget geckHome(context) { ), ], ), - ) + ), ]), ), + const SizedBox(height: 15), + Row(mainAxisAlignment: MainAxisAlignment.center, children: [ + ElevatedButton( + style: ElevatedButton.styleFrom( + primary: yellowC, // background + onPrimary: Colors.black, // foreground + ), + onPressed: () => Navigator.push( + context, + MaterialPageRoute(builder: (context) { + return const SubstrateSandBox(); + }), + ), + child: const Text( + 'SUBSTRATE SANDBOX', + style: TextStyle(fontSize: 20), + ), + ), + ]), Expanded( flex: 1, child: Container( diff --git a/lib/screens/substrate_sandbox.dart b/lib/screens/substrate_sandbox.dart new file mode 100644 index 0000000..89b4997 --- /dev/null +++ b/lib/screens/substrate_sandbox.dart @@ -0,0 +1,64 @@ +import 'package:flutter/material.dart'; +import 'package:gecko/globals.dart'; +import 'package:gecko/providers/substrate_sdk.dart'; +import 'package:provider/provider.dart'; + +class SubstrateSandBox extends StatelessWidget { + const SubstrateSandBox({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + SubstrateSdk _sub = Provider.of(context, listen: false); + + return StatefulWrapper( + onInit: () async { + await _sub.initApi(); + await _sub.connectNode(); + }, + child: Scaffold( + appBar: AppBar( + toolbarHeight: 60 * ratio, + title: const SizedBox( + height: 22, + child: Text('Substrate Sandbox'), + ), + ), + body: SafeArea( + child: Consumer(builder: (context, _sub, _) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('js-api chargé ?: ${_sub.sdkReady}'), + Text( + 'Noeud connecté ?: ${_sub.nodeConnected} (${_sub.subNode})'), + if (_sub.nodeConnected) + Text('Numéro de bloc: ${_sub.blocNumber}'), + ]); + }), + ), + ), + ); + } +} + +class StatefulWrapper extends StatefulWidget { + final Function onInit; + final Widget child; + const StatefulWrapper({Key? key, required this.onInit, required this.child}) + : super(key: key); + @override + _StatefulWrapperState createState() => _StatefulWrapperState(); +} + +class _StatefulWrapperState extends State { + @override + void initState() { + widget.onInit(); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return widget.child; + } +} diff --git a/pubspec.lock b/pubspec.lock index 5d802de..9d8eb23 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -8,6 +8,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "30.0.0" + aes_ecb_pkcs5_flutter: + dependency: transitive + description: + name: aes_ecb_pkcs5_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2" analyzer: dependency: transitive description: @@ -43,6 +50,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.8.2" + auth_header: + dependency: transitive + description: + name: auth_header + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" barcode: dependency: transitive description: @@ -347,6 +361,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_inappwebview: + dependency: transitive + description: + name: flutter_inappwebview + url: "https://pub.dartlang.org" + source: hosted + version: "5.3.2" flutter_launcher_icons: dependency: "direct main" description: @@ -404,6 +425,20 @@ packages: description: flutter source: sdk version: "0.0.0" + get: + dependency: transitive + description: + name: get + url: "https://pub.dartlang.org" + source: hosted + version: "4.6.1" + get_storage: + dependency: transitive + description: + name: get_storage + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" glob: dependency: transitive description: @@ -530,6 +565,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.0.0" + http_server: + dependency: transitive + description: + name: http_server + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" image: dependency: transitive description: @@ -591,6 +633,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.3" + jaguar: + dependency: transitive + description: + name: jaguar + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.12" + jaguar_common: + dependency: transitive + description: + name: jaguar_common + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + jaguar_flutter_asset: + dependency: transitive + description: + name: jaguar_flutter_asset + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" jdenticon_dart: dependency: "direct main" description: @@ -668,6 +731,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.1" + mobx: + dependency: transitive + description: + name: mobx + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.6+1" nested: dependency: transitive description: @@ -815,6 +885,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.3" + path_tree: + dependency: transitive + description: + name: path_tree + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" pdf: dependency: "direct main" description: @@ -885,6 +962,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.4.0" + polkawallet_sdk: + dependency: "direct main" + description: + name: polkawallet_sdk + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.1" pool: dependency: transitive description: @@ -1258,6 +1342,34 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0" + webview_flutter: + dependency: transitive + description: + name: webview_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "2.8.0" + webview_flutter_android: + dependency: transitive + description: + name: webview_flutter_android + url: "https://pub.dartlang.org" + source: hosted + version: "2.8.3" + webview_flutter_platform_interface: + dependency: transitive + description: + name: webview_flutter_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.0" + webview_flutter_wkwebview: + dependency: transitive + description: + name: webview_flutter_wkwebview + url: "https://pub.dartlang.org" + source: hosted + version: "2.7.1" win32: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index ee0dde1..ade1322 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -72,6 +72,7 @@ dependencies: desktop_window: ^0.4.0 durt: ^0.1.6 package_info_plus: ^1.3.0 + polkawallet_sdk: ^0.4.1 flutter_icons: android: "ic_launcher"