Skip to content

Commit

Permalink
feat: update interface
Browse files Browse the repository at this point in the history
add throw error for unsupport feature for remoteSigner
  • Loading branch information
ieow committed Dec 18, 2024
1 parent f0bc2f8 commit 241f152
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/helper/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ class CoreKitError extends AbstractCoreKitError {
return CoreKitError.fromCode(1214, extraMessage);
}

public static notSupportedForRemoteFactor(extraMessage = ""): ICoreKitError {
return CoreKitError.fromCode(1215, extraMessage);
}

// Initialization and session management
public static commitChangesBeforeMFA(extraMessage = ""): ICoreKitError {
return CoreKitError.fromCode(1301, extraMessage);
Expand Down
6 changes: 2 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BNString, KeyType, Point as TkeyPoint, ShareDescriptionMap } from "@tkey/common-types";
import { IRemoteClientState, TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss";
import { TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss";
import { WEB3AUTH_SIG_TYPE } from "@toruslabs/constants";
import type {
AGGREGATE_VERIFIER_TYPE,
Expand All @@ -22,7 +22,7 @@ import { SafeEventEmitter } from "@web3auth/auth";
import BN from "bn.js";

import { FactorKeyTypeShareDescription, TssShareType, USER_PATH, WEB3AUTH_NETWORK } from "./constants";
import { IRemoteSignerContext } from "./plugins/IRemoteSigner";
import { IRemoteClientState, IRemoteSignerContext } from "./plugins/ICustomSigner";
import { ISessionSigGenerator } from "./plugins/ISessionSigGenerator";

export type CoreKitMode = UX_MODE_TYPE | "nodejs" | "react-native";
Expand Down Expand Up @@ -177,7 +177,6 @@ export interface JWTLoginParams {
*/
prefetchTssPublicKeys?: number;
}

export interface Web3AuthState {
postBoxKey?: string;
signatures?: string[];
Expand Down Expand Up @@ -515,7 +514,6 @@ export interface SessionData {
tssPubKey: string;
signatures: string[];
userInfo: UserInfo;
remoteClientState?: IRemoteClientState;
}

export interface TkeyLocalStoreData {
Expand Down
20 changes: 18 additions & 2 deletions src/mpcCoreKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
Web3AuthState,
} from "./interfaces";
import { DefaultSessionSigGeneratorPlugin } from "./plugins/DefaultSessionSigGenerator";
import { ICustomDklsSignParams, ICustomFrostSignParams, IRemoteClientState } from "./plugins/IRemoteSigner";
import { ICustomDklsSignParams, ICustomFrostSignParams, IRemoteClientState } from "./plugins/ICustomSigner";
import { ISessionSigGenerator } from "./plugins/ISessionSigGenerator";
import {
deriveShareCoefficients,
Expand Down Expand Up @@ -588,6 +588,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
public async enableMFA(enableMFAParams: EnableMFAParams, recoveryFactor = true): Promise<string> {
this.checkReady();

if (!this.state.factorKey) {
if (this.state.remoteClient?.remoteFactorPub) {
throw CoreKitError.notSupportedForRemoteFactor("Cannot enable MFA with remote factor.");
}
throw CoreKitError.factorKeyNotPresent("Current factorKey not present in state when enabling MFA.");
}

const { postBoxKey } = this.state;
const hashedFactorKey = getHashedPrivateKey(postBoxKey, this.options.hashedFactorNonce);
if (!(await this.checkIfFactorKeyValid(hashedFactorKey))) {
Expand Down Expand Up @@ -652,6 +659,14 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
// mutation function
public async createFactor(createFactorParams: CreateFactorParams): Promise<string> {
this.checkReady();

if (!this.state.factorKey) {
if (this.state.remoteClient?.remoteFactorPub) {
throw CoreKitError.notSupportedForRemoteFactor("Cannot create a factor with remote factor.");
}
throw CoreKitError.factorKeyNotPresent("Current factorKey not present in state when creating a factor.");
}

const { shareType } = createFactorParams;

let { factorKey, shareDescription, additionalMetadata } = createFactorParams;
Expand Down Expand Up @@ -905,7 +920,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {

// mutation function
async deleteFactor(factorPub: Point, factorKey?: BNString): Promise<void> {
if (!this.state.factorKey && !this.state.remoteClient) {
if (!this.state.factorKey) {
if (this.state.remoteClient?.remoteFactorPub) throw CoreKitError.notSupportedForRemoteFactor("Cannot delete a remote factor.");
throw CoreKitError.factorKeyNotPresent("factorKey not present in state when deleting a factor.");
}
if (!this.tKey.metadata.factorPubs) {
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/IRemoteSigner.ts → src/plugins/ICustomSigner.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { FactorEnc, Point, ShareDescriptionMap } from "@tkey/common-types";
import { PointHex } from "@toruslabs/tss-client";
import { SafeEventEmitter } from "@web3auth/auth";

import { CreateFactorParams, WEB3AUTH_NETWORK_TYPE } from "../interfaces";

type SupportedCurve = "secp256k1" | "ed25519";
export interface IRemoteClientState {
remoteFactorPub: string;
metadataShare: string;
tssShareIndex: number;
}
export type SupportedCurve = "secp256k1" | "ed25519";

export type ICustomFrostSignParams = {
sessionId: string;
Expand Down Expand Up @@ -50,7 +46,15 @@ export interface ICustomDKLSSign {
export interface ICustomFrostSign {
sign: (params: ICustomFrostSignParams, msgHash: Uint8Array) => Promise<Uint8Array>;
}

export interface IRemoteClientState {
remoteFactorPub: string;
metadataShare: string;
tssShareIndex: number;
}

export interface IRemoteSignerContext {
stateEmitter: SafeEventEmitter;
setupRemoteSigning(params: Omit<IRemoteClientState, "tssShareIndex">, rehydrate?: boolean): Promise<void>;
createFactor(createFactorParams: CreateFactorParams): Promise<string>;
inputFactorKey(factorKey: string): Promise<void>;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./DefaultSessionSigGenerator";
export * from "./ICustomSigner";
export * from "./ISessionSigGenerator";

0 comments on commit 241f152

Please sign in to comment.