Skip to content

Commit 7db5d95

Browse files
committed
fix: better way check valid factorKey
1 parent 241f152 commit 7db5d95

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/mpcCoreKit.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
540540
public async inputFactorKey(factorKey: BNString): Promise<void> {
541541
const factorKeyBN = new BN(factorKey, "hex");
542542
this.checkReady();
543+
const point = Point.fromScalar(factorKeyBN, secp256k1);
544+
if (!this.getTssFactorPub().includes(point.toSEC1(secp256k1, true).toString("hex"))) {
545+
throw CoreKitError.providedFactorKeyInvalid();
546+
}
543547
try {
544548
// input tkey device share when required share > 0 ( or not reconstructed )
545549
// assumption tkey shares will not changed
@@ -649,9 +653,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
649653

650654
public getTssFactorPub = (): string[] => {
651655
this.checkReady();
652-
if (!this.state.factorKey) {
653-
throw CoreKitError.factorKeyNotPresent("factorKey not present in state when getting tss factor public key.");
654-
}
655656
const factorPubsList = this.tKey.metadata.factorPubs[this.tKey.tssTag];
656657
return factorPubsList.map((factorPub) => factorPub.toSEC1(factorKeyCurve, true).toString("hex"));
657658
};
@@ -1210,7 +1211,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
12101211
}
12111212

12121213
public getMetadataKey(): string {
1213-
return this.tkey.secp256k1Key.toString("hex");
1214+
return this.tkey.secp256k1Key.toString("hex").padStart(64, "0");
12141215
}
12151216

12161217
public getMetadataPublicKey(): string {
@@ -1342,22 +1343,25 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13421343
}
13431344

13441345
const hashedFactorKey = getHashedPrivateKey(this.state.postBoxKey, this.options.hashedFactorNonce);
1345-
this.state.factorKey = hashedFactorKey;
1346-
if (await this.checkIfFactorKeyValid(hashedFactorKey)) {
1347-
// Initialize tkey with existing hashed share if available.
1348-
const factorKeyMetadata: ShareStore = await this.getFactorKeyMetadata(hashedFactorKey);
1349-
try {
1350-
await this.tKey.inputShareStoreSafe(factorKeyMetadata, true);
1351-
await this.tKey.reconstructKey();
1352-
await this.finalizeTkey(hashedFactorKey);
1353-
} catch (err) {
1354-
log.error("error initializing tkey with hashed share", err);
1355-
}
1356-
} else {
1357-
const factorKeyMetadata = await this.tKey?.readMetadata<StringifiedType>(hashedFactorKey);
1358-
if (factorKeyMetadata.message === "SHARE_DELETED") {
1359-
// throw CoreKitError.hashedFactorDeleted();
1360-
log.warn("hashed factor deleted");
1346+
const point = Point.fromScalar(hashedFactorKey, secp256k1);
1347+
if (this.getTssFactorPub().includes(point.toSEC1(secp256k1, true).toString("hex"))) {
1348+
this.state.factorKey = hashedFactorKey;
1349+
if (await this.checkIfFactorKeyValid(hashedFactorKey)) {
1350+
// Initialize tkey with existing hashed share if available.
1351+
const factorKeyMetadata: ShareStore = await this.getFactorKeyMetadata(hashedFactorKey);
1352+
try {
1353+
await this.tKey.inputShareStoreSafe(factorKeyMetadata, true);
1354+
await this.tKey.reconstructKey();
1355+
await this.finalizeTkey(hashedFactorKey);
1356+
} catch (err) {
1357+
log.error("error initializing tkey with hashed share", err);
1358+
}
1359+
} else {
1360+
const factorKeyMetadata = await this.tKey?.readMetadata<StringifiedType>(hashedFactorKey);
1361+
if (factorKeyMetadata.message === "SHARE_DELETED") {
1362+
// throw CoreKitError.hashedFactorDeleted();
1363+
log.warn("hashed factor deleted");
1364+
}
13611365
}
13621366
}
13631367
}

src/plugins/ICustomSigner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FactorEnc, Point, ShareDescriptionMap } from "@tkey/common-types";
22
import { PointHex } from "@toruslabs/tss-client";
33
import { SafeEventEmitter } from "@web3auth/auth";
44

5-
import { CreateFactorParams, WEB3AUTH_NETWORK_TYPE } from "../interfaces";
5+
import { COREKIT_STATUS, CreateFactorParams, WEB3AUTH_NETWORK_TYPE } from "../interfaces";
66

77
export type SupportedCurve = "secp256k1" | "ed25519";
88

@@ -54,6 +54,7 @@ export interface IRemoteClientState {
5454
}
5555

5656
export interface IRemoteSignerContext {
57+
status: COREKIT_STATUS;
5758
stateEmitter: SafeEventEmitter;
5859
setupRemoteSigning(params: Omit<IRemoteClientState, "tssShareIndex">, rehydrate?: boolean): Promise<void>;
5960
createFactor(createFactorParams: CreateFactorParams): Promise<string>;

0 commit comments

Comments
 (0)