Skip to content

Commit

Permalink
Merge pull request #173 from Web3Auth/fix/atomicInProgress
Browse files Browse the repository at this point in the history
fix: atomic in progress
  • Loading branch information
himanshuchawla009 authored Oct 29, 2024
2 parents 56dffdc + e02aa5a commit 0dfbf03
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/mpcCoreKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {

private _keyType: KeyType;

private atomicCallStackCounter: number = 0;

constructor(options: Web3AuthOptions) {
if (!options.web3AuthClientId) {
throw CoreKitError.clientIdInvalid();
Expand Down Expand Up @@ -617,7 +619,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
return this.atomicSync(async () => {
await this.copyOrCreateShare(shareType, factorPub);
await this.backupMetadataShare(factorKey);
await this.addFactorDescription(factorKey, shareDescription, additionalMetadata);
await this.addFactorDescription({ factorKey, shareDescription, additionalMetadata, updateMetadata: false });

return scalarBNToBufferSEC1(factorKey).toString("hex");
}).catch((reason: Error) => {
Expand Down Expand Up @@ -851,13 +853,22 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
}

protected async atomicSync<T>(f: () => Promise<T>): Promise<T> {
this.atomicCallStackCounter += 1;

this.tkey.manualSync = true;
try {
const r = await f();
await this.commitChanges();
if (this.atomicCallStackCounter === 1) {
if (!this.options.manualSync) {
await this.commitChanges();
}
}
return r;
} finally {
this.tkey.manualSync = this.options.manualSync;
this.atomicCallStackCounter -= 1;
if (this.atomicCallStackCounter === 0) {
this.tkey.manualSync = this.options.manualSync;
}
}
}

Expand Down Expand Up @@ -939,9 +950,17 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
// Store factor description.
await this.backupMetadataShare(factorKey);
if (this.options.disableHashedFactorKey) {
await this.addFactorDescription(factorKey, FactorKeyTypeShareDescription.Other);
await this.addFactorDescription({
factorKey,
shareDescription: FactorKeyTypeShareDescription.Other,
updateMetadata: false,
});
} else {
await this.addFactorDescription(factorKey, FactorKeyTypeShareDescription.HashedShare);
await this.addFactorDescription({
factorKey,
shareDescription: FactorKeyTypeShareDescription.HashedShare,
updateMetadata: false,
});
}
});
}
Expand Down Expand Up @@ -1154,15 +1173,23 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
if (!this.tkey?.manualSync) await this.tkey?.syncLocalMetadataTransitions();
}

private async addFactorDescription(
factorKey: BN,
shareDescription: FactorKeyTypeShareDescription,
additionalMetadata: Record<string, string> = {},
updateMetadata = true
) {
private async addFactorDescription(args: {
factorKey: BN;
shareDescription: FactorKeyTypeShareDescription;
additionalMetadata?: Record<string, string>;
updateMetadata?: boolean;
}) {
const { factorKey, shareDescription, updateMetadata } = args;

let { additionalMetadata } = args;
if (!additionalMetadata) {
additionalMetadata = {};
}

const { tssIndex } = await this.tKey.getTSSShare(factorKey);
const tkeyPoint = getPubKeyPoint(factorKey, factorKeyCurve);
const factorPub = tkeyPoint.toSEC1(this.tkey.tssCurve, true).toString("hex");
const factorPoint = getPubKeyPoint(factorKey, factorKeyCurve);
const factorPub = factorPoint.toSEC1(this.tkey.tssCurve, true).toString("hex");

const params = {
module: shareDescription,
dateAdded: Date.now(),
Expand Down

0 comments on commit 0dfbf03

Please sign in to comment.