Skip to content

Commit 0415c0e

Browse files
committed
feat: break getTssData to 2 function for clearity
1 parent 2a4322d commit 0415c0e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/mpcCoreKit.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
256256
});
257257
}
258258

259-
public getTssData(args: { skipThrow?: boolean; keyType: KeyType }) {
260-
const result = this.tkey.metadata.getTssData(args.keyType, TSS_TAG_DEFAULT);
261-
if (!result && !args.skipThrow) {
262-
throw CoreKitError.noMetadataFound();
259+
public getTssData(keyType: KeyType) {
260+
const result = this.getTssDataNotThrow(keyType);
261+
if (!result) {
262+
throw CoreKitError.default("Legacy mode only support single curve, please congfiure with correct keyType");
263263
}
264264
return result;
265265
}
266266

267+
public getTssDataNotThrow(keyType: KeyType) {
268+
return this.tkey.metadata.getTssData(keyType, TSS_TAG_DEFAULT);
269+
}
270+
267271
// RecoverTssKey only valid for user that enable MFA where user has 2 type shares :
268272
// TssShareType.DEVICE and TssShareType.RECOVERY
269273
// if the factors key provided is the same type recovery will not works
@@ -591,7 +595,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
591595
if (this.options.legacyFlag) {
592596
const spKeyType = this.getServiceProviderKeyType();
593597
// Check for existing curve in tssData for legacy mode
594-
const tssData = this.getTssData({ keyType: spKeyType, skipThrow: true });
598+
const tssData = this.getTssDataNotThrow(spKeyType);
595599
if (!tssData) throw CoreKitError.default("Legacy mode only support single curve, please congfiure with correct keyType");
596600
}
597601

@@ -701,7 +705,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
701705
throw CoreKitError.factorKeyNotPresent("factorKey not present in state when getting tss factor public key.");
702706
}
703707
const firstKeyType = this.getSupportedCurveKeyTypes()[0];
704-
const tssData = this.getTssData({ keyType: firstKeyType });
708+
const tssData = this.getTssData(firstKeyType);
705709
const factorPubsList = tssData.factorPubs;
706710
return factorPubsList.map((factorPub) => factorPub.toSEC1(factorKeyCurve, true).toString("hex"));
707711
};
@@ -831,7 +835,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
831835
const clientIndex = parties - 1;
832836
// 1. setup
833837
// generate endpoints for servers
834-
const tssData = this.getTssData({ keyType });
838+
const tssData = this.getTssData(keyType);
835839
const { nodeIndexes } = await this.torusSp.getTSSPubKey(this.tKey.tssTag, tssData.tssNonce, keyType);
836840
const {
837841
endpoints,
@@ -948,7 +952,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
948952
throw CoreKitError.factorKeyNotPresent("factorKey not present in state when deleting a factor.");
949953
}
950954
const firstKeyType = this.getSupportedCurveKeyTypes()[0];
951-
const tssData = this.getTssData({ keyType: firstKeyType });
955+
const tssData = this.getTssData(firstKeyType);
952956
if (!tssData.factorPubs) {
953957
throw CoreKitError.factorPubsMissing();
954958
}
@@ -1216,7 +1220,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
12161220
}
12171221

12181222
private getTssNonce(keyType: KeyType): number {
1219-
const tssData = this.getTssData({ keyType });
1223+
const tssData = this.getTssData(keyType);
12201224
if (tssData.tssNonce === undefined) {
12211225
throw CoreKitError.tssNoncesMissing(`tssNonce not present for tag ${this.tKey.tssTag}`);
12221226
}
@@ -1321,8 +1325,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13211325
if (this.options.legacyFlag) {
13221326
const spKeyType = this.getServiceProviderKeyType();
13231327
// Check for existing curve in tssData for legacy mode
1324-
const tssData = this.getTssData({ keyType: spKeyType, skipThrow: true });
1325-
if (!tssData) throw CoreKitError.default("Legacy mode only support single curve, please congfiure with correct keyType");
1328+
// the function below will throw if tss data is not available
1329+
this.getTssData(spKeyType);
13261330
}
13271331

13281332
if (this.options.disableHashedFactorKey) {
@@ -1358,7 +1362,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13581362

13591363
let newCurveKeyType = false;
13601364
for (const keyType of this.supportedCurveKeyTypes) {
1361-
const tssData = this.getTssData({ skipThrow: true, keyType });
1365+
const tssData = this.getTssDataNotThrow(keyType);
13621366
if (!tssData) {
13631367
newCurveKeyType = true;
13641368
}
@@ -1368,7 +1372,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13681372
this.atomicSync(async () => {
13691373
// check for missing curve and initialize it
13701374
for (const keyType of this.supportedCurveKeyTypes) {
1371-
const tssData = this.getTssData({ skipThrow: true, keyType });
1375+
const tssData = this.getTssDataNotThrow(keyType);
13721376
if (!tssData) {
13731377
await this.tKey.initializeTss({
13741378
tssKeyType: keyType,
@@ -1501,7 +1505,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
15011505
private async copyOrCreateShare(newFactorTSSIndex: number, newFactorPub: Point) {
15021506
this.checkReady();
15031507
const firstKeyType = this.getSupportedCurveKeyTypes()[0];
1504-
const tssData = this.getTssData({ keyType: firstKeyType });
1508+
const tssData = this.getTssData(firstKeyType);
15051509
if (!tssData.factorPubs || !Array.isArray(tssData.factorPubs)) {
15061510
throw CoreKitError.factorPubsMissing("'factorPubs' is missing in the metadata. Failed to copy factor public key.");
15071511
}

0 commit comments

Comments
 (0)