shogun-core 4.2.3 → 4.2.5

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.
@@ -132720,6 +132720,17 @@ class ShogunCore {
132720
132720
  }
132721
132721
  });
132722
132722
  }
132723
+ /**
132724
+ * Manually initialize the Shogun SDK (for testing or delayed initialization)
132725
+ * @returns Promise that resolves when initialization is complete
132726
+ */
132727
+ async initialize() {
132728
+ // If already initialized, just wait a bit for any pending initialization
132729
+ if (this._gun) {
132730
+ return Promise.resolve();
132731
+ }
132732
+ return this.coreInitializer.initialize(this.config);
132733
+ }
132723
132734
  /**
132724
132735
  * Access to the Gun instance
132725
132736
  * @returns The Gun instance
@@ -133841,6 +133852,7 @@ class DataBase {
133841
133852
  constructor(gun, appScope = "shogun") {
133842
133853
  this.user = null;
133843
133854
  this.onAuthCallbacks = [];
133855
+ console.log("[DB] Initializing DataBase");
133844
133856
  // Initialize event emitter
133845
133857
  this.eventEmitter = new eventEmitter_1.EventEmitter();
133846
133858
  // Validate Gun instance
@@ -133860,31 +133872,38 @@ class DataBase {
133860
133872
  throw new Error(`Gun instance is invalid: gun.on is not a function. Received gun.on type: ${typeof gun.on}`);
133861
133873
  }
133862
133874
  this.gun = gun;
133875
+ console.log("[DB] Gun instance validated");
133863
133876
  // Recall only if NOT disabled and there's a "pair" in sessionStorage
133864
133877
  this.user = this.gun.user().recall({ sessionStorage: true });
133878
+ console.log("[DB] User recall completed");
133865
133879
  this.subscribeToAuthEvents();
133880
+ console.log("[DB] Auth events subscribed");
133866
133881
  this.crypto = crypto;
133867
133882
  this.sea = sea_1.default;
133868
133883
  this._rxjs = new rxjs_1.RxJS(this.gun);
133869
133884
  this.node = null;
133885
+ console.log("[DB] DataBase initialization completed");
133870
133886
  }
133871
133887
  /**
133872
133888
  * Initialize the GunInstance asynchronously
133873
133889
  * This method should be called after construction to perform async operations
133874
133890
  */
133875
133891
  initialize(appScope = "shogun") {
133892
+ console.log(`[DB] Initializing with appScope: ${appScope}`);
133876
133893
  try {
133877
133894
  const sessionResult = this.restoreSession();
133895
+ console.log(`[DB] Session restore result: ${sessionResult.success ? "success" : "failed"}`);
133878
133896
  this.node = this.gun.get(appScope);
133897
+ console.log("[DB] App scope node initialized");
133879
133898
  if (sessionResult.success) {
133880
- // Session automatically restored
133899
+ console.log("[DB] Session automatically restored");
133881
133900
  }
133882
133901
  else {
133883
- // No previous session to restore
133902
+ console.log("[DB] No previous session to restore");
133884
133903
  }
133885
133904
  }
133886
133905
  catch (error) {
133887
- console.error("Error during automatic session restoration:", error);
133906
+ console.error("[DB] Error during automatic session restoration:", error);
133888
133907
  }
133889
133908
  }
133890
133909
  subscribeToAuthEvents() {
@@ -133907,13 +133926,16 @@ class DataBase {
133907
133926
  * @param peer URL of the peer to add
133908
133927
  */
133909
133928
  addPeer(peer) {
133929
+ console.log(`[PEER] Adding peer: ${peer}`);
133910
133930
  this.gun.opt({ peers: [peer] });
133931
+ console.log(`[PEER] Peer added successfully`);
133911
133932
  }
133912
133933
  /**
133913
133934
  * Removes a peer from the network
133914
133935
  * @param peer URL of the peer to remove
133915
133936
  */
133916
133937
  removePeer(peer) {
133938
+ console.log(`[PEER] Removing peer: ${peer}`);
133917
133939
  try {
133918
133940
  // Get current peers from Gun instance
133919
133941
  const gunOpts = this.gun._.opt;
@@ -133925,13 +133947,14 @@ class DataBase {
133925
133947
  if (peerConnection && typeof peerConnection.close === "function") {
133926
133948
  peerConnection.close();
133927
133949
  }
133950
+ console.log(`[PEER] Peer removed successfully`);
133928
133951
  }
133929
133952
  else {
133930
- console.error(`Peer not found in current connections: ${peer}`);
133953
+ console.error(`[PEER] Peer not found in current connections: ${peer}`);
133931
133954
  }
133932
133955
  }
133933
133956
  catch (error) {
133934
- console.error(`Error removing peer ${peer}:`, error);
133957
+ console.error(`[PEER] Error removing peer ${peer}:`, error);
133935
133958
  }
133936
133959
  }
133937
133960
  /**
@@ -134142,16 +134165,7 @@ class DataBase {
134142
134165
  */
134143
134166
  async put(path, data) {
134144
134167
  const node = this.navigateToPath(this.node, path);
134145
- return new Promise((resolve, reject) => {
134146
- node.put(data, (ack) => {
134147
- if (ack && ack.err) {
134148
- reject(new Error(ack.err));
134149
- }
134150
- else {
134151
- resolve(ack);
134152
- }
134153
- });
134154
- });
134168
+ return await node.put(data).then();
134155
134169
  }
134156
134170
  /**
134157
134171
  * Sets data at the specified path
@@ -134161,16 +134175,7 @@ class DataBase {
134161
134175
  */
134162
134176
  async set(path, data) {
134163
134177
  const node = this.navigateToPath(this.node, path);
134164
- return new Promise((resolve, reject) => {
134165
- node.set(data, (ack) => {
134166
- if (ack && ack.err) {
134167
- reject(new Error(ack.err));
134168
- }
134169
- else {
134170
- resolve(ack);
134171
- }
134172
- });
134173
- });
134178
+ return await node.set(data).then();
134174
134179
  }
134175
134180
  /**
134176
134181
  * Removes data at the specified path
@@ -134179,16 +134184,7 @@ class DataBase {
134179
134184
  */
134180
134185
  async remove(path) {
134181
134186
  const node = this.navigateToPath(this.node, path);
134182
- return new Promise((resolve, reject) => {
134183
- node.put(null, (ack) => {
134184
- if (ack && ack.err) {
134185
- reject(new Error(ack.err));
134186
- }
134187
- else {
134188
- resolve(ack);
134189
- }
134190
- });
134191
- });
134187
+ return await node.put(null).then();
134192
134188
  }
134193
134189
  /**
134194
134190
  * Checks if a user is currently logged in
@@ -134691,18 +134687,20 @@ class DataBase {
134691
134687
  });
134692
134688
  }
134693
134689
  async runPostAuthOnAuthResult(username, userPub, authResult) {
134694
- // Setting up user profile after authentication
134690
+ console.log(`[POSTAUTH] Starting post-auth setup for user: ${username}, userPub: ${userPub}`);
134695
134691
  try {
134692
+ console.log(`[POSTAUTH] Validating parameters for user: ${username}`);
134696
134693
  // Validate required parameters
134697
134694
  if (!username ||
134698
134695
  typeof username !== "string" ||
134699
134696
  username.trim().length === 0) {
134697
+ console.error(`[POSTAUTH] Invalid username provided: ${username}`);
134700
134698
  throw new Error("Invalid username provided");
134701
134699
  }
134702
134700
  if (!userPub ||
134703
134701
  typeof userPub !== "string" ||
134704
134702
  userPub.trim().length === 0) {
134705
- console.error("Invalid userPub provided:", {
134703
+ console.error("[POSTAUTH] Invalid userPub provided:", {
134706
134704
  userPub,
134707
134705
  type: typeof userPub,
134708
134706
  authResult,
@@ -134711,34 +134709,46 @@ class DataBase {
134711
134709
  }
134712
134710
  // Additional validation for userPub format
134713
134711
  if (!userPub.includes(".") || userPub.length < 10) {
134714
- console.error("Invalid userPub format:", userPub);
134712
+ console.error(`[POSTAUTH] Invalid userPub format: ${userPub}`);
134715
134713
  throw new Error("Invalid userPub format");
134716
134714
  }
134715
+ console.log(`[POSTAUTH] Parameters validated for user: ${username}`);
134717
134716
  // Normalize username to prevent path issues
134718
134717
  const normalizedUsername = username.trim().toLowerCase();
134719
134718
  if (normalizedUsername.length === 0) {
134719
+ console.error(`[POSTAUTH] Normalized username is empty for user: ${username}`);
134720
134720
  throw new Error("Username cannot be empty");
134721
134721
  }
134722
- const existingUser = await new Promise((resolve) => {
134723
- this.gun.get(userPub).once((data) => {
134724
- resolve(data);
134725
- });
134722
+ console.log(`[POSTAUTH] Normalized username: ${normalizedUsername}`);
134723
+ console.log(`[POSTAUTH] Checking if user exists: ${userPub}`);
134724
+ // Add timeout to prevent hanging
134725
+ const existingUser = await Promise.race([
134726
+ this.gun.get(userPub).then(),
134727
+ new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout checking user existence")), 5000)),
134728
+ ]).catch((error) => {
134729
+ console.error(`[POSTAUTH] Error or timeout checking user existence:`, error);
134730
+ return null;
134726
134731
  });
134732
+ console.log(`[POSTAUTH] User existence check completed, existingUser:`, existingUser);
134727
134733
  const isNewUser = !existingUser || !existingUser.alias;
134728
- // const isNewUser = true;
134734
+ console.log(`[POSTAUTH] User is ${isNewUser ? "NEW" : "EXISTING"}: ${userPub}`);
134729
134735
  // Get user's encryption public key (epub) for comprehensive tracking
134730
134736
  const userInstance = this.gun.user();
134731
134737
  const userSea = userInstance?._?.sea;
134732
134738
  const epub = userSea?.epub;
134739
+ console.log(`[POSTAUTH] User epub retrieved: ${epub ? "yes" : "no"}`);
134733
134740
  // Enhanced user tracking system
134741
+ console.log(`[POSTAUTH] Setting up comprehensive user tracking for: ${normalizedUsername}`);
134734
134742
  const trackingResult = await this.setupComprehensiveUserTracking(normalizedUsername, userPub, epub);
134735
134743
  if (!trackingResult) {
134744
+ console.error(`[POSTAUTH] Comprehensive user tracking setup failed for: ${normalizedUsername}`);
134736
134745
  return {
134737
134746
  success: false,
134738
134747
  error: "Comprehensive user tracking setup failed",
134739
134748
  };
134740
134749
  }
134741
- return {
134750
+ console.log(`[POSTAUTH] User tracking setup completed successfully for: ${normalizedUsername}`);
134751
+ const result = {
134742
134752
  success: true,
134743
134753
  userPub: userPub,
134744
134754
  username: normalizedUsername,
@@ -134753,9 +134763,11 @@ class DataBase {
134753
134763
  }
134754
134764
  : undefined,
134755
134765
  };
134766
+ console.log(`[POSTAUTH] Post-auth setup completed successfully for user: ${username}`);
134767
+ return result;
134756
134768
  }
134757
134769
  catch (error) {
134758
- console.error(`Error in post-authentication setup: ${error}`);
134770
+ console.error(`[POSTAUTH] Error in post-authentication setup for ${username}:`, error);
134759
134771
  return {
134760
134772
  success: false,
134761
134773
  error: `Post-authentication setup failed: ${error}`,
@@ -134767,43 +134779,66 @@ class DataBase {
134767
134779
  * Creates multiple indexes for efficient user discovery
134768
134780
  */
134769
134781
  async setupComprehensiveUserTracking(username, userPub, epub) {
134782
+ console.log(`[TRACKING] Starting comprehensive user tracking setup for: ${username}, userPub: ${userPub}`);
134770
134783
  try {
134771
134784
  // 1. Create alias index: ~@alias -> userPub (for GunDB compatibility)
134785
+ console.log(`[TRACKING] Step 1: Creating alias index for ${username}`);
134772
134786
  const aliasIndexResult = await this.createAliasIndex(username, userPub);
134773
134787
  if (!aliasIndexResult) {
134788
+ console.error(`[TRACKING] Failed to create alias index for ${username}`);
134774
134789
  return false;
134775
134790
  }
134791
+ console.log(`[TRACKING] Step 1 completed: Alias index created for ${username}`);
134776
134792
  // 2. Create username mapping: usernames/alias -> userPub
134793
+ console.log(`[TRACKING] Step 2: Creating username mapping for ${username}`);
134777
134794
  const usernameMappingResult = await this.createUsernameMapping(username, userPub);
134778
134795
  if (!usernameMappingResult) {
134796
+ console.error(`[TRACKING] Failed to create username mapping for ${username}`);
134779
134797
  return false;
134780
134798
  }
134799
+ console.log(`[TRACKING] Step 2 completed: Username mapping created for ${username}`);
134781
134800
  // 3. Create user registry: users/userPub -> user data
134801
+ console.log(`[TRACKING] Step 3: Creating user registry for ${username}`);
134782
134802
  const userRegistryResult = await this.createUserRegistry(username, userPub, epub);
134783
134803
  if (!userRegistryResult) {
134804
+ console.error(`[TRACKING] Failed to create user registry for ${username}`);
134784
134805
  return false;
134785
134806
  }
134807
+ console.log(`[TRACKING] Step 3 completed: User registry created for ${username}`);
134786
134808
  // 4. Create reverse lookup: userPub -> alias
134809
+ console.log(`[TRACKING] Step 4: Creating reverse lookup for ${username}`);
134787
134810
  const reverseLookupResult = await this.createReverseLookup(username, userPub);
134788
134811
  if (!reverseLookupResult) {
134812
+ console.error(`[TRACKING] Failed to create reverse lookup for ${username}`);
134789
134813
  return false;
134790
134814
  }
134815
+ console.log(`[TRACKING] Step 4 completed: Reverse lookup created for ${username}`);
134791
134816
  // 5. Create epub index: epubKeys/epub -> userPub (for encryption lookups)
134792
134817
  if (epub) {
134818
+ console.log(`[TRACKING] Step 5: Creating epub index for ${username}`);
134793
134819
  const epubIndexResult = await this.createEpubIndex(epub, userPub);
134794
134820
  if (!epubIndexResult) {
134821
+ console.error(`[TRACKING] Failed to create epub index for ${username}`);
134795
134822
  return false;
134796
134823
  }
134824
+ console.log(`[TRACKING] Step 5 completed: Epub index created for ${username}`);
134825
+ }
134826
+ else {
134827
+ console.log(`[TRACKING] Step 5 skipped: No epub available for ${username}`);
134797
134828
  }
134798
134829
  // 6. Create user metadata in user's own node
134830
+ console.log(`[TRACKING] Step 6: Creating user metadata for ${username}`);
134799
134831
  const userMetadataResult = await this.createUserMetadata(username, userPub, epub);
134800
134832
  if (!userMetadataResult) {
134833
+ console.error(`[TRACKING] Failed to create user metadata for ${username}`);
134801
134834
  return false;
134802
134835
  }
134836
+ console.log(`[TRACKING] Step 6 completed: User metadata created for ${username}`);
134837
+ console.log(`[TRACKING] Comprehensive user tracking setup completed successfully for: ${username}`);
134803
134838
  return true;
134804
134839
  }
134805
134840
  catch (error) {
134806
- console.error(`Error in comprehensive user tracking setup: ${error}`);
134841
+ console.error(`[TRACKING] Error in comprehensive user tracking setup for ${username}:`, error);
134807
134842
  // Don't throw - continue with other operations
134808
134843
  return false;
134809
134844
  }
@@ -134895,20 +134930,18 @@ class DataBase {
134895
134930
  */
134896
134931
  async createReverseLookup(username, userPub) {
134897
134932
  try {
134898
- return new Promise((resolve) => {
134899
- this.node
134900
- .get("userAliases")
134901
- .get(userPub)
134902
- .put(username, (ack) => {
134903
- if (ack && ack.err) {
134904
- console.error(`Error creating reverse lookup: ${ack.err}`);
134905
- resolve(false);
134906
- }
134907
- else {
134908
- resolve(true);
134909
- }
134910
- });
134911
- });
134933
+ const ack = await this.node
134934
+ .get("userAliases")
134935
+ .get(userPub)
134936
+ .put(username)
134937
+ .then();
134938
+ if (ack.err) {
134939
+ console.error(`Error creating reverse lookup: ${ack.err}`);
134940
+ return false;
134941
+ }
134942
+ else {
134943
+ return true;
134944
+ }
134912
134945
  }
134913
134946
  catch (error) {
134914
134947
  console.error(`Error creating reverse lookup: ${error}`);
@@ -134981,14 +135014,9 @@ class DataBase {
134981
135014
  }
134982
135015
  // Method 1: Try GunDB standard alias lookup (~@alias)
134983
135016
  try {
134984
- const aliasData = this.gun.get(`~@${normalizedAlias}`);
134985
- const aliasDataObj = await new Promise((resolve) => {
134986
- aliasData.once((data) => {
134987
- resolve(data);
134988
- });
134989
- });
134990
- if (aliasDataObj && aliasDataObj["~pubKeyOfUser"]) {
134991
- const userPub = aliasDataObj["~pubKeyOfUser"]["#"] || aliasDataObj["~pubKeyOfUser"];
135017
+ const aliasData = await this.gun.get(`~@${normalizedAlias}`).then();
135018
+ if (aliasData && aliasData["~pubKeyOfUser"]) {
135019
+ const userPub = aliasData["~pubKeyOfUser"]["#"] || aliasData["~pubKeyOfUser"];
134992
135020
  if (userPub) {
134993
135021
  const userData = await this.getUserDataByPub(userPub);
134994
135022
  if (userData) {
@@ -135002,14 +135030,10 @@ class DataBase {
135002
135030
  }
135003
135031
  // Method 2: Try username mapping (usernames/alias -> userPub)
135004
135032
  try {
135005
- const userPub = await new Promise((resolve) => {
135006
- this.node
135007
- .get("usernames")
135008
- .get(normalizedAlias)
135009
- .once((data) => {
135010
- resolve(data);
135011
- });
135012
- });
135033
+ const userPub = await this.node
135034
+ .get("usernames")
135035
+ .get(normalizedAlias)
135036
+ .then();
135013
135037
  if (userPub) {
135014
135038
  const userData = await this.getUserDataByPub(userPub);
135015
135039
  if (userData) {
@@ -135039,14 +135063,7 @@ class DataBase {
135039
135063
  }
135040
135064
  // Method 1: Try user registry (users/userPub -> user data)
135041
135065
  try {
135042
- const userData = await new Promise((resolve) => {
135043
- this.node
135044
- .get("users")
135045
- .get(userPub)
135046
- .once((data) => {
135047
- resolve(data);
135048
- });
135049
- });
135066
+ const userData = await this.node.get("users").get(userPub).then();
135050
135067
  if (userData && userData.username) {
135051
135068
  return {
135052
135069
  userPub: userData.userPub || userPub,
@@ -135062,11 +135079,7 @@ class DataBase {
135062
135079
  }
135063
135080
  // Method 2: Try user's own node
135064
135081
  try {
135065
- const userNodeData = await new Promise((resolve) => {
135066
- this.gun.get(userPub).once((data) => {
135067
- resolve(data);
135068
- });
135069
- });
135082
+ const userNodeData = await this.gun.get(userPub).then();
135070
135083
  if (userNodeData && userNodeData.username) {
135071
135084
  return {
135072
135085
  userPub: userPub,
@@ -135097,14 +135110,7 @@ class DataBase {
135097
135110
  if (!epub || typeof epub !== "string") {
135098
135111
  return null;
135099
135112
  }
135100
- const userPub = await new Promise((resolve) => {
135101
- this.node
135102
- .get("epubKeys")
135103
- .get(epub)
135104
- .once((data) => {
135105
- resolve(data);
135106
- });
135107
- });
135113
+ const userPub = await this.node.get("epubKeys").get(epub).then();
135108
135114
  return userPub || null;
135109
135115
  }
135110
135116
  catch (error) {
@@ -135122,14 +135128,7 @@ class DataBase {
135122
135128
  if (!userPub || typeof userPub !== "string") {
135123
135129
  return null;
135124
135130
  }
135125
- const alias = await new Promise((resolve) => {
135126
- this.node
135127
- .get("userAliases")
135128
- .get(userPub)
135129
- .once((data) => {
135130
- resolve(data);
135131
- });
135132
- });
135131
+ const alias = await this.node.get("userAliases").get(userPub).then();
135133
135132
  return alias || null;
135134
135133
  }
135135
135134
  catch (error) {
@@ -135168,39 +135167,19 @@ class DataBase {
135168
135167
  const timestamp = Date.now();
135169
135168
  // Update in user registry
135170
135169
  try {
135171
- await new Promise((resolve, reject) => {
135172
- this.node
135173
- .get("users")
135174
- .get(userPub)
135175
- .get("lastSeen")
135176
- .put(timestamp, (ack) => {
135177
- if (ack && ack.err) {
135178
- reject(new Error(ack.err));
135179
- }
135180
- else {
135181
- resolve();
135182
- }
135183
- });
135184
- });
135170
+ await this.node
135171
+ .get("users")
135172
+ .get(userPub)
135173
+ .get("lastSeen")
135174
+ .put(timestamp)
135175
+ .then();
135185
135176
  }
135186
135177
  catch (error) {
135187
135178
  console.error(`Failed to update lastSeen in user registry:`, error);
135188
135179
  }
135189
135180
  // Update in user's own node
135190
135181
  try {
135191
- await new Promise((resolve, reject) => {
135192
- this.gun
135193
- .get(userPub)
135194
- .get("lastSeen")
135195
- .put(timestamp, (ack) => {
135196
- if (ack && ack.err) {
135197
- reject(new Error(ack.err));
135198
- }
135199
- else {
135200
- resolve();
135201
- }
135202
- });
135203
- });
135182
+ await this.gun.get(userPub).get("lastSeen").put(timestamp).then();
135204
135183
  }
135205
135184
  catch (error) {
135206
135185
  console.error(`Failed to update lastSeen in user node:`, error);
@@ -135451,19 +135430,10 @@ class DataBase {
135451
135430
  questions: JSON.stringify(securityQuestions),
135452
135431
  hint: encryptedHint,
135453
135432
  };
135454
- const ack = await new Promise((resolve, reject) => {
135455
- this.node
135456
- .get(userPub)
135457
- .get("security")
135458
- .put(securityPayload, (ack) => {
135459
- if (ack && ack.err) {
135460
- reject(new Error(ack.err));
135461
- }
135462
- else {
135463
- resolve(ack);
135464
- }
135465
- });
135466
- });
135433
+ const ack = await this.node.get(userPub)
135434
+ .get("security")
135435
+ .put(securityPayload)
135436
+ .then();
135467
135437
  if (ack.err) {
135468
135438
  console.error("Error saving security data to public graph:", ack.err);
135469
135439
  throw new Error(ack.err);
@@ -135486,26 +135456,15 @@ class DataBase {
135486
135456
  try {
135487
135457
  // Find the user's data using direct lookup
135488
135458
  const normalizedUsername = username.trim().toLowerCase();
135489
- const userPub = await new Promise((resolve) => {
135490
- this.node
135491
- .get("usernames")
135492
- .get(normalizedUsername)
135493
- .once((data) => {
135494
- resolve(data);
135495
- });
135496
- });
135459
+ const userPub = (await this.node.get("usernames").get(normalizedUsername).then()) ||
135460
+ null;
135497
135461
  if (!userPub) {
135498
135462
  return { success: false, error: "User not found" };
135499
135463
  }
135500
135464
  // Access the user's security data directly from their public key node
135501
- const securityData = await new Promise((resolve) => {
135502
- this.node
135503
- .get(userPub)
135504
- .get("security")
135505
- .once((data) => {
135506
- resolve(data);
135507
- });
135508
- });
135465
+ const securityData = await this.node.get(userPub)
135466
+ .get("security")
135467
+ .then();
135509
135468
  if (!securityData || !securityData.hint) {
135510
135469
  return {
135511
135470
  success: false,