shogun-core 3.3.6 → 3.3.8

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
@@ -79,16 +79,11 @@ const CONFIG = {
79
79
  },
80
80
  };
81
81
  class DataBase {
82
- constructor(gun, appScope = "shogun", options) {
82
+ constructor(gun, appScope = "shogun") {
83
83
  this.user = null;
84
84
  this.onAuthCallbacks = [];
85
- this.disableAutoRecall = false;
86
- this.silent = false;
87
85
  // Initialize event emitter
88
86
  this.eventEmitter = new eventEmitter_1.EventEmitter();
89
- // Set options
90
- this.disableAutoRecall = options?.disableAutoRecall || false;
91
- this.silent = options?.silent || false;
92
87
  // Validate Gun instance
93
88
  if (!gun) {
94
89
  throw new Error("Gun instance is required but was not provided");
@@ -107,14 +102,7 @@ class DataBase {
107
102
  }
108
103
  this.gun = gun;
109
104
  // Recall only if NOT disabled and there's a "pair" in sessionStorage
110
- if (!this.disableAutoRecall &&
111
- typeof sessionStorage !== "undefined" &&
112
- sessionStorage.getItem("pair")) {
113
- this.user = this.gun.user().recall({ sessionStorage: true });
114
- }
115
- else {
116
- this.user = this.gun.user();
117
- }
105
+ this.user = this.gun.user().recall({ sessionStorage: true });
118
106
  this.subscribeToAuthEvents();
119
107
  this.crypto = crypto;
120
108
  this.sea = sea_1.default;
@@ -1409,6 +1397,13 @@ class DataBase {
1409
1397
  : undefined,
1410
1398
  };
1411
1399
  }
1400
+ /**
1401
+ * Performs login with username and password
1402
+ * @param username Username
1403
+ * @param password Password
1404
+ * @param pair SEA pair (optional)
1405
+ * @returns Promise resolving to AuthResult object
1406
+ */
1412
1407
  async login(username, password, pair) {
1413
1408
  try {
1414
1409
  const loginResult = await this.performAuthentication(username, password, pair);
@@ -1422,6 +1417,7 @@ class DataBase {
1422
1417
  await new Promise((resolve) => setTimeout(resolve, 100));
1423
1418
  const userPub = this.gun.user().is?.pub;
1424
1419
  let alias = this.gun.user().is?.alias;
1420
+ let userPair = this.gun.user()?._?.sea;
1425
1421
  if (!alias) {
1426
1422
  alias = username;
1427
1423
  }
@@ -1454,7 +1450,7 @@ class DataBase {
1454
1450
  try {
1455
1451
  const userInfo = {
1456
1452
  alias: username,
1457
- pair: pair ?? null,
1453
+ pair: pair ?? userPair,
1458
1454
  userPub: userPub,
1459
1455
  };
1460
1456
  this.saveCredentials(userInfo);
@@ -1469,6 +1465,39 @@ class DataBase {
1469
1465
  return { success: false, error: String(error) };
1470
1466
  }
1471
1467
  }
1468
+ /**
1469
+ * Performs login with GunDB pair directly
1470
+ * @param username Username
1471
+ * @param pair SEA pair
1472
+ * @returns Promise resolving to AuthResult object
1473
+ */
1474
+ async loginWithPair(username, pair) {
1475
+ try {
1476
+ const loginResult = await this.performAuthentication(username, "", pair);
1477
+ if (!loginResult.success) {
1478
+ return {
1479
+ success: false,
1480
+ error: `User '${username}' not found. Please check your username or register first.`,
1481
+ };
1482
+ }
1483
+ await this.runPostAuthOnAuthResult(username, pair.pub || "", {
1484
+ success: true,
1485
+ userPub: pair.pub,
1486
+ });
1487
+ try {
1488
+ await this.updateUserLastSeen(pair.pub);
1489
+ }
1490
+ catch (lastSeenError) {
1491
+ console.error(`Error updating last seen: ${lastSeenError}`);
1492
+ // Continue with login even if last seen update fails
1493
+ }
1494
+ return this.buildLoginResult(username, this.gun.user().is?.pub || "");
1495
+ }
1496
+ catch (error) {
1497
+ console.error(`Exception during login with pair: ${error}`);
1498
+ return { success: false, error: String(error) };
1499
+ }
1500
+ }
1472
1501
  saveCredentials(userInfo) {
1473
1502
  try {
1474
1503
  const sessionInfo = {
@@ -82,7 +82,7 @@ class AuthManager {
82
82
  * @description Authenticates user using a GunDB pair directly.
83
83
  * Emits login event on success.
84
84
  */
85
- async loginWithPair(pair) {
85
+ async loginWithPair(username, pair) {
86
86
  try {
87
87
  if (!pair || !pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
88
88
  return {
@@ -91,7 +91,7 @@ class AuthManager {
91
91
  };
92
92
  }
93
93
  // Use the new loginWithPair method from GunInstance
94
- const result = await this.core.db.login("", "", pair);
94
+ const result = await this.core.db.loginWithPair(username, pair);
95
95
  if (result.success) {
96
96
  // Include SEA pair in the response
97
97
  const seaPair = this.core.user?._?.sea;
@@ -101,7 +101,8 @@ class AuthManager {
101
101
  this.currentAuthMethod = "pair";
102
102
  this.core.emit("auth:login", {
103
103
  userPub: result.userPub ?? "",
104
- method: "password",
104
+ method: "pair",
105
+ username,
105
106
  });
106
107
  }
107
108
  else {
@@ -97,7 +97,7 @@ class CoreInitializer {
97
97
  throw new Error(`Failed to create Gun instance: ${error}`);
98
98
  }
99
99
  try {
100
- this.core.db = new gundb_1.DataBase(this.core._gun, config.gunOptions?.scope || "", { disableAutoRecall: config.disableAutoRecall, silent: config.silent });
100
+ this.core.db = new gundb_1.DataBase(this.core._gun, config.gunOptions?.scope || "");
101
101
  // Note: user is a getter that returns _user, so we don't need to assign it
102
102
  }
103
103
  catch (error) {
@@ -19,7 +19,7 @@ import { PluginManager } from "./managers/PluginManager";
19
19
  * @since 2.0.0
20
20
  */
21
21
  export declare class ShogunCore implements IShogunCore {
22
- static readonly API_VERSION = "^3.0.11";
22
+ static readonly API_VERSION = "^3.3.8";
23
23
  db: DataBase;
24
24
  storage: ShogunStorage;
25
25
  provider?: ethers.Provider;
@@ -237,7 +237,7 @@ export declare class ShogunCore implements IShogunCore {
237
237
  * @description Authenticates user using a GunDB pair directly.
238
238
  * Emits login event on success.
239
239
  */
240
- loginWithPair(pair: ISEAPair): Promise<AuthResult>;
240
+ loginWithPair(username: string, pair: ISEAPair): Promise<AuthResult>;
241
241
  /**
242
242
  * Register a new user with provided credentials
243
243
  * @param username - Username
@@ -1,5 +1,10 @@
1
1
  /**
2
- * Esempio semplice che mostra le differenze principali tra i metodi API
2
+ * Example showing how to use the simplified ShogunCore API
3
+ *
4
+ * The API has been streamlined:
5
+ * - AutoQuickStart: Quick initialization helper
6
+ * - api.database: Direct access to DataBase for basic operations (get, put, set, remove, auth)
7
+ * - api helper methods: High-level helpers for profile, settings, collections, and array utilities
3
8
  */
4
9
  declare function simpleAPITest(): Promise<void>;
5
10
  export { simpleAPITest };
@@ -1,11 +1,17 @@
1
1
  /**
2
- * Simplified API layer to reduce complexity for common use cases.
3
- * Provides quick-start methods that wrap the full DataBase functionality.
2
+ * Simplified API layer focused on valuable helper methods.
3
+ * Provides quick-start initialization and high-level convenience methods.
4
+ *
5
+ * For basic operations (get, put, set, remove, auth), use DataBase directly.
6
+ * This class provides:
7
+ * - Quick initialization helpers (QuickStart, AutoQuickStart)
8
+ * - Array/Object conversion utilities for GunDB
9
+ * - High-level user data helpers (profile, settings, collections)
4
10
  */
5
- import { GunMessageGet, GunMessagePut } from "gun";
6
11
  import { DataBase } from "./db";
7
12
  /**
8
- * Simple API wrapper that provides common operations with minimal complexity.
13
+ * Simple API wrapper that provides high-level helper methods.
14
+ * For basic operations, use the DataBase instance directly via the `database` property.
9
15
  */
10
16
  export declare class SimpleGunAPI {
11
17
  private db;
@@ -15,183 +21,40 @@ export declare class SimpleGunAPI {
15
21
  */
16
22
  constructor(db: DataBase);
17
23
  /**
18
- * Get data at a given path.
19
- * @param path The path to retrieve data from.
20
- * @returns The data at the path, or null if not found or on error.
24
+ * Get direct access to the DataBase instance for full control.
25
+ * Use this for basic operations like get, put, set, remove, login, etc.
21
26
  */
22
- get<T = unknown>(path: string): Promise<T | null>;
23
- /**
24
- * Get the Gun node at a given path for chaining operations.
25
- * @param path The path to the node.
26
- * @returns The Gun node.
27
- */
28
- getNode(path: string): any;
29
- /**
30
- * Get the Gun node at a given path for direct chaining.
31
- * @param path The path to the node.
32
- * @returns The Gun node.
33
- */
34
- node(path: string): any;
35
- /**
36
- * Get a chainable wrapper for a Gun node at a given path.
37
- * @param path The path to the node.
38
- * @returns An object with chainable methods: get, put, set, once, then, map.
39
- */
40
- chain(path: string): {
41
- get: (subPath: string) => GunMessageGet<any, any>;
42
- put: (data: any) => Promise<GunMessagePut>;
43
- set: (data: any) => Promise<GunMessagePut>;
44
- once: () => Promise<any>;
45
- then: () => Promise<any>;
46
- map: (callback: (value: any, key: string) => any) => any;
47
- };
48
- /**
49
- * Put data at a given path.
50
- * @param path The path to put data to.
51
- * @param data The data to put.
52
- * @returns The GunMessagePut result.
53
- */
54
- put<T = unknown>(path: string, data: T): Promise<GunMessagePut>;
55
- /**
56
- * Set data at a given path (alternative to put).
57
- * @param path The path to set data to.
58
- * @param data The data to set.
59
- * @returns The GunMessagePut result.
60
- */
61
- set<T = unknown>(path: string, data: T): Promise<GunMessagePut>;
62
- /**
63
- * Remove data at a given path.
64
- * @param path The path to remove data from.
65
- * @returns The GunMessagePut result.
66
- */
67
- remove(path: string): Promise<GunMessagePut>;
68
- /**
69
- * Log in a user.
70
- * @param username The username.
71
- * @param password The password.
72
- * @returns The user info if successful, or null.
73
- */
74
- login(username: string, password: string): Promise<{
75
- userPub: string;
76
- username: string;
77
- } | null>;
78
- /**
79
- * Sign up a new user.
80
- * @param username The username.
81
- * @param password The password.
82
- * @returns The user info if successful, or null.
83
- */
84
- signup(username: string, password: string): Promise<{
85
- userPub: string;
86
- username: string;
87
- } | null>;
88
- /**
89
- * Log out the current user.
90
- */
91
- logout(): void;
92
- /**
93
- * Check if a user is currently logged in.
94
- * @returns True if logged in, false otherwise.
95
- */
96
- isLoggedIn(): boolean;
97
- /**
98
- * Get user data at a given path (requires login).
99
- * @param path The path to the user data.
100
- * @returns The user data, or null if not found or on error.
101
- */
102
- getUserData<T = unknown>(path: string): Promise<T | null>;
103
- /**
104
- * Put user data at a given path (requires login).
105
- * @param path The path to put data to.
106
- * @param data The data to put.
107
- * @returns True if successful, false otherwise.
108
- */
109
- putUserData<T = unknown>(path: string, data: T): Promise<boolean>;
110
- /**
111
- * Set user data at a given path (alternative to put, requires login).
112
- * @param path The path to set data to.
113
- * @param data The data to set.
114
- * @returns True if successful, false otherwise.
115
- */
116
- setUserData<T = unknown>(path: string, data: T): Promise<boolean>;
117
- /**
118
- * Remove user data at a given path (requires login).
119
- * @param path The path to remove data from.
120
- * @returns True if successful, false otherwise.
121
- */
122
- removeUserData(path: string): Promise<boolean>;
27
+ get database(): DataBase;
123
28
  /**
124
29
  * Convert an array to an indexed object for GunDB storage.
30
+ * GunDB doesn't store arrays natively, so this converts them to objects indexed by ID.
125
31
  * Example: [{id: '1', ...}, {id: '2', ...}] => { "1": {...}, "2": {...} }
126
- * @param arr The array to convert.
127
- * @returns The indexed object.
128
- * @private
129
- */
130
- private getIndexedObjectFromArray;
131
- /**
132
- * Convert an indexed object back to an array.
133
- * Example: { "1": {...}, "2": {...} } => [{id: '1', ...}, {id: '2', ...}]
134
- * @param indexedObj The indexed object to convert.
135
- * @returns The array.
136
- * @private
137
- */
138
- private getArrayFromIndexedObject;
139
- /**
140
- * Convert an array to an indexed object for GunDB storage (public method).
141
- * @param arr The array to convert.
142
- * @returns The indexed object.
32
+ * @param arr The array to convert (each item must have an 'id' property).
33
+ * @returns The indexed object suitable for GunDB storage.
143
34
  */
144
35
  arrayToIndexedObject<T extends {
145
36
  id: string | number;
146
37
  }>(arr: T[]): Record<string, T>;
147
38
  /**
148
- * Convert an indexed object to an array (public method).
39
+ * Convert an indexed object back to an array.
40
+ * Reverses the arrayToIndexedObject conversion.
41
+ * Example: { "1": {...}, "2": {...} } => [{id: '1', ...}, {id: '2', ...}]
149
42
  * @param indexedObj The indexed object to convert.
150
- * @returns The array.
43
+ * @returns The array of items.
151
44
  */
152
45
  indexedObjectToArray<T>(indexedObj: Record<string, T> | null): T[];
153
46
  /**
154
- * Get the GunDB user node at a given path (requires login).
155
- * Useful for advanced operations that need direct GunDB node access.
156
- * @param path The path to the user node.
157
- * @returns The Gun node.
158
- * @throws If not logged in.
159
- */
160
- getUserNode(path: string): any;
161
- /**
162
- * Get the GunDB global node at a given path.
163
- * Useful for advanced operations that need direct GunDB node access.
164
- * @param path The path to the global node.
165
- * @returns The Gun node.
47
+ * Get all user data (returns user's entire data tree).
48
+ * Requires user to be logged in.
49
+ * @returns The complete user data tree, or null if not logged in or on error.
166
50
  */
167
- getGlobalNode(path: string): any;
168
- /**
169
- * Get the current user info.
170
- * @returns The current user info, or null if not logged in.
171
- */
172
- getCurrentUser(): {
173
- pub: string;
174
- username?: string;
175
- } | null;
176
- /**
177
- * Check if a user exists by alias.
178
- * @param alias The user alias.
179
- * @returns True if the user exists, false otherwise.
180
- */
181
- userExists(alias: string): Promise<boolean>;
182
- /**
183
- * Get user info by alias.
184
- * @param alias The user alias.
185
- * @returns The user info, or null if not found.
186
- */
187
- getUser(alias: string): Promise<{
188
- userPub: string;
189
- username: string;
190
- } | null>;
51
+ getAllUserData(): Promise<Record<string, unknown> | null>;
191
52
  /**
192
- * Advanced user space operations
53
+ * Update user profile with common fields.
54
+ * Provides a standardized location for user profile data.
55
+ * @param profileData Profile data to save (name, email, bio, avatar, etc.)
56
+ * @returns True if successful, false otherwise.
193
57
  */
194
- getAllUserData(): Promise<Record<string, unknown> | null>;
195
58
  updateProfile(profileData: {
196
59
  name?: string;
197
60
  email?: string;
@@ -199,14 +62,63 @@ export declare class SimpleGunAPI {
199
62
  avatar?: string;
200
63
  [key: string]: unknown;
201
64
  }): Promise<boolean>;
65
+ /**
66
+ * Get user profile data.
67
+ * @returns The user profile data, or null if not found or not logged in.
68
+ */
202
69
  getProfile(): Promise<Record<string, unknown> | null>;
70
+ /**
71
+ * Save user settings.
72
+ * Provides a standardized location for application settings.
73
+ * @param settings Settings object to save.
74
+ * @returns True if successful, false otherwise.
75
+ */
203
76
  saveSettings(settings: Record<string, unknown>): Promise<boolean>;
77
+ /**
78
+ * Get user settings.
79
+ * @returns The user settings, or null if not found or not logged in.
80
+ */
204
81
  getSettings(): Promise<Record<string, unknown> | null>;
82
+ /**
83
+ * Save user preferences.
84
+ * Provides a standardized location for user preferences (distinct from settings).
85
+ * @param preferences Preferences object to save.
86
+ * @returns True if successful, false otherwise.
87
+ */
205
88
  savePreferences(preferences: Record<string, unknown>): Promise<boolean>;
89
+ /**
90
+ * Get user preferences.
91
+ * @returns The user preferences, or null if not found or not logged in.
92
+ */
206
93
  getPreferences(): Promise<Record<string, unknown> | null>;
94
+ /**
95
+ * Create a user collection with initial items.
96
+ * Provides a standardized location for user collections.
97
+ * @param collectionName The name of the collection.
98
+ * @param items The initial items for the collection.
99
+ * @returns True if successful, false otherwise.
100
+ */
207
101
  createCollection<T = unknown>(collectionName: string, items: Record<string, T>): Promise<boolean>;
102
+ /**
103
+ * Add an item to a user collection.
104
+ * @param collectionName The name of the collection.
105
+ * @param itemId The ID of the item to add.
106
+ * @param item The item data.
107
+ * @returns True if successful, false otherwise.
108
+ */
208
109
  addToCollection<T = unknown>(collectionName: string, itemId: string, item: T): Promise<boolean>;
110
+ /**
111
+ * Get a user collection.
112
+ * @param collectionName The name of the collection.
113
+ * @returns The collection data, or null if not found or not logged in.
114
+ */
209
115
  getCollection(collectionName: string): Promise<Record<string, unknown> | null>;
116
+ /**
117
+ * Remove an item from a user collection.
118
+ * @param collectionName The name of the collection.
119
+ * @param itemId The ID of the item to remove.
120
+ * @returns True if successful, false otherwise.
121
+ */
210
122
  removeFromCollection(collectionName: string, itemId: string): Promise<boolean>;
211
123
  }
212
124
  /**
@@ -29,12 +29,7 @@ declare class DataBase {
29
29
  private readonly onAuthCallbacks;
30
30
  private readonly eventEmitter;
31
31
  private _rxjs?;
32
- private disableAutoRecall;
33
- private silent;
34
- constructor(gun: IGunInstance, appScope?: string, options?: {
35
- disableAutoRecall?: boolean;
36
- silent?: boolean;
37
- });
32
+ constructor(gun: IGunInstance, appScope?: string);
38
33
  /**
39
34
  * Initialize the GunInstance asynchronously
40
35
  * This method should be called after construction to perform async operations
@@ -280,7 +275,21 @@ declare class DataBase {
280
275
  * Builds login result object
281
276
  */
282
277
  private buildLoginResult;
278
+ /**
279
+ * Performs login with username and password
280
+ * @param username Username
281
+ * @param password Password
282
+ * @param pair SEA pair (optional)
283
+ * @returns Promise resolving to AuthResult object
284
+ */
283
285
  login(username: string, password: string, pair?: ISEAPair | null): Promise<AuthResult>;
286
+ /**
287
+ * Performs login with GunDB pair directly
288
+ * @param username Username
289
+ * @param pair SEA pair
290
+ * @returns Promise resolving to AuthResult object
291
+ */
292
+ loginWithPair(username: string, pair: ISEAPair): Promise<AuthResult>;
284
293
  private saveCredentials;
285
294
  /**
286
295
  * Sets up security questions and password hint
@@ -10,7 +10,7 @@ import { EventEmitter } from "../utils/eventEmitter";
10
10
  export interface AuthEventData {
11
11
  userPub?: string;
12
12
  username?: string;
13
- method: "password" | "webauthn" | "web3" | "nostr" | "oauth" | "bitcoin";
13
+ method: "password" | "webauthn" | "web3" | "nostr" | "oauth" | "bitcoin" | "pair";
14
14
  provider?: string;
15
15
  }
16
16
  /**
@@ -40,6 +40,12 @@ export declare enum CorePlugins {
40
40
  OAuth = "oauth"
41
41
  }
42
42
  export type AuthMethod = "password" | "webauthn" | "web3" | "nostr" | "oauth" | "pair";
43
+ export interface AuthEventData {
44
+ userPub?: string;
45
+ username?: string;
46
+ method: "password" | "webauthn" | "web3" | "nostr" | "oauth" | "pair";
47
+ provider?: string;
48
+ }
43
49
  export interface AuthResult {
44
50
  success: boolean;
45
51
  error?: string;
@@ -133,7 +139,7 @@ export interface IShogunCore extends PluginManager {
133
139
  emit<K extends keyof ShogunEventMap>(eventName: K, data?: ShogunEventMap[K] extends void ? never : ShogunEventMap[K]): boolean;
134
140
  getRecentErrors(count?: number): ShogunError[];
135
141
  login(username: string, password: string, pair?: ISEAPair | null): Promise<AuthResult>;
136
- loginWithPair(pair: ISEAPair): Promise<AuthResult>;
142
+ loginWithPair(username: string, pair: ISEAPair): Promise<AuthResult>;
137
143
  signUp(username: string, password?: string, pair?: ISEAPair | null): Promise<SignUpResult>;
138
144
  getAuthenticationMethod(type: AuthMethod): any;
139
145
  setAuthMethod(method: AuthMethod): void;
@@ -36,7 +36,7 @@ export declare class AuthManager {
36
36
  * @description Authenticates user using a GunDB pair directly.
37
37
  * Emits login event on success.
38
38
  */
39
- loginWithPair(pair: ISEAPair): Promise<AuthResult>;
39
+ loginWithPair(username: string, pair: ISEAPair): Promise<AuthResult>;
40
40
  /**
41
41
  * Register a new user with provided credentials
42
42
  * @param username - Username
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shogun-core",
3
- "version": "3.3.6",
3
+ "version": "3.3.8",
4
4
  "description": "SHOGUN CORE - Core library for Shogun Ecosystem",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -50,22 +50,15 @@
50
50
  "author": "Scobru",
51
51
  "license": "MIT",
52
52
  "dependencies": {
53
- "@fluidkey/stealth-account-kit": "^1.1.0",
54
53
  "@noble/curves": "^1.9.1",
55
- "@scure/bip32": "^2.0.1",
56
54
  "assert": "^2.1.0",
57
- "base64url": "^3.0.1",
58
55
  "buffer": "^6.0.3",
59
56
  "constants-browserify": "^1.0.0",
60
57
  "crypto-browserify": "^3.12.0",
61
58
  "ethers": "^6.13.5",
62
59
  "gun": "git+https://github.com/amark/gun.git",
63
- "keccak256": "^1.0.6",
64
60
  "nostr-tools": "^2.15.0",
65
- "qs": "^6.14.0",
66
61
  "rxjs": "^7.8.2",
67
- "ts-node": "^10.9.2",
68
- "url": "^0.11.4",
69
62
  "uuid": "^11.1.0",
70
63
  "vm-browserify": "^1.1.2"
71
64
  },