Skip to content

Commit

Permalink
Only restore existing nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Mar 28, 2024
1 parent 8ac11d2 commit 4021de2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions lib/bloc/account/account_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {

Future connect({
String? mnemonic,
bool restored = false,
bool isRestore = true,
}) async {
_log.info("connect new mnemonic: ${mnemonic != null}, restored: $restored");
_log.info("connect new mnemonic: ${mnemonic != null}, restored: $isRestore");
emit(state.copyWith(connectionStatus: ConnectionStatus.CONNECTING));
if (mnemonic != null) {
await _credentialsManager.storeMnemonic(mnemonic: mnemonic);
emit(state.copyWith(
initial: false,
verificationStatus: restored ? VerificationStatus.VERIFIED : null,
verificationStatus: isRestore ? VerificationStatus.VERIFIED : null,
));
}
await _startSdkForever();
await _startSdkForever(isRestore: isRestore);
}

Future _startSdkForever() async {
Future _startSdkForever({bool isRestore = true}) async {
_log.info("starting sdk forever");
await _startSdkOnce();
await _startSdkOnce(isRestore: isRestore);

// in case we failed to start (lack of inet connection probably)
if (state.connectionStatus == ConnectionStatus.DISCONNECTED) {
Expand All @@ -113,7 +113,7 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
}
}

Future _startSdkOnce() async {
Future _startSdkOnce({bool isRestore = true}) async {
_log.info("starting sdk once");
var config = await Config.instance();
if (config.sdkConfig.apiKey != null) {
Expand All @@ -133,6 +133,7 @@ class AccountBloc extends Cubit<AccountState> with HydratedMixin {
final req = sdk.ConnectRequest(
config: config.sdkConfig,
seed: seed,
restoreOnly: isRestore,
);
await _breezSDK.connect(req: req);
_log.info("connected to breez lib");
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/node_connectivity_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class NodeConnectivityHandler extends Handler {
final accountBloc = context.read<AccountBloc>();
Future.delayed(const Duration(milliseconds: 500), () async {
try {
await accountBloc.connect(restored: true);
await accountBloc.connect();
} catch (error) {
_log.severe("Failed to reconnect");
rethrow;
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/initial_walkthrough/initial_walkthrough.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class InitialWalkthroughPageState extends State<InitialWalkthroughPage>
try {
await accountBloc.connect(
mnemonic: mnemonic ?? bip39.generateMnemonic(strength: 128),
restored: isRestore,
isRestore: isRestore,
);
themeProvider.setTheme('dark');
navigator.pushReplacementNamed('/');
Expand Down
2 changes: 1 addition & 1 deletion test/bloc/account/account_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void main() {
injector.keychain.write(CredentialsManager.accountMnemonic, "a3eed");
AccountBloc accBloc = AccountBloc(breezSDK, CredentialsManager(keyChain: injector.keychain));

await accBloc.connect(mnemonic: testMnemonic, restored: true);
await accBloc.connect(mnemonic: testMnemonic, isRestore: true);
var accountState = accBloc.state;
expect(accountState.blockheight, greaterThan(1));
expect(accountState.id?.length, equals(66));
Expand Down

0 comments on commit 4021de2

Please sign in to comment.