Skip to content

Commit d42a4d5

Browse files
committed
feat: update interface
1 parent 09c07af commit d42a4d5

File tree

3 files changed

+100
-72
lines changed

3 files changed

+100
-72
lines changed

src/interfaces.ts

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BNString, FactorEnc, KeyType, Point as TkeyPoint, ShareDescriptionMap } from "@tkey/common-types";
1+
import { BNString, KeyType, Point as TkeyPoint, ShareDescriptionMap } from "@tkey/common-types";
22
import { IRemoteClientState, TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss";
33
import { WEB3AUTH_SIG_TYPE } from "@toruslabs/constants";
44
import type {
@@ -13,7 +13,7 @@ import type {
1313
UX_MODE_TYPE,
1414
} from "@toruslabs/customauth";
1515
import { TorusKey } from "@toruslabs/torus.js";
16-
import { Client, PointHex } from "@toruslabs/tss-client";
16+
import { Client } from "@toruslabs/tss-client";
1717
// TODO: move the types to a base class for both dkls and frost in future
1818
import type { tssLib as TssDklsLib } from "@toruslabs/tss-dkls-lib";
1919
import type { tssLib as TssFrostLibEd25519 } from "@toruslabs/tss-frost-lib";
@@ -22,6 +22,7 @@ import { SafeEventEmitter } from "@web3auth/auth";
2222
import BN from "bn.js";
2323

2424
import { FactorKeyTypeShareDescription, TssShareType, USER_PATH, WEB3AUTH_NETWORK } from "./constants";
25+
import { IRemoteSignerContext } from "./plugins/IRemoteSigner";
2526
import { ISessionSigGenerator } from "./plugins/ISessionSigGenerator";
2627

2728
export type CoreKitMode = UX_MODE_TYPE | "nodejs" | "react-native";
@@ -337,7 +338,7 @@ export interface Web3AuthOptions {
337338
}
338339
export type Web3AuthOptionsWithDefaults = Required<Web3AuthOptions>;
339340

340-
export interface IMPCContext {
341+
export interface IMPCContext extends IRemoteSignerContext {
341342
stateEmitter: SafeEventEmitter;
342343
config: Web3AuthOptionsWithDefaults;
343344
status: COREKIT_STATUS;
@@ -544,59 +545,3 @@ export interface EthereumSigner {
544545
export type StateEmitterEvents = {
545546
LOGOUT: () => void;
546547
};
547-
548-
type SupportedCurve = "secp256k1" | "ed25519";
549-
// remote signer interface
550-
export type RemoteDklsSignParams = {
551-
factorEnc?: FactorEnc;
552-
sessionId: string;
553-
tssNonce: number;
554-
accountNonce: string;
555-
tssPubKeyHex: string;
556-
557-
nodeIndexes: number[];
558-
tssCommits: PointHex[];
559-
560-
signatures: string[];
561-
562-
serverEndpoints: {
563-
endpoints: string[];
564-
tssWSEndpoints: string[];
565-
partyIndexes: number[];
566-
};
567-
568-
curve: SupportedCurve;
569-
};
570-
571-
export type ICustomFrostSignParams = {
572-
sessionId: string;
573-
signatures: string[];
574-
tssCommits: PointHex[];
575-
factorEnc: FactorEnc;
576-
tssPubKeyHex: string;
577-
curve: SupportedCurve;
578-
579-
serverXCoords: number[];
580-
clientXCoord: number;
581-
serverCoefficients: string[];
582-
clientCoefficient: string;
583-
serverURLs: string[];
584-
};
585-
586-
export interface ICustomDklsSignParams {
587-
sessionId: string;
588-
signatures: string[];
589-
tssCommits: PointHex[];
590-
factorEnc: FactorEnc;
591-
tssPubKeyHex: string;
592-
curve: SupportedCurve;
593-
594-
participatingServerDKGIndexes: number[];
595-
clientIndex: number;
596-
tssNonce: string;
597-
accountNonce: string;
598-
599-
endpoints: string[];
600-
tssWSEndpoints: string[];
601-
partyIndexes: number[];
602-
}

src/mpcCoreKit.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ import {
4949
CreateFactorParams,
5050
EnableMFAParams,
5151
ICoreKit,
52-
ICustomDklsSignParams,
53-
ICustomFrostSignParams,
5452
IFactorKey,
5553
IMPCContext,
5654
InitParams,
@@ -71,6 +69,7 @@ import {
7169
Web3AuthState,
7270
} from "./interfaces";
7371
import { DefaultSessionSigGeneratorPlugin } from "./plugins/DefaultSessionSigGenerator";
72+
import { ICustomDklsSignParams, ICustomFrostSignParams } from "./plugins/IRemoteSigner";
7473
import { ISessionSigGenerator } from "./plugins/ISessionSigGenerator";
7574
import {
7675
deriveShareCoefficients,
@@ -732,6 +731,20 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
732731
return ed25519().keyFromPublic(p).getPublic();
733732
}
734733

734+
/**
735+
* Get public key in bip340 format.
736+
*
737+
* Throws an error if signature type is not bip340.
738+
*/
739+
public getPubKeyBip340(): Buffer {
740+
if (this._sigType !== "bip340") {
741+
throw CoreKitError.default(`getPubKeyBip340 not supported for signature type ${this.sigType}`);
742+
}
743+
744+
const p = this.tkey.tssCurve.keyFromPublic(this.getPubKey()).getPublic();
745+
return p.getX().toBuffer("be", 32);
746+
}
747+
735748
public async preSetupSigning(): Promise<ICustomDklsSignParams> {
736749
const { torusNodeTSSEndpoints } = fetchLocalConfig(this.options.web3AuthNetwork, this.keyType);
737750

@@ -742,7 +755,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
742755
}
743756

744757
const tssNonce = this.getTssNonce() || 0;
745-
746758
const vid = `${this.verifier}${DELIMITERS.Delimiter1}${this.verifierId}`;
747759
const sessionId = `${vid}${DELIMITERS.Delimiter2}default${DELIMITERS.Delimiter3}${tssNonce}${DELIMITERS.Delimiter4}`;
748760

@@ -764,7 +776,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
764776
nodeIndexesReturned: participatingServerDKGIndexes,
765777
} = generateTSSEndpoints(torusNodeTSSEndpoints, parties, clientIndex, nodeIndexes);
766778

767-
const factor = Point.fromSEC1(secp256k1, this.state.remoteClient?.remoteFactorPub);
779+
const factor = this.state.remoteClient?.remoteFactorPub
780+
? Point.fromSEC1(secp256k1, this.state.remoteClient?.remoteFactorPub)
781+
: Point.fromScalar(this.state.factorKey, secp256k1);
768782
const factorEnc = this.tKey.getFactorEncs(factor);
769783

770784
// Compute account nonce only supported for secp256k1
@@ -1088,6 +1102,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
10881102

10891103
async setupRemoteSigning(params: IRemoteClientState, rehydrate: boolean = false): Promise<void> {
10901104
const { remoteFactorPub, metadataShare } = params;
1105+
1106+
// rehydrate session
1107+
if (rehydrate) {
1108+
this.updateState({ remoteClient: params });
1109+
const sessionResult = await this.sessionManager.authorizeSession().catch(async (err) => {
1110+
log.error("rehydrate session error", err);
1111+
});
1112+
if (sessionResult) {
1113+
await this.rehydrateSession(sessionResult);
1114+
}
1115+
}
1116+
10911117
const details = this.getKeyDetails().shareDescriptions[remoteFactorPub];
10921118
if (!details) throw CoreKitError.default("factor description not found");
10931119

@@ -1225,7 +1251,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
12251251
tssPubKey: Buffer.from(tssPubKey).toString("hex"),
12261252
signatures: await this.getSessionSignatures(),
12271253
userInfo,
1228-
remoteClientState: this.state.remoteClient,
12291254
};
12301255
await this.sessionManager.createSession(payload);
12311256
// to accommodate async storage
@@ -1364,7 +1389,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13641389
}
13651390

13661391
const factorKey = new BN(result.factorKey, "hex");
1367-
if (!factorKey && !result.remoteClientState?.metadataShare) {
1392+
if (!result.factorKey && !this.state.remoteClient.metadataShare) {
13681393
throw CoreKitError.providedFactorKeyInvalid();
13691394
}
13701395

@@ -1374,13 +1399,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13741399

13751400
await this.tKey.initialize({ neverInitializeNewKey: true });
13761401

1377-
// skip input share store if factor key is not present
1378-
// tkey will be at state initalized
1379-
if (!result.factorKey) {
1380-
return;
1381-
}
1402+
const metadataShareStore = this.state.remoteClient?.metadataShare
1403+
? ShareStore.fromJSON(JSON.parse(this.state.remoteClient.metadataShare))
1404+
: await this.getFactorKeyMetadata(factorKey);
13821405

1383-
const metadataShareStore = await this.getFactorKeyMetadata(factorKey);
13841406
await this.tKey.inputShareStoreSafe(metadataShareStore, true);
13851407
await this.tKey.reconstructKey();
13861408

@@ -1392,7 +1414,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13921414
tssPubKey: this.tkey.getTSSPub().toSEC1(this.tKey.tssCurve, false),
13931415
signatures: result.signatures,
13941416
userInfo: result.userInfo,
1395-
remoteClient: result.remoteClientState,
13961417
});
13971418
} catch (err) {
13981419
log.warn("failed to authorize session", err);

src/plugins/IRemoteSigner.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { FactorEnc, Point, ShareDescriptionMap } from "@tkey/common-types";
2+
import { IRemoteClientState } from "@tkey/tss";
3+
import { PointHex } from "@toruslabs/tss-client";
4+
5+
import { CreateFactorParams, WEB3AUTH_NETWORK_TYPE } from "../interfaces";
6+
7+
type SupportedCurve = "secp256k1" | "ed25519";
8+
9+
export type ICustomFrostSignParams = {
10+
sessionId: string;
11+
signatures: string[];
12+
tssCommits: PointHex[];
13+
factorEnc: FactorEnc;
14+
tssPubKeyHex: string;
15+
curve: SupportedCurve;
16+
17+
serverXCoords: number[];
18+
clientXCoord: number;
19+
serverCoefficients: string[];
20+
clientCoefficient: string;
21+
serverURLs: string[];
22+
};
23+
24+
export interface ICustomDklsSignParams {
25+
sessionId: string;
26+
signatures: string[];
27+
tssCommits: PointHex[];
28+
factorEnc: FactorEnc;
29+
tssPubKeyHex: string;
30+
curve: SupportedCurve;
31+
32+
participatingServerDKGIndexes: number[];
33+
clientIndex: number;
34+
tssNonce: string;
35+
accountNonce: string;
36+
37+
endpoints: string[];
38+
tssWSEndpoints: string[];
39+
partyIndexes: number[];
40+
}
41+
42+
export interface ICustomDKLSSign {
43+
sign: (params: ICustomDklsSignParams, msgHash: Uint8Array) => Promise<{ v: number; r: Uint8Array; s: Uint8Array }>;
44+
}
45+
46+
export interface ICustomFrostSign {
47+
sign: (params: ICustomFrostSignParams, msgHash: Uint8Array) => Promise<Uint8Array>;
48+
}
49+
export interface IRemoteSignerContext {
50+
setupRemoteSigning(params: Omit<IRemoteClientState, "tssShareIndex">, rehydrate?: boolean): Promise<void>;
51+
createFactor(createFactorParams: CreateFactorParams): Promise<string>;
52+
inputFactorKey(factorKey: string): Promise<void>;
53+
deleteFactor(factorPub: Point, factorKey?: string): Promise<void>;
54+
getKeyDetails(): Record<string, unknown> & { shareDescriptions: ShareDescriptionMap };
55+
getMetadataKey(): string | undefined;
56+
getMetadataPublicKey(): string | undefined;
57+
getWeb3AuthNetwork(): WEB3AUTH_NETWORK_TYPE;
58+
59+
// Added new methods
60+
setCustomDKLSSign(customDKLSSign: ICustomDKLSSign): void;
61+
setCustomFrostSign(customDKLSSign: ICustomFrostSign): void;
62+
}

0 commit comments

Comments
 (0)