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,446 +0,0 @@
1
- import { BasePlugin } from "../base";
2
- import { NostrConnector, MESSAGE_TO_SIGN, deriveNostrKeys, } from "./nostrConnector";
3
- import { NostrSigner } from "./nostrSigner";
4
- import { ErrorHandler, ErrorType, createError } from "../../utils/errorHandler";
5
- /**
6
- * Plugin for managing Bitcoin wallet functionality in ShogunCore
7
- * Supports Alby, Nostr extensions, or direct key management
8
- */
9
- export class NostrConnectorPlugin extends BasePlugin {
10
- name = "nostr";
11
- version = "1.0.0";
12
- description = "Provides Bitcoin wallet connection and authentication for ShogunCore";
13
- bitcoinConnector = null;
14
- signer = null;
15
- /**
16
- * @inheritdoc
17
- */
18
- initialize(core) {
19
- super.initialize(core);
20
- // Initialize the Bitcoin wallet module
21
- this.bitcoinConnector = new NostrConnector();
22
- this.signer = new NostrSigner(this.bitcoinConnector);
23
- }
24
- /**
25
- * @inheritdoc
26
- */
27
- destroy() {
28
- if (this.bitcoinConnector) {
29
- this.bitcoinConnector.cleanup();
30
- }
31
- this.bitcoinConnector = null;
32
- this.signer = null;
33
- super.destroy();
34
- }
35
- /**
36
- * Ensure that the Bitcoin wallet module is initialized
37
- * @private
38
- */
39
- assertBitcoinConnector() {
40
- this.assertInitialized();
41
- if (!this.bitcoinConnector) {
42
- throw new Error("Bitcoin wallet module not initialized");
43
- }
44
- return this.bitcoinConnector;
45
- }
46
- /**
47
- * Assicura che il signer sia inizializzato
48
- * @private
49
- */
50
- assertSigner() {
51
- this.assertInitialized();
52
- if (!this.signer) {
53
- throw new Error("Nostr signer not initialized");
54
- }
55
- return this.signer;
56
- }
57
- /**
58
- * @inheritdoc
59
- */
60
- isAvailable() {
61
- return this.assertBitcoinConnector().isAvailable();
62
- }
63
- /**
64
- * Check if Nostr extension is available
65
- */
66
- isNostrExtensionAvailable() {
67
- return this.assertBitcoinConnector().isNostrExtensionAvailable();
68
- }
69
- /**
70
- * Connect to Nostr wallet automatically
71
- * This is a convenience method for easy wallet connection
72
- */
73
- async connectNostrWallet() {
74
- try {
75
- if (!this.isNostrExtensionAvailable()) {
76
- return {
77
- success: false,
78
- error: "Nostr extension not available. Please install a Nostr extension like nos2x, Alby, or Coracle.",
79
- };
80
- }
81
- const result = await this.connectBitcoinWallet("nostr");
82
- if (result.success) {
83
- }
84
- return result;
85
- }
86
- catch (error) {
87
- console.error("[nostrConnectorPlugin] Error connecting to Nostr wallet:", error);
88
- return {
89
- success: false,
90
- error: error.message || "Unknown error connecting to Nostr wallet",
91
- };
92
- }
93
- }
94
- /**
95
- * @inheritdoc
96
- */
97
- async connectBitcoinWallet(type = "nostr") {
98
- // Prioritize nostr over alby (since they are functionally identical)
99
- // If type is alby, try to use nostr instead
100
- if (type === "alby") {
101
- type = "nostr";
102
- }
103
- return this.assertBitcoinConnector().connectWallet(type);
104
- }
105
- /**
106
- * @inheritdoc
107
- */
108
- async generateCredentials(address, signature, message) {
109
- return this.assertBitcoinConnector().generateCredentials(address, signature, message);
110
- }
111
- /**
112
- * @inheritdoc
113
- */
114
- cleanup() {
115
- this.assertBitcoinConnector().cleanup();
116
- }
117
- /**
118
- * @inheritdoc
119
- */
120
- async verifySignature(message, signature, address) {
121
- return this.assertBitcoinConnector().verifySignature(message, signature, address);
122
- }
123
- /**
124
- * @inheritdoc
125
- */
126
- async generatePassword(signature) {
127
- return this.assertBitcoinConnector().generatePassword(signature);
128
- }
129
- // === NOSTR SIGNER METHODS ===
130
- /**
131
- * Creates a new Nostr signing credential
132
- * CONSISTENT with normal Nostr approach
133
- */
134
- async createSigningCredential(address) {
135
- try {
136
- const conn = this.assertBitcoinConnector();
137
- if (typeof conn.createSigningCredential === "function") {
138
- return await conn.createSigningCredential(address);
139
- }
140
- return await this.assertSigner().createSigningCredential(address);
141
- }
142
- catch (error) {
143
- console.error(`Error creating Nostr signing credential: ${error.message}`);
144
- throw error;
145
- }
146
- }
147
- /**
148
- * Creates an authenticator function for Nostr signing
149
- */
150
- createAuthenticator(address) {
151
- try {
152
- const conn = this.assertBitcoinConnector();
153
- if (typeof conn.createAuthenticator === "function") {
154
- return conn.createAuthenticator(address);
155
- }
156
- return this.assertSigner().createAuthenticator(address);
157
- }
158
- catch (error) {
159
- console.error(`Error creating Nostr authenticator: ${error.message}`);
160
- throw error;
161
- }
162
- }
163
- /**
164
- * Creates a derived key pair from Nostr credential
165
- */
166
- async createDerivedKeyPair(address, extra) {
167
- try {
168
- const conn = this.assertBitcoinConnector();
169
- if (typeof conn.createDerivedKeyPair === "function") {
170
- return await conn.createDerivedKeyPair(address, extra);
171
- }
172
- return await this.assertSigner().createDerivedKeyPair(address, extra);
173
- }
174
- catch (error) {
175
- console.error(`Error creating derived key pair: ${error.message}`);
176
- throw error;
177
- }
178
- }
179
- /**
180
- * Signs data with derived keys after Nostr verification
181
- */
182
- async signWithDerivedKeys(data, address, extra) {
183
- try {
184
- const conn = this.assertBitcoinConnector();
185
- if (typeof conn.signWithDerivedKeys === "function") {
186
- return await conn.signWithDerivedKeys(data, address, extra);
187
- }
188
- return await this.assertSigner().signWithDerivedKeys(data, address, extra);
189
- }
190
- catch (error) {
191
- console.error(`Error signing with derived keys: ${error.message}`);
192
- throw error;
193
- }
194
- }
195
- /**
196
- * Get signing credential by address
197
- */
198
- getSigningCredential(address) {
199
- const conn = this.assertBitcoinConnector();
200
- if (typeof conn.getSigningCredential === "function") {
201
- return conn.getSigningCredential(address);
202
- }
203
- return this.assertSigner().getCredential(address);
204
- }
205
- /**
206
- * List all signing credentials
207
- */
208
- listSigningCredentials() {
209
- const conn = this.assertBitcoinConnector();
210
- if (typeof conn.listSigningCredentials === "function") {
211
- return conn.listSigningCredentials();
212
- }
213
- return this.assertSigner().listCredentials();
214
- }
215
- /**
216
- * Remove a signing credential
217
- */
218
- removeSigningCredential(address) {
219
- const conn = this.assertBitcoinConnector();
220
- if (typeof conn.removeSigningCredential === "function") {
221
- return conn.removeSigningCredential(address);
222
- }
223
- return this.assertSigner().removeCredential(address);
224
- }
225
- // === CONSISTENCY METHODS ===
226
- /**
227
- * Creates a Gun user from Nostr signing credential
228
- * This ensures the SAME user is created as with normal approach
229
- */
230
- async createGunUserFromSigningCredential(address) {
231
- try {
232
- const conn = this.assertBitcoinConnector();
233
- if (typeof conn.createGunUserFromSigningCredential === "function") {
234
- return await conn.createGunUserFromSigningCredential(address);
235
- }
236
- const core = this.assertInitialized();
237
- return await this.assertSigner().createGunUser(address, core.gun);
238
- }
239
- catch (error) {
240
- console.error(`Error creating Gun user from Nostr signing credential: ${error.message}`);
241
- throw error;
242
- }
243
- }
244
- /**
245
- * Get the Gun user public key for a signing credential
246
- */
247
- getGunUserPubFromSigningCredential(address) {
248
- const conn = this.assertBitcoinConnector();
249
- if (typeof conn.getGunUserPubFromSigningCredential === "function") {
250
- return conn.getGunUserPubFromSigningCredential(address);
251
- }
252
- return this.assertSigner().getGunUserPub(address);
253
- }
254
- /**
255
- * Get the password (for consistency checking)
256
- */
257
- getPassword(address) {
258
- const conn = this.assertBitcoinConnector();
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.assertBitcoinConnector();
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 Nostr 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.assertBitcoinConnector();
288
- if (typeof conn.setupConsistentOneshotSigning === "function") {
289
- return await conn.setupConsistentOneshotSigning(address);
290
- }
291
- const credential = await this.createSigningCredential(address);
292
- const authenticator = this.createAuthenticator(address);
293
- const gunUser = await this.createGunUserFromSigningCredential(address);
294
- return {
295
- credential,
296
- authenticator,
297
- gunUser,
298
- username: credential.username,
299
- password: credential.password,
300
- };
301
- }
302
- catch (error) {
303
- console.error(`Error setting up consistent Nostr oneshot signing: ${error.message}`);
304
- throw error;
305
- }
306
- }
307
- // === EXISTING METHODS ===
308
- /**
309
- * Login with Bitcoin wallet
310
- * @param address - Bitcoin address
311
- * @returns {Promise<AuthResult>} Authentication result
312
- * @description Authenticates the user using Bitcoin wallet credentials after signature verification
313
- */
314
- async login(address) {
315
- try {
316
- const core = this.assertInitialized();
317
- if (!address) {
318
- throw createError(ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Bitcoin address required for login");
319
- }
320
- if (!this.isAvailable()) {
321
- throw createError(ErrorType.ENVIRONMENT, "BITCOIN_WALLET_UNAVAILABLE", "No Bitcoin wallet available in the browser");
322
- }
323
- const message = MESSAGE_TO_SIGN;
324
- const signature = await this.assertBitcoinConnector().requestSignature(address, message);
325
- const credentials = await this.generateCredentials(address, signature, message);
326
- if (!credentials?.username ||
327
- !credentials?.key ||
328
- !credentials.message ||
329
- !credentials.signature) {
330
- throw createError(ErrorType.AUTHENTICATION, "CREDENTIAL_GENERATION_FAILED", "Bitcoin wallet credentials not generated correctly or signature missing");
331
- }
332
- const isValid = await this.verifySignature(credentials.message, credentials.signature, address);
333
- if (!isValid) {
334
- console.error(`Signature verification failed for address: ${address}`);
335
- throw createError(ErrorType.SECURITY, "SIGNATURE_VERIFICATION_FAILED", "Bitcoin wallet signature verification failed");
336
- }
337
- // Deriva le chiavi da address, signature, message
338
- const k = await deriveNostrKeys(address, signature, message);
339
- // Set authentication method to nostr before login
340
- core.setAuthMethod("nostr");
341
- // Usa le chiavi derivate per login
342
- const loginResult = await core.login(credentials.username, "", k);
343
- if (!loginResult.success) {
344
- throw createError(ErrorType.AUTHENTICATION, "BITCOIN_LOGIN_FAILED", loginResult.error || "Failed to log in with Bitcoin credentials");
345
- }
346
- // Emit login event
347
- core.emit("auth:login", {
348
- userPub: loginResult.userPub || "",
349
- username: credentials.username,
350
- method: "bitcoin",
351
- });
352
- return loginResult;
353
- }
354
- catch (error) {
355
- // Handle both ShogunError and generic errors
356
- const errorType = error?.type || ErrorType.AUTHENTICATION;
357
- const errorCode = error?.code || "BITCOIN_LOGIN_ERROR";
358
- const errorMessage = error?.message || "Unknown error during Bitcoin wallet login";
359
- ErrorHandler.handle(errorType, errorCode, errorMessage, error);
360
- return { success: false, error: errorMessage };
361
- }
362
- }
363
- /**
364
- * Register new user with Nostr wallet
365
- * @param address - Nostr address
366
- * @returns {Promise<SignUpResult>} Registration result
367
- */
368
- async signUp(address) {
369
- try {
370
- const core = this.assertInitialized();
371
- if (!address) {
372
- throw createError(ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Bitcoin address required for signup");
373
- }
374
- if (!this.isAvailable()) {
375
- throw createError(ErrorType.ENVIRONMENT, "BITCOIN_WALLET_UNAVAILABLE", "No Bitcoin wallet available in the browser");
376
- }
377
- const message = MESSAGE_TO_SIGN;
378
- const signature = await this.assertBitcoinConnector().requestSignature(address, message);
379
- const credentials = await this.generateCredentials(address, signature, message);
380
- if (!credentials?.username ||
381
- !credentials?.key ||
382
- !credentials.message ||
383
- !credentials.signature) {
384
- throw createError(ErrorType.AUTHENTICATION, "CREDENTIAL_GENERATION_FAILED", "Bitcoin wallet credentials not generated correctly or signature missing");
385
- }
386
- // Verify signature
387
- const isValid = await this.verifySignature(credentials.message, credentials.signature, address);
388
- if (!isValid) {
389
- console.error(`Signature verification failed for address: ${address}`);
390
- throw createError(ErrorType.SECURITY, "SIGNATURE_VERIFICATION_FAILED", "Bitcoin wallet signature verification failed");
391
- }
392
- // Deriva le chiavi da address, signature, message
393
- const k = await deriveNostrKeys(address, signature, message);
394
- // Set authentication method to nostr before signup
395
- core.setAuthMethod("nostr");
396
- // Usa le chiavi derivate per signup
397
- const signupResult = await core.signUp(credentials.username, undefined, k);
398
- if (signupResult.success) {
399
- // Dopo la creazione, autentica subito
400
- const authResult = await core.login(credentials.username, "", k);
401
- if (authResult.success) {
402
- console.log(`Bitcoin wallet registration and login completed for user: ${credentials.username}`);
403
- // Emetti eventi
404
- core.emit("auth:signup", {
405
- userPub: authResult.userPub || "",
406
- username: credentials.username,
407
- method: "bitcoin",
408
- });
409
- return { ...authResult };
410
- }
411
- else {
412
- return { ...signupResult, error: "User created but login failed" };
413
- }
414
- }
415
- else {
416
- // Se l'errore è che l'utente esiste già, prova direttamente l'auth
417
- if (signupResult.error &&
418
- signupResult.error.toLowerCase().includes("exist")) {
419
- const authResult = await core.login(credentials.username, "", k);
420
- return { ...authResult };
421
- }
422
- return signupResult;
423
- }
424
- }
425
- catch (error) {
426
- // Handle both ShogunError and generic errors
427
- const errorType = error?.type || ErrorType.AUTHENTICATION;
428
- const errorCode = error?.code || "BITCOIN_SIGNUP_ERROR";
429
- const errorMessage = error?.message || "Unknown error during Bitcoin wallet signup";
430
- ErrorHandler.handle(errorType, errorCode, errorMessage, error);
431
- return { success: false, error: errorMessage };
432
- }
433
- }
434
- /**
435
- * Convenience method that matches the interface pattern
436
- */
437
- async loginWithBitcoinWallet(address) {
438
- return this.login(address);
439
- }
440
- /**
441
- * Convenience method that matches the interface pattern
442
- */
443
- async signUpWithBitcoinWallet(address) {
444
- return this.signUp(address);
445
- }
446
- }