1
1
import 'dart:io' ;
2
2
3
+ import 'package:flutter/foundation.dart' ;
3
4
import 'package:genesix/rust_bridge/api/table_generation.dart' ;
4
- import 'package:path_provider/path_provider.dart' ;
5
5
import 'package:riverpod_annotation/riverpod_annotation.dart' ;
6
6
import 'package:genesix/features/authentication/application/wallets_state_provider.dart' ;
7
7
import 'package:genesix/features/router/route_utils.dart' ;
@@ -11,6 +11,8 @@ import 'package:genesix/features/settings/application/settings_state_provider.da
11
11
import 'package:genesix/features/wallet/application/wallet_provider.dart' ;
12
12
import 'package:genesix/features/wallet/data/native_wallet_repository.dart' ;
13
13
import 'package:genesix/shared/utils/utils.dart' ;
14
+ import 'package:localstorage/localstorage.dart' ;
15
+ import 'package:genesix/shared/logger.dart' ;
14
16
15
17
part 'authentication_service.g.dart' ;
16
18
@@ -29,33 +31,55 @@ class Authentication extends _$Authentication {
29
31
final precomputedTablesPath = await _getPrecomputedTablesPath ();
30
32
final settings = ref.read (settingsProvider);
31
33
var walletPath = await getWalletPath (settings.network, name);
32
- var walletExists = await Directory (walletPath).exists ();
34
+
35
+ var walletExists = false ;
36
+ if (kIsWeb) {
37
+ var path = localStorage.getItem (walletPath);
38
+ walletExists = path != null ;
39
+ } else {
40
+ walletExists = await Directory (walletPath).exists ();
41
+ }
33
42
34
43
if (walletExists) {
35
44
throw Exception ('This wallet already exists: $name ' );
36
45
} else {
37
46
NativeWalletRepository walletRepository;
38
47
48
+ // remove prefix for rust call because it's already appended
49
+ var dbName = walletPath.replaceFirst (localStorageDBPrefix, "" );
39
50
if (seed != null ) {
40
51
walletRepository = await NativeWalletRepository .recover (
41
- walletPath , password, settings.network,
52
+ dbName , password, settings.network,
42
53
seed: seed, precomputeTablesPath: precomputedTablesPath);
43
54
} else {
44
55
walletRepository = await NativeWalletRepository .create (
45
- walletPath , password, settings.network,
56
+ dbName , password, settings.network,
46
57
precomputeTablesPath: precomputedTablesPath);
47
58
}
48
59
49
60
ref
50
61
.read (walletsProvider.notifier)
51
62
.setWalletAddress (name, walletRepository.address);
52
63
53
- ref.read (routerProvider).go (AppScreen .wallet.toPath);
54
-
55
64
state = AuthenticationState .signedIn (
56
65
name: name, nativeWallet: walletRepository);
57
66
58
- ref.read (walletStateProvider.notifier).connect ();
67
+ ref.read (routerProvider).go (AuthAppScreen .wallet.toPath);
68
+
69
+ try {
70
+ ref.read (walletStateProvider.notifier).connect ();
71
+ } finally {
72
+ // continue... it's ok if we can't connect
73
+ // the connect() func displays an error message
74
+ }
75
+
76
+ if (seed == null ) {
77
+ final seed = await walletRepository.getSeed ();
78
+
79
+ ref
80
+ .read (routerProvider)
81
+ .push (AuthAppScreen .walletSeedDialog.toPath, extra: seed);
82
+ }
59
83
}
60
84
}
61
85
@@ -64,13 +88,21 @@ class Authentication extends _$Authentication {
64
88
final precomputedTablesPath = await _getPrecomputedTablesPath ();
65
89
66
90
var walletPath = await getWalletPath (settings.network, name);
67
- var walletExists = await Directory (walletPath).exists ();
91
+
92
+ var walletExists = false ;
93
+ if (kIsWeb) {
94
+ var path = localStorage.getItem (walletPath);
95
+ walletExists = path != null ;
96
+ } else {
97
+ walletExists = await Directory (walletPath).exists ();
98
+ }
68
99
69
100
if (walletExists) {
70
101
NativeWalletRepository walletRepository;
102
+ var dbName = walletPath.replaceFirst (localStorageDBPrefix, "" );
71
103
try {
72
104
walletRepository = await NativeWalletRepository .open (
73
- walletPath , password, settings.network,
105
+ dbName , password, settings.network,
74
106
precomputeTablesPath: precomputedTablesPath);
75
107
} catch (e) {
76
108
rethrow ;
@@ -83,7 +115,7 @@ class Authentication extends _$Authentication {
83
115
state = AuthenticationState .signedIn (
84
116
name: name, nativeWallet: walletRepository);
85
117
86
- ref.read (routerProvider).go (AppScreen .wallet.toPath);
118
+ ref.read (routerProvider).go (AuthAppScreen .wallet.toPath);
87
119
88
120
ref.read (walletStateProvider.notifier).connect ();
89
121
} else {
@@ -106,12 +138,20 @@ class Authentication extends _$Authentication {
106
138
}
107
139
108
140
Future <String > _getPrecomputedTablesPath () async {
109
- final dir = await getApplicationCacheDirectory ();
110
- return "${dir .path }/" ;
141
+ if (kIsWeb) {
142
+ return "" ;
143
+ } else {
144
+ final dir = await getAppCacheDirPath ();
145
+ return "$dir /" ;
146
+ }
111
147
}
112
148
113
149
Future <bool > isPrecomputedTablesExists () async {
114
- return precomputedTablesExist (
115
- precomputedTablesPath: await _getPrecomputedTablesPath ());
150
+ if (kIsWeb) {
151
+ return true ;
152
+ } else {
153
+ return precomputedTablesExist (
154
+ precomputedTablesPath: await _getPrecomputedTablesPath ());
155
+ }
116
156
}
117
157
}
0 commit comments