Web3Auth / mpc-core-kit

15 stars 4 forks source link

Concurrent login doesn’t work as expected #115

Open huggingbot opened 3 months ago

huggingbot commented 3 months ago

Description

Sample code:

async function createCoreKitInstance() {
  const coreKitInstance = new Web3AuthMPCCoreKit({
    web3AuthClientId: CLIENT_ID,
    chainConfig,
    manualSync: false,
  });
  await coreKitInstance.init({ handleRedirectResult: false }).catch((err) => {
    throw new Error(err);
  });
  const idToken = generateIdToken(VERIFIER_ID);
  await coreKitInstance.loginWithJWT({ idToken, verifier: VERIFIER, verifierId: VERIFIER_ID }).catch((err) => {
    throw new Error(err);
  });
  return coreKitInstance;
}

const instancePromise1 = createCoreKitInstance();
const instancePromise2 = createCoreKitInstance();
const [instance1, instance2] = await Promise.all([instancePromise1, instancePromise2]);

Some of the errors are:

lwin-kyaw commented 2 months ago

Expected behaviors for concurrent logins based on the current implementations

SDK Version:

Existing user

New user

Both logins session should be successful without any LOCK ISSUES due to

lwin-kyaw commented 2 months ago

Description

  • The expected behaviour should be that one of the concurrent login should throw an error while the other should complete the new user login.

Sample code:

async function createCoreKitInstance() {
  const coreKitInstance = new Web3AuthMPCCoreKit({
    web3AuthClientId: CLIENT_ID,
    chainConfig,
    manualSync: false,
  });
  await coreKitInstance.init({ handleRedirectResult: false }).catch((err) => {
    throw new Error(err);
  });
  const idToken = generateIdToken(VERIFIER_ID);
  await coreKitInstance.loginWithJWT({ idToken, verifier: VERIFIER, verifierId: VERIFIER_ID }).catch((err) => {
    throw new Error(err);
  });
  return coreKitInstance;
}

const instancePromise1 = createCoreKitInstance();
const instancePromise2 = createCoreKitInstance();
const [instance1, instance2] = await Promise.all([instancePromise1, instancePromise2]);

Some of the errors are:

  • factorKey not present
  • TSS shares for instance1 and instance2 are different
  • factors variable is undefined, i.e. Cannot read properties of undefined (reading 'length'), when calling getKeyDetails
  • one of the instances' status is not updated, i.e. instance2.status !== COREKIT_STATUS.LOGGED_IN
  • Unable to reconstruct require 2 but have 1 error when calling loginWithJWT

Among the errors stated above, the following error messages are related to this issue, https://github.com/Web3Auth/mpc-core-kit/issues/131, please refer to that issue for more details.

Regarding to this error, TSS shares for instance1 and instance2 are different. It is because

const factorMetadata1 = await coreKit1.tKey.storageLayer.getMetadata<Record<string, string>>({ privKey: new BN(oauthKey1, "hex") }); const factorMetadata2 = await coreKit2.tKey.storageLayer.getMetadata<Record<string, string>>({ privKey: new BN(oauthKey2, "hex") }); // factorMetadata1 === factorMetadata2 console.log("factorMetadata1", factorMetadata1); console.log("factorMetadata2", factorMetadata2);


- for more details, please refer to this [comment](https://github.com/Web3Auth/mpc-core-kit/issues/115#issuecomment-2090013432) 
lwin-kyaw commented 2 months ago

I've made a draft PR, https://github.com/tkey/tkey-mpc/pull/10, which will fix this issue.