shogun-core 4.2.3 → 4.2.4

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
@@ -82,6 +82,7 @@ class DataBase {
82
82
  constructor(gun, appScope = "shogun") {
83
83
  this.user = null;
84
84
  this.onAuthCallbacks = [];
85
+ console.log("[DB] Initializing DataBase");
85
86
  // Initialize event emitter
86
87
  this.eventEmitter = new eventEmitter_1.EventEmitter();
87
88
  // Validate Gun instance
@@ -101,31 +102,38 @@ class DataBase {
101
102
  throw new Error(`Gun instance is invalid: gun.on is not a function. Received gun.on type: ${typeof gun.on}`);
102
103
  }
103
104
  this.gun = gun;
105
+ console.log("[DB] Gun instance validated");
104
106
  // Recall only if NOT disabled and there's a "pair" in sessionStorage
105
107
  this.user = this.gun.user().recall({ sessionStorage: true });
108
+ console.log("[DB] User recall completed");
106
109
  this.subscribeToAuthEvents();
110
+ console.log("[DB] Auth events subscribed");
107
111
  this.crypto = crypto;
108
112
  this.sea = sea_1.default;
109
113
  this._rxjs = new rxjs_1.RxJS(this.gun);
110
114
  this.node = null;
115
+ console.log("[DB] DataBase initialization completed");
111
116
  }
112
117
  /**
113
118
  * Initialize the GunInstance asynchronously
114
119
  * This method should be called after construction to perform async operations
115
120
  */
116
121
  initialize(appScope = "shogun") {
122
+ console.log(`[DB] Initializing with appScope: ${appScope}`);
117
123
  try {
118
124
  const sessionResult = this.restoreSession();
125
+ console.log(`[DB] Session restore result: ${sessionResult.success ? "success" : "failed"}`);
119
126
  this.node = this.gun.get(appScope);
127
+ console.log("[DB] App scope node initialized");
120
128
  if (sessionResult.success) {
121
- // Session automatically restored
129
+ console.log("[DB] Session automatically restored");
122
130
  }
123
131
  else {
124
- // No previous session to restore
132
+ console.log("[DB] No previous session to restore");
125
133
  }
126
134
  }
127
135
  catch (error) {
128
- console.error("Error during automatic session restoration:", error);
136
+ console.error("[DB] Error during automatic session restoration:", error);
129
137
  }
130
138
  }
131
139
  subscribeToAuthEvents() {
@@ -148,13 +156,16 @@ class DataBase {
148
156
  * @param peer URL of the peer to add
149
157
  */
150
158
  addPeer(peer) {
159
+ console.log(`[PEER] Adding peer: ${peer}`);
151
160
  this.gun.opt({ peers: [peer] });
161
+ console.log(`[PEER] Peer added successfully`);
152
162
  }
153
163
  /**
154
164
  * Removes a peer from the network
155
165
  * @param peer URL of the peer to remove
156
166
  */
157
167
  removePeer(peer) {
168
+ console.log(`[PEER] Removing peer: ${peer}`);
158
169
  try {
159
170
  // Get current peers from Gun instance
160
171
  const gunOpts = this.gun._.opt;
@@ -166,13 +177,14 @@ class DataBase {
166
177
  if (peerConnection && typeof peerConnection.close === "function") {
167
178
  peerConnection.close();
168
179
  }
180
+ console.log(`[PEER] Peer removed successfully`);
169
181
  }
170
182
  else {
171
- console.error(`Peer not found in current connections: ${peer}`);
183
+ console.error(`[PEER] Peer not found in current connections: ${peer}`);
172
184
  }
173
185
  }
174
186
  catch (error) {
175
- console.error(`Error removing peer ${peer}:`, error);
187
+ console.error(`[PEER] Error removing peer ${peer}:`, error);
176
188
  }
177
189
  }
178
190
  /**
@@ -383,16 +395,7 @@ class DataBase {
383
395
  */
384
396
  async put(path, data) {
385
397
  const node = this.navigateToPath(this.node, path);
386
- return new Promise((resolve, reject) => {
387
- node.put(data, (ack) => {
388
- if (ack && ack.err) {
389
- reject(new Error(ack.err));
390
- }
391
- else {
392
- resolve(ack);
393
- }
394
- });
395
- });
398
+ return await node.put(data).then();
396
399
  }
397
400
  /**
398
401
  * Sets data at the specified path
@@ -402,16 +405,7 @@ class DataBase {
402
405
  */
403
406
  async set(path, data) {
404
407
  const node = this.navigateToPath(this.node, path);
405
- return new Promise((resolve, reject) => {
406
- node.set(data, (ack) => {
407
- if (ack && ack.err) {
408
- reject(new Error(ack.err));
409
- }
410
- else {
411
- resolve(ack);
412
- }
413
- });
414
- });
408
+ return await node.set(data).then();
415
409
  }
416
410
  /**
417
411
  * Removes data at the specified path
@@ -420,16 +414,7 @@ class DataBase {
420
414
  */
421
415
  async remove(path) {
422
416
  const node = this.navigateToPath(this.node, path);
423
- return new Promise((resolve, reject) => {
424
- node.put(null, (ack) => {
425
- if (ack && ack.err) {
426
- reject(new Error(ack.err));
427
- }
428
- else {
429
- resolve(ack);
430
- }
431
- });
432
- });
417
+ return await node.put(null).then();
433
418
  }
434
419
  /**
435
420
  * Checks if a user is currently logged in
@@ -931,18 +916,20 @@ class DataBase {
931
916
  });
932
917
  }
933
918
  async runPostAuthOnAuthResult(username, userPub, authResult) {
934
- // Setting up user profile after authentication
919
+ console.log(`[POSTAUTH] Starting post-auth setup for user: ${username}, userPub: ${userPub}`);
935
920
  try {
921
+ console.log(`[POSTAUTH] Validating parameters for user: ${username}`);
936
922
  // Validate required parameters
937
923
  if (!username ||
938
924
  typeof username !== "string" ||
939
925
  username.trim().length === 0) {
926
+ console.error(`[POSTAUTH] Invalid username provided: ${username}`);
940
927
  throw new Error("Invalid username provided");
941
928
  }
942
929
  if (!userPub ||
943
930
  typeof userPub !== "string" ||
944
931
  userPub.trim().length === 0) {
945
- console.error("Invalid userPub provided:", {
932
+ console.error("[POSTAUTH] Invalid userPub provided:", {
946
933
  userPub,
947
934
  type: typeof userPub,
948
935
  authResult,
@@ -951,34 +938,38 @@ class DataBase {
951
938
  }
952
939
  // Additional validation for userPub format
953
940
  if (!userPub.includes(".") || userPub.length < 10) {
954
- console.error("Invalid userPub format:", userPub);
941
+ console.error(`[POSTAUTH] Invalid userPub format: ${userPub}`);
955
942
  throw new Error("Invalid userPub format");
956
943
  }
944
+ console.log(`[POSTAUTH] Parameters validated for user: ${username}`);
957
945
  // Normalize username to prevent path issues
958
946
  const normalizedUsername = username.trim().toLowerCase();
959
947
  if (normalizedUsername.length === 0) {
948
+ console.error(`[POSTAUTH] Normalized username is empty for user: ${username}`);
960
949
  throw new Error("Username cannot be empty");
961
950
  }
962
- const existingUser = await new Promise((resolve) => {
963
- this.gun.get(userPub).once((data) => {
964
- resolve(data);
965
- });
966
- });
951
+ console.log(`[POSTAUTH] Normalized username: ${normalizedUsername}`);
952
+ console.log(`[POSTAUTH] Checking if user exists: ${userPub}`);
953
+ const existingUser = await this.gun.get(userPub).then();
967
954
  const isNewUser = !existingUser || !existingUser.alias;
968
- // const isNewUser = true;
955
+ console.log(`[POSTAUTH] User is ${isNewUser ? "NEW" : "EXISTING"}: ${userPub}`);
969
956
  // Get user's encryption public key (epub) for comprehensive tracking
970
957
  const userInstance = this.gun.user();
971
958
  const userSea = userInstance?._?.sea;
972
959
  const epub = userSea?.epub;
960
+ console.log(`[POSTAUTH] User epub retrieved: ${epub ? "yes" : "no"}`);
973
961
  // Enhanced user tracking system
962
+ console.log(`[POSTAUTH] Setting up comprehensive user tracking for: ${normalizedUsername}`);
974
963
  const trackingResult = await this.setupComprehensiveUserTracking(normalizedUsername, userPub, epub);
975
964
  if (!trackingResult) {
965
+ console.error(`[POSTAUTH] Comprehensive user tracking setup failed for: ${normalizedUsername}`);
976
966
  return {
977
967
  success: false,
978
968
  error: "Comprehensive user tracking setup failed",
979
969
  };
980
970
  }
981
- return {
971
+ console.log(`[POSTAUTH] User tracking setup completed successfully for: ${normalizedUsername}`);
972
+ const result = {
982
973
  success: true,
983
974
  userPub: userPub,
984
975
  username: normalizedUsername,
@@ -993,9 +984,11 @@ class DataBase {
993
984
  }
994
985
  : undefined,
995
986
  };
987
+ console.log(`[POSTAUTH] Post-auth setup completed successfully for user: ${username}`);
988
+ return result;
996
989
  }
997
990
  catch (error) {
998
- console.error(`Error in post-authentication setup: ${error}`);
991
+ console.error(`[POSTAUTH] Error in post-authentication setup for ${username}:`, error);
999
992
  return {
1000
993
  success: false,
1001
994
  error: `Post-authentication setup failed: ${error}`,
@@ -1007,43 +1000,66 @@ class DataBase {
1007
1000
  * Creates multiple indexes for efficient user discovery
1008
1001
  */
1009
1002
  async setupComprehensiveUserTracking(username, userPub, epub) {
1003
+ console.log(`[TRACKING] Starting comprehensive user tracking setup for: ${username}, userPub: ${userPub}`);
1010
1004
  try {
1011
1005
  // 1. Create alias index: ~@alias -> userPub (for GunDB compatibility)
1006
+ console.log(`[TRACKING] Step 1: Creating alias index for ${username}`);
1012
1007
  const aliasIndexResult = await this.createAliasIndex(username, userPub);
1013
1008
  if (!aliasIndexResult) {
1009
+ console.error(`[TRACKING] Failed to create alias index for ${username}`);
1014
1010
  return false;
1015
1011
  }
1012
+ console.log(`[TRACKING] Step 1 completed: Alias index created for ${username}`);
1016
1013
  // 2. Create username mapping: usernames/alias -> userPub
1014
+ console.log(`[TRACKING] Step 2: Creating username mapping for ${username}`);
1017
1015
  const usernameMappingResult = await this.createUsernameMapping(username, userPub);
1018
1016
  if (!usernameMappingResult) {
1017
+ console.error(`[TRACKING] Failed to create username mapping for ${username}`);
1019
1018
  return false;
1020
1019
  }
1020
+ console.log(`[TRACKING] Step 2 completed: Username mapping created for ${username}`);
1021
1021
  // 3. Create user registry: users/userPub -> user data
1022
+ console.log(`[TRACKING] Step 3: Creating user registry for ${username}`);
1022
1023
  const userRegistryResult = await this.createUserRegistry(username, userPub, epub);
1023
1024
  if (!userRegistryResult) {
1025
+ console.error(`[TRACKING] Failed to create user registry for ${username}`);
1024
1026
  return false;
1025
1027
  }
1028
+ console.log(`[TRACKING] Step 3 completed: User registry created for ${username}`);
1026
1029
  // 4. Create reverse lookup: userPub -> alias
1030
+ console.log(`[TRACKING] Step 4: Creating reverse lookup for ${username}`);
1027
1031
  const reverseLookupResult = await this.createReverseLookup(username, userPub);
1028
1032
  if (!reverseLookupResult) {
1033
+ console.error(`[TRACKING] Failed to create reverse lookup for ${username}`);
1029
1034
  return false;
1030
1035
  }
1036
+ console.log(`[TRACKING] Step 4 completed: Reverse lookup created for ${username}`);
1031
1037
  // 5. Create epub index: epubKeys/epub -> userPub (for encryption lookups)
1032
1038
  if (epub) {
1039
+ console.log(`[TRACKING] Step 5: Creating epub index for ${username}`);
1033
1040
  const epubIndexResult = await this.createEpubIndex(epub, userPub);
1034
1041
  if (!epubIndexResult) {
1042
+ console.error(`[TRACKING] Failed to create epub index for ${username}`);
1035
1043
  return false;
1036
1044
  }
1045
+ console.log(`[TRACKING] Step 5 completed: Epub index created for ${username}`);
1046
+ }
1047
+ else {
1048
+ console.log(`[TRACKING] Step 5 skipped: No epub available for ${username}`);
1037
1049
  }
1038
1050
  // 6. Create user metadata in user's own node
1051
+ console.log(`[TRACKING] Step 6: Creating user metadata for ${username}`);
1039
1052
  const userMetadataResult = await this.createUserMetadata(username, userPub, epub);
1040
1053
  if (!userMetadataResult) {
1054
+ console.error(`[TRACKING] Failed to create user metadata for ${username}`);
1041
1055
  return false;
1042
1056
  }
1057
+ console.log(`[TRACKING] Step 6 completed: User metadata created for ${username}`);
1058
+ console.log(`[TRACKING] Comprehensive user tracking setup completed successfully for: ${username}`);
1043
1059
  return true;
1044
1060
  }
1045
1061
  catch (error) {
1046
- console.error(`Error in comprehensive user tracking setup: ${error}`);
1062
+ console.error(`[TRACKING] Error in comprehensive user tracking setup for ${username}:`, error);
1047
1063
  // Don't throw - continue with other operations
1048
1064
  return false;
1049
1065
  }
@@ -1135,20 +1151,18 @@ class DataBase {
1135
1151
  */
1136
1152
  async createReverseLookup(username, userPub) {
1137
1153
  try {
1138
- return new Promise((resolve) => {
1139
- this.node
1140
- .get("userAliases")
1141
- .get(userPub)
1142
- .put(username, (ack) => {
1143
- if (ack && ack.err) {
1144
- console.error(`Error creating reverse lookup: ${ack.err}`);
1145
- resolve(false);
1146
- }
1147
- else {
1148
- resolve(true);
1149
- }
1150
- });
1151
- });
1154
+ const ack = await this.node
1155
+ .get("userAliases")
1156
+ .get(userPub)
1157
+ .put(username)
1158
+ .then();
1159
+ if (ack.err) {
1160
+ console.error(`Error creating reverse lookup: ${ack.err}`);
1161
+ return false;
1162
+ }
1163
+ else {
1164
+ return true;
1165
+ }
1152
1166
  }
1153
1167
  catch (error) {
1154
1168
  console.error(`Error creating reverse lookup: ${error}`);
@@ -1221,14 +1235,9 @@ class DataBase {
1221
1235
  }
1222
1236
  // Method 1: Try GunDB standard alias lookup (~@alias)
1223
1237
  try {
1224
- const aliasData = this.gun.get(`~@${normalizedAlias}`);
1225
- const aliasDataObj = await new Promise((resolve) => {
1226
- aliasData.once((data) => {
1227
- resolve(data);
1228
- });
1229
- });
1230
- if (aliasDataObj && aliasDataObj["~pubKeyOfUser"]) {
1231
- const userPub = aliasDataObj["~pubKeyOfUser"]["#"] || aliasDataObj["~pubKeyOfUser"];
1238
+ const aliasData = await this.gun.get(`~@${normalizedAlias}`).then();
1239
+ if (aliasData && aliasData["~pubKeyOfUser"]) {
1240
+ const userPub = aliasData["~pubKeyOfUser"]["#"] || aliasData["~pubKeyOfUser"];
1232
1241
  if (userPub) {
1233
1242
  const userData = await this.getUserDataByPub(userPub);
1234
1243
  if (userData) {
@@ -1242,14 +1251,10 @@ class DataBase {
1242
1251
  }
1243
1252
  // Method 2: Try username mapping (usernames/alias -> userPub)
1244
1253
  try {
1245
- const userPub = await new Promise((resolve) => {
1246
- this.node
1247
- .get("usernames")
1248
- .get(normalizedAlias)
1249
- .once((data) => {
1250
- resolve(data);
1251
- });
1252
- });
1254
+ const userPub = await this.node
1255
+ .get("usernames")
1256
+ .get(normalizedAlias)
1257
+ .then();
1253
1258
  if (userPub) {
1254
1259
  const userData = await this.getUserDataByPub(userPub);
1255
1260
  if (userData) {
@@ -1279,14 +1284,7 @@ class DataBase {
1279
1284
  }
1280
1285
  // Method 1: Try user registry (users/userPub -> user data)
1281
1286
  try {
1282
- const userData = await new Promise((resolve) => {
1283
- this.node
1284
- .get("users")
1285
- .get(userPub)
1286
- .once((data) => {
1287
- resolve(data);
1288
- });
1289
- });
1287
+ const userData = await this.node.get("users").get(userPub).then();
1290
1288
  if (userData && userData.username) {
1291
1289
  return {
1292
1290
  userPub: userData.userPub || userPub,
@@ -1302,11 +1300,7 @@ class DataBase {
1302
1300
  }
1303
1301
  // Method 2: Try user's own node
1304
1302
  try {
1305
- const userNodeData = await new Promise((resolve) => {
1306
- this.gun.get(userPub).once((data) => {
1307
- resolve(data);
1308
- });
1309
- });
1303
+ const userNodeData = await this.gun.get(userPub).then();
1310
1304
  if (userNodeData && userNodeData.username) {
1311
1305
  return {
1312
1306
  userPub: userPub,
@@ -1337,14 +1331,7 @@ class DataBase {
1337
1331
  if (!epub || typeof epub !== "string") {
1338
1332
  return null;
1339
1333
  }
1340
- const userPub = await new Promise((resolve) => {
1341
- this.node
1342
- .get("epubKeys")
1343
- .get(epub)
1344
- .once((data) => {
1345
- resolve(data);
1346
- });
1347
- });
1334
+ const userPub = await this.node.get("epubKeys").get(epub).then();
1348
1335
  return userPub || null;
1349
1336
  }
1350
1337
  catch (error) {
@@ -1362,14 +1349,7 @@ class DataBase {
1362
1349
  if (!userPub || typeof userPub !== "string") {
1363
1350
  return null;
1364
1351
  }
1365
- const alias = await new Promise((resolve) => {
1366
- this.node
1367
- .get("userAliases")
1368
- .get(userPub)
1369
- .once((data) => {
1370
- resolve(data);
1371
- });
1372
- });
1352
+ const alias = await this.node.get("userAliases").get(userPub).then();
1373
1353
  return alias || null;
1374
1354
  }
1375
1355
  catch (error) {
@@ -1408,39 +1388,19 @@ class DataBase {
1408
1388
  const timestamp = Date.now();
1409
1389
  // Update in user registry
1410
1390
  try {
1411
- await new Promise((resolve, reject) => {
1412
- this.node
1413
- .get("users")
1414
- .get(userPub)
1415
- .get("lastSeen")
1416
- .put(timestamp, (ack) => {
1417
- if (ack && ack.err) {
1418
- reject(new Error(ack.err));
1419
- }
1420
- else {
1421
- resolve();
1422
- }
1423
- });
1424
- });
1391
+ await this.node
1392
+ .get("users")
1393
+ .get(userPub)
1394
+ .get("lastSeen")
1395
+ .put(timestamp)
1396
+ .then();
1425
1397
  }
1426
1398
  catch (error) {
1427
1399
  console.error(`Failed to update lastSeen in user registry:`, error);
1428
1400
  }
1429
1401
  // Update in user's own node
1430
1402
  try {
1431
- await new Promise((resolve, reject) => {
1432
- this.gun
1433
- .get(userPub)
1434
- .get("lastSeen")
1435
- .put(timestamp, (ack) => {
1436
- if (ack && ack.err) {
1437
- reject(new Error(ack.err));
1438
- }
1439
- else {
1440
- resolve();
1441
- }
1442
- });
1443
- });
1403
+ await this.gun.get(userPub).get("lastSeen").put(timestamp).then();
1444
1404
  }
1445
1405
  catch (error) {
1446
1406
  console.error(`Failed to update lastSeen in user node:`, error);
@@ -1691,19 +1651,10 @@ class DataBase {
1691
1651
  questions: JSON.stringify(securityQuestions),
1692
1652
  hint: encryptedHint,
1693
1653
  };
1694
- const ack = await new Promise((resolve, reject) => {
1695
- this.node
1696
- .get(userPub)
1697
- .get("security")
1698
- .put(securityPayload, (ack) => {
1699
- if (ack && ack.err) {
1700
- reject(new Error(ack.err));
1701
- }
1702
- else {
1703
- resolve(ack);
1704
- }
1705
- });
1706
- });
1654
+ const ack = await this.node.get(userPub)
1655
+ .get("security")
1656
+ .put(securityPayload)
1657
+ .then();
1707
1658
  if (ack.err) {
1708
1659
  console.error("Error saving security data to public graph:", ack.err);
1709
1660
  throw new Error(ack.err);
@@ -1726,26 +1677,15 @@ class DataBase {
1726
1677
  try {
1727
1678
  // Find the user's data using direct lookup
1728
1679
  const normalizedUsername = username.trim().toLowerCase();
1729
- const userPub = await new Promise((resolve) => {
1730
- this.node
1731
- .get("usernames")
1732
- .get(normalizedUsername)
1733
- .once((data) => {
1734
- resolve(data);
1735
- });
1736
- });
1680
+ const userPub = (await this.node.get("usernames").get(normalizedUsername).then()) ||
1681
+ null;
1737
1682
  if (!userPub) {
1738
1683
  return { success: false, error: "User not found" };
1739
1684
  }
1740
1685
  // Access the user's security data directly from their public key node
1741
- const securityData = await new Promise((resolve) => {
1742
- this.node
1743
- .get(userPub)
1744
- .get("security")
1745
- .once((data) => {
1746
- resolve(data);
1747
- });
1748
- });
1686
+ const securityData = await this.node.get(userPub)
1687
+ .get("security")
1688
+ .then();
1749
1689
  if (!securityData || !securityData.hint) {
1750
1690
  return {
1751
1691
  success: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shogun-core",
3
- "version": "4.2.3",
3
+ "version": "4.2.4",
4
4
  "description": "SHOGUN CORE - Core library for Shogun Ecosystem",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",