-
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 18 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,38 @@ | ||
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; | ||
|
||
class DaemonCubit extends Cubit<DaemonState> { | ||
DaemonCubit() : super(DaemonInitial()); | ||
|
||
Future<void> runPactusDaemon({ | ||
// required String workingDirectory, | ||
required String command, | ||
required List<String> arguments, | ||
}) async { | ||
emit(DaemonLoading()); | ||
|
||
try { | ||
// Get the directory of the script | ||
final scriptDir = dirname(Platform.script.toFilePath()); | ||
|
||
final targetPath = '$scriptDir/lib/src/core/native_resources/linux/'; | ||
|
||
// print('scriptDir: $targetPath'); | ||
|
||
final result = | ||
await Process.run(command, arguments, workingDirectory: targetPath); | ||
if (result.exitCode == 0) { | ||
// دستور با موفقیت اجرا شد | ||
esmaeil-ahmadipour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,35 @@ | ||
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 | ||
NodeConfigData._internal(); | ||
// Singleton instance | ||
static final NodeConfigData instance = NodeConfigData._internal(); | ||
|
||
// Private variables | ||
String? _workingDirectory; | ||
String? _restorationSeed; | ||
String? _password; | ||
String? _validatorQty; | ||
|
||
// Getters | ||
String get workingDirectory => _workingDirectory ?? ''; | ||
String get restorationSeed => _restorationSeed ?? ''; | ||
String get password => _password ?? ''; | ||
String get validatorQty => _validatorQty ?? ''; | ||
|
||
// Setters | ||
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