shogun-core 1.7.3 → 1.7.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.
@@ -106019,12 +106019,28 @@ class Web3ConnectorPlugin extends base_1.BasePlugin {
106019
106019
  */
106020
106020
  async createGunUserFromSigningCredential(address) {
106021
106021
  try {
106022
+ console.log(`🔧 createGunUserFromSigningCredential called with address:`, address);
106022
106023
  const conn = this.assertMetaMask();
106023
106024
  if (typeof conn.createGunUserFromSigningCredential === "function") {
106024
- return await conn.createGunUserFromSigningCredential(address);
106025
+ console.log(`🔧 Using connector's createGunUserFromSigningCredential`);
106026
+ const result = await conn.createGunUserFromSigningCredential(address);
106027
+ console.log(`🔧 Connector result:`, result);
106028
+ return result;
106025
106029
  }
106030
+ console.log(`🔧 Using fallback createGunUser`);
106026
106031
  const core = this.assertInitialized();
106027
- return await this.assertSigner().createGunUser(address, core.gun);
106032
+ // FIX: Use deterministic approach - try to authenticate first, then create if needed
106033
+ console.log(`🔧 Attempting authentication with deterministic pair`);
106034
+ const authResult = await this.assertSigner().authenticateWithExistingPair(address, core.gun);
106035
+ if (authResult.success) {
106036
+ console.log(`🔧 Authentication successful with existing user`);
106037
+ return authResult;
106038
+ }
106039
+ console.log(`🔧 Authentication failed, creating new user`);
106040
+ // If authentication failed, create new user
106041
+ const result = await this.assertSigner().createGunUser(address, core.gun);
106042
+ console.log(`🔧 User creation result:`, result);
106043
+ return result;
106028
106044
  }
106029
106045
  catch (error) {
106030
106046
  console.error(`Error creating Gun user from Web3 signing credential: ${error.message}`);
@@ -106034,12 +106050,12 @@ class Web3ConnectorPlugin extends base_1.BasePlugin {
106034
106050
  /**
106035
106051
  * Get the Gun user public key for a signing credential
106036
106052
  */
106037
- getGunUserPubFromSigningCredential(address) {
106053
+ async getGunUserPubFromSigningCredential(address) {
106038
106054
  const conn = this.assertMetaMask();
106039
106055
  if (typeof conn.getGunUserPubFromSigningCredential === "function") {
106040
- return conn.getGunUserPubFromSigningCredential(address);
106056
+ return await conn.getGunUserPubFromSigningCredential(address);
106041
106057
  }
106042
- return this.assertSigner().getGunUserPub(address);
106058
+ return await this.assertSigner().getGunUserPub(address);
106043
106059
  }
106044
106060
  /**
106045
106061
  * Get the password (for consistency checking)
@@ -106111,11 +106127,54 @@ class Web3ConnectorPlugin extends base_1.BasePlugin {
106111
106127
  if (!this.isAvailable()) {
106112
106128
  throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENVIRONMENT, "WEB3_UNAVAILABLE", "Web3 is not available in the browser");
106113
106129
  }
106114
- // Use setupConsistentOneshotSigning for login
106130
+ console.log(`🔧 Web3 login - starting login for address:`, address);
106131
+ // FIX: Use deterministic pair instead of generating new credentials
106132
+ // Get the existing credential if available
106133
+ const existingCredential = this.getSigningCredential(address);
106134
+ if (existingCredential) {
106135
+ console.log(`🔧 Web3 login - found existing credential, using it`);
106136
+ // Use existing credential to get Gun user
106137
+ const gunUser = await this.createGunUserFromSigningCredential(address);
106138
+ console.log(`🔧 Web3 login - existing credential result:`, gunUser);
106139
+ if (gunUser.success && gunUser.userPub) {
106140
+ // Set authentication method to web3
106141
+ core.setAuthMethod("web3");
106142
+ const loginResult = {
106143
+ success: true,
106144
+ user: {
106145
+ userPub: gunUser.userPub,
106146
+ username: address,
106147
+ },
106148
+ userPub: gunUser.userPub,
106149
+ };
106150
+ console.log(`🔧 Web3 login - returning result:`, {
106151
+ success: loginResult.success,
106152
+ userPub: loginResult.userPub
106153
+ ? loginResult.userPub.slice(0, 8) + "..."
106154
+ : "null",
106155
+ username: loginResult.user?.username,
106156
+ });
106157
+ // Emit login event
106158
+ core.emit("auth:login", {
106159
+ userPub: gunUser.userPub || "",
106160
+ username: address,
106161
+ method: "web3",
106162
+ });
106163
+ return loginResult;
106164
+ }
106165
+ }
106166
+ // If no existing credential or it failed, create a new one (for first-time login)
106167
+ console.log(`🔧 Web3 login - no existing credential, creating new one`);
106168
+ // Use setupConsistentOneshotSigning for first-time login
106115
106169
  const { gunUser } = await this.setupConsistentOneshotSigning(address);
106170
+ console.log(`🔧 Web3 login - setupConsistentOneshotSigning result:`, {
106171
+ gunUser,
106172
+ address,
106173
+ });
106116
106174
  if (!gunUser.success) {
106117
106175
  throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.AUTHENTICATION, "WEB3_LOGIN_FAILED", gunUser.error || "Failed to log in with Web3 credentials");
106118
106176
  }
106177
+ console.log(`🔧 Web3 login - gunUser success, userPub:`, gunUser.userPub ? gunUser.userPub.slice(0, 8) + "..." : "null");
106119
106178
  // Set authentication method to web3
106120
106179
  core.setAuthMethod("web3");
106121
106180
  // Return success result
@@ -106127,6 +106186,13 @@ class Web3ConnectorPlugin extends base_1.BasePlugin {
106127
106186
  },
106128
106187
  userPub: gunUser.userPub,
106129
106188
  };
106189
+ console.log(`🔧 Web3 login - returning result:`, {
106190
+ success: loginResult.success,
106191
+ userPub: loginResult.userPub
106192
+ ? loginResult.userPub.slice(0, 8) + "..."
106193
+ : "null",
106194
+ username: loginResult.user?.username,
106195
+ });
106130
106196
  // Emit login event
106131
106197
  core.emit("auth:login", {
106132
106198
  userPub: gunUser.userPub || "",
@@ -106302,14 +106368,52 @@ class Web3Signer {
106302
106368
  * CONSISTENT with normal approach: uses password as seed
106303
106369
  */
106304
106370
  async createDerivedKeyPair(address, extra) {
106305
- const credential = this.credentials.get(address.toLowerCase());
106306
- if (!credential) {
106307
- throw new Error(`Credential for address ${address} not found`);
106371
+ // Use the deterministic approach instead of stored credentials
106372
+ return this.createDerivedKeyPairFromAddress(address, extra);
106373
+ }
106374
+ /**
106375
+ * Authenticate with existing pair (for login)
106376
+ * This generates the deterministic pair from address and authenticates with GunDB
106377
+ * GunDB will recognize the user because the pair is deterministic
106378
+ */
106379
+ async authenticateWithExistingPair(address, gunInstance) {
106380
+ try {
106381
+ console.log(`🔧 Web3Signer - authenticating with deterministic pair for address:`, address);
106382
+ // Generate the deterministic pair directly from address (no need for stored credentials)
106383
+ const derivedPair = await this.createDerivedKeyPairFromAddress(address);
106384
+ console.log(`🔧 Web3Signer - deterministic pair created, attempting auth with GunDB`);
106385
+ return new Promise((resolve) => {
106386
+ // Authenticate directly with GunDB using the deterministic pair
106387
+ gunInstance.user().auth(derivedPair, (authAck) => {
106388
+ if (authAck.err) {
106389
+ console.log(`🔧 Web3Signer - auth failed:`, authAck.err);
106390
+ resolve({ success: false, error: authAck.err });
106391
+ }
106392
+ else {
106393
+ const userPub = authAck.pub;
106394
+ console.log(`🔧 Web3Signer - auth successful, userPub:`, userPub ? userPub.slice(0, 8) + "..." : "null");
106395
+ resolve({ success: true, userPub });
106396
+ }
106397
+ });
106398
+ });
106308
106399
  }
106400
+ catch (error) {
106401
+ console.error("Error authenticating with deterministic pair:", error);
106402
+ return { success: false, error: error.message };
106403
+ }
106404
+ }
106405
+ /**
106406
+ * Creates a derived key pair directly from address (deterministic)
106407
+ * This ensures the same pair is generated every time for the same address
106408
+ */
106409
+ async createDerivedKeyPairFromAddress(address, extra) {
106309
106410
  try {
106310
- // CONSISTENCY: Use the same approach as normal Web3
106311
- // Use password as seed (same as normal approach)
106312
- const derivedKeys = await (0, derive_1.default)(credential.password, // This is the key consistency point!
106411
+ // Generate deterministic password from address (same as createSigningCredential)
106412
+ const validAddress = ethers_1.ethers.getAddress(address.toLowerCase());
106413
+ const password = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(`${validAddress.toLowerCase()}:shogun-web3`));
106414
+ console.log(`🔧 Web3Signer - generating deterministic pair for address:`, validAddress);
106415
+ // Use the same derive function as normal approach
106416
+ const derivedKeys = await (0, derive_1.default)(password, // Deterministic password from address
106313
106417
  extra, { includeP256: true });
106314
106418
  return {
106315
106419
  pub: derivedKeys.pub,
@@ -106319,7 +106423,7 @@ class Web3Signer {
106319
106423
  };
106320
106424
  }
106321
106425
  catch (error) {
106322
- console.error("Error deriving keys from Web3 credential:", error);
106426
+ console.error("Error creating derived key pair from address:", error);
106323
106427
  throw error;
106324
106428
  }
106325
106429
  }
@@ -106329,42 +106433,39 @@ class Web3Signer {
106329
106433
  * FIX: Use derived pair instead of username/password for GunDB auth
106330
106434
  */
106331
106435
  async createGunUser(address, gunInstance) {
106332
- const credential = this.credentials.get(address.toLowerCase());
106333
- if (!credential) {
106334
- throw new Error(`Credential for address ${address} not found`);
106335
- }
106336
106436
  try {
106337
- // FIX: Use derived pair for GunDB authentication instead of username/password
106338
- const derivedPair = await this.createDerivedKeyPair(address);
106437
+ console.log(`🔧 Web3Signer - creating Gun user with deterministic pair for address:`, address);
106438
+ // Generate the deterministic pair directly from address
106439
+ const derivedPair = await this.createDerivedKeyPairFromAddress(address);
106339
106440
  return new Promise((resolve) => {
106340
106441
  // Use the derived pair directly for GunDB auth
106341
106442
  gunInstance.user().create(derivedPair, (ack) => {
106342
106443
  if (ack.err) {
106444
+ console.log(`🔧 Web3Signer - user creation failed, trying auth:`, ack.err);
106343
106445
  // Try to login if user already exists
106344
106446
  gunInstance.user().auth(derivedPair, (authAck) => {
106345
106447
  if (authAck.err) {
106448
+ console.log(`🔧 Web3Signer - auth also failed:`, authAck.err);
106346
106449
  resolve({ success: false, error: authAck.err });
106347
106450
  }
106348
106451
  else {
106349
106452
  const userPub = authAck.pub;
106350
- // Update credential with Gun user pub
106351
- credential.gunUserPub = userPub;
106352
- this.credentials.set(address.toLowerCase(), credential);
106453
+ console.log(`🔧 Web3Signer - auth successful, userPub:`, userPub ? userPub.slice(0, 8) + "..." : "null");
106353
106454
  resolve({ success: true, userPub });
106354
106455
  }
106355
106456
  });
106356
106457
  }
106357
106458
  else {
106459
+ console.log(`🔧 Web3Signer - user created successfully, now logging in`);
106358
106460
  // User created, now login
106359
106461
  gunInstance.user().auth(derivedPair, (authAck) => {
106360
106462
  if (authAck.err) {
106463
+ console.log(`🔧 Web3Signer - login after creation failed:`, authAck.err);
106361
106464
  resolve({ success: false, error: authAck.err });
106362
106465
  }
106363
106466
  else {
106364
106467
  const userPub = authAck.pub;
106365
- // Update credential with Gun user pub
106366
- credential.gunUserPub = userPub;
106367
- this.credentials.set(address.toLowerCase(), credential);
106468
+ console.log(`🔧 Web3Signer - login successful, userPub:`, userPub ? userPub.slice(0, 8) + "..." : "null");
106368
106469
  resolve({ success: true, userPub });
106369
106470
  }
106370
106471
  });
@@ -106411,32 +106512,51 @@ class Web3Signer {
106411
106512
  * Get the Gun user public key for a credential
106412
106513
  * This allows checking if the same user would be created
106413
106514
  */
106414
- getGunUserPub(address) {
106415
- const credential = this.credentials.get(address.toLowerCase());
106416
- return credential?.gunUserPub;
106515
+ async getGunUserPub(address) {
106516
+ try {
106517
+ // Generate the deterministic pair and return the public key
106518
+ const derivedPair = await this.createDerivedKeyPairFromAddress(address);
106519
+ return derivedPair.pub;
106520
+ }
106521
+ catch (error) {
106522
+ console.error("Error getting Gun user pub:", error);
106523
+ return undefined;
106524
+ }
106417
106525
  }
106418
106526
  /**
106419
106527
  * Get the password (for consistency checking)
106420
106528
  */
106421
106529
  getPassword(address) {
106422
- const credential = this.credentials.get(address.toLowerCase());
106423
- return credential?.password;
106530
+ try {
106531
+ // Generate deterministic password from address (same as createSigningCredential)
106532
+ const validAddress = ethers_1.ethers.getAddress(address.toLowerCase());
106533
+ const password = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(`${validAddress.toLowerCase()}:shogun-web3`));
106534
+ return password;
106535
+ }
106536
+ catch (error) {
106537
+ console.error("Error getting password:", error);
106538
+ return undefined;
106539
+ }
106424
106540
  }
106425
106541
  /**
106426
106542
  * Check if this credential would create the same Gun user as normal approach
106427
106543
  */
106428
106544
  async verifyConsistency(address, expectedUserPub) {
106429
- const credential = this.credentials.get(address.toLowerCase());
106430
- if (!credential) {
106545
+ try {
106546
+ // Generate the deterministic pair
106547
+ const derivedKeys = await this.createDerivedKeyPairFromAddress(address);
106548
+ return {
106549
+ consistent: expectedUserPub
106550
+ ? derivedKeys.pub === expectedUserPub
106551
+ : true,
106552
+ actualUserPub: derivedKeys.pub,
106553
+ expectedUserPub,
106554
+ };
106555
+ }
106556
+ catch (error) {
106557
+ console.error("Error verifying consistency:", error);
106431
106558
  return { consistent: false };
106432
106559
  }
106433
- // The derived keys should be the same as normal approach
106434
- const derivedKeys = await this.createDerivedKeyPair(address);
106435
- return {
106436
- consistent: expectedUserPub ? derivedKeys.pub === expectedUserPub : true,
106437
- actualUserPub: derivedKeys.pub,
106438
- expectedUserPub,
106439
- };
106440
106560
  }
106441
106561
  /**
106442
106562
  * Get credential by address