From f98c953d04773a3fc916717179dcfa04b9e9dffc Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 15:03:10 +0530 Subject: [PATCH 01/11] make ed25519 to use import flow by default --- demo/redirect-flow-example/package-lock.json | 60 ++++++++++---------- demo/redirect-flow-example/src/App.tsx | 1 + src/interfaces.ts | 4 +- src/mpcCoreKit.ts | 21 ++++++- src/utils.ts | 29 +++++++++- tests/ed25519.spec.ts | 4 +- 6 files changed, 81 insertions(+), 38 deletions(-) diff --git a/demo/redirect-flow-example/package-lock.json b/demo/redirect-flow-example/package-lock.json index 7565d7d..c6980c1 100644 --- a/demo/redirect-flow-example/package-lock.json +++ b/demo/redirect-flow-example/package-lock.json @@ -52,39 +52,39 @@ }, "../..": { "name": "@web3auth/mpc-core-kit", - "version": "3.0.0-alpha.4", + "version": "3.1.0", "license": "ISC", "dependencies": { - "@tkey/common-types": "13.0.0-alpha.6", - "@tkey/core": "13.0.0-alpha.6", - "@tkey/share-serialization": "13.0.0-alpha.6", - "@tkey/storage-layer-torus": "13.0.0-alpha.6", - "@tkey/tss": "13.0.0-alpha.5", - "@toruslabs/constants": "^13.4.0", - "@toruslabs/customauth": "19.0.0-alpha.7", + "@tkey/common-types": "^15.0.2", + "@tkey/core": "^15.0.2", + "@tkey/share-serialization": "^15.0.2", + "@tkey/storage-layer-torus": "^15.0.2", + "@tkey/tss": "^15.0.2", + "@toruslabs/constants": "^14.0.0", + "@toruslabs/customauth": "^20.1.0", "@toruslabs/elliptic-wrapper": "^0.1.0", - "@toruslabs/fetch-node-details": "^13.4.0", - "@toruslabs/fnd-base": "^13.4.0", - "@toruslabs/metadata-helpers": "^5.x", - "@toruslabs/openlogin-session-manager": "^3.1.1", - "@toruslabs/openlogin-utils": "^8.1.2", - "@toruslabs/torus.js": "13.0.0-alpha.7", - "@toruslabs/tss-client": "3.0.0-alpha.0", - "@toruslabs/tss-frost-client": "0.1.1-alpha.0", + "@toruslabs/fetch-node-details": "^14.0.1", + "@toruslabs/fnd-base": "^14.0.0", + "@toruslabs/metadata-helpers": "^6.0.0", + "@toruslabs/openlogin-utils": "^8.2.1", + "@toruslabs/session-manager": "^2.0.0", + "@toruslabs/torus.js": "^15.0.2", + "@toruslabs/tss-client": "^3.0.0", + "@toruslabs/tss-frost-client": "0.2.0", "@toruslabs/tss-frost-common": "^1.0.0", "bn.js": "^5.2.1", "bowser": "^2.11.0", - "elliptic": "^6.5.5", + "elliptic": "^6.5.7", "loglevel": "^1.9.1" }, "devDependencies": { "@babel/register": "^7.24.6", - "@toruslabs/config": "^2.0.2", - "@toruslabs/eslint-config-typescript": "^3.2.0", + "@toruslabs/config": "^2.2.0", + "@toruslabs/eslint-config-typescript": "^3.3.3", "@toruslabs/torus-scripts": "^5.3.1", - "@toruslabs/tss-dkls-lib": "3.0.0-alpha.0", - "@toruslabs/tss-frost-lib": "^0.1.1-alpha.0", - "@toruslabs/tss-lib-node": "^1.1.3", + "@toruslabs/tss-dkls-lib": "^3.0.0", + "@toruslabs/tss-frost-lib": "^0.2.0", + "@toruslabs/tss-lib-node": "^1.2.0", "@types/chai": "^4.3.16", "@types/elliptic": "^6.4.18", "@types/jsonwebtoken": "^9.0.6", @@ -93,20 +93,20 @@ "chai": "^5.1.1", "cross-env": "^7.0.3", "dotenv": "^16.4.5", - "esbuild-register": "^3.5.0", + "esbuild-register": "^3.6.0", "eslint": "^8.56.0", - "husky": "^9.0.11", + "husky": "^9.1.4", "jsonwebtoken": "^9.0.2", - "lint-staged": "^15.2.5", - "mocha": "^10.4.0", + "lint-staged": "^15.2.9", + "mocha": "^10.7.3", "node-fetch": "^3.3.2", - "prettier": "^3.3.0", - "rimraf": "^5.0.7", + "prettier": "^3.3.3", + "rimraf": "^6.0.1", "ts-node": "^10.9.2", "tsconfig-paths": "^4.2.0", "tsconfig-paths-webpack-plugin": "^4.1.0", - "tslib": "^2.6.2", - "typescript": "^5.4.5" + "tslib": "^2.6.3", + "typescript": "^5.5.4" }, "engines": { "node": ">=20.x" diff --git a/demo/redirect-flow-example/src/App.tsx b/demo/redirect-flow-example/src/App.tsx index ccd520c..51c2716 100644 --- a/demo/redirect-flow-example/src/App.tsx +++ b/demo/redirect-flow-example/src/App.tsx @@ -43,6 +43,7 @@ const coreKitInstance = new Web3AuthMPCCoreKit( storage: window.localStorage, // sessionTime: 3600, // <== can provide variable session time based on user subscribed plan tssLib, + useDkg: false } ); diff --git a/src/interfaces.ts b/src/interfaces.ts index 0715138..8bb23fc 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -79,7 +79,7 @@ export type MPCKeyDetails = { tssPubKey?: TkeyPoint; }; -export type OAuthLoginParams = (SubVerifierDetailsParams | AggregateVerifierLoginParams) & { importTssKey?: string; useDkg?: boolean }; +export type OAuthLoginParams = (SubVerifierDetailsParams | AggregateVerifierLoginParams) & { importTssKey?: string }; export type UserInfo = TorusVerifierResponse & LoginWindowResponse; export interface EnableMFAParams { @@ -407,7 +407,7 @@ 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 undefined + * @defaultValue true if keyType is ed25519, false for secp256k1 keys */ useDkg?: boolean; } diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index e7a016b..45a12d2 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -53,6 +53,7 @@ import { Web3AuthState, } from "./interfaces"; import { + bytesToHex, deriveShareCoefficients, ed25519, generateFactorKey, @@ -62,6 +63,7 @@ import { getSessionId, log, parseToken, + randomBytes, sampleEndpoints, scalarBNToBufferSEC1, } from "./utils"; @@ -245,7 +247,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { locationReplaceOnRedirect: true, serverTimeOffset: this.options.serverTimeOffset, keyType: this.keyType, - useDkg: this.options.useDkg, + useDkg: this.keyType === KeyType.ed25519 && this.options.useDkg === undefined ? true : this.options.useDkg, }, }); @@ -312,9 +314,22 @@ export class Web3AuthMPCCoreKit implements ICoreKit { if (this.isNodejsOrRN(this.options.uxMode)) { throw CoreKitError.oauthLoginUnsupported(`Oauth login is NOT supported in ${this.options.uxMode} mode.`); } - const { importTssKey } = params; + const { importTssKey: providedImportTssKey } = params; const tkeyServiceProvider = this.torusSp; + let importTssKey = providedImportTssKey; + + // use import key flow by default for ed25519 + if (!importTssKey && !this.options.useDkg) { + if (this.keyType === KeyType.ed25519) { + importTssKey = bytesToHex(randomBytes(32)); + } else if (this.keyType === KeyType.secp256k1) { + importTssKey = generateFactorKey().private.toString("hex", 64); + } else { + throw CoreKitError.default("Unsupported key type"); + } + } + try { // oAuth login. const verifierParams = params as SubVerifierDetailsParams; @@ -643,7 +658,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { */ public getPubKeyEd25519(): Buffer { const p = this.tkey.tssCurve.keyFromPublic(this.getPubKey()).getPublic(); - return ed25519.keyFromPublic(p).getPublic(); + return ed25519().keyFromPublic(p).getPublic(); } public async sign(data: Buffer, hashed: boolean = false): Promise { diff --git a/src/utils.ts b/src/utils.ts index b149144..04cc3a2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,8 +11,35 @@ import loglevel from "loglevel"; import { DELIMITERS, SCALAR_LEN } from "./constants"; import { CoreKitSigner, EthereumSigner, IAsyncStorage, IStorage } from "./interfaces"; -export const ed25519 = new EDDSA("ed25519"); +export const ed25519 = () => { + return new EDDSA("ed25519"); +}; + +const cr = () => + // We support: 1) browsers 2) node.js 19+ + typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : undefined; + +// Array where index 0xf0 (240) is mapped to string 'f0' +const hexes = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0")); +/** + * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' + */ +export function bytesToHex(bytes: Uint8Array): string { + let hex = ""; + for (let i = 0; i < bytes.length; i++) { + hex += hexes[bytes[i]]; + } + return hex; +} +export const randomBytes = (len = 32): Uint8Array => { + // CSPRNG (random number generator) + const crypto = cr(); // Can be shimmed in node.js <= 18 to prevent error: + // import { webcrypto } from 'node:crypto'; + // if (!globalThis.crypto) globalThis.crypto = webcrypto; + if (!crypto || !crypto.getRandomValues) throw new Error("crypto.getRandomValues must be defined"); + return crypto.getRandomValues(new Uint8Array(len)); +}; export const generateFactorKey = (): { private: BN; pub: TkeyPoint } => { const keyPair = factorKeyCurve.genKeyPair(); const pub = Point.fromElliptic(keyPair.getPublic()); diff --git a/tests/ed25519.spec.ts b/tests/ed25519.spec.ts index 23bf958..208beb5 100644 --- a/tests/ed25519.spec.ts +++ b/tests/ed25519.spec.ts @@ -143,8 +143,8 @@ variable.forEach((testVariable) => { const msg = "hello world"; const msgBuffer = Buffer.from(msg); - const signature = ed25519.makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex")); - const valid = ed25519.verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519()); + const signature = ed25519().makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex")); + const valid = ed25519().verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519()); assert(valid); }); }); From d3ce6aedc85abbbfeafad70b3d2a1097df8594f0 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 15:14:02 +0530 Subject: [PATCH 02/11] add import key flow to loginWithJWt --- src/mpcCoreKit.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 45a12d2..f389962 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -319,7 +319,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit { let importTssKey = providedImportTssKey; - // use import key flow by default for ed25519 if (!importTssKey && !this.options.useDkg) { if (this.keyType === KeyType.ed25519) { importTssKey = bytesToHex(randomBytes(32)); @@ -380,8 +379,17 @@ export class Web3AuthMPCCoreKit implements ICoreKit { throw CoreKitError.prefetchValueExceeded(`The prefetch value '${prefetchTssPublicKeys}' exceeds the maximum allowed limit of 3.`); } - const { verifier, verifierId, idToken, importTssKey } = params; - + const { verifier, verifierId, idToken, importTssKey: providedImportTssKey } = params; + let importTssKey = providedImportTssKey; + if (!importTssKey && !this.options.useDkg) { + if (this.keyType === KeyType.ed25519) { + importTssKey = bytesToHex(randomBytes(32)); + } else if (this.keyType === KeyType.secp256k1) { + importTssKey = generateFactorKey().private.toString("hex", 64); + } else { + throw CoreKitError.default("Unsupported key type"); + } + } this.torusSp.verifierName = verifier; this.torusSp.verifierId = verifierId; From 169ca14766578255f9dd53dd39d50f1bc9bc13f8 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 15:19:01 +0530 Subject: [PATCH 03/11] generate import key only for new users --- src/mpcCoreKit.ts | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index f389962..cacbbbe 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -314,21 +314,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit { if (this.isNodejsOrRN(this.options.uxMode)) { throw CoreKitError.oauthLoginUnsupported(`Oauth login is NOT supported in ${this.options.uxMode} mode.`); } - const { importTssKey: providedImportTssKey } = params; + const { importTssKey } = params; const tkeyServiceProvider = this.torusSp; - let importTssKey = providedImportTssKey; - - if (!importTssKey && !this.options.useDkg) { - if (this.keyType === KeyType.ed25519) { - importTssKey = bytesToHex(randomBytes(32)); - } else if (this.keyType === KeyType.secp256k1) { - importTssKey = generateFactorKey().private.toString("hex", 64); - } else { - throw CoreKitError.default("Unsupported key type"); - } - } - try { // oAuth login. const verifierParams = params as SubVerifierDetailsParams; @@ -379,17 +367,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { throw CoreKitError.prefetchValueExceeded(`The prefetch value '${prefetchTssPublicKeys}' exceeds the maximum allowed limit of 3.`); } - const { verifier, verifierId, idToken, importTssKey: providedImportTssKey } = params; - let importTssKey = providedImportTssKey; - if (!importTssKey && !this.options.useDkg) { - if (this.keyType === KeyType.ed25519) { - importTssKey = bytesToHex(randomBytes(32)); - } else if (this.keyType === KeyType.secp256k1) { - importTssKey = generateFactorKey().private.toString("hex", 64); - } else { - throw CoreKitError.default("Unsupported key type"); - } - } + const { verifier, verifierId, idToken, importTssKey } = params; this.torusSp.verifierName = verifier; this.torusSp.verifierId = verifierId; @@ -894,12 +872,22 @@ export class Web3AuthMPCCoreKit implements ICoreKit { return tssNonce; } - private async setupTkey(importTssKey?: string): Promise { + private async setupTkey(providedImportTssKey?: string): Promise { if (!this.state.postBoxKey) { throw CoreKitError.userNotLoggedIn(); } const existingUser = await this.isMetadataPresent(this.state.postBoxKey); + let importTssKey = providedImportTssKey; if (!existingUser) { + if (!importTssKey && !this.options.useDkg) { + if (this.keyType === KeyType.ed25519) { + importTssKey = bytesToHex(randomBytes(32)); + } else if (this.keyType === KeyType.secp256k1) { + importTssKey = generateFactorKey().private.toString("hex", 64); + } else { + throw CoreKitError.default("Unsupported key type"); + } + } await this.handleNewUser(importTssKey); } else { if (importTssKey) { From 96adb1c8d167249cb1e083208671e512529e66ea Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 22 Aug 2024 12:25:03 +0200 Subject: [PATCH 04/11] cleanup --- src/mpcCoreKit.ts | 11 ++++++----- src/utils.ts | 36 +++++++++++++----------------------- tests/ed25519.spec.ts | 4 ++-- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index cacbbbe..1852ae8 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -53,9 +53,9 @@ import { Web3AuthState, } from "./interfaces"; import { - bytesToHex, deriveShareCoefficients, ed25519, + generateEd25519Seed, generateFactorKey, generateSessionNonce, generateTSSEndpoints, @@ -63,7 +63,6 @@ import { getSessionId, log, parseToken, - randomBytes, sampleEndpoints, scalarBNToBufferSEC1, } from "./utils"; @@ -644,7 +643,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { */ public getPubKeyEd25519(): Buffer { const p = this.tkey.tssCurve.keyFromPublic(this.getPubKey()).getPublic(); - return ed25519().keyFromPublic(p).getPublic(); + return ed25519.keyFromPublic(p).getPublic(); } public async sign(data: Buffer, hashed: boolean = false): Promise { @@ -881,9 +880,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit { if (!existingUser) { if (!importTssKey && !this.options.useDkg) { if (this.keyType === KeyType.ed25519) { - importTssKey = bytesToHex(randomBytes(32)); + const k = generateEd25519Seed(); + importTssKey = k.toString("hex"); } else if (this.keyType === KeyType.secp256k1) { - importTssKey = generateFactorKey().private.toString("hex", 64); + const k = secp256k1.genKeyPair().getPrivate(); + importTssKey = scalarBNToBufferSEC1(k).toString("hex"); } else { throw CoreKitError.default("Unsupported key type"); } diff --git a/src/utils.ts b/src/utils.ts index 04cc3a2..26f326d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,35 +11,25 @@ import loglevel from "loglevel"; import { DELIMITERS, SCALAR_LEN } from "./constants"; import { CoreKitSigner, EthereumSigner, IAsyncStorage, IStorage } from "./interfaces"; -export const ed25519 = () => { - return new EDDSA("ed25519"); -}; - -const cr = () => - // We support: 1) browsers 2) node.js 19+ - typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : undefined; +export const ed25519 = new EDDSA("ed25519"); -// Array where index 0xf0 (240) is mapped to string 'f0' -const hexes = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0")); /** - * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' + * Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS. */ -export function bytesToHex(bytes: Uint8Array): string { - let hex = ""; - for (let i = 0; i < bytes.length; i++) { - hex += hexes[bytes[i]]; +export function randomBytes(bytesLength = 32): Uint8Array { + // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. + const crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : undefined; + + if (crypto && typeof crypto.getRandomValues === "function") { + return crypto.getRandomValues(new Uint8Array(bytesLength)); } - return hex; + throw new Error("crypto.getRandomValues must be defined"); +} + +export function generateEd25519Seed() { + return Buffer.from(randomBytes(32)); } -export const randomBytes = (len = 32): Uint8Array => { - // CSPRNG (random number generator) - const crypto = cr(); // Can be shimmed in node.js <= 18 to prevent error: - // import { webcrypto } from 'node:crypto'; - // if (!globalThis.crypto) globalThis.crypto = webcrypto; - if (!crypto || !crypto.getRandomValues) throw new Error("crypto.getRandomValues must be defined"); - return crypto.getRandomValues(new Uint8Array(len)); -}; export const generateFactorKey = (): { private: BN; pub: TkeyPoint } => { const keyPair = factorKeyCurve.genKeyPair(); const pub = Point.fromElliptic(keyPair.getPublic()); diff --git a/tests/ed25519.spec.ts b/tests/ed25519.spec.ts index 208beb5..23bf958 100644 --- a/tests/ed25519.spec.ts +++ b/tests/ed25519.spec.ts @@ -143,8 +143,8 @@ variable.forEach((testVariable) => { const msg = "hello world"; const msgBuffer = Buffer.from(msg); - const signature = ed25519().makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex")); - const valid = ed25519().verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519()); + const signature = ed25519.makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex")); + const valid = ed25519.verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519()); assert(valid); }); }); From 7acc24acc653e28706f7c50aff2c9466c695eb84 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:19:05 +0530 Subject: [PATCH 05/11] fix ed25519 instance export --- src/mpcCoreKit.ts | 2 +- src/utils.ts | 4 +++- tests/ed25519.spec.ts | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 1852ae8..9cee973 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -643,7 +643,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { */ public getPubKeyEd25519(): Buffer { const p = this.tkey.tssCurve.keyFromPublic(this.getPubKey()).getPublic(); - return ed25519.keyFromPublic(p).getPublic(); + return ed25519().keyFromPublic(p).getPublic(); } public async sign(data: Buffer, hashed: boolean = false): Promise { diff --git a/src/utils.ts b/src/utils.ts index 26f326d..d37210d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,7 +11,9 @@ import loglevel from "loglevel"; import { DELIMITERS, SCALAR_LEN } from "./constants"; import { CoreKitSigner, EthereumSigner, IAsyncStorage, IStorage } from "./interfaces"; -export const ed25519 = new EDDSA("ed25519"); +export const ed25519 = () => { + return new EDDSA("ed25519"); +}; /** * Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS. diff --git a/tests/ed25519.spec.ts b/tests/ed25519.spec.ts index 23bf958..208beb5 100644 --- a/tests/ed25519.spec.ts +++ b/tests/ed25519.spec.ts @@ -143,8 +143,8 @@ variable.forEach((testVariable) => { const msg = "hello world"; const msgBuffer = Buffer.from(msg); - const signature = ed25519.makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex")); - const valid = ed25519.verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519()); + const signature = ed25519().makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex")); + const valid = ed25519().verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519()); assert(valid); }); }); From daca1f2ccbb634a5702e10ed93493e573fa75b18 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:33:12 +0530 Subject: [PATCH 06/11] fix useDKG casing --- src/interfaces.ts | 2 +- src/mpcCoreKit.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 8bb23fc..00e2d7d 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -409,7 +409,7 @@ export interface Web3AuthOptions { * by default keys are generated on using dkg protocol on a distributed network * @defaultValue true if keyType is ed25519, false for secp256k1 keys */ - useDkg?: boolean; + useDKG?: boolean; } export type Web3AuthOptionsWithDefaults = Required; diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 9cee973..0f1faf2 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -246,7 +246,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { locationReplaceOnRedirect: true, serverTimeOffset: this.options.serverTimeOffset, keyType: this.keyType, - useDkg: this.keyType === KeyType.ed25519 && this.options.useDkg === undefined ? true : this.options.useDkg, + useDkg: this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? true : this.options.useDKG, }, }); @@ -878,7 +878,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { const existingUser = await this.isMetadataPresent(this.state.postBoxKey); let importTssKey = providedImportTssKey; if (!existingUser) { - if (!importTssKey && !this.options.useDkg) { + if (!importTssKey && !this.options.useDKG) { if (this.keyType === KeyType.ed25519) { const k = generateEd25519Seed(); importTssKey = k.toString("hex"); From 958edae3e77b0faef930a8eff35ce8800106a644 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:37:08 +0530 Subject: [PATCH 07/11] fix useDKG doc --- src/interfaces.ts | 2 +- src/mpcCoreKit.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 00e2d7d..fa19fa3 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -407,7 +407,7 @@ 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 true if keyType is ed25519, false for secp256k1 keys + * @defaultValue false if keyType is ed25519, true for secp256k1 keys */ useDKG?: boolean; } diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 0f1faf2..66ffe85 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -246,7 +246,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { locationReplaceOnRedirect: true, serverTimeOffset: this.options.serverTimeOffset, keyType: this.keyType, - useDkg: this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? true : this.options.useDKG, + useDkg: this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? false : this.options.useDKG, }, }); From 87577bc4bcbba8ba60cb97095ec307f17f504ed3 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:46:45 +0530 Subject: [PATCH 08/11] fix useDKG check --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c094abc..e3758c4 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "homepage": "https://github.com/Web3Auth/mpc-core-kit/tree/master#readme", "license": "ISC", "scripts": { - "test": "node --test -r esbuild-register tests/*.spec.ts", + "test": "node --test -r esbuild-register tests/backwardCompatible.spec.ts", "dev": "torus-scripts start", "build": "torus-scripts build", "release": "torus-scripts release", From 61944b3496f0af0c7090884a43d63bb808d9b581 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:47:04 +0530 Subject: [PATCH 09/11] fix useDKG check --- src/mpcCoreKit.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 66ffe85..739323e 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -196,6 +196,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit { return this.options.uxMode === UX_MODE.REDIRECT; } + private get useDKG(): boolean { + return this.options.useDKG === undefined ? false : 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 @@ -878,13 +882,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit { const existingUser = await this.isMetadataPresent(this.state.postBoxKey); let importTssKey = providedImportTssKey; if (!existingUser) { - if (!importTssKey && !this.options.useDKG) { + if (!importTssKey && !this.useDKG) { if (this.keyType === KeyType.ed25519) { const k = generateEd25519Seed(); importTssKey = k.toString("hex"); } else if (this.keyType === KeyType.secp256k1) { const k = secp256k1.genKeyPair().getPrivate(); - importTssKey = scalarBNToBufferSEC1(k).toString("hex"); + importTssKey = scalarBNToBufferSEC1(k).toString("hex", 64); } else { throw CoreKitError.default("Unsupported key type"); } From 576228db7031b08d9c7f360c2375e6d42ecb7180 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:50:26 +0530 Subject: [PATCH 10/11] fix test command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e3758c4..c094abc 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "homepage": "https://github.com/Web3Auth/mpc-core-kit/tree/master#readme", "license": "ISC", "scripts": { - "test": "node --test -r esbuild-register tests/backwardCompatible.spec.ts", + "test": "node --test -r esbuild-register tests/*.spec.ts", "dev": "torus-scripts start", "build": "torus-scripts build", "release": "torus-scripts release", From cb5cd7853e6d1f31a46bb472b79d421faf2c1017 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Aug 2024 16:52:43 +0530 Subject: [PATCH 11/11] make useDKG flag dependent on key type --- src/mpcCoreKit.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 739323e..de6a39f 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -197,7 +197,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { } private get useDKG(): boolean { - return this.options.useDKG === undefined ? false : this.options.useDKG; + return this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? false : this.options.useDKG; } // RecoverTssKey only valid for user that enable MFA where user has 2 type shares : @@ -250,7 +250,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { locationReplaceOnRedirect: true, serverTimeOffset: this.options.serverTimeOffset, keyType: this.keyType, - useDkg: this.keyType === KeyType.ed25519 && this.options.useDKG === undefined ? false : this.options.useDKG, + useDkg: this.useDKG, }, });