Skip to content

Commit cc7acc4

Browse files
committed
fix: checkIfFactorKeyValid, check in factorEncs
1 parent 1c6ea6f commit cc7acc4

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/mpcCoreKit.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
12781278

12791279
private async checkIfFactorKeyValid(factorKey: BN): Promise<boolean> {
12801280
this.checkReady();
1281+
const factorKeyPrivate = factorKeyCurve.keyFromPrivate(factorKey.toBuffer());
1282+
const factorPubX = factorKeyPrivate.getPublic().getX().toString("hex").padStart(64, "0");
1283+
const existingFactorEnc = this.tkey.metadata.factorEncs[this.tkey.tssTag][factorPubX];
1284+
if (!existingFactorEnc) {
1285+
return false;
1286+
}
12811287
const factorKeyMetadata = await this.tKey?.readMetadata<StringifiedType>(factorKey);
12821288
if (!factorKeyMetadata || factorKeyMetadata.message === "KEY_NOT_FOUND" || factorKeyMetadata.message === "SHARE_DELETED") {
12831289
return false;

tests/factors.spec.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "node:assert";
22
import test from "node:test";
33

4-
import { EllipticPoint, KeyType, Point, secp256k1 } from "@tkey/common-types";
4+
import { EllipticPoint, getPubKeyPoint, KeyType, Point, secp256k1 } from "@tkey/common-types";
55
import { factorKeyCurve } from "@tkey/tss";
66
import { tssLib as tssLibDKLS } from "@toruslabs/tss-dkls-lib";
77
import { tssLib as tssLibFROST } from "@toruslabs/tss-frost-lib";
@@ -158,7 +158,7 @@ export const FactorManipulationTest = async (testVariable: FactorTestVariable) =
158158
});
159159

160160
// enable mfa
161-
161+
let browserFactor: string;
162162
await t.test("enable MFA", async function () {
163163
const instance = await newInstance();
164164
assert.strictEqual(instance.status, COREKIT_STATUS.LOGGED_IN);
@@ -179,7 +179,7 @@ export const FactorManipulationTest = async (testVariable: FactorTestVariable) =
179179
const instance2 = await newInstance();
180180
assert.strictEqual(instance2.status, COREKIT_STATUS.REQUIRED_SHARE);
181181

182-
const browserFactor = await instance2.getDeviceFactor();
182+
browserFactor = await instance2.getDeviceFactor();
183183

184184
const factorBN = new BN(recoverFactor, "hex")
185185

@@ -210,9 +210,32 @@ export const FactorManipulationTest = async (testVariable: FactorTestVariable) =
210210
} else {
211211
await signSecp256k1Data({ coreKitInstance: instance3, msg: "hello world" });
212212
}
213-
214213
});
215214

215+
// replace factor
216+
await t.test("replace factor", async function () {
217+
const instance = await newInstance();
218+
219+
const deviceFactorKeyBN = new BN(browserFactor, "hex")
220+
await instance.inputFactorKey(deviceFactorKeyBN);
221+
assert.strictEqual(instance.status, COREKIT_STATUS.LOGGED_IN);
222+
223+
const newFactorkey = await instance.createFactor({ shareType: TssShareType.DEVICE });
224+
await instance.inputFactorKey(new BN(newFactorkey, "hex"));
225+
226+
assert.strictEqual(instance.status, COREKIT_STATUS.LOGGED_IN);
227+
228+
229+
const deviceFactorPub = getPubKeyPoint(deviceFactorKeyBN);
230+
await instance.deleteFactor(deviceFactorPub, browserFactor);
231+
232+
try {
233+
await instance.inputFactorKey(deviceFactorKeyBN);
234+
throw Error("should not be able to deleted input factor");
235+
} catch (e) {
236+
assert(e instanceof Error);
237+
}
238+
});
216239
});
217240
};
218241

tests/gating.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const variable: TestVariable[] = [
2323
description: "should not be gated when on devnet",
2424
web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET,
2525
uxMode: "nodejs",
26-
email: defaultTestEmail,
26+
// tkey tests seems use this verifierid, metadata retrun only have tkey that do not support tss
27+
email: defaultTestEmail + '1',
2728
web3ClientID: "torus-key-test",
2829
expectedErrorThrown: false,
2930
},

tests/sessionTime.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ const defaultTestEmail = "testEmail1";
2323
const isBasePlan = (id: string) => id === "BCriFlI9ihm81N-bc7x6N-xbqwBLuxfRDMmSH87spKH27QTNOPj1W9s2K3-mp9NzXuaRiqxvAGHyuGlXG5wLD1g";
2424
// BasePlan up to 1 day only
2525
const variable: TestVariable[] = [
26-
{ web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", email: defaultTestEmail, web3ClientID: "torus-key-test", sessionTime: 3600 },
26+
{
27+
web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET,
28+
uxMode: "nodejs",
29+
// tkey tests seems use this verifierid, metadata retrun only have tkey that do not support tss
30+
email: defaultTestEmail + "1",
31+
web3ClientID: "torus-key-test",
32+
sessionTime: 3600
33+
},
2734
{
2835
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
2936
uxMode: "nodejs",

0 commit comments

Comments
 (0)