-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functionality of initialize node in the background #67
Changes from all commits
33e1191
17b2610
ebf4458
80d9be8
4ecd0f8
fa8ef91
fd2fb79
c567375
b16a45c
a86fdfc
0d26618
2eda73a
b10f09e
a94e262
331c751
7cd1b30
136d6a1
3d90633
0e3c3f3
6f6a81f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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')); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
abstract class DaemonState {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add document here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add fully docs for this part. @PouriaMoradi021 |
||
|
||
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; | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add document here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add fully docs for this part. @PouriaMoradi021 |
||
// 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; | ||
} | ||
} |
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add document here.
@esmaeil-ahmadipour