shogun-core 2.0.0 → 2.0.3

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 (43) hide show
  1. package/dist/browser/shogun-core.js +74126 -91893
  2. package/dist/browser/shogun-core.js.map +1 -1
  3. package/dist/core.js +32 -36
  4. package/dist/gundb/crypto.js +17 -32
  5. package/dist/gundb/db.js +35 -82
  6. package/dist/gundb/derive.js +16 -19
  7. package/dist/gundb/errors.js +7 -17
  8. package/dist/gundb/index.js +1 -17
  9. package/dist/gundb/restricted-put.js +5 -11
  10. package/dist/gundb/rxjs.js +15 -19
  11. package/dist/gundb/types.js +1 -2
  12. package/dist/index.js +9 -34
  13. package/dist/plugins/base.js +2 -6
  14. package/dist/plugins/index.js +10 -36
  15. package/dist/plugins/nostr/index.js +4 -20
  16. package/dist/plugins/nostr/nostrConnector.js +22 -29
  17. package/dist/plugins/nostr/nostrConnectorPlugin.js +24 -28
  18. package/dist/plugins/nostr/nostrSigner.js +8 -15
  19. package/dist/plugins/nostr/types.js +1 -2
  20. package/dist/plugins/oauth/index.js +2 -7
  21. package/dist/plugins/oauth/oauthConnector.js +9 -16
  22. package/dist/plugins/oauth/oauthPlugin.js +21 -25
  23. package/dist/plugins/oauth/types.js +1 -2
  24. package/dist/plugins/web3/index.js +4 -20
  25. package/dist/plugins/web3/types.js +1 -2
  26. package/dist/plugins/web3/web3Connector.js +21 -27
  27. package/dist/plugins/web3/web3ConnectorPlugin.js +17 -21
  28. package/dist/plugins/web3/web3Signer.js +15 -22
  29. package/dist/plugins/webauthn/index.js +3 -19
  30. package/dist/plugins/webauthn/types.js +2 -5
  31. package/dist/plugins/webauthn/webauthn.js +21 -29
  32. package/dist/plugins/webauthn/webauthnPlugin.js +9 -13
  33. package/dist/plugins/webauthn/webauthnSigner.js +12 -19
  34. package/dist/storage/storage.js +1 -5
  35. package/dist/types/common.js +1 -2
  36. package/dist/types/events.js +2 -6
  37. package/dist/types/gundb/db.d.ts +2 -0
  38. package/dist/types/plugin.js +1 -2
  39. package/dist/types/shogun.js +4 -7
  40. package/dist/utils/errorHandler.js +4 -9
  41. package/dist/utils/eventEmitter.js +1 -5
  42. package/dist/utils/validation.js +7 -14
  43. package/package.json +9 -11
@@ -1,15 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NostrConnectorPlugin = void 0;
4
- const base_1 = require("../base");
5
- const nostrConnector_1 = require("./nostrConnector");
6
- const nostrSigner_1 = require("./nostrSigner");
7
- const errorHandler_1 = require("../../utils/errorHandler");
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";
8
5
  /**
9
6
  * Plugin for managing Bitcoin wallet functionality in ShogunCore
10
7
  * Supports Alby, Nostr extensions, or direct key management
11
8
  */
12
- class NostrConnectorPlugin extends base_1.BasePlugin {
9
+ export class NostrConnectorPlugin extends BasePlugin {
13
10
  name = "nostr";
14
11
  version = "1.0.0";
15
12
  description = "Provides Bitcoin wallet connection and authentication for ShogunCore";
@@ -21,8 +18,8 @@ class NostrConnectorPlugin extends base_1.BasePlugin {
21
18
  initialize(core) {
22
19
  super.initialize(core);
23
20
  // Initialize the Bitcoin wallet module
24
- this.bitcoinConnector = new nostrConnector_1.NostrConnector();
25
- this.signer = new nostrSigner_1.NostrSigner(this.bitcoinConnector);
21
+ this.bitcoinConnector = new NostrConnector();
22
+ this.signer = new NostrSigner(this.bitcoinConnector);
26
23
  }
27
24
  /**
28
25
  * @inheritdoc
@@ -318,33 +315,33 @@ class NostrConnectorPlugin extends base_1.BasePlugin {
318
315
  try {
319
316
  const core = this.assertInitialized();
320
317
  if (!address) {
321
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Bitcoin address required for login");
318
+ throw createError(ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Bitcoin address required for login");
322
319
  }
323
320
  if (!this.isAvailable()) {
324
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENVIRONMENT, "BITCOIN_WALLET_UNAVAILABLE", "No Bitcoin wallet available in the browser");
321
+ throw createError(ErrorType.ENVIRONMENT, "BITCOIN_WALLET_UNAVAILABLE", "No Bitcoin wallet available in the browser");
325
322
  }
326
- const message = nostrConnector_1.MESSAGE_TO_SIGN;
323
+ const message = MESSAGE_TO_SIGN;
327
324
  const signature = await this.assertBitcoinConnector().requestSignature(address, message);
328
325
  const credentials = await this.generateCredentials(address, signature, message);
329
326
  if (!credentials?.username ||
330
327
  !credentials?.key ||
331
328
  !credentials.message ||
332
329
  !credentials.signature) {
333
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.AUTHENTICATION, "CREDENTIAL_GENERATION_FAILED", "Bitcoin wallet credentials not generated correctly or signature missing");
330
+ throw createError(ErrorType.AUTHENTICATION, "CREDENTIAL_GENERATION_FAILED", "Bitcoin wallet credentials not generated correctly or signature missing");
334
331
  }
335
332
  const isValid = await this.verifySignature(credentials.message, credentials.signature, address);
336
333
  if (!isValid) {
337
334
  console.error(`Signature verification failed for address: ${address}`);
338
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.SECURITY, "SIGNATURE_VERIFICATION_FAILED", "Bitcoin wallet signature verification failed");
335
+ throw createError(ErrorType.SECURITY, "SIGNATURE_VERIFICATION_FAILED", "Bitcoin wallet signature verification failed");
339
336
  }
340
337
  // Deriva le chiavi da address, signature, message
341
- const k = await (0, nostrConnector_1.deriveNostrKeys)(address, signature, message);
338
+ const k = await deriveNostrKeys(address, signature, message);
342
339
  // Set authentication method to nostr before login
343
340
  core.setAuthMethod("nostr");
344
341
  // Usa le chiavi derivate per login
345
342
  const loginResult = await core.login(credentials.username, "", k);
346
343
  if (!loginResult.success) {
347
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.AUTHENTICATION, "BITCOIN_LOGIN_FAILED", loginResult.error || "Failed to log in with Bitcoin credentials");
344
+ throw createError(ErrorType.AUTHENTICATION, "BITCOIN_LOGIN_FAILED", loginResult.error || "Failed to log in with Bitcoin credentials");
348
345
  }
349
346
  // Emit login event
350
347
  core.emit("auth:login", {
@@ -356,10 +353,10 @@ class NostrConnectorPlugin extends base_1.BasePlugin {
356
353
  }
357
354
  catch (error) {
358
355
  // Handle both ShogunError and generic errors
359
- const errorType = error?.type || errorHandler_1.ErrorType.AUTHENTICATION;
356
+ const errorType = error?.type || ErrorType.AUTHENTICATION;
360
357
  const errorCode = error?.code || "BITCOIN_LOGIN_ERROR";
361
358
  const errorMessage = error?.message || "Unknown error during Bitcoin wallet login";
362
- errorHandler_1.ErrorHandler.handle(errorType, errorCode, errorMessage, error);
359
+ ErrorHandler.handle(errorType, errorCode, errorMessage, error);
363
360
  return { success: false, error: errorMessage };
364
361
  }
365
362
  }
@@ -372,28 +369,28 @@ class NostrConnectorPlugin extends base_1.BasePlugin {
372
369
  try {
373
370
  const core = this.assertInitialized();
374
371
  if (!address) {
375
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Bitcoin address required for signup");
372
+ throw createError(ErrorType.VALIDATION, "ADDRESS_REQUIRED", "Bitcoin address required for signup");
376
373
  }
377
374
  if (!this.isAvailable()) {
378
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENVIRONMENT, "BITCOIN_WALLET_UNAVAILABLE", "No Bitcoin wallet available in the browser");
375
+ throw createError(ErrorType.ENVIRONMENT, "BITCOIN_WALLET_UNAVAILABLE", "No Bitcoin wallet available in the browser");
379
376
  }
380
- const message = nostrConnector_1.MESSAGE_TO_SIGN;
377
+ const message = MESSAGE_TO_SIGN;
381
378
  const signature = await this.assertBitcoinConnector().requestSignature(address, message);
382
379
  const credentials = await this.generateCredentials(address, signature, message);
383
380
  if (!credentials?.username ||
384
381
  !credentials?.key ||
385
382
  !credentials.message ||
386
383
  !credentials.signature) {
387
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.AUTHENTICATION, "CREDENTIAL_GENERATION_FAILED", "Bitcoin wallet credentials not generated correctly or signature missing");
384
+ throw createError(ErrorType.AUTHENTICATION, "CREDENTIAL_GENERATION_FAILED", "Bitcoin wallet credentials not generated correctly or signature missing");
388
385
  }
389
386
  // Verify signature
390
387
  const isValid = await this.verifySignature(credentials.message, credentials.signature, address);
391
388
  if (!isValid) {
392
389
  console.error(`Signature verification failed for address: ${address}`);
393
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.SECURITY, "SIGNATURE_VERIFICATION_FAILED", "Bitcoin wallet signature verification failed");
390
+ throw createError(ErrorType.SECURITY, "SIGNATURE_VERIFICATION_FAILED", "Bitcoin wallet signature verification failed");
394
391
  }
395
392
  // Deriva le chiavi da address, signature, message
396
- const k = await (0, nostrConnector_1.deriveNostrKeys)(address, signature, message);
393
+ const k = await deriveNostrKeys(address, signature, message);
397
394
  // Set authentication method to nostr before signup
398
395
  core.setAuthMethod("nostr");
399
396
  // Usa le chiavi derivate per signup
@@ -427,10 +424,10 @@ class NostrConnectorPlugin extends base_1.BasePlugin {
427
424
  }
428
425
  catch (error) {
429
426
  // Handle both ShogunError and generic errors
430
- const errorType = error?.type || errorHandler_1.ErrorType.AUTHENTICATION;
427
+ const errorType = error?.type || ErrorType.AUTHENTICATION;
431
428
  const errorCode = error?.code || "BITCOIN_SIGNUP_ERROR";
432
429
  const errorMessage = error?.message || "Unknown error during Bitcoin wallet signup";
433
- errorHandler_1.ErrorHandler.handle(errorType, errorCode, errorMessage, error);
430
+ ErrorHandler.handle(errorType, errorCode, errorMessage, error);
434
431
  return { success: false, error: errorMessage };
435
432
  }
436
433
  }
@@ -447,4 +444,3 @@ class NostrConnectorPlugin extends base_1.BasePlugin {
447
444
  return this.signUp(address);
448
445
  }
449
446
  }
450
- exports.NostrConnectorPlugin = NostrConnectorPlugin;
@@ -1,23 +1,17 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.NostrSigner = void 0;
7
- const nostrConnector_1 = require("./nostrConnector");
8
- const derive_1 = __importDefault(require("../../gundb/derive"));
9
- const ethers_1 = require("ethers");
1
+ import { NostrConnector } from "./nostrConnector";
2
+ import derive from "../../gundb/derive";
3
+ import { ethers } from "ethers";
10
4
  /**
11
5
  * Nostr Signer - Provides oneshot signing functionality
12
6
  * Similar to webauthn.js but for Nostr/Bitcoin wallets
13
7
  * CONSISTENT with normal Nostr approach
14
8
  */
15
- class NostrSigner {
9
+ export class NostrSigner {
16
10
  nostrConnector;
17
11
  credentials = new Map();
18
12
  MESSAGE_TO_SIGN = "I Love Shogun!"; // Same as normal approach
19
13
  constructor(nostrConnector) {
20
- this.nostrConnector = nostrConnector || new nostrConnector_1.NostrConnector();
14
+ this.nostrConnector = nostrConnector || new NostrConnector();
21
15
  }
22
16
  /**
23
17
  * Creates a new Nostr signing credential
@@ -118,7 +112,7 @@ class NostrSigner {
118
112
  try {
119
113
  // SAME LOGIC as NostrConnector.generatePassword
120
114
  const normalizedSig = signature.toLowerCase().replace(/[^a-f0-9]/g, "");
121
- const passwordHash = ethers_1.ethers.sha256(ethers_1.ethers.toUtf8Bytes(normalizedSig));
115
+ const passwordHash = ethers.sha256(ethers.toUtf8Bytes(normalizedSig));
122
116
  return passwordHash;
123
117
  }
124
118
  catch (error) {
@@ -170,7 +164,7 @@ class NostrSigner {
170
164
  try {
171
165
  // CONSISTENCY: Use the same approach as normal Nostr
172
166
  // Use password as seed (same as normal approach)
173
- const derivedKeys = await (0, derive_1.default)(credential.password, // This is the key consistency point!
167
+ const derivedKeys = await derive(credential.password, // This is the key consistency point!
174
168
  extra, { includeP256: true });
175
169
  return {
176
170
  pub: derivedKeys.pub,
@@ -316,5 +310,4 @@ class NostrSigner {
316
310
  return this.credentials.delete(address.toLowerCase());
317
311
  }
318
312
  }
319
- exports.NostrSigner = NostrSigner;
320
- exports.default = NostrSigner;
313
+ export default NostrSigner;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,8 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OAuthPlugin = exports.OAuthConnector = void 0;
4
1
  // OAuth plugin exports
5
- var oauthConnector_1 = require("./oauthConnector");
6
- Object.defineProperty(exports, "OAuthConnector", { enumerable: true, get: function () { return oauthConnector_1.OAuthConnector; } });
7
- var oauthPlugin_1 = require("./oauthPlugin");
8
- Object.defineProperty(exports, "OAuthPlugin", { enumerable: true, get: function () { return oauthPlugin_1.OAuthPlugin; } });
2
+ export { OAuthConnector } from "./oauthConnector";
3
+ export { OAuthPlugin } from "./oauthPlugin";
@@ -1,20 +1,14 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.OAuthConnector = void 0;
7
1
  /**
8
2
  * OAuth Connector - Secure version for GunDB user creation
9
3
  */
10
- const eventEmitter_1 = require("../../utils/eventEmitter");
11
- const derive_1 = __importDefault(require("../../gundb/derive"));
12
- const validation_1 = require("../../utils/validation");
13
- const ethers_1 = require("ethers");
4
+ import { EventEmitter } from "../../utils/eventEmitter";
5
+ import derive from "../../gundb/derive";
6
+ import { generateUsernameFromIdentity, generateDeterministicPassword, } from "../../utils/validation";
7
+ import { ethers } from "ethers";
14
8
  /**
15
9
  * OAuth Connector
16
10
  */
17
- class OAuthConnector extends eventEmitter_1.EventEmitter {
11
+ export class OAuthConnector extends EventEmitter {
18
12
  DEFAULT_CONFIG = {
19
13
  providers: {
20
14
  google: {
@@ -394,15 +388,15 @@ class OAuthConnector extends eventEmitter_1.EventEmitter {
394
388
  throw new Error(`Provider ${provider} is not configured.`);
395
389
  }
396
390
  // Username uniforme
397
- const username = (0, validation_1.generateUsernameFromIdentity)(provider, userInfo);
391
+ const username = generateUsernameFromIdentity(provider, userInfo);
398
392
  try {
399
393
  console.log(`Generating credentials for ${provider} user: ${userInfo.id}`);
400
394
  const saltData = `${userInfo.id}_${provider}_${userInfo.email || "no-email"}`;
401
- const salt = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(saltData));
395
+ const salt = ethers.keccak256(ethers.toUtf8Bytes(saltData));
402
396
  // Password deterministica (compatibilità)
403
- const password = (0, validation_1.generateDeterministicPassword)(salt);
397
+ const password = generateDeterministicPassword(salt);
404
398
  // Deriva la chiave GunDB
405
- const key = await (0, derive_1.default)(password, salt, { includeP256: true });
399
+ const key = await derive(password, salt, { includeP256: true });
406
400
  const credentials = {
407
401
  username,
408
402
  password,
@@ -757,4 +751,3 @@ class OAuthConnector extends eventEmitter_1.EventEmitter {
757
751
  }
758
752
  }
759
753
  }
760
- exports.OAuthConnector = OAuthConnector;
@@ -1,15 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OAuthPlugin = void 0;
4
- const base_1 = require("../base");
5
- const oauthConnector_1 = require("./oauthConnector");
6
- const errorHandler_1 = require("../../utils/errorHandler");
7
- const storage_1 = require("../../storage/storage");
1
+ import { BasePlugin } from "../base";
2
+ import { OAuthConnector } from "./oauthConnector";
3
+ import { ErrorHandler, ErrorType, createError } from "../../utils/errorHandler";
4
+ import { ShogunStorage } from "../../storage/storage";
8
5
  /**
9
6
  * OAuth Plugin for ShogunCore
10
7
  * Provides authentication with external OAuth providers
11
8
  */
12
- class OAuthPlugin extends base_1.BasePlugin {
9
+ export class OAuthPlugin extends BasePlugin {
13
10
  name = "oauth";
14
11
  version = "1.0.0";
15
12
  description = "Provides OAuth authentication with external providers for ShogunCore";
@@ -31,9 +28,9 @@ class OAuthPlugin extends base_1.BasePlugin {
31
28
  */
32
29
  initialize(core) {
33
30
  this.core = core;
34
- this.storage = new storage_1.ShogunStorage();
31
+ this.storage = new ShogunStorage();
35
32
  // Inizializziamo il connector OAuth con la configurazione già presente
36
- this.oauthConnector = new oauthConnector_1.OAuthConnector(this.config);
33
+ this.oauthConnector = new OAuthConnector(this.config);
37
34
  // Valida la configurazione di sicurezza dopo l'inizializzazione
38
35
  this.validateOAuthSecurity();
39
36
  }
@@ -78,7 +75,7 @@ class OAuthPlugin extends base_1.BasePlugin {
78
75
  this.config = { ...this.config, ...config, providers: mergedProviders };
79
76
  // Inizializza il connector se non è già stato fatto
80
77
  if (!this.oauthConnector) {
81
- this.oauthConnector = new oauthConnector_1.OAuthConnector(this.config);
78
+ this.oauthConnector = new OAuthConnector(this.config);
82
79
  }
83
80
  else {
84
81
  // Update connector configuration se già inizializzato
@@ -88,7 +85,7 @@ class OAuthPlugin extends base_1.BasePlugin {
88
85
  }
89
86
  else {
90
87
  // Fallback: recreate connector
91
- this.oauthConnector = new oauthConnector_1.OAuthConnector(this.config);
88
+ this.oauthConnector = new OAuthConnector(this.config);
92
89
  }
93
90
  }
94
91
  // Validate security settings
@@ -180,20 +177,20 @@ class OAuthPlugin extends base_1.BasePlugin {
180
177
  try {
181
178
  const core = this.assertInitialized();
182
179
  if (!provider) {
183
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "PROVIDER_REQUIRED", "OAuth provider required for OAuth login");
180
+ throw createError(ErrorType.VALIDATION, "PROVIDER_REQUIRED", "OAuth provider required for OAuth login");
184
181
  }
185
182
  if (!this.isSupported()) {
186
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENVIRONMENT, "OAUTH_UNAVAILABLE", "OAuth is not supported in this environment");
183
+ throw createError(ErrorType.ENVIRONMENT, "OAUTH_UNAVAILABLE", "OAuth is not supported in this environment");
187
184
  }
188
185
  // Check if provider is available
189
186
  const availableProviders = this.getAvailableProviders();
190
187
  if (!availableProviders.includes(provider)) {
191
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "PROVIDER_NOT_CONFIGURED", `Provider ${provider} is not configured or available`);
188
+ throw createError(ErrorType.VALIDATION, "PROVIDER_NOT_CONFIGURED", `Provider ${provider} is not configured or available`);
192
189
  }
193
190
  // Initiate OAuth flow with the provider
194
191
  const oauthResult = await this.initiateOAuth(provider);
195
192
  if (!oauthResult.success) {
196
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.AUTHENTICATION, "OAUTH_INITIATION_FAILED", oauthResult.error || "Failed to initiate OAuth flow");
193
+ throw createError(ErrorType.AUTHENTICATION, "OAUTH_INITIATION_FAILED", oauthResult.error || "Failed to initiate OAuth flow");
197
194
  }
198
195
  // In a browser environment, this would redirect to the OAuth provider
199
196
  // The frontend should handle the redirect and then call handleOAuthCallback
@@ -210,10 +207,10 @@ class OAuthPlugin extends base_1.BasePlugin {
210
207
  }
211
208
  catch (error) {
212
209
  // Handle both ShogunError and generic errors
213
- const errorType = error?.type || errorHandler_1.ErrorType.AUTHENTICATION;
210
+ const errorType = error?.type || ErrorType.AUTHENTICATION;
214
211
  const errorCode = error?.code || "OAUTH_LOGIN_ERROR";
215
212
  const errorMessage = error?.message || "Unknown error during OAuth login";
216
- errorHandler_1.ErrorHandler.handle(errorType, errorCode, errorMessage, error);
213
+ ErrorHandler.handle(errorType, errorCode, errorMessage, error);
217
214
  return { success: false, error: errorMessage };
218
215
  }
219
216
  }
@@ -226,20 +223,20 @@ class OAuthPlugin extends base_1.BasePlugin {
226
223
  try {
227
224
  const core = this.assertInitialized();
228
225
  if (!provider) {
229
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "PROVIDER_REQUIRED", "OAuth provider required for OAuth signup");
226
+ throw createError(ErrorType.VALIDATION, "PROVIDER_REQUIRED", "OAuth provider required for OAuth signup");
230
227
  }
231
228
  if (!this.isSupported()) {
232
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENVIRONMENT, "OAUTH_UNAVAILABLE", "OAuth is not supported in this environment");
229
+ throw createError(ErrorType.ENVIRONMENT, "OAUTH_UNAVAILABLE", "OAuth is not supported in this environment");
233
230
  }
234
231
  // Check if provider is available
235
232
  const availableProviders = this.getAvailableProviders();
236
233
  if (!availableProviders.includes(provider)) {
237
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "PROVIDER_NOT_CONFIGURED", `Provider ${provider} is not configured or available`);
234
+ throw createError(ErrorType.VALIDATION, "PROVIDER_NOT_CONFIGURED", `Provider ${provider} is not configured or available`);
238
235
  }
239
236
  // Initiate OAuth flow with the provider
240
237
  const oauthResult = await this.initiateOAuth(provider);
241
238
  if (!oauthResult.success) {
242
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.AUTHENTICATION, "OAUTH_INITIATION_FAILED", oauthResult.error || "Failed to initiate OAuth flow");
239
+ throw createError(ErrorType.AUTHENTICATION, "OAUTH_INITIATION_FAILED", oauthResult.error || "Failed to initiate OAuth flow");
243
240
  }
244
241
  // In a browser environment, this would redirect to the OAuth provider
245
242
  // The frontend should handle the redirect and then call handleOAuthCallback
@@ -256,10 +253,10 @@ class OAuthPlugin extends base_1.BasePlugin {
256
253
  }
257
254
  catch (error) {
258
255
  // Handle both ShogunError and generic errors
259
- const errorType = error?.type || errorHandler_1.ErrorType.AUTHENTICATION;
256
+ const errorType = error?.type || ErrorType.AUTHENTICATION;
260
257
  const errorCode = error?.code || "OAUTH_SIGNUP_ERROR";
261
258
  const errorMessage = error?.message || "Unknown error during OAuth signup";
262
- errorHandler_1.ErrorHandler.handle(errorType, errorCode, errorMessage, error);
259
+ ErrorHandler.handle(errorType, errorCode, errorMessage, error);
263
260
  return { success: false, error: errorMessage };
264
261
  }
265
262
  }
@@ -397,4 +394,3 @@ class OAuthPlugin extends base_1.BasePlugin {
397
394
  return signupResult;
398
395
  }
399
396
  }
400
- exports.OAuthPlugin = OAuthPlugin;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,20 +1,4 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./web3ConnectorPlugin"), exports);
18
- __exportStar(require("./types"), exports);
19
- __exportStar(require("./web3Connector"), exports);
20
- __exportStar(require("./web3Signer"), exports);
1
+ export * from "./web3ConnectorPlugin";
2
+ export * from "./types";
3
+ export * from "./web3Connector";
4
+ export * from "./web3Signer";
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,20 +1,14 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Web3Connector = void 0;
7
1
  /**
8
2
  * The MetaMaskAuth class provides functionality for connecting, signing up, and logging in using MetaMask.
9
3
  */
10
- const ethers_1 = require("ethers");
11
- const errorHandler_1 = require("../../utils/errorHandler");
12
- const eventEmitter_1 = require("../../utils/eventEmitter");
13
- const derive_1 = __importDefault(require("../../gundb/derive"));
4
+ import { ethers } from "ethers";
5
+ import { ErrorHandler, ErrorType } from "../../utils/errorHandler";
6
+ import { EventEmitter } from "../../utils/eventEmitter";
7
+ import derive from "../../gundb/derive";
14
8
  /**
15
9
  * Class for MetaMask connection
16
10
  */
17
- class Web3Connector extends eventEmitter_1.EventEmitter {
11
+ class Web3Connector extends EventEmitter {
18
12
  MESSAGE_TO_SIGN = "I Love Shogun!";
19
13
  DEFAULT_CONFIG = {
20
14
  cacheDuration: 30 * 60 * 1000, // 30 minutes
@@ -43,7 +37,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
43
37
  // Check if ethereum is available from any provider
44
38
  const ethereumProvider = this.getAvailableEthereumProvider();
45
39
  if (ethereumProvider) {
46
- this.provider = new ethers_1.ethers.BrowserProvider(ethereumProvider);
40
+ this.provider = new ethers.BrowserProvider(ethereumProvider);
47
41
  }
48
42
  else {
49
43
  console.warn("No compatible Ethereum provider found");
@@ -129,7 +123,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
129
123
  // Check if ethereum is available from any provider
130
124
  const ethereumProvider = this.getAvailableEthereumProvider();
131
125
  if (ethereumProvider) {
132
- this.provider = new ethers_1.ethers.BrowserProvider(ethereumProvider);
126
+ this.provider = new ethers.BrowserProvider(ethereumProvider);
133
127
  }
134
128
  else {
135
129
  console.warn("No compatible Ethereum provider found");
@@ -220,13 +214,13 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
220
214
  }
221
215
  try {
222
216
  const normalizedAddress = String(address).trim().toLowerCase();
223
- if (!ethers_1.ethers.isAddress(normalizedAddress)) {
217
+ if (!ethers.isAddress(normalizedAddress)) {
224
218
  throw new Error("Invalid address format");
225
219
  }
226
- return ethers_1.ethers.getAddress(normalizedAddress);
220
+ return ethers.getAddress(normalizedAddress);
227
221
  }
228
222
  catch (error) {
229
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.VALIDATION, "INVALID_ADDRESS", "Invalid Ethereum address provided", error);
223
+ ErrorHandler.handle(ErrorType.VALIDATION, "INVALID_ADDRESS", "Invalid Ethereum address provided", error);
230
224
  throw error;
231
225
  }
232
226
  }
@@ -302,7 +296,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
302
296
  }
303
297
  catch (error) {
304
298
  console.error("Failed to connect to MetaMask:", error);
305
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "METAMASK_CONNECTION_ERROR", error.message ?? "Unknown error while connecting to MetaMask", error);
299
+ ErrorHandler.handle(ErrorType.WEBAUTHN, "METAMASK_CONNECTION_ERROR", error.message ?? "Unknown error while connecting to MetaMask", error);
306
300
  return { success: false, error: error.message };
307
301
  }
308
302
  }
@@ -332,7 +326,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
332
326
  return this.generateCredentialsFromSignature(validAddress, signature);
333
327
  }
334
328
  catch (error) {
335
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "CREDENTIALS_GENERATION_ERROR", error.message ?? "Error generating MetaMask credentials", error);
329
+ ErrorHandler.handle(ErrorType.WEBAUTHN, "CREDENTIALS_GENERATION_ERROR", error.message ?? "Error generating MetaMask credentials", error);
336
330
  throw error;
337
331
  }
338
332
  }
@@ -340,9 +334,9 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
340
334
  * Generates credentials from a signature
341
335
  */
342
336
  async generateCredentialsFromSignature(address, signature) {
343
- const hashedAddress = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(address));
337
+ const hashedAddress = ethers.keccak256(ethers.toUtf8Bytes(address));
344
338
  const salt = `${address}_${signature}`;
345
- return await (0, derive_1.default)(hashedAddress, salt, {
339
+ return await derive(hashedAddress, salt, {
346
340
  includeP256: true,
347
341
  });
348
342
  }
@@ -352,7 +346,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
352
346
  generateFallbackCredentials(address) {
353
347
  console.warn("Using fallback credentials generation for address:", address);
354
348
  // Generate a deterministic but insecure fallback
355
- const fallbackSignature = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(address + "fallback"));
349
+ const fallbackSignature = ethers.keccak256(ethers.toUtf8Bytes(address + "fallback"));
356
350
  return {
357
351
  username: address.toLowerCase(),
358
352
  password: fallbackSignature,
@@ -433,8 +427,8 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
433
427
  */
434
428
  setCustomProvider(rpcUrl, privateKey) {
435
429
  try {
436
- this.customProvider = new ethers_1.ethers.JsonRpcProvider(rpcUrl);
437
- this.customWallet = new ethers_1.ethers.Wallet(privateKey, this.customProvider);
430
+ this.customProvider = new ethers.JsonRpcProvider(rpcUrl);
431
+ this.customWallet = new ethers.Wallet(privateKey, this.customProvider);
438
432
  }
439
433
  catch (error) {
440
434
  throw new Error(`Error configuring provider: ${error.message ?? "Unknown error"}`);
@@ -482,7 +476,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
482
476
  if (!signature) {
483
477
  throw new Error("Invalid signature");
484
478
  }
485
- const hash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(signature));
479
+ const hash = ethers.keccak256(ethers.toUtf8Bytes(signature));
486
480
  return hash.slice(2, 66); // Remove 0x and use first 32 bytes
487
481
  }
488
482
  /**
@@ -497,7 +491,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
497
491
  throw new Error("Invalid message or signature");
498
492
  }
499
493
  try {
500
- return ethers_1.ethers.verifyMessage(message, signature);
494
+ return ethers.verifyMessage(message, signature);
501
495
  }
502
496
  catch (error) {
503
497
  throw new Error("Invalid message or signature");
@@ -517,7 +511,7 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
517
511
  await ethereum.request({
518
512
  method: "eth_requestAccounts",
519
513
  });
520
- const provider = new ethers_1.ethers.BrowserProvider(ethereum);
514
+ const provider = new ethers.BrowserProvider(ethereum);
521
515
  return provider.getSigner();
522
516
  }
523
517
  catch (error) {
@@ -525,10 +519,10 @@ class Web3Connector extends eventEmitter_1.EventEmitter {
525
519
  }
526
520
  }
527
521
  }
528
- exports.Web3Connector = Web3Connector;
529
522
  if (typeof window !== "undefined") {
530
523
  window.Web3Connector = Web3Connector;
531
524
  }
532
525
  else if (typeof global !== "undefined") {
533
526
  global.Web3Connector = Web3Connector;
534
527
  }
528
+ export { Web3Connector };