shogun-core 3.0.4 → 3.0.6

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 (93) hide show
  1. package/dist/browser/shogun-core.js +91002 -0
  2. package/dist/browser/shogun-core.js.map +1 -0
  3. package/dist/config/simplified-config.js +230 -0
  4. package/dist/core.js +338 -0
  5. package/dist/gundb/crypto.js +268 -0
  6. package/dist/gundb/db.js +1829 -0
  7. package/dist/gundb/derive.js +229 -0
  8. package/dist/gundb/errors.js +66 -0
  9. package/dist/gundb/index.js +6 -0
  10. package/dist/gundb/rxjs.js +445 -0
  11. package/dist/gundb/simple-api.js +438 -0
  12. package/dist/gundb/types.js +4 -0
  13. package/dist/index.js +16 -0
  14. package/dist/interfaces/common.js +1 -0
  15. package/dist/interfaces/events.js +36 -0
  16. package/dist/interfaces/plugin.js +1 -0
  17. package/dist/interfaces/shogun.js +34 -0
  18. package/dist/managers/AuthManager.js +225 -0
  19. package/dist/managers/CoreInitializer.js +227 -0
  20. package/dist/managers/EventManager.js +67 -0
  21. package/dist/managers/PluginManager.js +296 -0
  22. package/dist/migration-test.js +91 -0
  23. package/dist/plugins/base.js +47 -0
  24. package/dist/plugins/index.js +15 -0
  25. package/dist/plugins/nostr/index.js +4 -0
  26. package/dist/plugins/nostr/nostrConnector.js +413 -0
  27. package/dist/plugins/nostr/nostrConnectorPlugin.js +446 -0
  28. package/dist/plugins/nostr/nostrSigner.js +313 -0
  29. package/dist/plugins/nostr/types.js +1 -0
  30. package/dist/plugins/oauth/index.js +3 -0
  31. package/dist/plugins/oauth/oauthConnector.js +753 -0
  32. package/dist/plugins/oauth/oauthPlugin.js +396 -0
  33. package/dist/plugins/oauth/types.js +1 -0
  34. package/dist/plugins/web3/index.js +4 -0
  35. package/dist/plugins/web3/types.js +1 -0
  36. package/dist/plugins/web3/web3Connector.js +528 -0
  37. package/dist/plugins/web3/web3ConnectorPlugin.js +448 -0
  38. package/dist/plugins/web3/web3Signer.js +308 -0
  39. package/dist/plugins/webauthn/index.js +3 -0
  40. package/dist/plugins/webauthn/types.js +11 -0
  41. package/dist/plugins/webauthn/webauthn.js +478 -0
  42. package/dist/plugins/webauthn/webauthnPlugin.js +398 -0
  43. package/dist/plugins/webauthn/webauthnSigner.js +304 -0
  44. package/dist/storage/storage.js +147 -0
  45. package/dist/types/config/simplified-config.d.ts +114 -0
  46. package/dist/types/core.d.ts +305 -0
  47. package/dist/types/gundb/crypto.d.ts +95 -0
  48. package/dist/types/gundb/db.d.ts +401 -0
  49. package/dist/types/gundb/derive.d.ts +21 -0
  50. package/dist/types/gundb/errors.d.ts +42 -0
  51. package/dist/types/gundb/index.d.ts +3 -0
  52. package/dist/types/gundb/rxjs.d.ts +110 -0
  53. package/dist/types/gundb/simple-api.d.ts +90 -0
  54. package/dist/types/gundb/types.d.ts +264 -0
  55. package/dist/types/index.d.ts +14 -0
  56. package/dist/types/interfaces/common.d.ts +85 -0
  57. package/dist/types/interfaces/events.d.ts +131 -0
  58. package/dist/types/interfaces/plugin.d.ts +162 -0
  59. package/dist/types/interfaces/shogun.d.ts +215 -0
  60. package/dist/types/managers/AuthManager.d.ts +72 -0
  61. package/dist/types/managers/CoreInitializer.d.ts +40 -0
  62. package/dist/types/managers/EventManager.d.ts +49 -0
  63. package/dist/types/managers/PluginManager.d.ts +145 -0
  64. package/dist/types/migration-test.d.ts +16 -0
  65. package/dist/types/plugins/base.d.ts +35 -0
  66. package/dist/types/plugins/index.d.ts +14 -0
  67. package/dist/types/plugins/nostr/index.d.ts +4 -0
  68. package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
  69. package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
  70. package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
  71. package/dist/types/plugins/nostr/types.d.ts +122 -0
  72. package/dist/types/plugins/oauth/index.d.ts +3 -0
  73. package/dist/types/plugins/oauth/oauthConnector.d.ts +110 -0
  74. package/dist/types/plugins/oauth/oauthPlugin.d.ts +91 -0
  75. package/dist/types/plugins/oauth/types.d.ts +114 -0
  76. package/dist/types/plugins/web3/index.d.ts +4 -0
  77. package/dist/types/plugins/web3/types.d.ts +107 -0
  78. package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
  79. package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
  80. package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
  81. package/dist/types/plugins/webauthn/index.d.ts +3 -0
  82. package/dist/types/plugins/webauthn/types.d.ts +162 -0
  83. package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
  84. package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +158 -0
  85. package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
  86. package/dist/types/storage/storage.d.ts +50 -0
  87. package/dist/types/utils/errorHandler.d.ts +119 -0
  88. package/dist/types/utils/eventEmitter.d.ts +39 -0
  89. package/dist/types/utils/validation.d.ts +27 -0
  90. package/dist/utils/errorHandler.js +241 -0
  91. package/dist/utils/eventEmitter.js +76 -0
  92. package/dist/utils/validation.js +72 -0
  93. package/package.json +1 -1
@@ -0,0 +1,448 @@
1
+ import { BasePlugin } from "../base";
2
+ import { Web3Connector } from "./web3Connector";
3
+ import { Web3Signer } from "./web3Signer";
4
+ import { ErrorHandler, ErrorType, createError } from "../../utils/errorHandler";
5
+ /**
6
+ * Plugin per la gestione delle funzionalità Web3 in ShogunCore
7
+ */
8
+ export class Web3ConnectorPlugin extends BasePlugin {
9
+ name = "web3";
10
+ version = "1.0.0";
11
+ description = "Provides Ethereum wallet connection and authentication for ShogunCore";
12
+ Web3 = null;
13
+ signer = null;
14
+ /**
15
+ * @inheritdoc
16
+ */
17
+ initialize(core) {
18
+ super.initialize(core);
19
+ // Inizializziamo il modulo Web3
20
+ this.Web3 = new Web3Connector();
21
+ this.signer = new Web3Signer(this.Web3);
22
+ // Rimuovo i console.log superflui
23
+ }
24
+ /**
25
+ * @inheritdoc
26
+ */
27
+ destroy() {
28
+ if (this.Web3) {
29
+ this.Web3.cleanup();
30
+ }
31
+ this.Web3 = null;
32
+ this.signer = null;
33
+ super.destroy();
34
+ // Linea 50
35
+ }
36
+ /**
37
+ * Assicura che il modulo Web3 sia inizializzato
38
+ * @private
39
+ */
40
+ assertMetaMask() {
41
+ this.assertInitialized();
42
+ if (!this.Web3) {
43
+ throw new Error("Web3 module not initialized");
44
+ }
45
+ return this.Web3;
46
+ }
47
+ /**
48
+ * Assicura che il signer sia inizializzato
49
+ * @private
50
+ */
51
+ assertSigner() {
52
+ this.assertInitialized();
53
+ if (!this.signer) {
54
+ throw new Error("Web3 signer not initialized");
55
+ }
56
+ return this.signer;
57
+ }
58
+ /**
59
+ * @inheritdoc
60
+ */
61
+ isAvailable() {
62
+ return this.assertMetaMask().isAvailable();
63
+ }
64
+ /**
65
+ * @inheritdoc
66
+ */
67
+ async connectMetaMask() {
68
+ return this.assertMetaMask().connectMetaMask();
69
+ }
70
+ /**
71
+ * @inheritdoc
72
+ */
73
+ async generateCredentials(address) {
74
+ // Rimuovo i console.log superflui
75
+ return this.assertMetaMask().generateCredentials(address);
76
+ }
77
+ /**
78
+ * @inheritdoc
79
+ */
80
+ cleanup() {
81
+ this.assertMetaMask().cleanup();
82
+ }
83
+ /**
84
+ * @inheritdoc
85
+ */
86
+ setCustomProvider(rpcUrl, privateKey) {
87
+ this.assertMetaMask().setCustomProvider(rpcUrl, privateKey);
88
+ }
89
+ /**
90
+ * @inheritdoc
91
+ */
92
+ async getSigner() {
93
+ return this.assertMetaMask().getSigner();
94
+ }
95
+ /**
96
+ * @inheritdoc
97
+ */
98
+ async getProvider() {
99
+ return this.assertMetaMask().getProvider();
100
+ }
101
+ /**
102
+ * @inheritdoc
103
+ */
104
+ async generatePassword(signature) {
105
+ return this.assertMetaMask().generatePassword(signature);
106
+ }
107
+ /**
108
+ * @inheritdoc
109
+ */
110
+ async verifySignature(message, signature) {
111
+ return this.assertMetaMask().verifySignature(message, signature);
112
+ }
113
+ // === WEB3 SIGNER METHODS ===
114
+ /**
115
+ * Creates a new Web3 signing credential
116
+ * CONSISTENT with normal Web3 approach
117
+ */
118
+ async createSigningCredential(address) {
119
+ try {
120
+ const conn = this.assertMetaMask();
121
+ if (typeof conn.createSigningCredential === "function") {
122
+ return await conn.createSigningCredential(address);
123
+ }
124
+ return await this.assertSigner().createSigningCredential(address);
125
+ }
126
+ catch (error) {
127
+ console.error(`Error creating Web3 signing credential: ${error.message}`);
128
+ throw error;
129
+ }
130
+ }
131
+ /**
132
+ * Creates an authenticator function for Web3 signing
133
+ */
134
+ createAuthenticator(address) {
135
+ try {
136
+ const conn = this.assertMetaMask();
137
+ if (typeof conn.createAuthenticator === "function") {
138
+ return conn.createAuthenticator(address);
139
+ }
140
+ return this.assertSigner().createAuthenticator(address);
141
+ }
142
+ catch (error) {
143
+ console.error(`Error creating Web3 authenticator: ${error.message}`);
144
+ throw error;
145
+ }
146
+ }
147
+ /**
148
+ * Creates a derived key pair from Web3 credential
149
+ */
150
+ async createDerivedKeyPair(address, extra) {
151
+ try {
152
+ const conn = this.assertMetaMask();
153
+ if (typeof conn.createDerivedKeyPair === "function") {
154
+ return await conn.createDerivedKeyPair(address, extra);
155
+ }
156
+ return await this.assertSigner().createDerivedKeyPair(address, extra);
157
+ }
158
+ catch (error) {
159
+ console.error(`Error creating derived key pair: ${error.message}`);
160
+ throw error;
161
+ }
162
+ }
163
+ /**
164
+ * Signs data with derived keys after Web3 verification
165
+ */
166
+ async signWithDerivedKeys(data, address, extra) {
167
+ try {
168
+ const conn = this.assertMetaMask();
169
+ if (typeof conn.signWithDerivedKeys === "function") {
170
+ return await conn.signWithDerivedKeys(data, address, extra);
171
+ }
172
+ return await this.assertSigner().signWithDerivedKeys(data, address, extra);
173
+ }
174
+ catch (error) {
175
+ console.error(`Error signing with derived keys: ${error.message}`);
176
+ throw error;
177
+ }
178
+ }
179
+ /**
180
+ * Get signing credential by address
181
+ */
182
+ getSigningCredential(address) {
183
+ const conn = this.assertMetaMask();
184
+ if (typeof conn.getSigningCredential === "function") {
185
+ return conn.getSigningCredential(address);
186
+ }
187
+ return this.assertSigner().getCredential(address);
188
+ }
189
+ /**
190
+ * List all signing credentials
191
+ */
192
+ listSigningCredentials() {
193
+ const conn = this.assertMetaMask();
194
+ if (typeof conn.listSigningCredentials === "function") {
195
+ return conn.listSigningCredentials();
196
+ }
197
+ return this.assertSigner().listCredentials();
198
+ }
199
+ /**
200
+ * Remove a signing credential
201
+ */
202
+ removeSigningCredential(address) {
203
+ const conn = this.assertMetaMask();
204
+ if (typeof conn.removeSigningCredential === "function") {
205
+ return conn.removeSigningCredential(address);
206
+ }
207
+ return this.assertSigner().removeCredential(address);
208
+ }
209
+ // === CONSISTENCY METHODS ===
210
+ /**
211
+ * Creates a Gun user from Web3 signing credential
212
+ * This ensures the SAME user is created as with normal approach
213
+ */
214
+ async createGunUserFromSigningCredential(address) {
215
+ try {
216
+ console.log(`🔧 createGunUserFromSigningCredential called with address:`, address);
217
+ const conn = this.assertMetaMask();
218
+ if (typeof conn.createGunUserFromSigningCredential === "function") {
219
+ console.log(`🔧 Using connector's createGunUserFromSigningCredential`);
220
+ const result = await conn.createGunUserFromSigningCredential(address);
221
+ console.log(`🔧 Connector result:`, result);
222
+ return result;
223
+ }
224
+ console.log(`🔧 Using fallback createGunUser`);
225
+ const core = this.assertInitialized();
226
+ // FIX: Use deterministic approach - try to authenticate first, then create if needed
227
+ console.log(`🔧 Attempting authentication with deterministic pair`);
228
+ const authResult = await this.assertSigner().authenticateWithExistingPair(address, core.gun);
229
+ if (authResult.success) {
230
+ console.log(`🔧 Authentication successful with existing user`);
231
+ return authResult;
232
+ }
233
+ console.log(`🔧 Authentication failed, creating new user`);
234
+ // If authentication failed, create new user
235
+ const result = await this.assertSigner().createGunUser(address, core.gun);
236
+ console.log(`🔧 User creation result:`, result);
237
+ return result;
238
+ }
239
+ catch (error) {
240
+ console.error(`Error creating Gun user from Web3 signing credential: ${error.message}`);
241
+ throw error;
242
+ }
243
+ }
244
+ /**
245
+ * Get the Gun user public key for a signing credential
246
+ */
247
+ async getGunUserPubFromSigningCredential(address) {
248
+ const conn = this.assertMetaMask();
249
+ if (typeof conn.getGunUserPubFromSigningCredential === "function") {
250
+ return await conn.getGunUserPubFromSigningCredential(address);
251
+ }
252
+ return await this.assertSigner().getGunUserPub(address);
253
+ }
254
+ /**
255
+ * Get the password (for consistency checking)
256
+ */
257
+ getPassword(address) {
258
+ const conn = this.assertMetaMask();
259
+ if (typeof conn.getPassword === "function") {
260
+ return conn.getPassword(address);
261
+ }
262
+ return this.assertSigner().getPassword(address);
263
+ }
264
+ /**
265
+ * Verify consistency between oneshot and normal approaches
266
+ * This ensures both approaches create the same Gun user
267
+ */
268
+ async verifyConsistency(address, expectedUserPub) {
269
+ try {
270
+ const conn = this.assertMetaMask();
271
+ if (typeof conn.verifyConsistency === "function") {
272
+ return await conn.verifyConsistency(address, expectedUserPub);
273
+ }
274
+ return await this.assertSigner().verifyConsistency(address, expectedUserPub);
275
+ }
276
+ catch (error) {
277
+ console.error(`Error verifying Web3 consistency: ${error.message}`);
278
+ return { consistent: false };
279
+ }
280
+ }
281
+ /**
282
+ * Complete oneshot workflow that creates the SAME Gun user as normal approach
283
+ * This is the recommended method for oneshot signing with full consistency
284
+ */
285
+ async setupConsistentOneshotSigning(address) {
286
+ try {
287
+ const conn = this.assertMetaMask();
288
+ if (typeof conn.setupConsistentOneshotSigning === "function") {
289
+ return await conn.setupConsistentOneshotSigning(address);
290
+ }
291
+ // Fallback implementation when connector doesn't have the method
292
+ const credential = await this.createSigningCredential(address);
293
+ const authenticator = this.createAuthenticator(address);
294
+ const gunUser = await this.createGunUserFromSigningCredential(address);
295
+ return {
296
+ credential,
297
+ authenticator,
298
+ gunUser,
299
+ username: address,
300
+ password: "web3-generated-password",
301
+ };
302
+ }
303
+ catch (error) {
304
+ console.error(`Error setting up consistent Web3 oneshot signing: ${error.message}`);
305
+ throw error;
306
+ }
307
+ }
308
+ // === EXISTING METHODS ===
309
+ /**
310
+ * Login con Web3
311
+ * @param address - Indirizzo Ethereum
312
+ * @returns {Promise<AuthResult>} Risultato dell'autenticazione
313
+ * @description Autentica l'utente usando le credenziali del wallet Web3 dopo la verifica della firma
314
+ */
315
+ async login(address) {
316
+ try {
317
+ const core = this.assertInitialized();
318
+ if (!address) {
319
+ throw createError(ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Ethereum address required for Web3 login");
320
+ }
321
+ if (!this.isAvailable()) {
322
+ throw createError(ErrorType.ENVIRONMENT, "WEB3_UNAVAILABLE", "Web3 is not available in the browser");
323
+ }
324
+ console.log(`🔧 Web3 login - starting login for address:`, address);
325
+ // FIX: Use deterministic pair instead of generating new credentials
326
+ // Get the existing credential if available
327
+ const existingCredential = this.getSigningCredential(address);
328
+ if (existingCredential) {
329
+ console.log(`🔧 Web3 login - found existing credential, using it`);
330
+ // Use existing credential to get Gun user
331
+ const gunUser = await this.createGunUserFromSigningCredential(address);
332
+ console.log(`🔧 Web3 login - existing credential result:`, gunUser);
333
+ if (gunUser.success && gunUser.userPub) {
334
+ // Set authentication method to web3
335
+ core.setAuthMethod("web3");
336
+ const loginResult = {
337
+ success: true,
338
+ user: {
339
+ userPub: gunUser.userPub,
340
+ username: address,
341
+ },
342
+ userPub: gunUser.userPub,
343
+ };
344
+ console.log(`🔧 Web3 login - returning result:`, {
345
+ success: loginResult.success,
346
+ userPub: loginResult.userPub
347
+ ? loginResult.userPub.slice(0, 8) + "..."
348
+ : "null",
349
+ username: loginResult.user?.username,
350
+ });
351
+ // Emit login event
352
+ core.emit("auth:login", {
353
+ userPub: gunUser.userPub || "",
354
+ username: address,
355
+ method: "web3",
356
+ });
357
+ return loginResult;
358
+ }
359
+ }
360
+ // If no existing credential or it failed, create a new one (for first-time login)
361
+ console.log(`🔧 Web3 login - no existing credential, creating new one`);
362
+ // Use setupConsistentOneshotSigning for first-time login
363
+ const { gunUser } = await this.setupConsistentOneshotSigning(address);
364
+ console.log(`🔧 Web3 login - setupConsistentOneshotSigning result:`, {
365
+ gunUser,
366
+ address,
367
+ });
368
+ if (!gunUser.success) {
369
+ throw createError(ErrorType.AUTHENTICATION, "WEB3_LOGIN_FAILED", gunUser.error || "Failed to log in with Web3 credentials");
370
+ }
371
+ console.log(`🔧 Web3 login - gunUser success, userPub:`, gunUser.userPub ? gunUser.userPub.slice(0, 8) + "..." : "null");
372
+ // Set authentication method to web3
373
+ core.setAuthMethod("web3");
374
+ // Return success result
375
+ const loginResult = {
376
+ success: true,
377
+ user: {
378
+ userPub: gunUser.userPub,
379
+ username: address,
380
+ },
381
+ userPub: gunUser.userPub,
382
+ };
383
+ console.log(`🔧 Web3 login - returning result:`, {
384
+ success: loginResult.success,
385
+ userPub: loginResult.userPub
386
+ ? loginResult.userPub.slice(0, 8) + "..."
387
+ : "null",
388
+ username: loginResult.user?.username,
389
+ });
390
+ // Emit login event
391
+ core.emit("auth:login", {
392
+ userPub: gunUser.userPub || "",
393
+ username: address,
394
+ method: "web3",
395
+ });
396
+ return loginResult;
397
+ }
398
+ catch (error) {
399
+ // Handle both ShogunError and generic errors
400
+ const errorType = error?.type || ErrorType.AUTHENTICATION;
401
+ const errorCode = error?.code || "WEB3_LOGIN_ERROR";
402
+ const errorMessage = error?.message || "Unknown error during Web3 login";
403
+ ErrorHandler.handle(errorType, errorCode, errorMessage, error);
404
+ return { success: false, error: errorMessage };
405
+ }
406
+ }
407
+ /**
408
+ * Register new user with Web3 wallet
409
+ * @param address - Ethereum address
410
+ * @returns {Promise<SignUpResult>} Registration result
411
+ */
412
+ async signUp(address) {
413
+ try {
414
+ const core = this.assertInitialized();
415
+ if (!address) {
416
+ throw createError(ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Ethereum address required for Web3 registration");
417
+ }
418
+ if (!this.isAvailable()) {
419
+ throw createError(ErrorType.ENVIRONMENT, "WEB3_UNAVAILABLE", "Web3 is not available in the browser");
420
+ }
421
+ // Use setupConsistentOneshotSigning for signup
422
+ const { gunUser } = await this.setupConsistentOneshotSigning(address);
423
+ if (!gunUser.success) {
424
+ throw createError(ErrorType.AUTHENTICATION, "WEB3_SIGNUP_FAILED", gunUser.error || "Failed to sign up with Web3 credentials");
425
+ }
426
+ // Set authentication method to web3
427
+ core.setAuthMethod("web3");
428
+ // Return success result
429
+ const signupResult = {
430
+ success: true,
431
+ user: {
432
+ userPub: gunUser.userPub,
433
+ username: address,
434
+ },
435
+ userPub: gunUser.userPub,
436
+ };
437
+ return signupResult;
438
+ }
439
+ catch (error) {
440
+ // Handle both ShogunError and generic errors
441
+ const errorType = error?.type || ErrorType.AUTHENTICATION;
442
+ const errorCode = error?.code || "WEB3_SIGNUP_ERROR";
443
+ const errorMessage = error?.message || "Unknown error during Web3 registration";
444
+ ErrorHandler.handle(errorType, errorCode, errorMessage, error);
445
+ return { success: false, error: errorMessage };
446
+ }
447
+ }
448
+ }