shogun-core 3.3.6 → 3.3.7

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/core.js CHANGED
@@ -215,8 +215,8 @@ class ShogunCore {
215
215
  * @description Authenticates user using a GunDB pair directly.
216
216
  * Emits login event on success.
217
217
  */
218
- async loginWithPair(pair) {
219
- return this.authManager.loginWithPair(pair);
218
+ async loginWithPair(username, pair) {
219
+ return this.authManager.loginWithPair(username, pair);
220
220
  }
221
221
  /**
222
222
  * Register a new user with provided credentials
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) {
@@ -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
@@ -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.7",
4
4
  "description": "SHOGUN CORE - Core library for Shogun Ecosystem",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -1,70 +0,0 @@
1
- "use strict";
2
- /**
3
- * Event types and interfaces for Shogun SDK
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ShogunEventEmitter = void 0;
7
- /**
8
- * Event emitter class for Shogun SDK
9
- */
10
- class ShogunEventEmitter {
11
- constructor() {
12
- this.listeners = new Map();
13
- }
14
- on(eventName, listener) {
15
- if (!this.listeners.has(eventName)) {
16
- this.listeners.set(eventName, []);
17
- }
18
- this.listeners.get(eventName).push(listener);
19
- return this;
20
- }
21
- off(eventName, listener) {
22
- const eventListeners = this.listeners.get(eventName);
23
- if (eventListeners) {
24
- const index = eventListeners.indexOf(listener);
25
- if (index > -1) {
26
- eventListeners.splice(index, 1);
27
- }
28
- }
29
- return this;
30
- }
31
- once(eventName, listener) {
32
- const onceListener = (...args) => {
33
- this.off(eventName, onceListener);
34
- listener(...args);
35
- };
36
- return this.on(eventName, onceListener);
37
- }
38
- emit(eventName, data) {
39
- const eventListeners = this.listeners.get(eventName);
40
- if (!eventListeners || eventListeners.length === 0) {
41
- return false;
42
- }
43
- eventListeners.forEach((listener) => {
44
- try {
45
- listener(data);
46
- }
47
- catch (error) {
48
- console.error(`Error in event listener for ${eventName}:`, error);
49
- }
50
- });
51
- return true;
52
- }
53
- removeAllListeners(eventName) {
54
- if (eventName) {
55
- this.listeners.delete(eventName);
56
- }
57
- else {
58
- this.listeners.clear();
59
- }
60
- return this;
61
- }
62
- listenerCount(eventName) {
63
- const eventListeners = this.listeners.get(eventName);
64
- return eventListeners ? eventListeners.length : 0;
65
- }
66
- eventNames() {
67
- return Array.from(this.listeners.keys());
68
- }
69
- }
70
- exports.ShogunEventEmitter = ShogunEventEmitter;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- /**
3
- * Core types and interfaces for Shogun SDK
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CorePlugins = exports.PluginCategory = void 0;
7
- var PluginCategory;
8
- (function (PluginCategory) {
9
- PluginCategory["Authentication"] = "authentication";
10
- PluginCategory["Wallet"] = "wallet";
11
- PluginCategory["Privacy"] = "privacy";
12
- PluginCategory["Identity"] = "identity";
13
- PluginCategory["Utility"] = "utility";
14
- })(PluginCategory || (exports.PluginCategory = PluginCategory = {}));
15
- var CorePlugins;
16
- (function (CorePlugins) {
17
- CorePlugins["WebAuthn"] = "webauthn";
18
- CorePlugins["Web3"] = "web3";
19
- CorePlugins["Nostr"] = "nostr";
20
- CorePlugins["OAuth"] = "oauth";
21
- })(CorePlugins || (exports.CorePlugins = CorePlugins = {}));
@@ -1,59 +0,0 @@
1
- /**
2
- * Event types and interfaces for Shogun SDK
3
- */
4
- export interface AuthEventData {
5
- userPub?: string;
6
- username?: string;
7
- method: "password" | "webauthn" | "web3" | "nostr" | "oauth" | "bitcoin" | "pair";
8
- provider?: string;
9
- }
10
- export interface WalletEventData {
11
- address: string;
12
- path?: string;
13
- }
14
- export interface ErrorEventData {
15
- action: string;
16
- message: string;
17
- type: string;
18
- details?: any;
19
- }
20
- export interface GunDataEventData {
21
- path: string;
22
- data?: any;
23
- success: boolean;
24
- error?: string;
25
- timestamp: number;
26
- }
27
- export interface GunPeerEventData {
28
- peer: string;
29
- action: "add" | "remove" | "connect" | "disconnect";
30
- timestamp: number;
31
- }
32
- export interface PluginEventData {
33
- name: string;
34
- version?: string;
35
- category?: string;
36
- }
37
- export interface DebugEventData {
38
- action: string;
39
- data?: any;
40
- timestamp: number;
41
- }
42
- export interface UsernameChangedEventData {
43
- oldUsername: string;
44
- newUsername: string;
45
- userPub: string;
46
- }
47
- /**
48
- * Event emitter class for Shogun SDK
49
- */
50
- export declare class ShogunEventEmitter {
51
- private listeners;
52
- on(eventName: string, listener: Function): this;
53
- off(eventName: string, listener: Function): this;
54
- once(eventName: string, listener: Function): this;
55
- emit(eventName: string, data?: any): boolean;
56
- removeAllListeners(eventName?: string): this;
57
- listenerCount(eventName: string): number;
58
- eventNames(): string[];
59
- }
@@ -1,188 +0,0 @@
1
- /**
2
- * Core types and interfaces for Shogun SDK
3
- */
4
- export declare enum PluginCategory {
5
- Authentication = "authentication",
6
- Wallet = "wallet",
7
- Privacy = "privacy",
8
- Identity = "identity",
9
- Utility = "utility"
10
- }
11
- export declare enum CorePlugins {
12
- WebAuthn = "webauthn",
13
- Web3 = "web3",
14
- Nostr = "nostr",
15
- OAuth = "oauth"
16
- }
17
- export type AuthMethod = "password" | "webauthn" | "web3" | "nostr" | "oauth" | "bitcoin" | "pair";
18
- export interface ISEAPair {
19
- pub: string;
20
- priv: string;
21
- epub: string;
22
- epriv: string;
23
- }
24
- export interface AuthResult {
25
- success: boolean;
26
- userPub?: string;
27
- username?: string;
28
- sessionToken?: string;
29
- authMethod: AuthMethod;
30
- sea?: ISEAPair;
31
- error?: string;
32
- provider?: string;
33
- redirectUrl?: string;
34
- pendingAuth?: boolean;
35
- message?: string;
36
- isNewUser?: boolean;
37
- user?: {
38
- userPub: string;
39
- username: string;
40
- email?: string;
41
- name?: string;
42
- picture?: string;
43
- oauth?: {
44
- provider: string;
45
- id: string;
46
- email: string;
47
- name: string;
48
- picture: string;
49
- lastLogin: number;
50
- };
51
- };
52
- }
53
- export interface SignUpResult {
54
- success: boolean;
55
- userPub?: string;
56
- username?: string;
57
- pub?: string;
58
- authMethod?: AuthMethod;
59
- sessionToken?: string;
60
- isNewUser?: boolean;
61
- sea?: ISEAPair;
62
- error?: string;
63
- message?: string;
64
- provider?: string;
65
- redirectUrl?: string;
66
- pendingAuth?: boolean;
67
- user?: {
68
- userPub: string;
69
- username: string;
70
- email?: string;
71
- name?: string;
72
- picture?: string;
73
- oauth?: {
74
- provider: string;
75
- id: string;
76
- email: string;
77
- name: string;
78
- picture: string;
79
- lastLogin: number;
80
- };
81
- };
82
- }
83
- export interface WebauthnConfig {
84
- enabled: boolean;
85
- rpName?: string;
86
- rpId?: string;
87
- }
88
- export interface Web3Config {
89
- enabled: boolean;
90
- }
91
- export interface NostrConfig {
92
- enabled: boolean;
93
- }
94
- export interface OAuthProviderConfig {
95
- clientId: string;
96
- clientSecret?: string;
97
- }
98
- export interface OAuthConfig {
99
- enabled: boolean;
100
- usePKCE?: boolean;
101
- allowUnsafeClientSecret?: boolean;
102
- providers?: {
103
- google?: OAuthProviderConfig;
104
- github?: OAuthProviderConfig;
105
- [key: string]: OAuthProviderConfig | undefined;
106
- };
107
- }
108
- export interface TimeoutsConfig {
109
- login?: number;
110
- signup?: number;
111
- operation?: number;
112
- }
113
- export interface PluginsConfig {
114
- autoRegister?: string[];
115
- }
116
- export interface ShogunSDKConfig {
117
- gunInstance?: any;
118
- authToken?: string;
119
- scope?: string;
120
- peers?: string[];
121
- webauthn?: WebauthnConfig;
122
- web3?: Web3Config;
123
- nostr?: NostrConfig;
124
- oauth?: OAuthConfig;
125
- timeouts?: TimeoutsConfig;
126
- plugins?: PluginsConfig;
127
- appToken?: string;
128
- gunOptions?: any;
129
- disableAutoRecall?: boolean;
130
- silent?: boolean;
131
- }
132
- export interface ShogunEvents {
133
- error: (data: any) => void;
134
- "auth:signup": (data: any) => void;
135
- "auth:login": (data: any) => void;
136
- "auth:logout": () => void;
137
- "auth:username_changed": (data: any) => void;
138
- "wallet:created": (data: any) => void;
139
- "gun:put": (data: any) => void;
140
- "gun:get": (data: any) => void;
141
- "gun:set": (data: any) => void;
142
- "gun:remove": (data: any) => void;
143
- "gun:peer:add": (data: any) => void;
144
- "gun:peer:remove": (data: any) => void;
145
- "gun:peer:connect": (data: any) => void;
146
- "gun:peer:disconnect": (data: any) => void;
147
- "plugin:registered": (data: any) => void;
148
- "plugin:unregistered": (data: any) => void;
149
- debug: (data: any) => void;
150
- }
151
- export interface Wallets {
152
- secp256k1Bitcoin?: {
153
- privateKey: string;
154
- publicKey: string;
155
- address: string;
156
- };
157
- secp256k1Ethereum?: {
158
- privateKey: string;
159
- publicKey: string;
160
- address: string;
161
- };
162
- }
163
- export interface IShogunCore {
164
- registerPlugin: (plugin: any) => void;
165
- unregisterPlugin: (pluginName: string) => void;
166
- getPlugin: (name: string) => any;
167
- getPlugins: () => any[];
168
- hasPlugin: (name: string) => boolean;
169
- gun: any;
170
- db: any;
171
- rx: any;
172
- storage: any;
173
- config: any;
174
- on: (eventName: string, listener: Function) => any;
175
- off: (eventName: string, listener: Function) => any;
176
- once: (eventName: string, listener: Function) => any;
177
- removeAllListeners: (eventName?: string) => any;
178
- emit: (eventName: string, data?: any) => boolean;
179
- getRecentErrors: (count?: number) => any[];
180
- login: (username: string, password: string, pair?: any) => Promise<AuthResult>;
181
- signUp: (username: string, password?: string, pair?: any) => Promise<SignUpResult>;
182
- getAuthenticationMethod: (type: AuthMethod) => any;
183
- getCurrentUser: () => any;
184
- changeUsername: (newUsername: string) => Promise<any>;
185
- logout: () => void;
186
- isLoggedIn: () => boolean;
187
- }
188
- export type ShogunCoreConfig = ShogunSDKConfig;