Skip to content

Commit 46458fe

Browse files
committed
change s5 login impl to same as vup chat
1 parent cef71ef commit 46458fe

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

lib/definitons/logging.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class FileLogger extends Logger {
77

88
FileLogger({required this.file});
99

10-
// for now this is the only function I'm over-writing
1110
@override
1211
Future<void> info(String s) async {
1312
final sink = File(file).openWrite(mode: FileMode.append);
@@ -44,3 +43,39 @@ class FileLogger extends Logger {
4443
await sink.close();
4544
}
4645
}
46+
47+
/// Logger only intended for debug logging, pipes warnings to file,
48+
/// evething else goes to stdout
49+
class DebugLogger extends Logger {
50+
final String file;
51+
52+
DebugLogger({required this.file});
53+
54+
@override
55+
Future<void> info(String s) async {
56+
print(s);
57+
}
58+
59+
@override
60+
void error(String s) async {
61+
print(s);
62+
}
63+
64+
@override
65+
void verbose(String s) async {
66+
print(s);
67+
}
68+
69+
// S5 is very spammy on the warn calls, so I'm dumping this to a file
70+
@override
71+
void warn(String s) async {
72+
final sink = File(file).openWrite(mode: FileMode.append);
73+
sink.write(s);
74+
await sink.close();
75+
}
76+
77+
@override
78+
void catched(e, st, [context]) async {
79+
print(e);
80+
}
81+
}

lib/functions/s5.dart

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:s5/s5.dart';
1515
import 'dart:convert';
1616
import 'package:http/http.dart' as http;
1717
import 'package:s5_deploy/functions/config.dart';
18+
import 'package:s5_deploy/functions/spinner.dart';
1819
import 'package:xdg_directories/xdg_directories.dart';
1920

2021
Future<S5> initS5(
@@ -48,14 +49,19 @@ Future<S5> initS5(
4849
setConfig(DeployConfig(seed: seed, dataKeys: []));
4950
await s5.recoverIdentityFromSeedPhrase(seed);
5051

52+
// now check if already registered on node
53+
List<String>? urls;
5154
// then check if already registered
52-
Map<dynamic, dynamic> data = (s5.api as S5NodeAPIWithIdentity).accounts;
53-
final Map<String, dynamic> accounts =
54-
data['accounts'] as Map<String, dynamic>;
55-
final List<String> urls =
56-
accounts.values.map((account) => account['url'] as String).toList();
57-
// And if the nodeURL isn't on the seed already, authenticate on that server
58-
if (!urls.contains(nodeURL)) {
55+
if ((s5.api as S5NodeAPIWithIdentity).accounts.isNotEmpty) {
56+
Map<dynamic, dynamic> data = (s5.api as S5NodeAPIWithIdentity).accounts;
57+
final Map<String, dynamic> accounts = (data['accounts'] as Map).map(
58+
(key, value) => MapEntry(key as String, value),
59+
);
60+
urls =
61+
accounts.values.map((account) => account['url'] as String).toList();
62+
// And if the nodeURL isn't on the seed already, authenticate on that server
63+
}
64+
if (urls == null || !urls.contains(nodeURL)) {
5965
print("Registering @ $nodeURL");
6066
await s5.registerOnNewStorageService(
6167
nodeURL,
@@ -136,28 +142,27 @@ Future<CID> updateResolver(
136142
setConfig(conf);
137143
}
138144

145+
// Then we get get an set the resolver
139146
final resolverSeed = s5.api.crypto.hashBlake3Sync(
140147
Uint8List.fromList(
141-
validatePhrase(seed, crypto: s5.api.crypto) + utf8.encode(dataKey),
148+
validatePhrase(seed, crypto: s5.api.crypto) +
149+
utf8.encode(dataKey), // this identifies the backup
142150
),
143151
);
144152

145153
final s5User = await s5.api.crypto.newKeyPairEd25519(seed: resolverSeed);
146154

147155
SignedRegistryEntry? existing;
156+
int revision = 0;
148157

149158
try {
150159
final res = await s5.api.registryGet(s5User.publicKey);
151160
existing = res;
152-
if (existing != null) {
153-
print(
154-
'Revision ${existing.revision} -> ${existing.revision + 1}',
155-
);
156-
}
161+
revision = existing!.revision + 1;
157162
} catch (e) {
158163
existing = null;
159164

160-
print('Revision 1');
165+
revision = 1;
161166
}
162167

163168
final sre = await signRegistryEntry(
@@ -176,6 +181,13 @@ Future<CID> updateResolver(
176181
s5User.publicKey,
177182
),
178183
));
184+
185+
if (resolverCID.hash.toBase64Url() != "") {
186+
spinner.success();
187+
spinner = spinStart("Updating to revision $revision");
188+
spinner.success();
189+
}
190+
179191
return resolverCID;
180192
} else {
181193
spinner.fail();

lib/s5_deploy.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:io';
33
import 'package:args/args.dart';
44
import 'package:cli_spin/cli_spin.dart';
55
import 'package:dcli/dcli.dart';
6-
import 'package:lib5/identity.dart';
76
import 'package:lib5/node.dart';
87
import 'package:mime/mime.dart';
98
import 'package:path/path.dart';
@@ -120,7 +119,6 @@ void s5Deploy(List<String> args) async {
120119
spinner = spinStart("Updating resolver link...");
121120
final resolverCID = await updateResolver(s5, results.arguments.first,
122121
staticCID, spinner, (results['dataKey'] as String?));
123-
spinner.success();
124122

125123
// Then a little url manipulation
126124
final nodeURI = Uri.parse(nodeURL);

0 commit comments

Comments
 (0)