Skip to content

Commit f1c2c50

Browse files
Merge pull request #212 from Web3Auth/feat/inputFactorKeyCheck
fix: check for invalid factor key during inputfactor
2 parents 8bc90ab + 5917fd4 commit f1c2c50

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/mpcCoreKit.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
512512
// input tkey device share when required share > 0 ( or not reconstructed )
513513
// assumption tkey shares will not changed
514514
if (!this.tKey.secp256k1Key) {
515+
const factorKeyPrivate = factorKeyCurve.keyFromPrivate(factorKey.toBuffer());
516+
const factorPubX = factorKeyPrivate.getPublic().getX().toString("hex").padStart(64, "0");
517+
const factorEncExist = this.tkey.metadata.factorEncs?.[this.tkey.tssTag]?.[factorPubX];
518+
if (!factorEncExist) {
519+
throw CoreKitError.providedFactorKeyInvalid("Invalid FactorKey provided. Failed to input factor key.");
520+
}
515521
const factorKeyMetadata = await this.getFactorKeyMetadata(factorKey);
516522
await this.tKey.inputShareStoreSafe(factorKeyMetadata, true);
517523
}

tests/factors.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export const FactorManipulationTest = async (testVariable: FactorTestVariable) =
158158

159159
const browserFactor = await instance2.getDeviceFactor();
160160

161+
const factorBN = new BN(recoverFactor, "hex")
162+
161163
// login with mfa factor
162164
await instance2.inputFactorKey(new BN(recoverFactor, "hex"));
163165
assert.strictEqual(instance2.status, COREKIT_STATUS.LOGGED_IN);
@@ -167,6 +169,15 @@ export const FactorManipulationTest = async (testVariable: FactorTestVariable) =
167169
const instance3 = await newInstance();
168170
assert.strictEqual(instance3.status, COREKIT_STATUS.REQUIRED_SHARE);
169171

172+
173+
174+
try {
175+
await instance3.inputFactorKey(factorBN.subn(1));
176+
throw Error("should not be able to input factor");
177+
} catch (e) {
178+
assert(e instanceof Error);
179+
}
180+
170181
await instance3.inputFactorKey(new BN(browserFactor, "hex"));
171182
assert.strictEqual(instance3.status, COREKIT_STATUS.LOGGED_IN);
172183
});

0 commit comments

Comments
 (0)