Skip to content

Commit e43fa47

Browse files
custom session sig setter
1 parent 78457af commit e43fa47

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,5 @@ export interface EthereumSigner {
477477
export interface Secp256k1PrecomputedClient {
478478
client: Client;
479479
serverCoeffs: Record<string, string>;
480+
signatures: string[];
480481
}

src/mpcCoreKit.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
7575

7676
public torusSp: TSSTorusServiceProvider | null = null;
7777

78+
public fetchSessionSignatures: () => Promise<string[]>;
79+
7880
private options: Web3AuthOptionsWithDefaults;
7981

8082
private storageLayer: TorusStorageLayer | null = null;
@@ -124,7 +126,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
124126
if (!options.disableHashedFactorKey) options.disableHashedFactorKey = false;
125127
if (!options.hashedFactorNonce) options.hashedFactorNonce = options.web3AuthClientId;
126128
if (options.disableSessionManager === undefined) options.disableSessionManager = false;
127-
129+
this.fetchSessionSignatures = () => Promise.resolve(this.signatures);
128130
this.options = options as Web3AuthOptionsWithDefaults;
129131

130132
this.currentStorage = new AsyncStorage(this._storageBaseKey, options.storage);
@@ -153,6 +155,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
153155
return this.state?.signatures ? this.state.signatures : [];
154156
}
155157

158+
get config(): Web3AuthOptionsWithDefaults {
159+
return this.options;
160+
}
161+
156162
public get _storageKey(): string {
157163
return this._storageBaseKey;
158164
}
@@ -197,6 +203,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
197203
return this.keyType === KeyType.ed25519 && this.options.useClientGeneratedTSSKey === undefined ? true : !!this.options.useClientGeneratedTSSKey;
198204
}
199205

206+
public setSessionSigGenerator(sessionSigGenerator: () => Promise<string[]>) {
207+
this.fetchSessionSignatures = sessionSigGenerator;
208+
}
209+
200210
// RecoverTssKey only valid for user that enable MFA where user has 2 type shares :
201211
// TssShareType.DEVICE and TssShareType.RECOVERY
202212
// if the factors key provided is the same type recovery will not works
@@ -331,12 +341,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
331341

332342
if (this.isRedirectMode) return;
333343

334-
this.updateState({
335-
postBoxKey: this._getPostBoxKey(loginResponse),
336-
postboxKeyNodeIndexes: loginResponse.nodesData?.nodeIndexes,
337-
userInfo: loginResponse.userInfo,
338-
signatures: this._getSignatures(loginResponse.sessionData.sessionTokenData),
339-
});
344+
await this._finalizeOauthLogin(loginResponse, loginResponse.userInfo, true, importTssKey);
340345
} else if (aggregateParams.subVerifierDetailsArray) {
341346
const loginResponse = await tkeyServiceProvider.triggerAggregateLogin({
342347
aggregateVerifierType: aggregateParams.aggregateVerifierType || AGGREGATE_VERIFIER.SINGLE_VERIFIER_ID,
@@ -346,15 +351,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
346351

347352
if (this.isRedirectMode) return;
348353

349-
this.updateState({
350-
postBoxKey: this._getPostBoxKey(loginResponse),
351-
postboxKeyNodeIndexes: loginResponse.nodesData?.nodeIndexes,
352-
userInfo: loginResponse.userInfo[0],
353-
signatures: this._getSignatures(loginResponse.sessionData.sessionTokenData),
354-
});
354+
await this._finalizeOauthLogin(loginResponse, loginResponse.userInfo[0], true, importTssKey);
355355
}
356-
357-
await this.setupTkey(importTssKey);
358356
} catch (err: unknown) {
359357
log.error("login error", err);
360358
if (err instanceof CoreError) {
@@ -401,18 +399,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
401399

402400
// wait for prefetch completed before setup tkey
403401
const [loginResponse] = await Promise.all([loginPromise, ...prefetchTssPubs]);
404-
405-
const postBoxKey = this._getPostBoxKey(loginResponse);
406-
407-
this.torusSp.postboxKey = new BN(postBoxKey, "hex");
408-
409-
this.updateState({
410-
postBoxKey,
411-
postboxKeyNodeIndexes: loginResponse.nodesData?.nodeIndexes || [],
412-
userInfo: { ...parseToken(idToken), verifier, verifierId },
413-
signatures: this._getSignatures(loginResponse.sessionData.sessionTokenData),
414-
});
415-
await this.setupTkey(importTssKey);
402+
const userInfo = { ...parseToken(idToken), verifier, verifierId };
403+
await this._finalizeOauthLogin(loginResponse, userInfo, true, importTssKey);
416404
} catch (err: unknown) {
417405
log.error("login error", err);
418406
if (err instanceof CoreError) {
@@ -478,6 +466,20 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
478466
}
479467
}
480468

469+
public async _finalizeOauthLogin(loginResponse: TorusKey, userInfo: UserInfo, persistSessionSigs = true, importTssKey?: string): Promise<void> {
470+
const postBoxKey = this._getPostBoxKey(loginResponse);
471+
this.torusSp.postboxKey = new BN(postBoxKey, "hex");
472+
473+
this.updateState({
474+
postBoxKey,
475+
postboxKeyNodeIndexes: loginResponse.nodesData?.nodeIndexes,
476+
userInfo,
477+
signatures: persistSessionSigs ? this._getSignatures(loginResponse.sessionData.sessionTokenData) : [],
478+
});
479+
480+
await this.setupTkey(importTssKey);
481+
}
482+
481483
public async inputFactorKey(factorKey: BN): Promise<void> {
482484
this.checkReady();
483485
try {
@@ -658,6 +660,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
658660
public async precompute_secp256k1(): Promise<{
659661
client: Client;
660662
serverCoeffs: Record<string, string>;
663+
signatures: string[];
661664
}> {
662665
this.wasmLib = await this.loadTssWasm();
663666
// PreSetup
@@ -707,7 +710,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
707710
throw CoreKitError.activeSessionNotFound();
708711
}
709712

710-
const { signatures } = this;
713+
const signatures = await this.fetchSessionSignatures();
711714
if (!signatures) {
712715
throw CoreKitError.signaturesNotPresent();
713716
}
@@ -740,6 +743,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
740743
return {
741744
client,
742745
serverCoeffs,
746+
signatures,
743747
};
744748
}
745749

@@ -774,7 +778,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
774778
throw CoreKitError.factorInUseCannotBeDeleted("Cannot delete current active factor");
775779
}
776780

777-
await this.tKey.deleteFactorPub({ factorKey: this.state.factorKey, deleteFactorPub: factorPub, authSignatures: this.signatures });
781+
const authSignatures = await this.fetchSessionSignatures();
782+
await this.tKey.deleteFactorPub({ factorKey: this.state.factorKey, deleteFactorPub: factorPub, authSignatures });
778783
const factorPubHex = fpp.toSEC1(factorKeyCurve, true).toString("hex");
779784
const allDesc = this.tKey.metadata.getShareDescription();
780785
const keyDesc = allDesc[factorPubHex];
@@ -1224,11 +1229,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
12241229
if (this.tKey.metadata.factorPubs[this.tKey.tssTag].length >= MAX_FACTORS) {
12251230
throw CoreKitError.maximumFactorsReached(`The maximum number of allowable factors (${MAX_FACTORS}) has been reached.`);
12261231
}
1227-
1232+
const authSignatures = await this.fetchSessionSignatures();
12281233
// Generate new share.
12291234
await this.tkey.addFactorPub({
12301235
existingFactorKey: this.state.factorKey,
1231-
authSignatures: this.signatures,
1236+
authSignatures,
12321237
newFactorPub,
12331238
newTSSIndex: newFactorTSSIndex,
12341239
refreshShares: this.state.tssShareIndex !== newFactorTSSIndex, // Refresh shares if we have a new factor key index.
@@ -1364,9 +1369,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
13641369
}
13651370

13661371
const isAlreadyPrecomputed = precomputedTssClient?.client && precomputedTssClient?.serverCoeffs;
1367-
const { client, serverCoeffs } = isAlreadyPrecomputed ? precomputedTssClient : await this.precompute_secp256k1();
1372+
const { client, serverCoeffs, signatures } = isAlreadyPrecomputed ? precomputedTssClient : await this.precompute_secp256k1();
13681373

1369-
const { signatures } = this;
13701374
if (!signatures) {
13711375
throw CoreKitError.signaturesNotPresent();
13721376
}
@@ -1430,10 +1434,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
14301434
const serverURLs = endpoints.map((x) => x.url);
14311435
const pubKeyHex = ec.pointToBuffer(tssPubKeyPoint, Buffer).toString("hex");
14321436
const serverCoefficientsHex = serverCoefficients.map((c) => ec.scalarToBuffer(c, Buffer).toString("hex"));
1437+
const authSignatures = await this.fetchSessionSignatures();
1438+
14331439
const signature = await signEd25519(
14341440
this.wasmLib as FrostWasmLib,
14351441
session,
1436-
this.signatures,
1442+
authSignatures,
14371443
serverXCoords,
14381444
serverURLs,
14391445
clientXCoord,

0 commit comments

Comments
 (0)