Skip to content

Commit 9556451

Browse files
committed
feat: update interface
1 parent 49d2251 commit 9556451

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;
@@ -527,59 +528,3 @@ export interface Secp256k1PrecomputedClient {
527528
export type StateEmitterEvents = {
528529
LOGOUT: () => void;
529530
};
530-
531-
type SupportedCurve = "secp256k1" | "ed25519";
532-
// remote signer interface
533-
export type RemoteDklsSignParams = {
534-
factorEnc?: FactorEnc;
535-
sessionId: string;
536-
tssNonce: number;
537-
accountNonce: string;
538-
tssPubKeyHex: string;
539-
540-
nodeIndexes: number[];
541-
tssCommits: PointHex[];
542-
543-
signatures: string[];
544-
545-
serverEndpoints: {
546-
endpoints: string[];
547-
tssWSEndpoints: string[];
548-
partyIndexes: number[];
549-
};
550-
551-
curve: SupportedCurve;
552-
};
553-
554-
export type ICustomFrostSignParams = {
555-
sessionId: string;
556-
signatures: string[];
557-
tssCommits: PointHex[];
558-
factorEnc: FactorEnc;
559-
tssPubKeyHex: string;
560-
curve: SupportedCurve;
561-
562-
serverXCoords: number[];
563-
clientXCoord: number;
564-
serverCoefficients: string[];
565-
clientCoefficient: string;
566-
serverURLs: string[];
567-
};
568-
569-
export interface ICustomDklsSignParams {
570-
sessionId: string;
571-
signatures: string[];
572-
tssCommits: PointHex[];
573-
factorEnc: FactorEnc;
574-
tssPubKeyHex: string;
575-
curve: SupportedCurve;
576-
577-
participatingServerDKGIndexes: number[];
578-
clientIndex: number;
579-
tssNonce: string;
580-
accountNonce: string;
581-
582-
endpoints: string[];
583-
tssWSEndpoints: string[];
584-
partyIndexes: number[];
585-
}

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
@@ -1061,6 +1075,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
10611075

10621076
async setupRemoteSigning(params: IRemoteClientState, rehydrate: boolean = false): Promise<void> {
10631077
const { remoteFactorPub, metadataShare } = params;
1078+
1079+
// rehydrate session
1080+
if (rehydrate) {
1081+
this.updateState({ remoteClient: params });
1082+
const sessionResult = await this.sessionManager.authorizeSession().catch(async (err) => {
1083+
log.error("rehydrate session error", err);
1084+
});
1085+
if (sessionResult) {
1086+
await this.rehydrateSession(sessionResult);
1087+
}
1088+
}
1089+
10641090
const details = this.getKeyDetails().shareDescriptions[remoteFactorPub];
10651091
if (!details) throw CoreKitError.default("factor description not found");
10661092

@@ -1198,7 +1224,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
11981224
tssPubKey: Buffer.from(tssPubKey).toString("hex"),
11991225
signatures: await this.getSessionSignatures(),
12001226
userInfo,
1201-
remoteClientState: this.state.remoteClient,
12021227
};
12031228
await this.sessionManager.createSession(payload);
12041229
// to accommodate async storage
@@ -1337,7 +1362,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13371362
}
13381363

13391364
const factorKey = new BN(result.factorKey, "hex");
1340-
if (!factorKey && !result.remoteClientState?.metadataShare) {
1365+
if (!result.factorKey && !this.state.remoteClient.metadataShare) {
13411366
throw CoreKitError.providedFactorKeyInvalid();
13421367
}
13431368

@@ -1347,13 +1372,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13471372

13481373
await this.tKey.initialize({ neverInitializeNewKey: true });
13491374

1350-
// skip input share store if factor key is not present
1351-
// tkey will be at state initalized
1352-
if (!result.factorKey) {
1353-
return;
1354-
}
1375+
const metadataShareStore = this.state.remoteClient?.metadataShare
1376+
? ShareStore.fromJSON(JSON.parse(this.state.remoteClient.metadataShare))
1377+
: await this.getFactorKeyMetadata(factorKey);
13551378

1356-
const metadataShareStore = await this.getFactorKeyMetadata(factorKey);
13571379
await this.tKey.inputShareStoreSafe(metadataShareStore, true);
13581380
await this.tKey.reconstructKey();
13591381

@@ -1365,7 +1387,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13651387
tssPubKey: this.tkey.getTSSPub().toSEC1(this.tKey.tssCurve, false),
13661388
signatures: result.signatures,
13671389
userInfo: result.userInfo,
1368-
remoteClient: result.remoteClientState,
13691390
});
13701391
} catch (err) {
13711392
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)