Skip to content

Commit

Permalink
seperate useDKG and client side mpc key flags
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshuchawla009 committed Sep 9, 2024
1 parent aa8ca1e commit e64adc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/helper/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class CoreKitError extends AbstractCoreKitError {
1004: "OAuth login is NOT supported in this UX mode.",
1005: "No valid storage option found.",
1006: "No data found in storage.",
1007: "Invalid config.",

// TSS and key management errors
1101: "'tssLib' is required when running in this UX mode.",
Expand Down Expand Up @@ -210,6 +211,10 @@ class CoreKitError extends AbstractCoreKitError {
return CoreKitError.fromCode(1006, extraMessage);
}

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

// TSS and key management errors
public static tssLibRequired(extraMessage = ""): ICoreKitError {
return CoreKitError.fromCode(1101, extraMessage);
Expand Down
13 changes: 12 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,20 @@ export interface Web3AuthOptions {
/**
* Set this flag to false to generate keys on client side
* by default keys are generated on using dkg protocol on a distributed network
* @defaultValue false if keyType is ed25519, true for secp256k1 keys
* Note: This option is not supported for ed25519 key type
* @defaultValue `true`
*/
useDKG?: boolean;

/**
* @defaultValue `false` for secp256k1 and `true` for ed25519
* Set this flag to true to use the client generated key for signing
* Note: This option is set to true for ed25519 key type by default to ensure ed25519 mpc key seed exportablity.
* The seed thn can be used for importing user's key other wallets like phantom etc
* If you set this flag to false for ed25519 key type, you will not be able to export the seed and
* only scalar will be exported, scalar can be used for signing outside of this sdk but not for importing the key in other wallets.
*/
useClientGeneratedKey?: boolean;
}

export type Web3AuthOptionsWithDefaults = Required<Web3AuthOptions>;
Expand Down
12 changes: 10 additions & 2 deletions src/mpcCoreKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CoreError } from "@tkey/core";
import { ShareSerializationModule } from "@tkey/share-serialization";
import { TorusStorageLayer } from "@tkey/storage-layer-torus";
import { factorKeyCurve, getPubKeyPoint, lagrangeInterpolation, TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss";
import { SIGNER_MAP } from "@toruslabs/constants";
import { KEY_TYPE, SIGNER_MAP } from "@toruslabs/constants";
import { AGGREGATE_VERIFIER, TORUS_METHOD, TorusAggregateLoginResponse, TorusLoginResponse, UX_MODE } from "@toruslabs/customauth";
import type { UX_MODE_TYPE } from "@toruslabs/customauth/dist/types/utils/enums";
import { Ed25519Curve } from "@toruslabs/elliptic-wrapper";
Expand Down Expand Up @@ -205,6 +205,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
return this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? false : this.options.useDKG;
}

private get useClientGeneratedKey(): boolean {
return this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? true : !!this.options.useDKG;
}

// RecoverTssKey only valid for user that enable MFA where user has 2 type shares :
// TssShareType.DEVICE and TssShareType.RECOVERY
// if the factors key provided is the same type recovery will not works
Expand Down Expand Up @@ -245,6 +249,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
throw CoreKitError.nodeDetailsRetrievalFailed();
}

if (this.keyType === KEY_TYPE.ED25519 && this.useDKG !== undefined) {
throw CoreKitError.invalidConfig("DKG is not supported for ed25519 key type");
}

this.torusSp = new TSSTorusServiceProvider({
customAuthArgs: {
web3AuthClientId: this.options.web3AuthClientId,
Expand Down Expand Up @@ -887,7 +895,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
const existingUser = await this.isMetadataPresent(this.state.postBoxKey);
let importTssKey = providedImportTssKey;
if (!existingUser) {
if (!importTssKey && !this.useDKG) {
if (!importTssKey && this.useClientGeneratedKey) {
if (this.keyType === KeyType.ed25519) {
const k = generateEd25519Seed();
importTssKey = k.toString("hex");
Expand Down

0 comments on commit e64adc6

Please sign in to comment.