shogun-core 3.0.3 → 3.0.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.
Files changed (95) hide show
  1. package/package.json +1 -1
  2. package/dist/browser/shogun-core.js +0 -92128
  3. package/dist/browser/shogun-core.js.map +0 -1
  4. package/dist/config/simplified-config.js +0 -230
  5. package/dist/core.js +0 -338
  6. package/dist/gundb/crypto.js +0 -268
  7. package/dist/gundb/db.js +0 -1833
  8. package/dist/gundb/derive.js +0 -229
  9. package/dist/gundb/errors.js +0 -66
  10. package/dist/gundb/index.js +0 -6
  11. package/dist/gundb/restricted-put.js +0 -81
  12. package/dist/gundb/rxjs.js +0 -445
  13. package/dist/gundb/simple-api.js +0 -438
  14. package/dist/gundb/types.js +0 -4
  15. package/dist/index.js +0 -16
  16. package/dist/interfaces/common.js +0 -1
  17. package/dist/interfaces/events.js +0 -36
  18. package/dist/interfaces/plugin.js +0 -1
  19. package/dist/interfaces/shogun.js +0 -34
  20. package/dist/managers/AuthManager.js +0 -225
  21. package/dist/managers/CoreInitializer.js +0 -234
  22. package/dist/managers/EventManager.js +0 -67
  23. package/dist/managers/PluginManager.js +0 -296
  24. package/dist/migration-test.js +0 -91
  25. package/dist/plugins/base.js +0 -47
  26. package/dist/plugins/index.js +0 -15
  27. package/dist/plugins/nostr/index.js +0 -4
  28. package/dist/plugins/nostr/nostrConnector.js +0 -413
  29. package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -446
  30. package/dist/plugins/nostr/nostrSigner.js +0 -313
  31. package/dist/plugins/nostr/types.js +0 -1
  32. package/dist/plugins/oauth/index.js +0 -3
  33. package/dist/plugins/oauth/oauthConnector.js +0 -753
  34. package/dist/plugins/oauth/oauthPlugin.js +0 -396
  35. package/dist/plugins/oauth/types.js +0 -1
  36. package/dist/plugins/web3/index.js +0 -4
  37. package/dist/plugins/web3/types.js +0 -1
  38. package/dist/plugins/web3/web3Connector.js +0 -528
  39. package/dist/plugins/web3/web3ConnectorPlugin.js +0 -448
  40. package/dist/plugins/web3/web3Signer.js +0 -308
  41. package/dist/plugins/webauthn/index.js +0 -3
  42. package/dist/plugins/webauthn/types.js +0 -11
  43. package/dist/plugins/webauthn/webauthn.js +0 -478
  44. package/dist/plugins/webauthn/webauthnPlugin.js +0 -398
  45. package/dist/plugins/webauthn/webauthnSigner.js +0 -304
  46. package/dist/storage/storage.js +0 -147
  47. package/dist/types/config/simplified-config.d.ts +0 -114
  48. package/dist/types/core.d.ts +0 -305
  49. package/dist/types/gundb/crypto.d.ts +0 -95
  50. package/dist/types/gundb/db.d.ts +0 -404
  51. package/dist/types/gundb/derive.d.ts +0 -21
  52. package/dist/types/gundb/errors.d.ts +0 -42
  53. package/dist/types/gundb/index.d.ts +0 -3
  54. package/dist/types/gundb/restricted-put.d.ts +0 -15
  55. package/dist/types/gundb/rxjs.d.ts +0 -110
  56. package/dist/types/gundb/simple-api.d.ts +0 -90
  57. package/dist/types/gundb/types.d.ts +0 -264
  58. package/dist/types/index.d.ts +0 -14
  59. package/dist/types/interfaces/common.d.ts +0 -85
  60. package/dist/types/interfaces/events.d.ts +0 -131
  61. package/dist/types/interfaces/plugin.d.ts +0 -162
  62. package/dist/types/interfaces/shogun.d.ts +0 -215
  63. package/dist/types/managers/AuthManager.d.ts +0 -72
  64. package/dist/types/managers/CoreInitializer.d.ts +0 -40
  65. package/dist/types/managers/EventManager.d.ts +0 -49
  66. package/dist/types/managers/PluginManager.d.ts +0 -145
  67. package/dist/types/migration-test.d.ts +0 -16
  68. package/dist/types/plugins/base.d.ts +0 -35
  69. package/dist/types/plugins/index.d.ts +0 -14
  70. package/dist/types/plugins/nostr/index.d.ts +0 -4
  71. package/dist/types/plugins/nostr/nostrConnector.d.ts +0 -119
  72. package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +0 -163
  73. package/dist/types/plugins/nostr/nostrSigner.d.ts +0 -105
  74. package/dist/types/plugins/nostr/types.d.ts +0 -122
  75. package/dist/types/plugins/oauth/index.d.ts +0 -3
  76. package/dist/types/plugins/oauth/oauthConnector.d.ts +0 -110
  77. package/dist/types/plugins/oauth/oauthPlugin.d.ts +0 -91
  78. package/dist/types/plugins/oauth/types.d.ts +0 -114
  79. package/dist/types/plugins/web3/index.d.ts +0 -4
  80. package/dist/types/plugins/web3/types.d.ts +0 -107
  81. package/dist/types/plugins/web3/web3Connector.d.ts +0 -129
  82. package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +0 -160
  83. package/dist/types/plugins/web3/web3Signer.d.ts +0 -114
  84. package/dist/types/plugins/webauthn/index.d.ts +0 -3
  85. package/dist/types/plugins/webauthn/types.d.ts +0 -162
  86. package/dist/types/plugins/webauthn/webauthn.d.ts +0 -129
  87. package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +0 -158
  88. package/dist/types/plugins/webauthn/webauthnSigner.d.ts +0 -91
  89. package/dist/types/storage/storage.d.ts +0 -50
  90. package/dist/types/utils/errorHandler.d.ts +0 -119
  91. package/dist/types/utils/eventEmitter.d.ts +0 -39
  92. package/dist/types/utils/validation.d.ts +0 -27
  93. package/dist/utils/errorHandler.js +0 -241
  94. package/dist/utils/eventEmitter.js +0 -76
  95. package/dist/utils/validation.js +0 -72
@@ -1,225 +0,0 @@
1
- import { ErrorHandler, ErrorType } from "../utils/errorHandler";
2
- /**
3
- * Manages authentication operations for ShogunCore
4
- */
5
- export class AuthManager {
6
- core;
7
- currentAuthMethod;
8
- constructor(core) {
9
- this.core = core;
10
- }
11
- /**
12
- * Check if user is logged in
13
- * @returns {boolean} True if user is logged in, false otherwise
14
- * @description Verifies authentication status by checking GunInstance login state
15
- * and presence of authentication credentials in storage
16
- */
17
- isLoggedIn() {
18
- return this.core.db.isLoggedIn();
19
- }
20
- /**
21
- * Perform user logout
22
- * @description Logs out the current user from GunInstance and emits logout event.
23
- * If user is not authenticated, the logout operation is ignored.
24
- */
25
- logout() {
26
- try {
27
- if (!this.isLoggedIn()) {
28
- return;
29
- }
30
- this.core.db.logout();
31
- this.core.emit("auth:logout");
32
- }
33
- catch (error) {
34
- ErrorHandler.handle(ErrorType.AUTHENTICATION, "LOGOUT_FAILED", error instanceof Error ? error.message : "Error during logout", error);
35
- }
36
- }
37
- /**
38
- * Authenticate user with username and password
39
- * @param username - Username
40
- * @param password - User password
41
- * @returns {Promise<AuthResult>} Promise with authentication result
42
- * @description Attempts to log in user with provided credentials.
43
- * Emits login event on success.
44
- */
45
- async login(username, password, pair) {
46
- try {
47
- if (!this.currentAuthMethod) {
48
- this.currentAuthMethod = "password";
49
- }
50
- const result = await this.core.db.login(username, password, pair);
51
- if (result.success) {
52
- // Include SEA pair in the response
53
- const seaPair = this.core.user?._?.sea;
54
- if (seaPair) {
55
- result.sea = seaPair;
56
- }
57
- this.core.emit("auth:login", {
58
- userPub: result.userPub ?? "",
59
- method: this.currentAuthMethod === "pair"
60
- ? "password"
61
- : this.currentAuthMethod || "password",
62
- });
63
- }
64
- else {
65
- result.error = result.error || "Wrong user or password";
66
- }
67
- return result;
68
- }
69
- catch (error) {
70
- ErrorHandler.handle(ErrorType.AUTHENTICATION, "LOGIN_FAILED", error.message ?? "Unknown error during login", error);
71
- return {
72
- success: false,
73
- error: error.message ?? "Unknown error during login",
74
- };
75
- }
76
- }
77
- /**
78
- * Login with GunDB pair directly
79
- * @param pair - GunDB SEA pair for authentication
80
- * @returns {Promise<AuthResult>} Promise with authentication result
81
- * @description Authenticates user using a GunDB pair directly.
82
- * Emits login event on success.
83
- */
84
- async loginWithPair(pair) {
85
- try {
86
- if (!pair || !pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
87
- return {
88
- success: false,
89
- error: "Invalid pair structure - missing required keys",
90
- };
91
- }
92
- // Use the new loginWithPair method from GunInstance
93
- const result = await this.core.db.login("", "", pair);
94
- if (result.success) {
95
- // Include SEA pair in the response
96
- const seaPair = this.core.user?._?.sea;
97
- if (seaPair) {
98
- result.sea = seaPair;
99
- }
100
- this.currentAuthMethod = "pair";
101
- this.core.emit("auth:login", {
102
- userPub: result.userPub ?? "",
103
- method: "password",
104
- });
105
- }
106
- else {
107
- result.error =
108
- result.error || "Authentication failed with provided pair";
109
- }
110
- return result;
111
- }
112
- catch (error) {
113
- ErrorHandler.handle(ErrorType.AUTHENTICATION, "PAIR_LOGIN_FAILED", error.message ?? "Unknown error during pair login", error);
114
- return {
115
- success: false,
116
- error: error.message ?? "Unknown error during pair login",
117
- };
118
- }
119
- }
120
- /**
121
- * Register a new user with provided credentials
122
- * @param username - Username
123
- * @param password - Password
124
- * @param email - Email (optional)
125
- * @param pair - Pair of keys
126
- * @returns {Promise<SignUpResult>} Registration result
127
- * @description Creates a new user account with the provided credentials.
128
- * Validates password requirements and emits signup event on success.
129
- */
130
- async signUp(username, password, pair) {
131
- try {
132
- if (!this.core.db) {
133
- throw new Error("Database not initialized");
134
- }
135
- // For password-based signup, ensure password is provided
136
- if (!pair && (!password || password.trim() === "")) {
137
- throw new Error("Password is required for password-based signup");
138
- }
139
- const result = await this.core.db.signUp(username, password || "", pair);
140
- if (result.success) {
141
- // Update current authentication method
142
- this.currentAuthMethod = pair ? "web3" : "password";
143
- this.core.emit("auth:signup", {
144
- userPub: result.userPub,
145
- username,
146
- method: this.currentAuthMethod,
147
- });
148
- this.core.emit("debug", {
149
- action: "signup_success",
150
- userPub: result.userPub,
151
- method: this.currentAuthMethod,
152
- });
153
- }
154
- else {
155
- this.core.emit("debug", {
156
- action: "signup_failed",
157
- error: result.error,
158
- username,
159
- });
160
- }
161
- return result;
162
- }
163
- catch (error) {
164
- if (typeof console !== "undefined" && console.error) {
165
- console.error(`Error during registration for user ${username}:`, error);
166
- }
167
- this.core.emit("debug", {
168
- action: "signup_error",
169
- error: error instanceof Error ? error.message : String(error),
170
- username,
171
- });
172
- return {
173
- success: false,
174
- error: `Registration failed: ${error instanceof Error ? error.message : String(error)}`,
175
- };
176
- }
177
- }
178
- /**
179
- * Set the current authentication method
180
- * This is used by plugins to indicate which authentication method was used
181
- * @param method The authentication method used
182
- */
183
- setAuthMethod(method) {
184
- this.currentAuthMethod = method;
185
- }
186
- /**
187
- * Get the current authentication method
188
- * @returns The current authentication method or undefined if not set
189
- */
190
- getAuthMethod() {
191
- return this.currentAuthMethod;
192
- }
193
- /**
194
- * Get an authentication method plugin by type
195
- * @param type The type of authentication method
196
- * @returns The authentication plugin or undefined if not available
197
- * This is a more modern approach to accessing authentication methods
198
- */
199
- getAuthenticationMethod(type) {
200
- switch (type) {
201
- case "webauthn":
202
- return this.core.getPlugin("WebAuthn");
203
- case "web3":
204
- return this.core.getPlugin("Web3");
205
- case "nostr":
206
- return this.core.getPlugin("Nostr");
207
- case "oauth":
208
- return this.core.getPlugin("OAuth");
209
- case "password":
210
- default:
211
- return {
212
- login: async (username, password) => {
213
- return await this.login(username, password);
214
- },
215
- signUp: async (username, password, confirm) => {
216
- // For password-based signup, validate password confirmation
217
- if (confirm && password !== confirm) {
218
- throw new Error("Password and confirm password do not match");
219
- }
220
- return await this.signUp(username, password);
221
- },
222
- };
223
- }
224
- }
225
- }
@@ -1,234 +0,0 @@
1
- import { ShogunStorage } from "../storage/storage";
2
- import { ErrorHandler } from "../utils/errorHandler";
3
- import { WebauthnPlugin } from "../plugins/webauthn/webauthnPlugin";
4
- import { Web3ConnectorPlugin } from "../plugins/web3/web3ConnectorPlugin";
5
- import { NostrConnectorPlugin } from "../plugins/nostr/nostrConnectorPlugin";
6
- import { OAuthPlugin } from "../plugins/oauth/oauthPlugin";
7
- import { restrictedPut, DataBase, RxJS, createGun, Gun, derive, } from "../gundb";
8
- /**
9
- * Handles initialization of ShogunCore components
10
- */
11
- export class CoreInitializer {
12
- core;
13
- constructor(core) {
14
- this.core = core;
15
- }
16
- /**
17
- * Initialize the Shogun SDK
18
- * @param config - SDK Configuration object
19
- * @description Creates a new instance of ShogunCore with the provided configuration.
20
- * Initializes all required components including storage, event emitter, GunInstance connection,
21
- * and plugin system.
22
- */
23
- async initialize(config) {
24
- // Polyfill console for environments where it might be missing
25
- if (typeof console === "undefined") {
26
- global.console = {
27
- log: () => { },
28
- warn: () => { },
29
- error: () => { },
30
- info: () => { },
31
- debug: () => { },
32
- };
33
- }
34
- // Initialize storage
35
- this.core.storage = new ShogunStorage();
36
- // Setup error handler
37
- ErrorHandler.addListener((error) => {
38
- this.core.emit("error", {
39
- action: error.code,
40
- message: error.message,
41
- type: error.type,
42
- });
43
- });
44
- // Setup Gun instance
45
- await this.initializeGun(config);
46
- // Setup Gun user
47
- await this.initializeGunUser();
48
- // Setup Gun event forwarding
49
- this.setupGunEventForwarding();
50
- // Setup wallet derivation
51
- this.setupWalletDerivation();
52
- // Initialize RxJS
53
- this.core.rx = new RxJS(this.core.gun);
54
- // Register built-in plugins
55
- this.registerBuiltinPlugins(config);
56
- // Initialize async components
57
- await this.initializeAsync();
58
- }
59
- /**
60
- * Initialize Gun instance
61
- */
62
- async initializeGun(config) {
63
- console.log("Initialize Gun instance", config);
64
- if (config.gunOptions.authToken) {
65
- restrictedPut(Gun, config.gunOptions.authToken);
66
- }
67
- try {
68
- if (config.gunInstance && config.gunInstance instanceof Gun) {
69
- console.log("config.gunInstance", config.gunInstance);
70
- this.core._gun = config.gunInstance;
71
- }
72
- else {
73
- console.log("config.gunOptions", config.gunOptions);
74
- this.core._gun = createGun(config.gunOptions);
75
- // Explicitly apply peers configuration if not already set
76
- if (config.gunOptions?.peers &&
77
- Array.isArray(config.gunOptions.peers)) {
78
- console.log("Applying peers configuration:", config.gunOptions.peers);
79
- this.core._gun.opt({ peers: config.gunOptions.peers });
80
- console.log("Peers after explicit application:", this.core._gun?.opt?.peers);
81
- }
82
- }
83
- }
84
- catch (error) {
85
- if (typeof console !== "undefined" && console.error) {
86
- console.error("Error creating Gun instance:", error);
87
- }
88
- throw new Error(`Failed to create Gun instance: ${error}`);
89
- }
90
- try {
91
- console.log("Initialize Gun instance", this.core.gun);
92
- this.core.db = new DataBase(this.core.gun, config.gunOptions.scope || "");
93
- this.core._gun = this.core.db.gun;
94
- }
95
- catch (error) {
96
- if (typeof console !== "undefined" && console.error) {
97
- console.error("Error initializing GunInstance:", error);
98
- }
99
- throw new Error(`Failed to initialize GunInstance: ${error}`);
100
- }
101
- }
102
- /**
103
- * Initialize Gun user
104
- */
105
- async initializeGunUser() {
106
- try {
107
- this.core._user = this.core.gun.user().recall({ sessionStorage: true });
108
- }
109
- catch (error) {
110
- if (typeof console !== "undefined" && console.error) {
111
- console.error("Error initializing Gun user:", error);
112
- }
113
- throw new Error(`Failed to initialize Gun user: ${error}`);
114
- }
115
- this.core.gun.on("auth", (user) => {
116
- this.core._user = this.core.gun.user().recall({ sessionStorage: true });
117
- this.core.emit("auth:login", {
118
- userPub: user.pub,
119
- method: "password",
120
- });
121
- });
122
- }
123
- /**
124
- * Setup Gun event forwarding
125
- */
126
- setupGunEventForwarding() {
127
- const gunEvents = ["gun:put", "gun:get", "gun:set", "gun:remove"];
128
- gunEvents.forEach((eventName) => {
129
- this.core.db.on(eventName, (data) => {
130
- this.core.emit(eventName, data);
131
- });
132
- });
133
- const peerEvents = [
134
- "gun:peer:add",
135
- "gun:peer:remove",
136
- "gun:peer:connect",
137
- "gun:peer:disconnect",
138
- ];
139
- peerEvents.forEach((eventName) => {
140
- this.core.db.on(eventName, (data) => {
141
- this.core.emit(eventName, data);
142
- });
143
- });
144
- }
145
- /**
146
- * Setup wallet derivation
147
- */
148
- setupWalletDerivation() {
149
- this.core.gun.on("auth", async (user) => {
150
- if (!user)
151
- return;
152
- const priv = user._?.sea?.epriv;
153
- const pub = user._?.sea?.epub;
154
- this.core.wallets = await derive(priv, pub, {
155
- includeSecp256k1Bitcoin: true,
156
- includeSecp256k1Ethereum: true,
157
- });
158
- });
159
- }
160
- /**
161
- * Register built-in plugins based on configuration
162
- */
163
- registerBuiltinPlugins(config) {
164
- try {
165
- // Register OAuth plugin if configuration is provided
166
- if (config.oauth) {
167
- if (typeof console !== "undefined" && console.warn) {
168
- console.warn("OAuth plugin will be registered with provided configuration");
169
- }
170
- const oauthPlugin = new OAuthPlugin();
171
- if (typeof oauthPlugin.configure === "function") {
172
- oauthPlugin.configure(config.oauth);
173
- }
174
- this.core.pluginManager.register(oauthPlugin);
175
- }
176
- // Register WebAuthn plugin if configuration is provided
177
- if (config.webauthn) {
178
- if (typeof console !== "undefined" && console.warn) {
179
- console.warn("WebAuthn plugin will be registered with provided configuration");
180
- }
181
- const webauthnPlugin = new WebauthnPlugin();
182
- if (typeof webauthnPlugin.configure === "function") {
183
- webauthnPlugin.configure(config.webauthn);
184
- }
185
- this.core.pluginManager.register(webauthnPlugin);
186
- }
187
- // Register Web3 plugin if configuration is provided
188
- if (config.web3) {
189
- if (typeof console !== "undefined" && console.warn) {
190
- console.warn("Web3 plugin will be registered with provided configuration");
191
- }
192
- const web3Plugin = new Web3ConnectorPlugin();
193
- if (typeof web3Plugin.configure === "function") {
194
- web3Plugin.configure(config.web3);
195
- }
196
- this.core.pluginManager.register(web3Plugin);
197
- }
198
- // Register Nostr plugin if configuration is provided
199
- if (config.nostr) {
200
- if (typeof console !== "undefined" && console.warn) {
201
- console.warn("Nostr plugin will be registered with provided configuration");
202
- }
203
- const nostrPlugin = new NostrConnectorPlugin();
204
- if (typeof nostrPlugin.configure === "function") {
205
- nostrPlugin.configure(config.nostr);
206
- }
207
- this.core.pluginManager.register(nostrPlugin);
208
- }
209
- }
210
- catch (error) {
211
- if (typeof console !== "undefined" && console.error) {
212
- console.error("Error registering builtin plugins:", error);
213
- }
214
- }
215
- }
216
- /**
217
- * Initialize async components
218
- */
219
- async initializeAsync() {
220
- try {
221
- await this.core.db.initialize();
222
- this.core.emit("debug", {
223
- action: "core_initialized",
224
- timestamp: Date.now(),
225
- });
226
- }
227
- catch (error) {
228
- if (typeof console !== "undefined" && console.error) {
229
- console.error("Error during Shogun Core initialization:", error);
230
- }
231
- throw error;
232
- }
233
- }
234
- }
@@ -1,67 +0,0 @@
1
- import { ShogunEventEmitter } from "../interfaces/events";
2
- /**
3
- * Manages event operations for ShogunCore
4
- */
5
- export class EventManager {
6
- eventEmitter;
7
- constructor() {
8
- this.eventEmitter = new ShogunEventEmitter();
9
- }
10
- /**
11
- * Emits an event through the core's event emitter.
12
- * Plugins should use this method to emit events instead of accessing the private eventEmitter directly.
13
- * @param eventName The name of the event to emit.
14
- * @param data The data to pass with the event.
15
- * @returns {boolean} Indicates if the event had listeners.
16
- */
17
- emit(eventName, data) {
18
- return this.eventEmitter.emit(eventName, data);
19
- }
20
- /**
21
- * Add an event listener
22
- * @param eventName The name of the event to listen for
23
- * @param listener The callback function to execute when the event is emitted
24
- * @returns {this} Returns this instance for method chaining
25
- */
26
- on(eventName, listener) {
27
- this.eventEmitter.on(eventName, listener);
28
- return this;
29
- }
30
- /**
31
- * Add a one-time event listener
32
- * @param eventName The name of the event to listen for
33
- * @param listener The callback function to execute when the event is emitted
34
- * @returns {this} Returns this instance for method chaining
35
- */
36
- once(eventName, listener) {
37
- this.eventEmitter.once(eventName, listener);
38
- return this;
39
- }
40
- /**
41
- * Remove an event listener
42
- * @param eventName The name of the event to stop listening for
43
- * @param listener The callback function to remove
44
- * @returns {this} Returns this instance for method chaining
45
- */
46
- off(eventName, listener) {
47
- this.eventEmitter.off(eventName, listener);
48
- return this;
49
- }
50
- /**
51
- * Remove all listeners for a specific event or all events
52
- * @param eventName Optional. The name of the event to remove listeners for.
53
- * If not provided, all listeners for all events are removed.
54
- * @returns {this} Returns this instance for method chaining
55
- */
56
- removeAllListeners(eventName) {
57
- this.eventEmitter.removeAllListeners(eventName);
58
- return this;
59
- }
60
- /**
61
- * Get the underlying event emitter instance
62
- * @returns The ShogunEventEmitter instance
63
- */
64
- getEventEmitter() {
65
- return this.eventEmitter;
66
- }
67
- }