-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality of initialize node in the background (#67)
* chore: add `native_resources` directory to `gitignore` * fix: fix conflict at using import files of `fluent_ui.dart` and `material.dart` - flutter version upgrade to `3.27.3` - refactor customized_widget/screens by replacing fluent_ui widgets - remove duplicate folder `splash_screen` * fix: fix conflict at using import files of `fluent_ui.dart` and `material.dart` - flutter version upgrade to `3.27.3` - refactor customized_widget/screens by replacing fluent_ui widgets - remove duplicate folder `splash_screen` * docs: update `CHANGELOG.md` & `pubspec.yaml` files * ci: update `ci.yml` & `release.yml` files * update: add/update `native_resources` directory to `gitignore` * fix: fixing some issues on toolbar logo widget tests * chore: add some packages/libraries - `file_selector` for call select file and folder by native ui - `bip39_mnemonic` for create wallet seeds - `crypto` , `bcrypt` and `convert` used for secure layer - `process_run` for call commands on daemon files * feat: create `SeedGenerator` tools for create secure and random seeds * update: modify `RestorationSeedPage` to include sample seed generation - fix color of text in `WelcomePage` - add seed generation 12 & 24 modes to `RestorationSeedPage` * update: modify `ValidatorConfigPage` to add directory selector for initial node address setup * chore: update some packages & libraries * chore: update native modules (macos/windows/linux) * feat: add logic code in background for initialized node * docs: update `CHANGELOG.md` & `pubspec.yaml` files * docs: update docs for `DaemonCubit` class * docs: update docs for `NodeConfigData` class --------- Co-authored-by: PouriaMoradi021 <[email protected]> Co-authored-by: = <=>
- Loading branch information
1 parent
e43458b
commit abc9ccc
Showing
19 changed files
with
529 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'dart:io'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:gui/src/core/utils/daemon_manager/bloc/daemon_state.dart'; | ||
import 'package:path/path.dart' show dirname; | ||
|
||
/// [DaemonCubit] Documentation: | ||
/// `DaemonCubit` manages the execution of the Pactus daemon process. | ||
/// It extends `Cubit<DaemonState>` and provides methods to run the daemon | ||
/// while handling its execution state. | ||
/// | ||
/// ## Features: | ||
/// - Runs the Pactus daemon using a given command and arguments. | ||
/// - Emits different states (`DaemonLoading`, `DaemonSuccess`, `DaemonError`) | ||
/// based on the execution outcome. | ||
/// - Handles standard output and errors from the process. | ||
/// | ||
/// ## Usage: | ||
/// ```dart | ||
/// final daemonCubit = DaemonCubit(); | ||
/// daemonCubit.runPactusDaemon(command: 'pactusd', arguments: ['--start']); | ||
/// ``` | ||
/// | ||
/// ## Notes: | ||
/// - The working directory is set to a Linux-specific path. | ||
/// - Needs adaptation for other operating systems. | ||
/// - Handles exceptions and errors gracefully. | ||
class DaemonCubit extends Cubit<DaemonState> { | ||
DaemonCubit() : super(DaemonInitial()); | ||
|
||
/// [runPactusDaemon] Documentation: | ||
/// Runs the Pactus daemon process. | ||
/// | ||
/// - [command]: The command to execute (e.g., "pactusd"). | ||
/// - [arguments]: A list of arguments to pass to the command. | ||
/// | ||
/// Emits: | ||
/// - `DaemonLoading` before execution starts. | ||
/// - `DaemonSuccess` if the process runs successfully. | ||
/// - `DaemonError` if an error occurs. | ||
Future<void> runPactusDaemon({ | ||
// required String workingDirectory, | ||
required String command, | ||
required List<String> arguments, | ||
}) async { | ||
emit(DaemonLoading()); | ||
|
||
try { | ||
final scriptDir = dirname(Platform.script.toFilePath()); | ||
// TODO(Esmaeil): this part need handled for another os | ||
final targetPath = '$scriptDir/lib/src/core/native_resources/linux/'; | ||
|
||
final result = | ||
await Process.run(command, arguments, workingDirectory: targetPath); | ||
if (result.exitCode == 0) { | ||
emit(DaemonSuccess('${result.stdout}')); | ||
} else { | ||
emit(DaemonError('${result.stderr}')); | ||
} | ||
} on Exception catch (e) { | ||
emit(DaemonError('Exception occurred: $e')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
abstract class DaemonState {} | ||
|
||
class DaemonInitial extends DaemonState {} | ||
|
||
class DaemonLoading extends DaemonState {} | ||
|
||
class DaemonSuccess extends DaemonState { | ||
DaemonSuccess(this.output); | ||
final String output; | ||
} | ||
|
||
class DaemonError extends DaemonState { | ||
DaemonError(this.error); | ||
final String error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// [NodeConfigData] Documentation: | ||
/// `NodeConfigData` is a singleton class that stores and manages | ||
/// configuration data related to a Pactus blockchain node initialization . | ||
/// | ||
/// ## Features: | ||
/// - Implements the Singleton pattern to ensure a single instance. | ||
/// - Provides getters and setters for essential configuration properties. | ||
/// - Stores working directory, restoration seed, password, and validator | ||
/// quantity. | ||
/// | ||
/// ## Usage: | ||
/// ```dart | ||
/// final config = NodeConfigData.instance; | ||
/// config.workingDirectory = "/path/to/dir"; | ||
/// print(config.workingDirectory); | ||
/// ``` | ||
/// | ||
/// ## Notes: | ||
/// - Default values are empty strings to prevent null issues. | ||
class NodeConfigData { | ||
// Private constructor to enforce the Singleton pattern | ||
NodeConfigData._internal(); | ||
|
||
/// The single instance of [NodeConfigData] | ||
static final NodeConfigData instance = NodeConfigData._internal(); | ||
|
||
// Private variables | ||
String? _workingDirectory; | ||
String? _restorationSeed; | ||
String? _password; | ||
String? _validatorQty; | ||
|
||
// Getters - return empty strings if values are null | ||
String get workingDirectory => _workingDirectory ?? ''; | ||
String get restorationSeed => _restorationSeed ?? ''; | ||
String get password => _password ?? ''; | ||
String get validatorQty => _validatorQty ?? ''; | ||
|
||
// Setters - update private variables | ||
set workingDirectory(String value) { | ||
_workingDirectory = value; | ||
} | ||
|
||
set restorationSeed(String value) { | ||
_restorationSeed = value; | ||
} | ||
|
||
set password(String value) { | ||
_password = value; | ||
} | ||
|
||
set validatorQty(String value) { | ||
_validatorQty = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:math'; | ||
import 'dart:typed_data'; | ||
import 'package:bcrypt/bcrypt.dart'; | ||
import 'package:bip39_mnemonic/bip39_mnemonic.dart'; | ||
import 'package:convert/convert.dart'; | ||
|
||
class SeedGenerator { | ||
Mnemonic? generateSeed(int wordCount) { | ||
Mnemonic? mnemonic; | ||
try { | ||
// Determine the entropy length based on the word count | ||
final entropyLength = (wordCount == 12) | ||
? 128 | ||
: (wordCount == 24) | ||
? 256 | ||
: 128; | ||
|
||
// Generate secure random bytes for entropy | ||
final secureRandomBytes = generateSecureRandomBytes(entropyLength ~/ 8); | ||
|
||
// Generate a secure passphrase | ||
final securePassphrase = generateSecurePassphrase(secureRandomBytes); | ||
|
||
// Generate the mnemonic based on the entropy and passphrase | ||
mnemonic = Mnemonic.generate( | ||
Language.english, | ||
passphrase: securePassphrase, | ||
entropyLength: entropyLength, | ||
); | ||
} on Exception catch (_) { | ||
throw Exception('Error generating seed!'); | ||
} | ||
return mnemonic; | ||
} | ||
|
||
Uint8List generateSecureRandomBytes(int length) { | ||
final random = Random.secure(); | ||
final randomBytes = List<int>.generate(length, (_) => random.nextInt(256)); | ||
return Uint8List.fromList(randomBytes); | ||
} | ||
|
||
String generateSecurePassphrase(Uint8List randomBytes) { | ||
final bcryptHash = BCrypt.hashpw(hex.encode(randomBytes), BCrypt.gensalt()); | ||
return bcryptHash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.