shogun-core 4.2.5 → 4.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/gundb/db.js CHANGED
@@ -854,30 +854,22 @@ class DataBase {
854
854
  }
855
855
  // Set the user instance
856
856
  this.user = this.gun.user();
857
- // Run post-authentication tasks
858
- try {
859
- const postAuthResult = await this.runPostAuthOnAuthResult(username, authResult.userPub, authResult);
860
- // Return the post-auth result which includes the complete user data
861
- return postAuthResult;
862
- }
863
- catch (postAuthError) {
864
- console.error(`Post-auth error: ${postAuthError}`);
865
- // Even if post-auth fails, the user was created and authenticated successfully
866
- return {
867
- success: true,
868
- userPub: authResult.userPub,
869
- username: username,
870
- isNewUser: true,
871
- sea: this.gun.user()?._?.sea
872
- ? {
873
- pub: this.gun.user()._?.sea.pub,
874
- priv: this.gun.user()._?.sea.priv,
875
- epub: this.gun.user()._?.sea.epub,
876
- epriv: this.gun.user()._?.sea.epriv,
877
- }
878
- : undefined,
879
- };
880
- }
857
+ console.log(`[SIGNUP] Signup completed successfully for user: ${username}`);
858
+ // Return the signup result
859
+ return {
860
+ success: true,
861
+ userPub: authResult.userPub,
862
+ username: username,
863
+ isNewUser: true,
864
+ sea: this.gun.user()?._?.sea
865
+ ? {
866
+ pub: this.gun.user()._?.sea.pub,
867
+ priv: this.gun.user()._?.sea.priv,
868
+ epub: this.gun.user()._?.sea.epub,
869
+ epriv: this.gun.user()._?.sea.epriv,
870
+ }
871
+ : undefined,
872
+ };
881
873
  }
882
874
  catch (error) {
883
875
  console.error(`Exception during signup for ${username}: ${error}`);
@@ -915,475 +907,6 @@ class DataBase {
915
907
  resolve({ success: true, userPub: pair.pub });
916
908
  });
917
909
  }
918
- async runPostAuthOnAuthResult(username, userPub, authResult) {
919
- console.log(`[POSTAUTH] Starting post-auth setup for user: ${username}, userPub: ${userPub}`);
920
- try {
921
- console.log(`[POSTAUTH] Validating parameters for user: ${username}`);
922
- // Validate required parameters
923
- if (!username ||
924
- typeof username !== "string" ||
925
- username.trim().length === 0) {
926
- console.error(`[POSTAUTH] Invalid username provided: ${username}`);
927
- throw new Error("Invalid username provided");
928
- }
929
- if (!userPub ||
930
- typeof userPub !== "string" ||
931
- userPub.trim().length === 0) {
932
- console.error("[POSTAUTH] Invalid userPub provided:", {
933
- userPub,
934
- type: typeof userPub,
935
- authResult,
936
- });
937
- throw new Error("Invalid userPub provided");
938
- }
939
- // Additional validation for userPub format
940
- if (!userPub.includes(".") || userPub.length < 10) {
941
- console.error(`[POSTAUTH] Invalid userPub format: ${userPub}`);
942
- throw new Error("Invalid userPub format");
943
- }
944
- console.log(`[POSTAUTH] Parameters validated for user: ${username}`);
945
- // Normalize username to prevent path issues
946
- const normalizedUsername = username.trim().toLowerCase();
947
- if (normalizedUsername.length === 0) {
948
- console.error(`[POSTAUTH] Normalized username is empty for user: ${username}`);
949
- throw new Error("Username cannot be empty");
950
- }
951
- console.log(`[POSTAUTH] Normalized username: ${normalizedUsername}`);
952
- console.log(`[POSTAUTH] Checking if user exists: ${userPub}`);
953
- // Add timeout to prevent hanging
954
- const existingUser = await Promise.race([
955
- this.gun.get(userPub).then(),
956
- new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout checking user existence")), 5000)),
957
- ]).catch((error) => {
958
- console.error(`[POSTAUTH] Error or timeout checking user existence:`, error);
959
- return null;
960
- });
961
- console.log(`[POSTAUTH] User existence check completed, existingUser:`, existingUser);
962
- const isNewUser = !existingUser || !existingUser.alias;
963
- console.log(`[POSTAUTH] User is ${isNewUser ? "NEW" : "EXISTING"}: ${userPub}`);
964
- // Get user's encryption public key (epub) for comprehensive tracking
965
- const userInstance = this.gun.user();
966
- const userSea = userInstance?._?.sea;
967
- const epub = userSea?.epub;
968
- console.log(`[POSTAUTH] User epub retrieved: ${epub ? "yes" : "no"}`);
969
- // Enhanced user tracking system
970
- console.log(`[POSTAUTH] Setting up comprehensive user tracking for: ${normalizedUsername}`);
971
- const trackingResult = await this.setupComprehensiveUserTracking(normalizedUsername, userPub, epub);
972
- if (!trackingResult) {
973
- console.error(`[POSTAUTH] Comprehensive user tracking setup failed for: ${normalizedUsername}`);
974
- return {
975
- success: false,
976
- error: "Comprehensive user tracking setup failed",
977
- };
978
- }
979
- console.log(`[POSTAUTH] User tracking setup completed successfully for: ${normalizedUsername}`);
980
- const result = {
981
- success: true,
982
- userPub: userPub,
983
- username: normalizedUsername,
984
- isNewUser: isNewUser,
985
- // Get the SEA pair from the user object
986
- sea: userSea
987
- ? {
988
- pub: userSea.pub,
989
- priv: userSea.priv,
990
- epub: userSea.epub,
991
- epriv: userSea.epriv,
992
- }
993
- : undefined,
994
- };
995
- console.log(`[POSTAUTH] Post-auth setup completed successfully for user: ${username}`);
996
- return result;
997
- }
998
- catch (error) {
999
- console.error(`[POSTAUTH] Error in post-authentication setup for ${username}:`, error);
1000
- return {
1001
- success: false,
1002
- error: `Post-authentication setup failed: ${error}`,
1003
- };
1004
- }
1005
- }
1006
- /**
1007
- * Sets up comprehensive user tracking system for agile user lookup
1008
- * Creates multiple indexes for efficient user discovery
1009
- */
1010
- async setupComprehensiveUserTracking(username, userPub, epub) {
1011
- console.log(`[TRACKING] Starting comprehensive user tracking setup for: ${username}, userPub: ${userPub}`);
1012
- try {
1013
- // 1. Create alias index: ~@alias -> userPub (for GunDB compatibility)
1014
- console.log(`[TRACKING] Step 1: Creating alias index for ${username}`);
1015
- const aliasIndexResult = await this.createAliasIndex(username, userPub);
1016
- if (!aliasIndexResult) {
1017
- console.error(`[TRACKING] Failed to create alias index for ${username}`);
1018
- return false;
1019
- }
1020
- console.log(`[TRACKING] Step 1 completed: Alias index created for ${username}`);
1021
- // 2. Create username mapping: usernames/alias -> userPub
1022
- console.log(`[TRACKING] Step 2: Creating username mapping for ${username}`);
1023
- const usernameMappingResult = await this.createUsernameMapping(username, userPub);
1024
- if (!usernameMappingResult) {
1025
- console.error(`[TRACKING] Failed to create username mapping for ${username}`);
1026
- return false;
1027
- }
1028
- console.log(`[TRACKING] Step 2 completed: Username mapping created for ${username}`);
1029
- // 3. Create user registry: users/userPub -> user data
1030
- console.log(`[TRACKING] Step 3: Creating user registry for ${username}`);
1031
- const userRegistryResult = await this.createUserRegistry(username, userPub, epub);
1032
- if (!userRegistryResult) {
1033
- console.error(`[TRACKING] Failed to create user registry for ${username}`);
1034
- return false;
1035
- }
1036
- console.log(`[TRACKING] Step 3 completed: User registry created for ${username}`);
1037
- // 4. Create reverse lookup: userPub -> alias
1038
- console.log(`[TRACKING] Step 4: Creating reverse lookup for ${username}`);
1039
- const reverseLookupResult = await this.createReverseLookup(username, userPub);
1040
- if (!reverseLookupResult) {
1041
- console.error(`[TRACKING] Failed to create reverse lookup for ${username}`);
1042
- return false;
1043
- }
1044
- console.log(`[TRACKING] Step 4 completed: Reverse lookup created for ${username}`);
1045
- // 5. Create epub index: epubKeys/epub -> userPub (for encryption lookups)
1046
- if (epub) {
1047
- console.log(`[TRACKING] Step 5: Creating epub index for ${username}`);
1048
- const epubIndexResult = await this.createEpubIndex(epub, userPub);
1049
- if (!epubIndexResult) {
1050
- console.error(`[TRACKING] Failed to create epub index for ${username}`);
1051
- return false;
1052
- }
1053
- console.log(`[TRACKING] Step 5 completed: Epub index created for ${username}`);
1054
- }
1055
- else {
1056
- console.log(`[TRACKING] Step 5 skipped: No epub available for ${username}`);
1057
- }
1058
- // 6. Create user metadata in user's own node
1059
- console.log(`[TRACKING] Step 6: Creating user metadata for ${username}`);
1060
- const userMetadataResult = await this.createUserMetadata(username, userPub, epub);
1061
- if (!userMetadataResult) {
1062
- console.error(`[TRACKING] Failed to create user metadata for ${username}`);
1063
- return false;
1064
- }
1065
- console.log(`[TRACKING] Step 6 completed: User metadata created for ${username}`);
1066
- console.log(`[TRACKING] Comprehensive user tracking setup completed successfully for: ${username}`);
1067
- return true;
1068
- }
1069
- catch (error) {
1070
- console.error(`[TRACKING] Error in comprehensive user tracking setup for ${username}:`, error);
1071
- // Don't throw - continue with other operations
1072
- return false;
1073
- }
1074
- }
1075
- /**
1076
- * Creates alias index following GunDB pattern: ~@alias -> userPub
1077
- */
1078
- async createAliasIndex(username, userPub) {
1079
- try {
1080
- const aliasNode = this.gun.get(`~@${username}`);
1081
- // For Gun.js alias validation to pass, the data must be exactly equal to the key
1082
- // The key is `~@${username}`, so we store that as the data
1083
- return new Promise((resolve) => {
1084
- aliasNode.put(`~@${username}`, (ack) => {
1085
- if (ack && ack.err) {
1086
- console.error(`Error creating alias index: ${ack.err}`);
1087
- resolve(false);
1088
- }
1089
- else {
1090
- resolve(true);
1091
- }
1092
- });
1093
- });
1094
- }
1095
- catch (error) {
1096
- console.error(`Error creating alias index: ${error}`);
1097
- return false;
1098
- }
1099
- }
1100
- /**
1101
- * Creates username mapping: usernames/alias -> userPub
1102
- */
1103
- async createUsernameMapping(username, userPub) {
1104
- try {
1105
- return new Promise((resolve) => {
1106
- this.node
1107
- .get("usernames")
1108
- .get(username)
1109
- .put(userPub, (ack) => {
1110
- if (ack && ack.err) {
1111
- console.error(`Error creating username mapping: ${ack.err}`);
1112
- resolve(false);
1113
- }
1114
- else {
1115
- resolve(true);
1116
- }
1117
- });
1118
- });
1119
- }
1120
- catch (error) {
1121
- console.error(`Error creating username mapping: ${error}`);
1122
- return false;
1123
- }
1124
- }
1125
- /**
1126
- * Creates user registry: users/userPub -> user data
1127
- */
1128
- async createUserRegistry(username, userPub, epub) {
1129
- try {
1130
- const userData = {
1131
- username: username,
1132
- userPub: userPub,
1133
- epub: epub,
1134
- registeredAt: Date.now().toString(),
1135
- lastSeen: Date.now().toString(),
1136
- };
1137
- return new Promise((resolve) => {
1138
- this.node
1139
- .get("users")
1140
- .get(userPub)
1141
- .put(userData, (ack) => {
1142
- if (ack && ack.err) {
1143
- console.error(`Error creating user registry: ${ack.err}`);
1144
- resolve(false);
1145
- }
1146
- else {
1147
- resolve(true);
1148
- }
1149
- });
1150
- });
1151
- }
1152
- catch (error) {
1153
- console.error(`Error creating user registry: ${error}`);
1154
- return false;
1155
- }
1156
- }
1157
- /**
1158
- * Creates reverse lookup: userPub -> alias
1159
- */
1160
- async createReverseLookup(username, userPub) {
1161
- try {
1162
- const ack = await this.node
1163
- .get("userAliases")
1164
- .get(userPub)
1165
- .put(username)
1166
- .then();
1167
- if (ack.err) {
1168
- console.error(`Error creating reverse lookup: ${ack.err}`);
1169
- return false;
1170
- }
1171
- else {
1172
- return true;
1173
- }
1174
- }
1175
- catch (error) {
1176
- console.error(`Error creating reverse lookup: ${error}`);
1177
- return false;
1178
- }
1179
- }
1180
- /**
1181
- * Creates epub index: epubKeys/epub -> userPub
1182
- */
1183
- async createEpubIndex(epub, userPub) {
1184
- try {
1185
- return new Promise((resolve) => {
1186
- this.node
1187
- .get("epubKeys")
1188
- .get(epub)
1189
- .put(userPub, (ack) => {
1190
- if (ack && ack.err) {
1191
- console.error(`Error creating epub index: ${ack.err}`);
1192
- resolve(false);
1193
- }
1194
- else {
1195
- resolve(true);
1196
- }
1197
- });
1198
- });
1199
- }
1200
- catch (error) {
1201
- console.error(`Error creating epub index: ${error}`);
1202
- return false;
1203
- }
1204
- }
1205
- /**
1206
- * Creates user metadata in user's own node
1207
- */
1208
- async createUserMetadata(username, userPub, epub) {
1209
- try {
1210
- const userMetadata = {
1211
- username: username,
1212
- epub: epub,
1213
- registeredAt: Date.now(),
1214
- lastSeen: Date.now(),
1215
- };
1216
- return new Promise((resolve) => {
1217
- this.gun.get(userPub).put(userMetadata, (ack) => {
1218
- if (ack && ack.err) {
1219
- console.error(`Error creating user metadata: ${ack.err}`);
1220
- resolve(false);
1221
- }
1222
- else {
1223
- resolve(true);
1224
- }
1225
- });
1226
- });
1227
- }
1228
- catch (error) {
1229
- console.error(`Error creating user metadata: ${error}`);
1230
- return false;
1231
- }
1232
- }
1233
- /**
1234
- * Gets user information by alias using the comprehensive tracking system
1235
- * @param alias Username/alias to lookup
1236
- * @returns Promise resolving to user information or null if not found
1237
- */
1238
- async getUserByAlias(alias) {
1239
- try {
1240
- const normalizedAlias = alias.trim().toLowerCase();
1241
- if (!normalizedAlias) {
1242
- return null;
1243
- }
1244
- // Method 1: Try GunDB standard alias lookup (~@alias)
1245
- try {
1246
- const aliasData = await this.gun.get(`~@${normalizedAlias}`).then();
1247
- if (aliasData && aliasData["~pubKeyOfUser"]) {
1248
- const userPub = aliasData["~pubKeyOfUser"]["#"] || aliasData["~pubKeyOfUser"];
1249
- if (userPub) {
1250
- const userData = await this.getUserDataByPub(userPub);
1251
- if (userData) {
1252
- return userData;
1253
- }
1254
- }
1255
- }
1256
- }
1257
- catch (error) {
1258
- console.error(`GunDB alias lookup failed for ${normalizedAlias}:`, error);
1259
- }
1260
- // Method 2: Try username mapping (usernames/alias -> userPub)
1261
- try {
1262
- const userPub = await this.node
1263
- .get("usernames")
1264
- .get(normalizedAlias)
1265
- .then();
1266
- if (userPub) {
1267
- const userData = await this.getUserDataByPub(userPub);
1268
- if (userData) {
1269
- return userData;
1270
- }
1271
- }
1272
- }
1273
- catch (error) {
1274
- console.error(`Username mapping lookup failed for ${normalizedAlias}:`, error);
1275
- }
1276
- return null;
1277
- }
1278
- catch (error) {
1279
- console.error(`Error looking up user by alias ${alias}:`, error);
1280
- return null;
1281
- }
1282
- }
1283
- /**
1284
- * Gets user information by public key
1285
- * @param userPub User's public key
1286
- * @returns Promise resolving to user information or null if not found
1287
- */
1288
- async getUserDataByPub(userPub) {
1289
- try {
1290
- if (!userPub || typeof userPub !== "string") {
1291
- return null;
1292
- }
1293
- // Method 1: Try user registry (users/userPub -> user data)
1294
- try {
1295
- const userData = await this.node.get("users").get(userPub).then();
1296
- if (userData && userData.username) {
1297
- return {
1298
- userPub: userData.userPub || userPub,
1299
- epub: userData.epub || null,
1300
- username: userData.username,
1301
- registeredAt: userData.registeredAt || 0,
1302
- lastSeen: userData.lastSeen || 0,
1303
- };
1304
- }
1305
- }
1306
- catch (error) {
1307
- console.error(`User registry lookup failed for ${userPub}:`, error);
1308
- }
1309
- // Method 2: Try user's own node
1310
- try {
1311
- const userNodeData = await this.gun.get(userPub).then();
1312
- if (userNodeData && userNodeData.username) {
1313
- return {
1314
- userPub: userPub,
1315
- epub: userNodeData.epub || null,
1316
- username: userNodeData.username,
1317
- registeredAt: userNodeData.registeredAt || 0,
1318
- lastSeen: userNodeData.lastSeen || 0,
1319
- };
1320
- }
1321
- }
1322
- catch (error) {
1323
- console.error(`User node lookup failed for ${userPub}:`, error);
1324
- }
1325
- return null;
1326
- }
1327
- catch (error) {
1328
- console.error(`Error looking up user data by pub ${userPub}:`, error);
1329
- return null;
1330
- }
1331
- }
1332
- /**
1333
- * Gets user public key by encryption public key (epub)
1334
- * @param epub User's encryption public key
1335
- * @returns Promise resolving to user public key or null if not found
1336
- */
1337
- async getUserPubByEpub(epub) {
1338
- try {
1339
- if (!epub || typeof epub !== "string") {
1340
- return null;
1341
- }
1342
- const userPub = await this.node.get("epubKeys").get(epub).then();
1343
- return userPub || null;
1344
- }
1345
- catch (error) {
1346
- console.error(`Error looking up user pub by epub ${epub}:`, error);
1347
- return null;
1348
- }
1349
- }
1350
- /**
1351
- * Gets user alias by public key
1352
- * @param userPub User's public key
1353
- * @returns Promise resolving to user alias or null if not found
1354
- */
1355
- async getUserAliasByPub(userPub) {
1356
- try {
1357
- if (!userPub || typeof userPub !== "string") {
1358
- return null;
1359
- }
1360
- const alias = await this.node.get("userAliases").get(userPub).then();
1361
- return alias || null;
1362
- }
1363
- catch (error) {
1364
- console.error(`Error looking up user alias by pub ${userPub}:`, error);
1365
- return null;
1366
- }
1367
- }
1368
- /**
1369
- * Gets all registered users (for admin purposes)
1370
- * @returns Promise resolving to array of user information
1371
- */
1372
- async getAllRegisteredUsers() {
1373
- try {
1374
- const users = [];
1375
- // Get all users from the users registry
1376
- const usersNode = this.node.get("users");
1377
- // Note: This is a simplified approach. In a real implementation,
1378
- // you might want to use Gun's map functionality or iterate through
1379
- // known user public keys
1380
- return users;
1381
- }
1382
- catch (error) {
1383
- console.error(`Error getting all registered users:`, error);
1384
- return [];
1385
- }
1386
- }
1387
910
  /**
1388
911
  * Updates user's last seen timestamp
1389
912
  * @param userPub User's public key
@@ -1498,17 +1021,7 @@ class DataBase {
1498
1021
  error: "Authentication failed: No user pub returned.",
1499
1022
  };
1500
1023
  }
1501
- // Pass the userPub to runPostAuthOnAuthResult
1502
- try {
1503
- await this.runPostAuthOnAuthResult(alias, userPub, {
1504
- success: true,
1505
- userPub: userPub,
1506
- });
1507
- }
1508
- catch (postAuthError) {
1509
- console.error(`Post-auth error during login: ${postAuthError}`);
1510
- // Continue with login even if post-auth fails
1511
- }
1024
+ console.log(`[LOGIN] Login completed successfully for user: ${username}`);
1512
1025
  // Update user's last seen timestamp
1513
1026
  try {
1514
1027
  await this.updateUserLastSeen(userPub);
@@ -1551,10 +1064,7 @@ class DataBase {
1551
1064
  error: `User '${username}' not found. Please check your username or register first.`,
1552
1065
  };
1553
1066
  }
1554
- await this.runPostAuthOnAuthResult(username, pair.pub || "", {
1555
- success: true,
1556
- userPub: pair.pub,
1557
- });
1067
+ console.log(`[LOGIN] Login with pair completed for user: ${username}`);
1558
1068
  try {
1559
1069
  await this.updateUserLastSeen(pair.pub);
1560
1070
  }
@@ -185,83 +185,6 @@ declare class DataBase {
185
185
  * Creates a new user in Gun with pair-based authentication (for Web3/plugins)
186
186
  */
187
187
  private createNewUserWithPair;
188
- private runPostAuthOnAuthResult;
189
- /**
190
- * Sets up comprehensive user tracking system for agile user lookup
191
- * Creates multiple indexes for efficient user discovery
192
- */
193
- private setupComprehensiveUserTracking;
194
- /**
195
- * Creates alias index following GunDB pattern: ~@alias -> userPub
196
- */
197
- private createAliasIndex;
198
- /**
199
- * Creates username mapping: usernames/alias -> userPub
200
- */
201
- private createUsernameMapping;
202
- /**
203
- * Creates user registry: users/userPub -> user data
204
- */
205
- private createUserRegistry;
206
- /**
207
- * Creates reverse lookup: userPub -> alias
208
- */
209
- private createReverseLookup;
210
- /**
211
- * Creates epub index: epubKeys/epub -> userPub
212
- */
213
- private createEpubIndex;
214
- /**
215
- * Creates user metadata in user's own node
216
- */
217
- private createUserMetadata;
218
- /**
219
- * Gets user information by alias using the comprehensive tracking system
220
- * @param alias Username/alias to lookup
221
- * @returns Promise resolving to user information or null if not found
222
- */
223
- getUserByAlias(alias: string): Promise<{
224
- userPub: string;
225
- epub: string | null;
226
- username: string;
227
- registeredAt: number;
228
- lastSeen: number;
229
- } | null>;
230
- /**
231
- * Gets user information by public key
232
- * @param userPub User's public key
233
- * @returns Promise resolving to user information or null if not found
234
- */
235
- getUserDataByPub(userPub: string): Promise<{
236
- userPub: string;
237
- epub: string | null;
238
- username: string;
239
- registeredAt: number;
240
- lastSeen: number;
241
- } | null>;
242
- /**
243
- * Gets user public key by encryption public key (epub)
244
- * @param epub User's encryption public key
245
- * @returns Promise resolving to user public key or null if not found
246
- */
247
- getUserPubByEpub(epub: string): Promise<string | null>;
248
- /**
249
- * Gets user alias by public key
250
- * @param userPub User's public key
251
- * @returns Promise resolving to user alias or null if not found
252
- */
253
- getUserAliasByPub(userPub: string): Promise<string | null>;
254
- /**
255
- * Gets all registered users (for admin purposes)
256
- * @returns Promise resolving to array of user information
257
- */
258
- getAllRegisteredUsers(): Promise<Array<{
259
- userPub: string;
260
- epub: string | null;
261
- username: string;
262
- registeredAt: number;
263
- lastSeen: number;
264
- }>>;
265
188
  /**
266
189
  * Updates user's last seen timestamp
267
190
  * @param userPub User's public key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shogun-core",
3
- "version": "4.2.5",
3
+ "version": "4.2.6",
4
4
  "description": "SHOGUN CORE - Core library for Shogun Ecosystem",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",