shogun-core 1.1.4 → 1.2.2
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/README.md +61 -1327
- package/dist/browser/shogun-core.js +1 -1
- package/dist/browser/shogun-core.js.LICENSE.txt +0 -9
- package/dist/browser/shogun-core.light.js +1 -1
- package/dist/browser/shogun-core.vendors.light.js +1 -1
- package/dist/browser.js +27 -11
- package/dist/core.js +603 -0
- package/dist/{gun → gundb}/crypto.js +38 -8
- package/dist/gundb/gun.js +676 -0
- package/dist/{gun → gundb}/index.js +0 -5
- package/dist/{gun → gundb}/utils.js +6 -0
- package/dist/index.js +1 -807
- package/dist/plugins/index.js +15 -28
- package/dist/plugins/{stealth → nostr}/index.js +3 -4
- package/dist/plugins/nostr/nostrConnector.js +656 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +259 -0
- package/dist/plugins/{metamask → web3}/index.js +2 -2
- package/dist/plugins/{metamask/metamask.js → web3/web3Connector.js} +8 -8
- package/dist/plugins/{metamask/metamaskPlugin.js → web3/web3ConnectorPlugin.js} +32 -42
- package/dist/plugins/webauthn/webauthnPlugin.js +4 -0
- package/dist/types/browser.d.ts +9 -4
- package/dist/types/core.d.ts +221 -0
- package/dist/types/{gun → gundb}/crypto.d.ts +20 -5
- package/dist/types/{gun → gundb}/gun.d.ts +56 -28
- package/dist/types/gundb/index.d.ts +1 -0
- package/dist/types/{gun → gundb}/utils.d.ts +1 -0
- package/dist/types/index.d.ts +1 -282
- package/dist/types/plugins/index.d.ts +7 -10
- package/dist/types/plugins/nostr/index.d.ts +3 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +111 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +87 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/web3/index.d.ts +3 -0
- package/dist/types/plugins/{metamask → web3}/types.d.ts +4 -4
- package/dist/types/plugins/{metamask/metamask.d.ts → web3/web3Connector.d.ts} +7 -7
- package/dist/types/plugins/{metamask/metamaskPlugin.d.ts → web3/web3ConnectorPlugin.d.ts} +4 -4
- package/dist/types/shogun.js +40 -15
- package/dist/types/types/shogun.d.ts +67 -67
- package/dist/types/utils/errorHandler.d.ts +39 -36
- package/dist/types/utils/utility.d.ts +0 -4
- package/dist/utils/errorHandler.js +43 -40
- package/dist/utils/utility.js +0 -8
- package/package.json +2 -2
- package/dist/config.js +0 -18
- package/dist/gun/gun.js +0 -542
- package/dist/plugins/stealth/stealth.js +0 -176
- package/dist/plugins/stealth/stealthPlugin.js +0 -113
- package/dist/plugins/stealth/types.js +0 -2
- package/dist/plugins/utils/stubs/didStub.js +0 -35
- package/dist/plugins/utils/stubs/stealthStub.js +0 -35
- package/dist/plugins/utils/stubs/webauthnStub.js +0 -29
- package/dist/plugins/wallet/index.js +0 -20
- package/dist/plugins/wallet/types.js +0 -15
- package/dist/plugins/wallet/walletManager.js +0 -1832
- package/dist/plugins/wallet/walletPlugin.js +0 -236
- package/dist/types/config.d.ts +0 -15
- package/dist/types/gun/index.d.ts +0 -6
- package/dist/types/gun/types.d.ts +0 -2
- package/dist/types/plugins/metamask/index.d.ts +0 -3
- package/dist/types/plugins/stealth/index.d.ts +0 -3
- package/dist/types/plugins/stealth/stealth.d.ts +0 -93
- package/dist/types/plugins/stealth/stealthPlugin.d.ts +0 -60
- package/dist/types/plugins/stealth/types.d.ts +0 -93
- package/dist/types/plugins/utils/stubs/didStub.d.ts +0 -15
- package/dist/types/plugins/utils/stubs/stealthStub.d.ts +0 -15
- package/dist/types/plugins/utils/stubs/webauthnStub.d.ts +0 -13
- package/dist/types/plugins/wallet/index.d.ts +0 -3
- package/dist/types/plugins/wallet/types.d.ts +0 -167
- package/dist/types/plugins/wallet/walletManager.d.ts +0 -306
- package/dist/types/plugins/wallet/walletPlugin.d.ts +0 -126
- package/dist/types/utils/stubs/didStub.d.ts +0 -15
- package/dist/types/utils/stubs/stealthStub.d.ts +0 -15
- package/dist/types/utils/stubs/webauthnStub.d.ts +0 -13
- package/dist/utils/stubs/didStub.js +0 -35
- package/dist/utils/stubs/stealthStub.js +0 -35
- package/dist/utils/stubs/webauthnStub.js +0 -29
- /package/dist/{gun → gundb}/errors.js +0 -0
- /package/dist/{gun → gundb}/rxjs-integration.js +0 -0
- /package/dist/{gun → plugins/nostr}/types.js +0 -0
- /package/dist/plugins/{metamask → web3}/types.js +0 -0
- /package/dist/types/{gun → gundb}/errors.d.ts +0 -0
- /package/dist/types/{gun → gundb}/rxjs-integration.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -14,811 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
const gun_1 = require("./gun/gun");
|
|
19
|
-
const eventEmitter_1 = require("./utils/eventEmitter");
|
|
20
|
-
const storage_1 = require("./storage/storage");
|
|
21
|
-
const shogun_1 = require("./types/shogun");
|
|
22
|
-
const logger_1 = require("./utils/logger");
|
|
23
|
-
const errorHandler_1 = require("./utils/errorHandler");
|
|
24
|
-
const rxjs_integration_1 = require("./gun/rxjs-integration");
|
|
25
|
-
const webauthnPlugin_1 = require("./plugins/webauthn/webauthnPlugin");
|
|
26
|
-
const metamaskPlugin_1 = require("./plugins/metamask/metamaskPlugin");
|
|
27
|
-
const stealthPlugin_1 = require("./plugins/stealth/stealthPlugin");
|
|
28
|
-
const walletPlugin_1 = require("./plugins/wallet/walletPlugin");
|
|
29
|
-
var utils_1 = require("./contracts/utils");
|
|
30
|
-
Object.defineProperty(exports, "RelayVerifier", { enumerable: true, get: function () { return utils_1.RelayVerifier; } });
|
|
31
|
-
__exportStar(require("./utils/errorHandler"), exports);
|
|
32
|
-
__exportStar(require("./gun/rxjs-integration"), exports);
|
|
17
|
+
__exportStar(require("./core"), exports);
|
|
33
18
|
__exportStar(require("./plugins"), exports);
|
|
34
|
-
// Export relay verification
|
|
35
|
-
__exportStar(require("./contracts/entryPoint"), exports);
|
|
36
|
-
__exportStar(require("./contracts/utils"), exports);
|
|
37
|
-
__exportStar(require("./contracts/registry"), exports);
|
|
38
|
-
__exportStar(require("./contracts/relay"), exports);
|
|
39
|
-
/**
|
|
40
|
-
* Main ShogunCore class - implements the IShogunCore interface
|
|
41
|
-
*
|
|
42
|
-
* This is the primary entry point for the Shogun SDK, providing access to:
|
|
43
|
-
* - Decentralized database (GunDB)
|
|
44
|
-
* - Authentication methods (traditional, WebAuthn, MetaMask)
|
|
45
|
-
* - Plugin system for extensibility
|
|
46
|
-
* - DID (Decentralized Identity) management
|
|
47
|
-
* - RxJS integration for reactive programming
|
|
48
|
-
*
|
|
49
|
-
* @since 2.0.0
|
|
50
|
-
*/
|
|
51
|
-
class ShogunCore {
|
|
52
|
-
/** Current API version - used for deprecation warnings and migration guidance */
|
|
53
|
-
static API_VERSION = "2.0.0";
|
|
54
|
-
/** Gun database instance */
|
|
55
|
-
gun;
|
|
56
|
-
/** Gun user instance */
|
|
57
|
-
user = null;
|
|
58
|
-
/** GunDB wrapper */
|
|
59
|
-
gundb;
|
|
60
|
-
/** Storage implementation */
|
|
61
|
-
storage;
|
|
62
|
-
/** Event emitter for SDK events */
|
|
63
|
-
eventEmitter;
|
|
64
|
-
/** Ethereum provider */
|
|
65
|
-
provider;
|
|
66
|
-
/** SDK configuration */
|
|
67
|
-
config;
|
|
68
|
-
/** RxJS integration */
|
|
69
|
-
rx;
|
|
70
|
-
/** Plugin registry */
|
|
71
|
-
plugins = new Map();
|
|
72
|
-
/**
|
|
73
|
-
* Initialize the Shogun SDK
|
|
74
|
-
* @param config - SDK Configuration object
|
|
75
|
-
* @description Creates a new instance of ShogunCore with the provided configuration.
|
|
76
|
-
* Initializes all required components including storage, event emitter, GunDB connection,
|
|
77
|
-
* and plugin system.
|
|
78
|
-
*/
|
|
79
|
-
constructor(config) {
|
|
80
|
-
(0, logger_1.log)("Initializing ShogunSDK");
|
|
81
|
-
this.config = config;
|
|
82
|
-
if (config.logging) {
|
|
83
|
-
(0, logger_1.configureLogging)(config.logging);
|
|
84
|
-
(0, logger_1.log)("Logging configured with custom settings");
|
|
85
|
-
}
|
|
86
|
-
this.storage = new storage_1.ShogunStorage();
|
|
87
|
-
this.eventEmitter = new eventEmitter_1.EventEmitter();
|
|
88
|
-
errorHandler_1.ErrorHandler.addListener((error) => {
|
|
89
|
-
this.eventEmitter.emit("error", {
|
|
90
|
-
action: error.code,
|
|
91
|
-
message: error.message,
|
|
92
|
-
type: error.type,
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
// If an external Gun instance is provided, use it
|
|
96
|
-
if (config.gun) {
|
|
97
|
-
(0, logger_1.log)("Using externally provided Gun instance");
|
|
98
|
-
const gun = config.gun;
|
|
99
|
-
gun.opt({
|
|
100
|
-
localStorage: false,
|
|
101
|
-
radisk: false,
|
|
102
|
-
authToken: config.authToken,
|
|
103
|
-
});
|
|
104
|
-
this.gundb = new gun_1.GunDB(gun, config.authToken);
|
|
105
|
-
this.gun = gun;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
(0, logger_1.logError)("Missing Gun instance");
|
|
109
|
-
throw new Error("Missing Gun instance");
|
|
110
|
-
}
|
|
111
|
-
this.user = this.gun.user().recall({ sessionStorage: true });
|
|
112
|
-
this.rx = new rxjs_integration_1.GunRxJS(this.gun);
|
|
113
|
-
this.registerBuiltinPlugins(config);
|
|
114
|
-
if (config.plugins?.autoRegister &&
|
|
115
|
-
config.plugins.autoRegister.length > 0) {
|
|
116
|
-
for (const plugin of config.plugins.autoRegister) {
|
|
117
|
-
try {
|
|
118
|
-
this.register(plugin);
|
|
119
|
-
(0, logger_1.log)(`Auto-registered plugin: ${plugin.name}`);
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
(0, logger_1.logError)(`Failed to auto-register plugin ${plugin.name}:`, error);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
(0, logger_1.log)("ShogunSDK initialized!");
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Register built-in plugins based on configuration
|
|
130
|
-
* @private
|
|
131
|
-
*/
|
|
132
|
-
registerBuiltinPlugins(config) {
|
|
133
|
-
try {
|
|
134
|
-
// Authentication plugins group
|
|
135
|
-
if (config.webauthn?.enabled) {
|
|
136
|
-
const webauthnPlugin = new webauthnPlugin_1.WebauthnPlugin();
|
|
137
|
-
webauthnPlugin._category = shogun_1.PluginCategory.Authentication;
|
|
138
|
-
this.register(webauthnPlugin);
|
|
139
|
-
(0, logger_1.log)("Webauthn plugin registered");
|
|
140
|
-
}
|
|
141
|
-
if (config.metamask?.enabled) {
|
|
142
|
-
const metamaskPlugin = new metamaskPlugin_1.MetaMaskPlugin();
|
|
143
|
-
metamaskPlugin._category = shogun_1.PluginCategory.Authentication;
|
|
144
|
-
this.register(metamaskPlugin);
|
|
145
|
-
(0, logger_1.log)("MetaMask plugin registered");
|
|
146
|
-
}
|
|
147
|
-
// Privacy plugins group
|
|
148
|
-
if (config.stealth?.enabled) {
|
|
149
|
-
const stealthPlugin = new stealthPlugin_1.StealthPlugin();
|
|
150
|
-
stealthPlugin._category = shogun_1.PluginCategory.Privacy;
|
|
151
|
-
this.register(stealthPlugin);
|
|
152
|
-
(0, logger_1.log)("Stealth plugin registered");
|
|
153
|
-
}
|
|
154
|
-
// Wallet plugins group
|
|
155
|
-
if (config.walletManager?.enabled) {
|
|
156
|
-
const walletPlugin = new walletPlugin_1.WalletPlugin();
|
|
157
|
-
walletPlugin._category = shogun_1.PluginCategory.Wallet;
|
|
158
|
-
this.register(walletPlugin);
|
|
159
|
-
(0, logger_1.log)("Wallet plugin registered");
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
catch (error) {
|
|
163
|
-
(0, logger_1.logError)("Error registering builtin plugins:", error);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
// 🔌 PLUGIN MANAGER 🔌
|
|
167
|
-
/**
|
|
168
|
-
* Register a new plugin with the SDK
|
|
169
|
-
* @param plugin The plugin to register
|
|
170
|
-
* @throws Error if a plugin with the same name is already registered
|
|
171
|
-
*/
|
|
172
|
-
register(plugin) {
|
|
173
|
-
if (this.plugins.has(plugin.name)) {
|
|
174
|
-
throw new Error(`Plugin with name "${plugin.name}" already registered`);
|
|
175
|
-
}
|
|
176
|
-
plugin.initialize(this);
|
|
177
|
-
this.plugins.set(plugin.name, plugin);
|
|
178
|
-
(0, logger_1.log)(`Registered plugin: ${plugin.name}`);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Unregister a plugin from the SDK
|
|
182
|
-
* @param pluginName Name of the plugin to unregister
|
|
183
|
-
*/
|
|
184
|
-
unregister(pluginName) {
|
|
185
|
-
const plugin = this.plugins.get(pluginName);
|
|
186
|
-
if (!plugin) {
|
|
187
|
-
(0, logger_1.log)(`Plugin "${pluginName}" not found, nothing to unregister`);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
if (plugin.destroy) {
|
|
191
|
-
plugin.destroy();
|
|
192
|
-
}
|
|
193
|
-
this.plugins.delete(pluginName);
|
|
194
|
-
(0, logger_1.log)(`Unregistered plugin: ${pluginName}`);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Retrieve a registered plugin by name
|
|
198
|
-
* @param name Name of the plugin
|
|
199
|
-
* @returns The requested plugin or undefined if not found
|
|
200
|
-
* @template T Type of the plugin or its public interface
|
|
201
|
-
*/
|
|
202
|
-
getPlugin(name) {
|
|
203
|
-
return this.plugins.get(name);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Check if a plugin is registered
|
|
207
|
-
* @param name Name of the plugin to check
|
|
208
|
-
* @returns true if the plugin is registered, false otherwise
|
|
209
|
-
*/
|
|
210
|
-
hasPlugin(name) {
|
|
211
|
-
return this.plugins.has(name);
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Get all plugins of a specific category
|
|
215
|
-
* @param category Category of plugins to filter
|
|
216
|
-
* @returns Array of plugins in the specified category
|
|
217
|
-
*/
|
|
218
|
-
getPluginsByCategory(category) {
|
|
219
|
-
const result = [];
|
|
220
|
-
this.plugins.forEach((plugin) => {
|
|
221
|
-
if (plugin._category === category) {
|
|
222
|
-
result.push(plugin);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
return result;
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Get an authentication method plugin by type
|
|
229
|
-
* @param type The type of authentication method
|
|
230
|
-
* @returns The authentication plugin or undefined if not available
|
|
231
|
-
* This is a more modern approach to accessing authentication methods
|
|
232
|
-
*/
|
|
233
|
-
getAuthenticationMethod(type) {
|
|
234
|
-
switch (type) {
|
|
235
|
-
case "webauthn":
|
|
236
|
-
return this.getPlugin(shogun_1.CorePlugins.WebAuthn);
|
|
237
|
-
case "metamask":
|
|
238
|
-
return this.getPlugin(shogun_1.CorePlugins.MetaMask);
|
|
239
|
-
case "password":
|
|
240
|
-
default:
|
|
241
|
-
// Default authentication is provided by the core class
|
|
242
|
-
return {
|
|
243
|
-
login: (username, password) => {
|
|
244
|
-
this.login(username, password);
|
|
245
|
-
},
|
|
246
|
-
signUp: (username, password, confirm) => {
|
|
247
|
-
this.signUp(username, password, confirm);
|
|
248
|
-
},
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
// 🔄 RXJS INTEGRATION 🔄
|
|
253
|
-
/**
|
|
254
|
-
* Observe a Gun node for changes
|
|
255
|
-
* @param path - Path to observe (can be a string or a Gun chain)
|
|
256
|
-
* @returns Observable that emits whenever the node changes
|
|
257
|
-
*/
|
|
258
|
-
rxGet(path) {
|
|
259
|
-
return this.rx.observe(path);
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Match data based on Gun's '.map()' and convert to Observable
|
|
263
|
-
* @param path - Path to the collection
|
|
264
|
-
* @param matchFn - Optional function to filter results
|
|
265
|
-
* @returns Observable array of matched items
|
|
266
|
-
*/
|
|
267
|
-
match(path, matchFn) {
|
|
268
|
-
return this.rx.match(path, matchFn);
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Put data and return an Observable
|
|
272
|
-
* @param path - Path where to put the data
|
|
273
|
-
* @param oata - Data to put
|
|
274
|
-
* @returns Observable that completes when the put is acknowledged
|
|
275
|
-
*/
|
|
276
|
-
rxPut(path, data) {
|
|
277
|
-
return this.rx.put(path, data);
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Set data on a node and return an Observable
|
|
281
|
-
* @param path - Path to the collection
|
|
282
|
-
* @param data - Data to set
|
|
283
|
-
* @returns Observable that completes when the set is acknowledged
|
|
284
|
-
*/
|
|
285
|
-
rxSet(path, data) {
|
|
286
|
-
return this.rx.set(path, data);
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Get data once and return as Observable
|
|
290
|
-
* @param path - Path to get data from
|
|
291
|
-
* @returns Observable that emits the data once
|
|
292
|
-
*/
|
|
293
|
-
rxOnce(path) {
|
|
294
|
-
return this.rx.once(path);
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Compute derived values from gun data
|
|
298
|
-
* @param sources - Array of paths or observables to compute from
|
|
299
|
-
* @param computeFn - Function that computes a new value from the sources
|
|
300
|
-
* @returns Observable of computed values
|
|
301
|
-
*/
|
|
302
|
-
compute(sources, computeFn) {
|
|
303
|
-
return this.rx.compute(sources, computeFn);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* User put data and return an Observable (for authenticated users)
|
|
307
|
-
* @param path - Path where to put the data
|
|
308
|
-
* @param data - Data to put
|
|
309
|
-
* @returns Observable that completes when the put is acknowledged
|
|
310
|
-
*/
|
|
311
|
-
rxUserPut(path, data) {
|
|
312
|
-
return this.rx.userPut(path, data);
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Observe user data
|
|
316
|
-
* @param path - Path to observe in user space
|
|
317
|
-
* @returns Observable that emits whenever the user data changes
|
|
318
|
-
*/
|
|
319
|
-
observeUser(path) {
|
|
320
|
-
return this.rx.observeUser(path);
|
|
321
|
-
}
|
|
322
|
-
// *********************************************************************************************************
|
|
323
|
-
// 🔐 ERROR HANDLER 🔐
|
|
324
|
-
// *********************************************************************************************************
|
|
325
|
-
/**
|
|
326
|
-
* Retrieve recent errors logged by the system
|
|
327
|
-
* @param count - Number of errors to retrieve (default: 10)
|
|
328
|
-
* @returns List of most recent errors
|
|
329
|
-
*/
|
|
330
|
-
getRecentErrors(count = 10) {
|
|
331
|
-
return errorHandler_1.ErrorHandler.getRecentErrors(count);
|
|
332
|
-
}
|
|
333
|
-
// *********************************************************************************************************
|
|
334
|
-
// 🔐 LOGGING 🔐
|
|
335
|
-
// *********************************************************************************************************
|
|
336
|
-
/**
|
|
337
|
-
* Configure logging behavior for the Shogun SDK
|
|
338
|
-
* @param {LoggingConfig} config - Logging configuration object containing:
|
|
339
|
-
* - level: The minimum log level to display (error, warn, info, debug, trace)
|
|
340
|
-
* - logToConsole: Whether to output logs to the console (default: true)
|
|
341
|
-
* - customLogger: Optional custom logger implementation
|
|
342
|
-
* - logTimestamps: Whether to include timestamps in logs (default: true)
|
|
343
|
-
* @returns {void}
|
|
344
|
-
* @description Updates the logging configuration for the SDK. Changes take effect immediately
|
|
345
|
-
* for all subsequent log operations.
|
|
346
|
-
*/
|
|
347
|
-
configureLogging(config) {
|
|
348
|
-
(0, logger_1.configureLogging)(config);
|
|
349
|
-
(0, logger_1.log)("Logging reconfigured with new settings");
|
|
350
|
-
}
|
|
351
|
-
// *********************************************************************************************************
|
|
352
|
-
// 🔐 AUTHENTICATION
|
|
353
|
-
// *********************************************************************************************************
|
|
354
|
-
/**
|
|
355
|
-
* Check if user is logged in
|
|
356
|
-
* @returns {boolean} True if user is logged in, false otherwise
|
|
357
|
-
* @description Verifies authentication status by checking GunDB login state
|
|
358
|
-
* and presence of authentication credentials in storage
|
|
359
|
-
*/
|
|
360
|
-
isLoggedIn() {
|
|
361
|
-
const gunLoggedIn = this.gundb.isLoggedIn();
|
|
362
|
-
const gunUser = this.gun.user();
|
|
363
|
-
if (gunLoggedIn) {
|
|
364
|
-
return true;
|
|
365
|
-
}
|
|
366
|
-
// @ts-ignore - Accessing internal Gun property that is not fully typed
|
|
367
|
-
const hasPair = gunUser && gunUser._ && gunUser._.sea;
|
|
368
|
-
const hasLocalPair = this.storage.getItem("pair");
|
|
369
|
-
return !!hasPair || !!hasLocalPair;
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Perform user logout
|
|
373
|
-
* @description Logs out the current user from GunDB and emits logout event.
|
|
374
|
-
* If user is not authenticated, the logout operation is ignored.
|
|
375
|
-
*/
|
|
376
|
-
logout() {
|
|
377
|
-
try {
|
|
378
|
-
if (!this.isLoggedIn()) {
|
|
379
|
-
(0, logger_1.log)("Logout ignored: user not authenticated");
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
this.gundb.logout();
|
|
383
|
-
this.eventEmitter.emit("auth:logout", {});
|
|
384
|
-
(0, logger_1.log)("Logout completed successfully");
|
|
385
|
-
}
|
|
386
|
-
catch (error) {
|
|
387
|
-
// Use centralized error handler
|
|
388
|
-
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.AUTHENTICATION, "LOGOUT_FAILED", error instanceof Error ? error.message : "Error during logout", error);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Authenticate user with username and password
|
|
393
|
-
* @param username - Username
|
|
394
|
-
* @param password - User password
|
|
395
|
-
* @returns {Promise<AuthResult>} Promise with authentication result
|
|
396
|
-
* @description Attempts to log in user with provided credentials.
|
|
397
|
-
* Emits login event on success.
|
|
398
|
-
*/
|
|
399
|
-
async login(username, password) {
|
|
400
|
-
(0, logger_1.log)("Login");
|
|
401
|
-
try {
|
|
402
|
-
(0, logger_1.log)(`Login attempt for user: ${username}`);
|
|
403
|
-
// Verify parameters
|
|
404
|
-
if (!username || !password) {
|
|
405
|
-
return {
|
|
406
|
-
success: false,
|
|
407
|
-
error: "Username and password are required",
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
// Timeout after a configurable interval (default 15 seconds)
|
|
411
|
-
const timeoutDuration = this.config?.timeouts?.login ?? 15000;
|
|
412
|
-
// Utilizziamo il timeout di ShogunCore invece di quello interno di GunDB
|
|
413
|
-
const loginPromiseWithTimeout = new Promise(async (resolve) => {
|
|
414
|
-
const timeoutId = setTimeout(() => {
|
|
415
|
-
resolve({
|
|
416
|
-
success: false,
|
|
417
|
-
error: "Login timeout",
|
|
418
|
-
});
|
|
419
|
-
}, timeoutDuration);
|
|
420
|
-
try {
|
|
421
|
-
// Utilizziamo il metodo login di GunDB
|
|
422
|
-
const gunLoginResult = (await this.gundb.login(username, password));
|
|
423
|
-
clearTimeout(timeoutId);
|
|
424
|
-
const walletPlugin = this.getPlugin(shogun_1.CorePlugins.WalletManager);
|
|
425
|
-
if (gunLoginResult && walletPlugin) {
|
|
426
|
-
const mainWallet = walletPlugin.getMainWalletCredentials();
|
|
427
|
-
this.storage.setItem("main-wallet", JSON.stringify(mainWallet));
|
|
428
|
-
}
|
|
429
|
-
if (!gunLoginResult.success) {
|
|
430
|
-
resolve({
|
|
431
|
-
success: false,
|
|
432
|
-
error: gunLoginResult.error || "Login failed",
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
else {
|
|
436
|
-
resolve({
|
|
437
|
-
success: true,
|
|
438
|
-
userPub: gunLoginResult.userPub || "",
|
|
439
|
-
username: gunLoginResult.username || username,
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
catch (error) {
|
|
444
|
-
clearTimeout(timeoutId);
|
|
445
|
-
resolve({
|
|
446
|
-
success: false,
|
|
447
|
-
error: error.message || "Login error",
|
|
448
|
-
});
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
const result = await loginPromiseWithTimeout;
|
|
452
|
-
if (result.success) {
|
|
453
|
-
this.eventEmitter.emit("auth:login", {
|
|
454
|
-
userPub: result.userPub ?? "",
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
return result;
|
|
458
|
-
}
|
|
459
|
-
catch (error) {
|
|
460
|
-
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.AUTHENTICATION, "LOGIN_FAILED", error.message ?? "Unknown error during login", error);
|
|
461
|
-
return {
|
|
462
|
-
success: false,
|
|
463
|
-
error: error.message ?? "Unknown error during login",
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Register a new user with provided credentials
|
|
469
|
-
* @param username - Username
|
|
470
|
-
* @param password - Password
|
|
471
|
-
* @param passwordConfirmation - Password confirmation
|
|
472
|
-
* @returns {Promise<SignUpResult>} Registration result
|
|
473
|
-
* @description Creates a new user account with the provided credentials.
|
|
474
|
-
* Validates password requirements and emits signup event on success.
|
|
475
|
-
*/
|
|
476
|
-
async signUp(username, password, passwordConfirmation) {
|
|
477
|
-
(0, logger_1.log)("Sign up");
|
|
478
|
-
try {
|
|
479
|
-
if (!username || !password) {
|
|
480
|
-
return {
|
|
481
|
-
success: false,
|
|
482
|
-
error: "Username and password are required",
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
if (passwordConfirmation !== undefined &&
|
|
486
|
-
password !== passwordConfirmation) {
|
|
487
|
-
return {
|
|
488
|
-
success: false,
|
|
489
|
-
error: "Passwords do not match",
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
if (password.length < 6) {
|
|
493
|
-
return {
|
|
494
|
-
success: false,
|
|
495
|
-
error: "Password must be at least 6 characters long",
|
|
496
|
-
};
|
|
497
|
-
}
|
|
498
|
-
// Emettiamo un evento di debug per monitorare il flusso
|
|
499
|
-
this.eventEmitter.emit("debug", {
|
|
500
|
-
action: "signup_start",
|
|
501
|
-
username,
|
|
502
|
-
timestamp: Date.now(),
|
|
503
|
-
});
|
|
504
|
-
(0, logger_1.log)(`Inizializzazione registrazione per utente: ${username}`);
|
|
505
|
-
const signupPromise = new Promise((resolve) => {
|
|
506
|
-
// Utilizziamo direttamente il metodo signUp di GunDB che ora ha il suo timeout integrato
|
|
507
|
-
this.gundb.signUp(username, password).then((gunResult) => {
|
|
508
|
-
(0, logger_1.log)(`GunDB registration result: ${gunResult.success ? "success" : "failed"}`);
|
|
509
|
-
// Emettiamo un evento di debug per monitorare il flusso
|
|
510
|
-
this.eventEmitter.emit("debug", {
|
|
511
|
-
action: "gundb_signup_complete",
|
|
512
|
-
success: gunResult.success,
|
|
513
|
-
error: gunResult.error,
|
|
514
|
-
timestamp: Date.now(),
|
|
515
|
-
});
|
|
516
|
-
if (!gunResult.success) {
|
|
517
|
-
resolve({
|
|
518
|
-
success: false,
|
|
519
|
-
error: gunResult.error || "Registration failed in GunDB",
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
else {
|
|
523
|
-
resolve({
|
|
524
|
-
success: true,
|
|
525
|
-
userPub: gunResult.userPub || "",
|
|
526
|
-
username: username || "",
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
});
|
|
530
|
-
});
|
|
531
|
-
const timeoutDuration = this.config?.timeouts?.signup ?? 30000; // Timeout predefinito di 30 secondi
|
|
532
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
533
|
-
setTimeout(() => {
|
|
534
|
-
(0, logger_1.logError)(`Timeout a livello ShogunCore durante la registrazione utente: ${username}`);
|
|
535
|
-
// Emettiamo un evento di debug per monitorare il flusso
|
|
536
|
-
this.eventEmitter.emit("debug", {
|
|
537
|
-
action: "signup_timeout",
|
|
538
|
-
username,
|
|
539
|
-
timestamp: Date.now(),
|
|
540
|
-
});
|
|
541
|
-
resolve({
|
|
542
|
-
success: false,
|
|
543
|
-
error: "Registration timeout at ShogunCore level",
|
|
544
|
-
});
|
|
545
|
-
}, timeoutDuration);
|
|
546
|
-
});
|
|
547
|
-
// Use Promise.race to handle timeout
|
|
548
|
-
const result = await Promise.race([signupPromise, timeoutPromise]);
|
|
549
|
-
if (result.success) {
|
|
550
|
-
(0, logger_1.log)(`Registrazione completata con successo per: ${username}`);
|
|
551
|
-
this.eventEmitter.emit("auth:signup", {
|
|
552
|
-
userPub: result.userPub ?? "",
|
|
553
|
-
username,
|
|
554
|
-
});
|
|
555
|
-
// Per evitare di complicare ulteriormente il processo, disabilita temporaneamente
|
|
556
|
-
// la creazione del DID durante il signup finché il problema non è risolto completamente
|
|
557
|
-
// Invece, creeremo il DID al primo accesso successivo dell'utente
|
|
558
|
-
// Emettiamo un evento di debug per monitorare il flusso
|
|
559
|
-
this.eventEmitter.emit("debug", {
|
|
560
|
-
action: "signup_complete",
|
|
561
|
-
username,
|
|
562
|
-
userPub: result.userPub,
|
|
563
|
-
timestamp: Date.now(),
|
|
564
|
-
});
|
|
565
|
-
return result;
|
|
566
|
-
}
|
|
567
|
-
// Emettiamo un evento di debug per monitorare il flusso in caso di fallimento
|
|
568
|
-
this.eventEmitter.emit("debug", {
|
|
569
|
-
action: "signup_failed",
|
|
570
|
-
username,
|
|
571
|
-
error: result.error,
|
|
572
|
-
timestamp: Date.now(),
|
|
573
|
-
});
|
|
574
|
-
return result;
|
|
575
|
-
}
|
|
576
|
-
catch (error) {
|
|
577
|
-
(0, logger_1.logError)(`Error during registration for user ${username}:`, error);
|
|
578
|
-
// Emettiamo un evento di debug per monitorare il flusso in caso di eccezione
|
|
579
|
-
this.eventEmitter.emit("debug", {
|
|
580
|
-
action: "signup_exception",
|
|
581
|
-
username,
|
|
582
|
-
error: error.message || "Unknown error",
|
|
583
|
-
timestamp: Date.now(),
|
|
584
|
-
});
|
|
585
|
-
return {
|
|
586
|
-
success: false,
|
|
587
|
-
error: error.message ?? "Unknown error during registration",
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
// 🤫 PRIVATE HELPER METHODS 🤫
|
|
592
|
-
/**
|
|
593
|
-
* Create a new user with GunDB
|
|
594
|
-
* @param username - Username
|
|
595
|
-
* @param password - Password
|
|
596
|
-
* @returns {Promise<{success: boolean, userPub?: string, error?: string}>} Promise with success status and user public key
|
|
597
|
-
* @description Creates a new user in GunDB with error handling
|
|
598
|
-
*/
|
|
599
|
-
createUserWithGunDB(username, password) {
|
|
600
|
-
(0, logger_1.log)(`Ensuring user exists with GunDB: ${username}`);
|
|
601
|
-
return new Promise(async (resolve) => {
|
|
602
|
-
try {
|
|
603
|
-
const authUser = () => {
|
|
604
|
-
return new Promise((resolveAuth) => {
|
|
605
|
-
try {
|
|
606
|
-
this.gundb.logout();
|
|
607
|
-
}
|
|
608
|
-
catch (e) {
|
|
609
|
-
/* ignore logout errors */
|
|
610
|
-
}
|
|
611
|
-
this.gun.user().auth(username, password, (ack) => {
|
|
612
|
-
if (ack.err) {
|
|
613
|
-
resolveAuth({ err: ack.err });
|
|
614
|
-
}
|
|
615
|
-
else {
|
|
616
|
-
const user = this.gundb.gun.user();
|
|
617
|
-
const userPub = user.is?.pub || "";
|
|
618
|
-
if (!user.is || !userPub) {
|
|
619
|
-
resolveAuth({
|
|
620
|
-
err: "Authentication failed after apparent success.",
|
|
621
|
-
});
|
|
622
|
-
}
|
|
623
|
-
else {
|
|
624
|
-
resolveAuth({ pub: userPub });
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
});
|
|
628
|
-
});
|
|
629
|
-
};
|
|
630
|
-
const createUser = () => {
|
|
631
|
-
return new Promise((resolveCreate) => {
|
|
632
|
-
try {
|
|
633
|
-
this.gundb.logout();
|
|
634
|
-
}
|
|
635
|
-
catch (e) {
|
|
636
|
-
/* ignore logout errors */
|
|
637
|
-
}
|
|
638
|
-
this.gundb.gun.user().create(username, password, (ack) => {
|
|
639
|
-
resolveCreate({ err: ack.err, pub: ack.pub }); // pub might be present on success
|
|
640
|
-
});
|
|
641
|
-
});
|
|
642
|
-
};
|
|
643
|
-
(0, logger_1.log)(`Attempting login first for ${username}...`);
|
|
644
|
-
let loginResult = await authUser();
|
|
645
|
-
if (loginResult.pub) {
|
|
646
|
-
(0, logger_1.log)(`Login successful for existing user. Pub: ${loginResult.pub}`);
|
|
647
|
-
resolve({
|
|
648
|
-
success: true,
|
|
649
|
-
userPub: loginResult.pub,
|
|
650
|
-
});
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
(0, logger_1.log)(`Login failed (${loginResult.err ?? "unknown reason"}), attempting user creation...`);
|
|
654
|
-
const createResult = await createUser();
|
|
655
|
-
if (createResult.err) {
|
|
656
|
-
(0, logger_1.log)(`User creation error: ${createResult.err}`);
|
|
657
|
-
resolve({
|
|
658
|
-
success: false,
|
|
659
|
-
error: `User creation failed: ${createResult.err}`,
|
|
660
|
-
});
|
|
661
|
-
return;
|
|
662
|
-
}
|
|
663
|
-
(0, logger_1.log)(`User created successfully, attempting login again for confirmation...`);
|
|
664
|
-
loginResult = await authUser();
|
|
665
|
-
if (loginResult.pub) {
|
|
666
|
-
(0, logger_1.log)(`Post-creation login successful! User pub: ${loginResult.pub}`);
|
|
667
|
-
resolve({
|
|
668
|
-
success: true,
|
|
669
|
-
userPub: loginResult.pub,
|
|
670
|
-
});
|
|
671
|
-
}
|
|
672
|
-
else {
|
|
673
|
-
(0, logger_1.logError)(`Post-creation login failed unexpectedly: ${loginResult.err}`);
|
|
674
|
-
resolve({
|
|
675
|
-
success: false,
|
|
676
|
-
error: `User created, but subsequent login failed: ${loginResult.err}`,
|
|
677
|
-
});
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
catch (error) {
|
|
681
|
-
const errorMsg = error.message ?? "Unknown error during user existence check";
|
|
682
|
-
(0, logger_1.logError)(`Error in createUserWithGunDB: ${errorMsg}`, error);
|
|
683
|
-
resolve({
|
|
684
|
-
success: false,
|
|
685
|
-
error: errorMsg,
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
// 🔫 GUN ACTIONS 🔫
|
|
691
|
-
/**
|
|
692
|
-
* Retrieves data from a Gun node at the specified path
|
|
693
|
-
* @param path - The path to the Gun node
|
|
694
|
-
* @returns Promise that resolves with the node data or rejects with an error
|
|
695
|
-
*/
|
|
696
|
-
get(path) {
|
|
697
|
-
return new Promise((resolve, reject) => {
|
|
698
|
-
this.gundb.gun.get(path).once((data) => {
|
|
699
|
-
if (data.err) {
|
|
700
|
-
reject(data.err);
|
|
701
|
-
}
|
|
702
|
-
else {
|
|
703
|
-
resolve(data);
|
|
704
|
-
}
|
|
705
|
-
});
|
|
706
|
-
});
|
|
707
|
-
}
|
|
708
|
-
/**
|
|
709
|
-
* Stores data in Gun at the root level
|
|
710
|
-
* @param data - The data to store
|
|
711
|
-
* @returns Promise that resolves when data is stored or rejects with an error
|
|
712
|
-
*/
|
|
713
|
-
put(data) {
|
|
714
|
-
return new Promise((resolve, reject) => {
|
|
715
|
-
this.gundb.gun.put(data, (ack) => {
|
|
716
|
-
if (ack.err) {
|
|
717
|
-
reject(ack.err);
|
|
718
|
-
}
|
|
719
|
-
else {
|
|
720
|
-
resolve(ack);
|
|
721
|
-
}
|
|
722
|
-
});
|
|
723
|
-
});
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Stores data in the authenticated user's space
|
|
727
|
-
* @param data - The data to store in user space
|
|
728
|
-
* @returns Promise that resolves when data is stored or rejects with an error
|
|
729
|
-
*/
|
|
730
|
-
userPut(data) {
|
|
731
|
-
return new Promise((resolve, reject) => {
|
|
732
|
-
this.gundb.gun.user().put(data, (ack) => {
|
|
733
|
-
if (ack.err) {
|
|
734
|
-
reject(ack.err);
|
|
735
|
-
}
|
|
736
|
-
else {
|
|
737
|
-
resolve(ack);
|
|
738
|
-
}
|
|
739
|
-
});
|
|
740
|
-
});
|
|
741
|
-
}
|
|
742
|
-
/**
|
|
743
|
-
* Retrieves data from the authenticated user's space at the specified path
|
|
744
|
-
* @param path - The path to the user data
|
|
745
|
-
* @returns Promise that resolves with the user data or rejects with an error
|
|
746
|
-
*/
|
|
747
|
-
userGet(path) {
|
|
748
|
-
return new Promise((resolve, reject) => {
|
|
749
|
-
this.gundb.gun
|
|
750
|
-
.user()
|
|
751
|
-
.get(path)
|
|
752
|
-
.once((data) => {
|
|
753
|
-
if (data.err) {
|
|
754
|
-
reject(data.err);
|
|
755
|
-
}
|
|
756
|
-
else {
|
|
757
|
-
resolve(data);
|
|
758
|
-
}
|
|
759
|
-
});
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
// 📢 EVENT EMITTER 📢
|
|
763
|
-
/**
|
|
764
|
-
* Emits an event through the core's event emitter.
|
|
765
|
-
* Plugins should use this method to emit events instead of accessing the private eventEmitter directly.
|
|
766
|
-
* @param eventName The name of the event to emit.
|
|
767
|
-
* @param data The data to pass with the event.
|
|
768
|
-
*/
|
|
769
|
-
emit(eventName, data) {
|
|
770
|
-
return this.eventEmitter.emit(eventName, data);
|
|
771
|
-
}
|
|
772
|
-
/**
|
|
773
|
-
* Add an event listener
|
|
774
|
-
* @param eventName The name of the event to listen for
|
|
775
|
-
* @param listener The callback function to execute when the event is emitted
|
|
776
|
-
*/
|
|
777
|
-
on(eventName, listener) {
|
|
778
|
-
this.eventEmitter.on(eventName, listener);
|
|
779
|
-
return this;
|
|
780
|
-
}
|
|
781
|
-
/**
|
|
782
|
-
* Add a one-time event listener
|
|
783
|
-
* @param eventName The name of the event to listen for
|
|
784
|
-
* @param listener The callback function to execute when the event is emitted
|
|
785
|
-
*/
|
|
786
|
-
once(eventName, listener) {
|
|
787
|
-
this.eventEmitter.once(eventName, listener);
|
|
788
|
-
return this;
|
|
789
|
-
}
|
|
790
|
-
/**
|
|
791
|
-
* Remove an event listener
|
|
792
|
-
* @param eventName The name of the event to stop listening for
|
|
793
|
-
* @param listener The callback function to remove
|
|
794
|
-
*/
|
|
795
|
-
off(eventName, listener) {
|
|
796
|
-
this.eventEmitter.off(eventName, listener);
|
|
797
|
-
return this;
|
|
798
|
-
}
|
|
799
|
-
/**
|
|
800
|
-
* Remove all listeners for a specific event or all events
|
|
801
|
-
* @param eventName Optional. The name of the event to remove listeners for.
|
|
802
|
-
* If not provided, all listeners for all events are removed.
|
|
803
|
-
*/
|
|
804
|
-
removeAllListeners(eventName) {
|
|
805
|
-
this.eventEmitter.removeAllListeners(eventName);
|
|
806
|
-
return this;
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
exports.ShogunCore = ShogunCore;
|
|
810
|
-
// Export all types
|
|
811
|
-
__exportStar(require("./types/shogun"), exports);
|
|
812
|
-
// Export classes
|
|
813
|
-
var gun_2 = require("./gun/gun");
|
|
814
|
-
Object.defineProperty(exports, "GunDB", { enumerable: true, get: function () { return gun_2.GunDB; } });
|
|
815
|
-
var metamask_1 = require("./plugins/metamask/metamask");
|
|
816
|
-
Object.defineProperty(exports, "MetaMask", { enumerable: true, get: function () { return metamask_1.MetaMask; } });
|
|
817
|
-
var stealth_1 = require("./plugins/stealth/stealth");
|
|
818
|
-
Object.defineProperty(exports, "Stealth", { enumerable: true, get: function () { return stealth_1.Stealth; } });
|
|
819
|
-
var webauthn_1 = require("./plugins/webauthn/webauthn");
|
|
820
|
-
Object.defineProperty(exports, "Webauthn", { enumerable: true, get: function () { return webauthn_1.Webauthn; } });
|
|
821
|
-
var storage_2 = require("./storage/storage");
|
|
822
|
-
Object.defineProperty(exports, "ShogunStorage", { enumerable: true, get: function () { return storage_2.ShogunStorage; } });
|
|
823
|
-
var events_1 = require("./types/events");
|
|
824
|
-
Object.defineProperty(exports, "ShogunEventEmitter", { enumerable: true, get: function () { return events_1.ShogunEventEmitter; } });
|