From 7f72f57f8dd81af57ba59607c321bb0aa5cfcd09 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Thu, 23 Nov 2023 12:10:32 +0530 Subject: [PATCH 01/22] Add latest SFA Service Provider package --- package-lock.json | 48 +++++++--- packages/service-provider-sfa/README.md | 27 ++++++ packages/service-provider-sfa/package.json | 63 +++++++++++++ .../src/SfaServiceProvider.ts | 94 +++++++++++++++++++ packages/service-provider-sfa/src/index.ts | 1 + .../service-provider-sfa/src/interfaces.ts | 30 ++++++ packages/service-provider-sfa/torus.config.js | 1 + packages/service-provider-sfa/tsconfig.json | 4 + .../service-provider-sfa/webpack.config.js | 6 ++ 9 files changed, 262 insertions(+), 12 deletions(-) create mode 100644 packages/service-provider-sfa/README.md create mode 100644 packages/service-provider-sfa/package.json create mode 100644 packages/service-provider-sfa/src/SfaServiceProvider.ts create mode 100644 packages/service-provider-sfa/src/index.ts create mode 100644 packages/service-provider-sfa/src/interfaces.ts create mode 100644 packages/service-provider-sfa/torus.config.js create mode 100644 packages/service-provider-sfa/tsconfig.json create mode 100644 packages/service-provider-sfa/webpack.config.js diff --git a/package-lock.json b/package-lock.json index ec26058bb..7ea6643b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3956,6 +3956,10 @@ "resolved": "packages/service-provider-base", "link": true }, + "node_modules/@tkey/service-provider-sfa": { + "resolved": "packages/service-provider-sfa", + "link": true + }, "node_modules/@tkey/service-provider-torus": { "resolved": "packages/service-provider-torus", "link": true @@ -4024,9 +4028,9 @@ } }, "node_modules/@toruslabs/constants": { - "version": "13.0.2", - "resolved": "https://registry.npmjs.org/@toruslabs/constants/-/constants-13.0.2.tgz", - "integrity": "sha512-QcW1gH2tLI/8j4/s5XZUbeBNCXvpwJrE8Ui8S59NLsLKdrntfIKvs8nK9BDYAruwjTYG7z4dhqvE68mAmF2tjQ==", + "version": "13.0.4", + "resolved": "https://registry.npmjs.org/@toruslabs/constants/-/constants-13.0.4.tgz", + "integrity": "sha512-5RThL0rNl5o5sWG9p+s1NPxUOS+nlDn7cTd2wL+hE/Zm0F+p67ZFV16Emr90RAPpiYgZBngzpNAmCZ7xcgktQw==", "engines": { "node": ">=18.x", "npm": ">=9.x" @@ -4114,12 +4118,12 @@ } }, "node_modules/@toruslabs/fetch-node-details": { - "version": "13.0.2", - "resolved": "https://registry.npmjs.org/@toruslabs/fetch-node-details/-/fetch-node-details-13.0.2.tgz", - "integrity": "sha512-0WXglIa2qDeerlMJkEJeEur9TvIPODUT7QqRgwSl/p0EOrpWc4p777szTofdn6cXHzVqIx90TbyOsOPs7bbViA==", + "version": "13.0.4", + "resolved": "https://registry.npmjs.org/@toruslabs/fetch-node-details/-/fetch-node-details-13.0.4.tgz", + "integrity": "sha512-6xOOTKJuqmHeF3IodNWGcAA2BHBcgiCvHoc0u8QfCU0KCOvgD+eRV352aHf7nzVTkeokE9WLAqY93/Wni5iOjA==", "dependencies": { - "@toruslabs/constants": "^13.0.2", - "@toruslabs/fnd-base": "^13.0.2", + "@toruslabs/constants": "^13.0.4", + "@toruslabs/fnd-base": "^13.0.4", "@toruslabs/http-helpers": "^5.0.0", "loglevel": "^1.8.1" }, @@ -4132,11 +4136,11 @@ } }, "node_modules/@toruslabs/fnd-base": { - "version": "13.0.2", - "resolved": "https://registry.npmjs.org/@toruslabs/fnd-base/-/fnd-base-13.0.2.tgz", - "integrity": "sha512-FLrs6BBFxiUWprnmdjm2k+fYwVdhP2h0OnIUEBVSXngzlJ3G+Qhj10PoOrWXhxAD3Ifbw5Kw3LorW158eazkRQ==", + "version": "13.0.4", + "resolved": "https://registry.npmjs.org/@toruslabs/fnd-base/-/fnd-base-13.0.4.tgz", + "integrity": "sha512-RuOm0jSiWK8IkR2nhqdLYU33JsGDgC9e5rG9PqobfXD3C/jsH6EfR6BaYBdunG0rQo626qPeOeiW7bIFpfJjbA==", "dependencies": { - "@toruslabs/constants": "^13.0.2" + "@toruslabs/constants": "^13.0.4" }, "engines": { "node": ">=18.x", @@ -23293,6 +23297,26 @@ "@babel/runtime": "7.x" } }, + "packages/service-provider-sfa": { + "version": "12.0.0", + "license": "MIT", + "dependencies": { + "@tkey/service-provider-base": "^12.0.0", + "@toruslabs/fetch-node-details": "^13.0.4", + "@toruslabs/torus.js": "^12.0.1", + "bn.js": "^5.2.1" + }, + "devDependencies": { + "@types/bn.js": "^5.1.2" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, "packages/service-provider-torus": { "name": "@tkey/service-provider-torus", "version": "12.0.0", diff --git a/packages/service-provider-sfa/README.md b/packages/service-provider-sfa/README.md new file mode 100644 index 000000000..2e94d6c6a --- /dev/null +++ b/packages/service-provider-sfa/README.md @@ -0,0 +1,27 @@ +# tKey Single Factor Auth Service Provider + +[![npm version](https://img.shields.io/npm/v/@tkey/service-provider-sfa?label=%22%22)](https://www.npmjs.com/package/@tkey/service-provider-sfa/v/latest) [![minzip](https://img.shields.io/bundlephobia/minzip/@tkey/service-provider-sfa?label=%22%22)](https://bundlephobia.com/result?p=@tkey/service-provider-sfa@latest) + +Service Provider in `tKey` is used for generating a social login share of the private key share managed by a wallet service provider via +their own authentication flows. + +## Installation + +```shell +npm install --save @tkey/service-provider-sfa +``` + +### See the full [SDK Reference](https://web3auth.io/docs/sdk/core-kit/tkey/usage#log-in) on the Web3Auth Documentation + +## Example + +```js +import SFAServiceProvider from '@tkey/service-provider-sfa'; + +const web3AuthOptions: any = { + clientId, // Get your Client ID from Web3Auth Dashboard + web3AuthNetwork: 'testnet', // ["cyan", "testnet", "mainnet", "aqua", "sapphire_devnet", "sapphire_mainnet"] +}; + +const serviceProvider = new SFAServiceProvider({web3AuthOptions}); +``` diff --git a/packages/service-provider-sfa/package.json b/packages/service-provider-sfa/package.json new file mode 100644 index 000000000..20b07143f --- /dev/null +++ b/packages/service-provider-sfa/package.json @@ -0,0 +1,63 @@ +{ + "name": "@tkey/service-provider-sfa", + "version": "12.0.0", + "description": "TKey Torus Service Provider Module", + "author": "Torus Labs", + "homepage": "https://github.com/tkey/tkey#readme", + "license": "MIT", + "main": "dist/serviceProviderSfa.cjs.js", + "module": "dist/serviceProviderSfa.esm.js", + "unpkg": "dist/serviceProviderSfa.umd.min.js", + "jsdelivr": "dist/serviceProviderSfa.umd.min.js", + "types": "dist/types/index.d.ts", + "files": [ + "dist", + "src" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/tkey/tkey.git" + }, + "scripts": { + "test": "cross-env MOCKED=true mocha --config ../../.mocharc.json ", + "coverage": "nyc npm test", + "coverage-production": "nyc npm run test-production", + "test-development": "cross-env MOCKED=false METADATA=http://localhost:5051 mocha --config ../../.mocharc.json ", + "test-production": "cross-env MOCKED=false METADATA=https://metadata.tor.us mocha --config ../../.mocharc.json ", + "test-debugger": "mocha --config ../../.mocharc.json --inspect-brk", + "dev": "rimraf dist/ && cross-env NODE_ENV=development torus-scripts build", + "build": "rimraf dist/ && cross-env NODE_ENV=production torus-scripts build", + "lint": "eslint --fix 'src/**/*.ts'", + "prepack": "npm run build", + "pre-commit": "lint-staged" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + }, + "dependencies": { + "@tkey/service-provider-base": "^12.0.0", + "@toruslabs/fetch-node-details": "^13.0.4", + "@toruslabs/torus.js": "^12.0.1", + "bn.js": "^5.2.1" + }, + "devDependencies": { + "@types/bn.js": "^5.1.2" + }, + "bugs": { + "url": "https://github.com/tkey/tkey/issues" + }, + "lint-staged": { + "!(*d).ts": [ + "npm run lint --", + "prettier --write 'src/**/*.ts'" + ] + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "gitHead": "9967ce9f795f495f28ef0da1fc50acde31dcc258" +} diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts new file mode 100644 index 000000000..7aff8c30a --- /dev/null +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -0,0 +1,94 @@ +import { type StringifiedType } from "@tkey/common-types"; +import { ServiceProviderBase } from "@tkey/service-provider-base"; +import { NodeDetailManager } from "@toruslabs/fetch-node-details"; +import Torus, { keccak256, TorusKey } from "@toruslabs/torus.js"; +import BN from "bn.js"; + +import { AggregateVerifierParams, LoginParams, SfaServiceProviderArgs, Web3AuthOptions } from "./interfaces"; + +class SfaServiceProvider extends ServiceProviderBase { + web3AuthOptions: Web3AuthOptions; + + authInstance: Torus; + + public torusKey: TorusKey; + + private nodeDetailManagerInstance: NodeDetailManager; + + constructor({ enableLogging = false, postboxKey, web3AuthOptions }: SfaServiceProviderArgs) { + super({ enableLogging, postboxKey }); + this.web3AuthOptions = web3AuthOptions; + this.authInstance = new Torus({ + clientId: web3AuthOptions.clientId, + enableOneKey: true, + network: web3AuthOptions.network, + }); + Torus.enableLogging(enableLogging); + this.serviceProviderName = "SfaServiceProvider"; + this.nodeDetailManagerInstance = new NodeDetailManager({ network: web3AuthOptions.network, enableLogging }); + } + + static fromJSON(value: StringifiedType): SfaServiceProvider { + const { enableLogging, postboxKey, web3AuthOptions, serviceProviderName, torusKey } = value; + if (serviceProviderName !== "SfaServiceProvider") return undefined; + + const sfaSP = new SfaServiceProvider({ + enableLogging, + postboxKey, + web3AuthOptions, + }); + + sfaSP.torusKey = torusKey; + + return sfaSP; + } + + async connect(params: LoginParams): Promise { + const { verifier, verifierId, idToken, subVerifierInfoArray } = params; + const verifierDetails = { verifier, verifierId }; + + // fetch node details. + const { torusNodeEndpoints, torusNodePub, torusIndexes } = await this.nodeDetailManagerInstance.getNodeDetails(verifierDetails); + + if (params.serverTimeOffset) { + this.authInstance.serverTimeOffset = params.serverTimeOffset; + } + // Does the key assign + if (this.authInstance.isLegacyNetwork) await this.authInstance.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }); + + let finalIdToken = idToken; + let finalVerifierParams = { verifier_id: verifierId }; + if (subVerifierInfoArray && subVerifierInfoArray?.length > 0) { + const aggregateVerifierParams: AggregateVerifierParams = { verify_params: [], sub_verifier_ids: [], verifier_id: "" }; + const aggregateIdTokenSeeds = []; + for (let index = 0; index < subVerifierInfoArray.length; index += 1) { + const userInfo = subVerifierInfoArray[index]; + aggregateVerifierParams.verify_params.push({ verifier_id: verifierId, idtoken: userInfo.idToken }); + aggregateVerifierParams.sub_verifier_ids.push(userInfo.verifier); + aggregateIdTokenSeeds.push(userInfo.idToken); + } + aggregateIdTokenSeeds.sort(); + + finalIdToken = keccak256(Buffer.from(aggregateIdTokenSeeds.join(String.fromCharCode(29)), "utf8")).slice(2); + + aggregateVerifierParams.verifier_id = verifierId; + finalVerifierParams = aggregateVerifierParams; + } + + const torusKey = await this.authInstance.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, finalVerifierParams, finalIdToken); + this.torusKey = torusKey; + const postboxKey = Torus.getPostboxKey(torusKey); + this.postboxKey = new BN(postboxKey, 16); + return this.postboxKey; + } + + toJSON(): StringifiedType { + return { + ...super.toJSON(), + serviceProviderName: this.serviceProviderName, + web3AuthOptions: this.web3AuthOptions, + }; + } +} + +export default SfaServiceProvider; diff --git a/packages/service-provider-sfa/src/index.ts b/packages/service-provider-sfa/src/index.ts new file mode 100644 index 000000000..87736e2ae --- /dev/null +++ b/packages/service-provider-sfa/src/index.ts @@ -0,0 +1 @@ +export { default, default as SfaServiceProvider } from "./SfaServiceProvider"; diff --git a/packages/service-provider-sfa/src/interfaces.ts b/packages/service-provider-sfa/src/interfaces.ts new file mode 100644 index 000000000..8f955966f --- /dev/null +++ b/packages/service-provider-sfa/src/interfaces.ts @@ -0,0 +1,30 @@ +import { type ServiceProviderArgs } from "@tkey/common-types"; +import { type TORUS_NETWORK_TYPE } from "@toruslabs/constants"; + +export interface Web3AuthOptions { + clientId: string; + network: TORUS_NETWORK_TYPE; +} +export interface SfaServiceProviderArgs extends ServiceProviderArgs { + web3AuthOptions: Web3AuthOptions; +} + +export interface TorusSubVerifierInfo { + verifier: string; + idToken: string; +} + +export type AggregateVerifierParams = { + verify_params: { verifier_id: string; idtoken: string }[]; + sub_verifier_ids: string[]; + verifier_id: string; +}; + +export type LoginParams = { + verifier: string; + verifierId: string; + idToken: string; + subVerifierInfoArray?: TorusSubVerifierInfo[]; + // offset in seconds + serverTimeOffset?: number; +}; diff --git a/packages/service-provider-sfa/torus.config.js b/packages/service-provider-sfa/torus.config.js new file mode 100644 index 000000000..66be7083b --- /dev/null +++ b/packages/service-provider-sfa/torus.config.js @@ -0,0 +1 @@ +module.exports = require("../../torus.config"); diff --git a/packages/service-provider-sfa/tsconfig.json b/packages/service-provider-sfa/tsconfig.json new file mode 100644 index 000000000..ef502e89c --- /dev/null +++ b/packages/service-provider-sfa/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src", "test"] +} diff --git a/packages/service-provider-sfa/webpack.config.js b/packages/service-provider-sfa/webpack.config.js new file mode 100644 index 000000000..f0607506c --- /dev/null +++ b/packages/service-provider-sfa/webpack.config.js @@ -0,0 +1,6 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const generateWebpackConfig = require("../../webpack.config"); + +const config = generateWebpackConfig({ }); + +exports.baseConfig = config.baseConfig; From 5be1f60e98ae61d06dee8f2589fa7833bf43ad2b Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Thu, 23 Nov 2023 12:14:58 +0530 Subject: [PATCH 02/22] add torusKey in service provider torus --- .../service-provider-torus/src/TorusServiceProvider.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 6abe82e05..38050a919 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -10,7 +10,7 @@ import CustomAuth, { TorusHybridAggregateLoginResponse, TorusLoginResponse, } from "@toruslabs/customauth"; -import Torus from "@toruslabs/torus.js"; +import Torus, { TorusKey } from "@toruslabs/torus.js"; import BN from "bn.js"; class TorusServiceProvider extends ServiceProviderBase { @@ -18,6 +18,8 @@ class TorusServiceProvider extends ServiceProviderBase { singleLoginKey: BN; + public torusKey: TorusKey; + customAuthArgs: CustomAuthArgs; constructor({ enableLogging = false, postboxKey, customAuthArgs }: TorusServiceProviderArgs) { @@ -45,6 +47,7 @@ class TorusServiceProvider extends ServiceProviderBase { async triggerLogin(params: SubVerifierDetails): Promise { const obj = await this.customAuthInstance.triggerLogin(params); const localPrivKey = Torus.getPostboxKey(obj); + this.torusKey = obj; this.postboxKey = new BN(localPrivKey, "hex"); return obj; } @@ -52,6 +55,7 @@ class TorusServiceProvider extends ServiceProviderBase { async triggerAggregateLogin(params: AggregateLoginParams): Promise { const obj = await this.customAuthInstance.triggerAggregateLogin(params); const localPrivKey = Torus.getPostboxKey(obj); + this.torusKey = obj; this.postboxKey = new BN(localPrivKey, "hex"); return obj; } @@ -60,6 +64,7 @@ class TorusServiceProvider extends ServiceProviderBase { const obj = await this.customAuthInstance.triggerHybridAggregateLogin(params); const aggregateLoginKey = Torus.getPostboxKey(obj.aggregateLogins[0]); const singleLoginKey = Torus.getPostboxKey(obj.singleLogin); + this.torusKey = null; this.postboxKey = new BN(aggregateLoginKey, "hex"); this.singleLoginKey = new BN(singleLoginKey, "hex"); return obj; From b8acf7078b81e9bd24fd1200ad04b175f7303292 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Fri, 24 Nov 2023 13:54:45 +0530 Subject: [PATCH 03/22] Add migratableKey --- .../src/SfaServiceProvider.ts | 8 ++++++++ .../src/TorusServiceProvider.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts index 7aff8c30a..422e61ff7 100644 --- a/packages/service-provider-sfa/src/SfaServiceProvider.ts +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -13,6 +13,8 @@ class SfaServiceProvider extends ServiceProviderBase { public torusKey: TorusKey; + public migratableKey: BN | null = null; + private nodeDetailManagerInstance: NodeDetailManager; constructor({ enableLogging = false, postboxKey, web3AuthOptions }: SfaServiceProviderArgs) { @@ -77,6 +79,12 @@ class SfaServiceProvider extends ServiceProviderBase { const torusKey = await this.authInstance.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, finalVerifierParams, finalIdToken); this.torusKey = torusKey; + const { finalKeyData, oAuthKeyData } = torusKey; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; + + if (!torusKey.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } const postboxKey = Torus.getPostboxKey(torusKey); this.postboxKey = new BN(postboxKey, 16); return this.postboxKey; diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 38050a919..923e0ff4f 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -20,6 +20,8 @@ class TorusServiceProvider extends ServiceProviderBase { public torusKey: TorusKey; + public migratableKey: BN | null = null; + customAuthArgs: CustomAuthArgs; constructor({ enableLogging = false, postboxKey, customAuthArgs }: TorusServiceProviderArgs) { @@ -48,6 +50,13 @@ class TorusServiceProvider extends ServiceProviderBase { const obj = await this.customAuthInstance.triggerLogin(params); const localPrivKey = Torus.getPostboxKey(obj); this.torusKey = obj; + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; + + if (!obj.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } + this.postboxKey = new BN(localPrivKey, "hex"); return obj; } @@ -56,6 +65,13 @@ class TorusServiceProvider extends ServiceProviderBase { const obj = await this.customAuthInstance.triggerAggregateLogin(params); const localPrivKey = Torus.getPostboxKey(obj); this.torusKey = obj; + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; + + if (!obj.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } + this.postboxKey = new BN(localPrivKey, "hex"); return obj; } From 98a5792699aceb005df7d82fa82b6c19eb472827 Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 24 Nov 2023 18:20:32 +0800 Subject: [PATCH 04/22] feat: support key migration from service provider --- package-lock.json | 1 + .../common-types/src/baseTypes/commonTypes.ts | 2 ++ packages/core/src/core.ts | 18 +++++++++++++++++- .../src/ServiceProviderBase.ts | 2 ++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 7ea6643b5..14f02c803 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23298,6 +23298,7 @@ } }, "packages/service-provider-sfa": { + "name": "@tkey/service-provider-sfa", "version": "12.0.0", "license": "MIT", "dependencies": { diff --git a/packages/common-types/src/baseTypes/commonTypes.ts b/packages/common-types/src/baseTypes/commonTypes.ts index d000e8fc7..a0b55d5bb 100644 --- a/packages/common-types/src/baseTypes/commonTypes.ts +++ b/packages/common-types/src/baseTypes/commonTypes.ts @@ -45,6 +45,8 @@ export interface IServiceProvider extends ISerializable { postboxKey: BN; serviceProviderName: string; + + migratableKey? : BN | null; encrypt(msg: Buffer): Promise; decrypt(msg: EncryptedMessage): Promise; diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index b9877a8f2..6c7899c98 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -252,8 +252,24 @@ class ThresholdKey implements ITKey { if (neverInitializeNewKey) { throw CoreError.default("key has not been generated yet"); } + // no metadata set, assumes new user - await this._initializeNewKey({ initializeModules: true, importedKey: importKey, delete1OutOf1: p.delete1OutOf1 }); + + // check for serviceprovider migratableKey for import key from service provider for new user + // provided no importKey is provided ( importKey take precedent ) + if (this.serviceProvider.migratableKey && !importKey) { + // importkey from server provider need to be atomic, hence manual sync is required. + const tempStateManualSync = this.manualSync; + this.manualSync = true; + await this._initializeNewKey({ initializeModules: true, importedKey: this.serviceProvider.migratableKey, delete1OutOf1: true }); + this.syncLocalMetadataTransitions(); + // restore manual sync flag + this.manualSync = tempStateManualSync; + } else { + await this._initializeNewKey({ initializeModules: true, importedKey: importKey, delete1OutOf1: p.delete1OutOf1 }); + } + + // return after created new tkey account ( skip other steps) return this.getKeyDetails(); } // else we continue with catching up share and metadata diff --git a/packages/service-provider-base/src/ServiceProviderBase.ts b/packages/service-provider-base/src/ServiceProviderBase.ts index ff3c6e609..36dc93209 100644 --- a/packages/service-provider-base/src/ServiceProviderBase.ts +++ b/packages/service-provider-base/src/ServiceProviderBase.ts @@ -22,6 +22,8 @@ class ServiceProviderBase implements IServiceProvider { serviceProviderName: string; + migratableKey: BN | null = null; + constructor({ enableLogging = false, postboxKey }: ServiceProviderArgs) { this.enableLogging = enableLogging; this.postboxKey = new BN(postboxKey, "hex"); From 6d21e23aa41bf0d57b98d70969d58a849e140692 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 24 Jan 2024 13:44:26 +0530 Subject: [PATCH 05/22] linting fixes --- packages/common-types/src/baseTypes/commonTypes.ts | 4 ++-- packages/service-provider-torus/src/TorusServiceProvider.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/common-types/src/baseTypes/commonTypes.ts b/packages/common-types/src/baseTypes/commonTypes.ts index a0b55d5bb..6e57e77a6 100644 --- a/packages/common-types/src/baseTypes/commonTypes.ts +++ b/packages/common-types/src/baseTypes/commonTypes.ts @@ -45,8 +45,8 @@ export interface IServiceProvider extends ISerializable { postboxKey: BN; serviceProviderName: string; - - migratableKey? : BN | null; + + migratableKey?: BN | null; encrypt(msg: Buffer): Promise; decrypt(msg: EncryptedMessage): Promise; diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 923e0ff4f..e93ff0ea4 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -71,7 +71,7 @@ class TorusServiceProvider extends ServiceProviderBase { if (!obj.metadata.upgraded) { this.migratableKey = new BN(privKey, "hex"); } - + this.postboxKey = new BN(localPrivKey, "hex"); return obj; } From 9a3ac68d233375e60ac987fba6f2f9ac9aa64a73 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 25 Jan 2024 10:12:35 +0800 Subject: [PATCH 06/22] fix: await sync --- packages/core/src/core.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 6c7899c98..ba5b40938 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -262,7 +262,7 @@ class ThresholdKey implements ITKey { const tempStateManualSync = this.manualSync; this.manualSync = true; await this._initializeNewKey({ initializeModules: true, importedKey: this.serviceProvider.migratableKey, delete1OutOf1: true }); - this.syncLocalMetadataTransitions(); + await this.syncLocalMetadataTransitions(); // restore manual sync flag this.manualSync = tempStateManualSync; } else { From 0050b45c2d661f52df73a1d5bdec196d1d435433 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Thu, 23 Nov 2023 12:10:32 +0530 Subject: [PATCH 07/22] Add latest SFA Service Provider package --- packages/service-provider-sfa/README.md | 27 ++++++ packages/service-provider-sfa/package.json | 63 +++++++++++++ .../src/SfaServiceProvider.ts | 94 +++++++++++++++++++ packages/service-provider-sfa/src/index.ts | 1 + .../service-provider-sfa/src/interfaces.ts | 30 ++++++ packages/service-provider-sfa/torus.config.js | 1 + packages/service-provider-sfa/tsconfig.json | 4 + .../service-provider-sfa/webpack.config.js | 6 ++ 8 files changed, 226 insertions(+) create mode 100644 packages/service-provider-sfa/README.md create mode 100644 packages/service-provider-sfa/package.json create mode 100644 packages/service-provider-sfa/src/SfaServiceProvider.ts create mode 100644 packages/service-provider-sfa/src/index.ts create mode 100644 packages/service-provider-sfa/src/interfaces.ts create mode 100644 packages/service-provider-sfa/torus.config.js create mode 100644 packages/service-provider-sfa/tsconfig.json create mode 100644 packages/service-provider-sfa/webpack.config.js diff --git a/packages/service-provider-sfa/README.md b/packages/service-provider-sfa/README.md new file mode 100644 index 000000000..2e94d6c6a --- /dev/null +++ b/packages/service-provider-sfa/README.md @@ -0,0 +1,27 @@ +# tKey Single Factor Auth Service Provider + +[![npm version](https://img.shields.io/npm/v/@tkey/service-provider-sfa?label=%22%22)](https://www.npmjs.com/package/@tkey/service-provider-sfa/v/latest) [![minzip](https://img.shields.io/bundlephobia/minzip/@tkey/service-provider-sfa?label=%22%22)](https://bundlephobia.com/result?p=@tkey/service-provider-sfa@latest) + +Service Provider in `tKey` is used for generating a social login share of the private key share managed by a wallet service provider via +their own authentication flows. + +## Installation + +```shell +npm install --save @tkey/service-provider-sfa +``` + +### See the full [SDK Reference](https://web3auth.io/docs/sdk/core-kit/tkey/usage#log-in) on the Web3Auth Documentation + +## Example + +```js +import SFAServiceProvider from '@tkey/service-provider-sfa'; + +const web3AuthOptions: any = { + clientId, // Get your Client ID from Web3Auth Dashboard + web3AuthNetwork: 'testnet', // ["cyan", "testnet", "mainnet", "aqua", "sapphire_devnet", "sapphire_mainnet"] +}; + +const serviceProvider = new SFAServiceProvider({web3AuthOptions}); +``` diff --git a/packages/service-provider-sfa/package.json b/packages/service-provider-sfa/package.json new file mode 100644 index 000000000..20b07143f --- /dev/null +++ b/packages/service-provider-sfa/package.json @@ -0,0 +1,63 @@ +{ + "name": "@tkey/service-provider-sfa", + "version": "12.0.0", + "description": "TKey Torus Service Provider Module", + "author": "Torus Labs", + "homepage": "https://github.com/tkey/tkey#readme", + "license": "MIT", + "main": "dist/serviceProviderSfa.cjs.js", + "module": "dist/serviceProviderSfa.esm.js", + "unpkg": "dist/serviceProviderSfa.umd.min.js", + "jsdelivr": "dist/serviceProviderSfa.umd.min.js", + "types": "dist/types/index.d.ts", + "files": [ + "dist", + "src" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/tkey/tkey.git" + }, + "scripts": { + "test": "cross-env MOCKED=true mocha --config ../../.mocharc.json ", + "coverage": "nyc npm test", + "coverage-production": "nyc npm run test-production", + "test-development": "cross-env MOCKED=false METADATA=http://localhost:5051 mocha --config ../../.mocharc.json ", + "test-production": "cross-env MOCKED=false METADATA=https://metadata.tor.us mocha --config ../../.mocharc.json ", + "test-debugger": "mocha --config ../../.mocharc.json --inspect-brk", + "dev": "rimraf dist/ && cross-env NODE_ENV=development torus-scripts build", + "build": "rimraf dist/ && cross-env NODE_ENV=production torus-scripts build", + "lint": "eslint --fix 'src/**/*.ts'", + "prepack": "npm run build", + "pre-commit": "lint-staged" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + }, + "dependencies": { + "@tkey/service-provider-base": "^12.0.0", + "@toruslabs/fetch-node-details": "^13.0.4", + "@toruslabs/torus.js": "^12.0.1", + "bn.js": "^5.2.1" + }, + "devDependencies": { + "@types/bn.js": "^5.1.2" + }, + "bugs": { + "url": "https://github.com/tkey/tkey/issues" + }, + "lint-staged": { + "!(*d).ts": [ + "npm run lint --", + "prettier --write 'src/**/*.ts'" + ] + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "gitHead": "9967ce9f795f495f28ef0da1fc50acde31dcc258" +} diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts new file mode 100644 index 000000000..7aff8c30a --- /dev/null +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -0,0 +1,94 @@ +import { type StringifiedType } from "@tkey/common-types"; +import { ServiceProviderBase } from "@tkey/service-provider-base"; +import { NodeDetailManager } from "@toruslabs/fetch-node-details"; +import Torus, { keccak256, TorusKey } from "@toruslabs/torus.js"; +import BN from "bn.js"; + +import { AggregateVerifierParams, LoginParams, SfaServiceProviderArgs, Web3AuthOptions } from "./interfaces"; + +class SfaServiceProvider extends ServiceProviderBase { + web3AuthOptions: Web3AuthOptions; + + authInstance: Torus; + + public torusKey: TorusKey; + + private nodeDetailManagerInstance: NodeDetailManager; + + constructor({ enableLogging = false, postboxKey, web3AuthOptions }: SfaServiceProviderArgs) { + super({ enableLogging, postboxKey }); + this.web3AuthOptions = web3AuthOptions; + this.authInstance = new Torus({ + clientId: web3AuthOptions.clientId, + enableOneKey: true, + network: web3AuthOptions.network, + }); + Torus.enableLogging(enableLogging); + this.serviceProviderName = "SfaServiceProvider"; + this.nodeDetailManagerInstance = new NodeDetailManager({ network: web3AuthOptions.network, enableLogging }); + } + + static fromJSON(value: StringifiedType): SfaServiceProvider { + const { enableLogging, postboxKey, web3AuthOptions, serviceProviderName, torusKey } = value; + if (serviceProviderName !== "SfaServiceProvider") return undefined; + + const sfaSP = new SfaServiceProvider({ + enableLogging, + postboxKey, + web3AuthOptions, + }); + + sfaSP.torusKey = torusKey; + + return sfaSP; + } + + async connect(params: LoginParams): Promise { + const { verifier, verifierId, idToken, subVerifierInfoArray } = params; + const verifierDetails = { verifier, verifierId }; + + // fetch node details. + const { torusNodeEndpoints, torusNodePub, torusIndexes } = await this.nodeDetailManagerInstance.getNodeDetails(verifierDetails); + + if (params.serverTimeOffset) { + this.authInstance.serverTimeOffset = params.serverTimeOffset; + } + // Does the key assign + if (this.authInstance.isLegacyNetwork) await this.authInstance.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }); + + let finalIdToken = idToken; + let finalVerifierParams = { verifier_id: verifierId }; + if (subVerifierInfoArray && subVerifierInfoArray?.length > 0) { + const aggregateVerifierParams: AggregateVerifierParams = { verify_params: [], sub_verifier_ids: [], verifier_id: "" }; + const aggregateIdTokenSeeds = []; + for (let index = 0; index < subVerifierInfoArray.length; index += 1) { + const userInfo = subVerifierInfoArray[index]; + aggregateVerifierParams.verify_params.push({ verifier_id: verifierId, idtoken: userInfo.idToken }); + aggregateVerifierParams.sub_verifier_ids.push(userInfo.verifier); + aggregateIdTokenSeeds.push(userInfo.idToken); + } + aggregateIdTokenSeeds.sort(); + + finalIdToken = keccak256(Buffer.from(aggregateIdTokenSeeds.join(String.fromCharCode(29)), "utf8")).slice(2); + + aggregateVerifierParams.verifier_id = verifierId; + finalVerifierParams = aggregateVerifierParams; + } + + const torusKey = await this.authInstance.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, finalVerifierParams, finalIdToken); + this.torusKey = torusKey; + const postboxKey = Torus.getPostboxKey(torusKey); + this.postboxKey = new BN(postboxKey, 16); + return this.postboxKey; + } + + toJSON(): StringifiedType { + return { + ...super.toJSON(), + serviceProviderName: this.serviceProviderName, + web3AuthOptions: this.web3AuthOptions, + }; + } +} + +export default SfaServiceProvider; diff --git a/packages/service-provider-sfa/src/index.ts b/packages/service-provider-sfa/src/index.ts new file mode 100644 index 000000000..87736e2ae --- /dev/null +++ b/packages/service-provider-sfa/src/index.ts @@ -0,0 +1 @@ +export { default, default as SfaServiceProvider } from "./SfaServiceProvider"; diff --git a/packages/service-provider-sfa/src/interfaces.ts b/packages/service-provider-sfa/src/interfaces.ts new file mode 100644 index 000000000..8f955966f --- /dev/null +++ b/packages/service-provider-sfa/src/interfaces.ts @@ -0,0 +1,30 @@ +import { type ServiceProviderArgs } from "@tkey/common-types"; +import { type TORUS_NETWORK_TYPE } from "@toruslabs/constants"; + +export interface Web3AuthOptions { + clientId: string; + network: TORUS_NETWORK_TYPE; +} +export interface SfaServiceProviderArgs extends ServiceProviderArgs { + web3AuthOptions: Web3AuthOptions; +} + +export interface TorusSubVerifierInfo { + verifier: string; + idToken: string; +} + +export type AggregateVerifierParams = { + verify_params: { verifier_id: string; idtoken: string }[]; + sub_verifier_ids: string[]; + verifier_id: string; +}; + +export type LoginParams = { + verifier: string; + verifierId: string; + idToken: string; + subVerifierInfoArray?: TorusSubVerifierInfo[]; + // offset in seconds + serverTimeOffset?: number; +}; diff --git a/packages/service-provider-sfa/torus.config.js b/packages/service-provider-sfa/torus.config.js new file mode 100644 index 000000000..66be7083b --- /dev/null +++ b/packages/service-provider-sfa/torus.config.js @@ -0,0 +1 @@ +module.exports = require("../../torus.config"); diff --git a/packages/service-provider-sfa/tsconfig.json b/packages/service-provider-sfa/tsconfig.json new file mode 100644 index 000000000..ef502e89c --- /dev/null +++ b/packages/service-provider-sfa/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src", "test"] +} diff --git a/packages/service-provider-sfa/webpack.config.js b/packages/service-provider-sfa/webpack.config.js new file mode 100644 index 000000000..f0607506c --- /dev/null +++ b/packages/service-provider-sfa/webpack.config.js @@ -0,0 +1,6 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const generateWebpackConfig = require("../../webpack.config"); + +const config = generateWebpackConfig({ }); + +exports.baseConfig = config.baseConfig; From 71c3b08d033cbd59a35e01a6faa4e24350238f38 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Thu, 23 Nov 2023 12:14:58 +0530 Subject: [PATCH 08/22] add torusKey in service provider torus --- .../service-provider-torus/src/TorusServiceProvider.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index ab0c3c4e2..d105a4c01 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -10,7 +10,7 @@ import CustomAuth, { TorusHybridAggregateLoginResponse, TorusLoginResponse, } from "@toruslabs/customauth"; -import Torus from "@toruslabs/torus.js"; +import Torus, { TorusKey } from "@toruslabs/torus.js"; import BN from "bn.js"; class TorusServiceProvider extends ServiceProviderBase { @@ -18,6 +18,8 @@ class TorusServiceProvider extends ServiceProviderBase { singleLoginKey: BN; + public torusKey: TorusKey; + customAuthArgs: CustomAuthArgs; constructor({ enableLogging = false, postboxKey, customAuthArgs }: TorusServiceProviderArgs) { @@ -51,6 +53,7 @@ class TorusServiceProvider extends ServiceProviderBase { // `obj` maybe `null` in redirect mode. if (obj) { const localPrivKey = Torus.getPostboxKey(obj); + this.torusKey = obj; this.postboxKey = new BN(localPrivKey, "hex"); } @@ -66,6 +69,7 @@ class TorusServiceProvider extends ServiceProviderBase { // `obj` maybe `null` in redirect mode. if (obj) { const localPrivKey = Torus.getPostboxKey(obj); + this.torusKey = obj; this.postboxKey = new BN(localPrivKey, "hex"); } return obj; @@ -81,6 +85,7 @@ class TorusServiceProvider extends ServiceProviderBase { if (obj) { const aggregateLoginKey = Torus.getPostboxKey(obj.aggregateLogins[0]); const singleLoginKey = Torus.getPostboxKey(obj.singleLogin); + this.torusKey = null; this.postboxKey = new BN(aggregateLoginKey, "hex"); this.singleLoginKey = new BN(singleLoginKey, "hex"); } From 1246c17464162dba1d10c439901abb4e21f9a1bb Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Fri, 24 Nov 2023 13:54:45 +0530 Subject: [PATCH 09/22] Add migratableKey --- .../src/SfaServiceProvider.ts | 8 ++++++++ .../src/TorusServiceProvider.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts index 7aff8c30a..422e61ff7 100644 --- a/packages/service-provider-sfa/src/SfaServiceProvider.ts +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -13,6 +13,8 @@ class SfaServiceProvider extends ServiceProviderBase { public torusKey: TorusKey; + public migratableKey: BN | null = null; + private nodeDetailManagerInstance: NodeDetailManager; constructor({ enableLogging = false, postboxKey, web3AuthOptions }: SfaServiceProviderArgs) { @@ -77,6 +79,12 @@ class SfaServiceProvider extends ServiceProviderBase { const torusKey = await this.authInstance.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, finalVerifierParams, finalIdToken); this.torusKey = torusKey; + const { finalKeyData, oAuthKeyData } = torusKey; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; + + if (!torusKey.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } const postboxKey = Torus.getPostboxKey(torusKey); this.postboxKey = new BN(postboxKey, 16); return this.postboxKey; diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index d105a4c01..454fb26e2 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -20,6 +20,8 @@ class TorusServiceProvider extends ServiceProviderBase { public torusKey: TorusKey; + public migratableKey: BN | null = null; + customAuthArgs: CustomAuthArgs; constructor({ enableLogging = false, postboxKey, customAuthArgs }: TorusServiceProviderArgs) { @@ -54,6 +56,13 @@ class TorusServiceProvider extends ServiceProviderBase { if (obj) { const localPrivKey = Torus.getPostboxKey(obj); this.torusKey = obj; + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; + + if (!obj.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } + this.postboxKey = new BN(localPrivKey, "hex"); } @@ -70,6 +79,13 @@ class TorusServiceProvider extends ServiceProviderBase { if (obj) { const localPrivKey = Torus.getPostboxKey(obj); this.torusKey = obj; + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; + + if (!obj.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } + this.postboxKey = new BN(localPrivKey, "hex"); } return obj; From 62dbbecafe1238f777d66ff77c67c41fd147fc9a Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 24 Nov 2023 18:20:32 +0800 Subject: [PATCH 10/22] feat: support key migration from service provider --- .../common-types/src/baseTypes/commonTypes.ts | 2 ++ packages/core/src/core.ts | 28 +++++++++++++++---- .../src/ServiceProviderBase.ts | 2 ++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/common-types/src/baseTypes/commonTypes.ts b/packages/common-types/src/baseTypes/commonTypes.ts index a7036d36c..88a88a48e 100644 --- a/packages/common-types/src/baseTypes/commonTypes.ts +++ b/packages/common-types/src/baseTypes/commonTypes.ts @@ -49,6 +49,8 @@ export interface IServiceProvider extends ISerializable { postboxKey: BN; serviceProviderName: string; + + migratableKey? : BN | null; encrypt(msg: Buffer): Promise; decrypt(msg: EncryptedMessage): Promise; diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 1d1d7ffce..1ef0301dd 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -267,13 +267,29 @@ class ThresholdKey implements ITKey { if (neverInitializeNewKey) { throw CoreError.default("key has not been generated yet"); } + // no metadata set, assumes new user - await this._initializeNewKey({ - initializeModules: true, - importedKey: importKey, - importEd25519Seed: params?.importEd25519Seed, - delete1OutOf1: p.delete1OutOf1, - }); + + // check for serviceprovider migratableKey for import key from service provider for new user + // provided no importKey is provided ( importKey take precedent ) + if (this.serviceProvider.migratableKey && !importKey) { + // importkey from server provider need to be atomic, hence manual sync is required. + const tempStateManualSync = this.manualSync; + this.manualSync = true; + await this._initializeNewKey({ initializeModules: true, importedKey: this.serviceProvider.migratableKey, delete1OutOf1: true }); + this.syncLocalMetadataTransitions(); + // restore manual sync flag + this.manualSync = tempStateManualSync; + } else { + await this._initializeNewKey({ + initializeModules: true, + importedKey: importKey, + delete1OutOf1: p.delete1OutOf1, + importEd25519Seed: params?.importEd25519Seed, + }); + } + + // return after created new tkey account ( skip other steps) return this.getKeyDetails(); } // else we continue with catching up share and metadata diff --git a/packages/service-provider-base/src/ServiceProviderBase.ts b/packages/service-provider-base/src/ServiceProviderBase.ts index ff3c6e609..36dc93209 100644 --- a/packages/service-provider-base/src/ServiceProviderBase.ts +++ b/packages/service-provider-base/src/ServiceProviderBase.ts @@ -22,6 +22,8 @@ class ServiceProviderBase implements IServiceProvider { serviceProviderName: string; + migratableKey: BN | null = null; + constructor({ enableLogging = false, postboxKey }: ServiceProviderArgs) { this.enableLogging = enableLogging; this.postboxKey = new BN(postboxKey, "hex"); From b99f55bbaab0ba7a896fc2a053fa10c0dd1130a3 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 24 Jan 2024 13:44:26 +0530 Subject: [PATCH 11/22] linting fixes --- .../common-types/src/baseTypes/commonTypes.ts | 4 ++-- .../src/TorusServiceProvider.ts | 16 ++++------------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/packages/common-types/src/baseTypes/commonTypes.ts b/packages/common-types/src/baseTypes/commonTypes.ts index 88a88a48e..e392ca7bd 100644 --- a/packages/common-types/src/baseTypes/commonTypes.ts +++ b/packages/common-types/src/baseTypes/commonTypes.ts @@ -49,8 +49,8 @@ export interface IServiceProvider extends ISerializable { postboxKey: BN; serviceProviderName: string; - - migratableKey? : BN | null; + + migratableKey?: BN | null; encrypt(msg: Buffer): Promise; decrypt(msg: EncryptedMessage): Promise; diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 454fb26e2..4255bae04 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -75,19 +75,11 @@ class TorusServiceProvider extends ServiceProviderBase { async triggerAggregateLogin(params: AggregateLoginParams): Promise { const obj = await this.customAuthInstance.triggerAggregateLogin(params); - // `obj` maybe `null` in redirect mode. - if (obj) { - const localPrivKey = Torus.getPostboxKey(obj); - this.torusKey = obj; - const { finalKeyData, oAuthKeyData } = obj; - const privKey = finalKeyData.privKey || oAuthKeyData.privKey; - - if (!obj.metadata.upgraded) { - this.migratableKey = new BN(privKey, "hex"); - } - - this.postboxKey = new BN(localPrivKey, "hex"); + if (!obj.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); } + + this.postboxKey = new BN(localPrivKey, "hex"); return obj; } From 0151bed63e35ca322a832e954a2e6bac0a5be843 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 25 Jan 2024 10:12:35 +0800 Subject: [PATCH 12/22] fix: await sync --- packages/core/src/core.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 1ef0301dd..f98ba103e 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -277,7 +277,7 @@ class ThresholdKey implements ITKey { const tempStateManualSync = this.manualSync; this.manualSync = true; await this._initializeNewKey({ initializeModules: true, importedKey: this.serviceProvider.migratableKey, delete1OutOf1: true }); - this.syncLocalMetadataTransitions(); + await this.syncLocalMetadataTransitions(); // restore manual sync flag this.manualSync = tempStateManualSync; } else { From 9af7adb8a110f5fc877352173754f629c4f34ab5 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Mon, 10 Jun 2024 14:34:28 +0530 Subject: [PATCH 13/22] Fix after rebase --- .../src/TorusServiceProvider.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 4255bae04..c736b8c23 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -75,11 +75,18 @@ class TorusServiceProvider extends ServiceProviderBase { async triggerAggregateLogin(params: AggregateLoginParams): Promise { const obj = await this.customAuthInstance.triggerAggregateLogin(params); - if (!obj.metadata.upgraded) { - this.migratableKey = new BN(privKey, "hex"); - } + if (obj) { + const localPrivKey = Torus.getPostboxKey(obj); + this.torusKey = obj; + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; - this.postboxKey = new BN(localPrivKey, "hex"); + if (!obj.metadata.upgraded) { + this.migratableKey = new BN(privKey, "hex"); + } + + this.postboxKey = new BN(localPrivKey, "hex"); + } return obj; } From 0223ef37c937bc8c38ae5fd26c463eb43d774b1e Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 10:37:23 +0530 Subject: [PATCH 14/22] Update package-lock.json --- package-lock.json | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/package-lock.json b/package-lock.json index d29ed2840..349ebfb7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4375,6 +4375,10 @@ "resolved": "packages/service-provider-base", "link": true }, + "node_modules/@tkey/service-provider-sfa": { + "resolved": "packages/service-provider-sfa", + "link": true + }, "node_modules/@tkey/service-provider-torus": { "resolved": "packages/service-provider-torus", "link": true @@ -25133,6 +25137,122 @@ "@babel/runtime": "7.x" } }, + "packages/service-provider-sfa": { + "name": "@tkey/service-provider-sfa", + "version": "12.0.0", + "license": "MIT", + "dependencies": { + "@tkey/service-provider-base": "^12.0.0", + "@toruslabs/fetch-node-details": "^13.0.4", + "@toruslabs/torus.js": "^12.0.1", + "bn.js": "^5.2.1" + }, + "devDependencies": { + "@types/bn.js": "^5.1.2" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, + "packages/service-provider-sfa/node_modules/@tkey/common-types": { + "version": "12.1.2", + "resolved": "https://registry.npmjs.org/@tkey/common-types/-/common-types-12.1.2.tgz", + "integrity": "sha512-Cat3qT34AeiR8HvVYW+sAR5b9dlKlRUShsGThKoxe9u5ppZK4othNlnFZ7921+kclB/gLAtoAXQp9pjtQncJ3A==", + "dependencies": { + "@toruslabs/customauth": "^18.1.0", + "@toruslabs/eccrypto": "^4.0.0", + "@toruslabs/torus.js": "^12.3.6", + "bn.js": "^5.2.1", + "elliptic": "^6.5.5", + "serialize-error": "^8.1.0", + "ts-custom-error": "^3.3.1" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, + "packages/service-provider-sfa/node_modules/@tkey/service-provider-base": { + "version": "12.1.2", + "resolved": "https://registry.npmjs.org/@tkey/service-provider-base/-/service-provider-base-12.1.2.tgz", + "integrity": "sha512-mPY3AuyX4coNVWEu4echuI2brAAMCNv8dIM5+n8HUSNgOk0nOirzz2v0TqGs1BNWiYq2eKIBtpOuF5n2rFY7mw==", + "dependencies": { + "@tkey/common-types": "^12.1.2", + "bn.js": "^5.2.1", + "elliptic": "^6.5.5" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, + "packages/service-provider-sfa/node_modules/@toruslabs/customauth": { + "version": "18.1.0", + "resolved": "https://registry.npmjs.org/@toruslabs/customauth/-/customauth-18.1.0.tgz", + "integrity": "sha512-cXNXkv2DOZu+XQc0ZEqzPwQJWTXkdxTKz9/uzVsF1i9A6KyrTupX1yN8k/mXZybLJ2R3cz1/e9iezn1NPhUPiA==", + "dependencies": { + "@chaitanyapotti/register-service-worker": "^1.7.4", + "@toruslabs/broadcast-channel": "^10.0.2", + "@toruslabs/constants": "^13.2.0", + "@toruslabs/eccrypto": "^4.0.0", + "@toruslabs/fetch-node-details": "^13.2.0", + "@toruslabs/http-helpers": "^6.1.0", + "@toruslabs/metadata-helpers": "^5.1.0", + "@toruslabs/openlogin-session-manager": "^3.1.1", + "@toruslabs/torus.js": "^12.2.0", + "base64url": "^3.0.1", + "bowser": "^2.11.0", + "events": "^3.3.0", + "jwt-decode": "^4.0.0", + "lodash.merge": "^4.6.2", + "loglevel": "^1.9.1" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "^7.x", + "@sentry/types": "^7.x" + }, + "peerDependenciesMeta": { + "@sentry/types": { + "optional": true + } + } + }, + "packages/service-provider-sfa/node_modules/@toruslabs/torus.js": { + "version": "12.3.6", + "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-12.3.6.tgz", + "integrity": "sha512-cXPC+Cyw4W05eZW784pPd+QyJRmvK54wTYbBocU9gVaqZhGXNryVrqhBYZSOmAr3pUtOubjRZfeXCZLn7BA77Q==", + "dependencies": { + "@toruslabs/constants": "^13.4.0", + "@toruslabs/eccrypto": "^4.0.0", + "@toruslabs/http-helpers": "^6.1.1", + "bn.js": "^5.2.1", + "elliptic": "^6.5.5", + "ethereum-cryptography": "^2.1.3", + "json-stable-stringify": "^1.1.1", + "loglevel": "^1.9.1" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, "packages/service-provider-torus": { "name": "@tkey/service-provider-torus", "version": "13.0.0-alpha.4", From c24c5ffed3b350cc0b04148f8317b3fcd04e9ae4 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 10:39:20 +0530 Subject: [PATCH 15/22] Update packages for service-provider-sfa --- package-lock.json | 83 ++-------------------- packages/service-provider-sfa/package.json | 10 +-- 2 files changed, 10 insertions(+), 83 deletions(-) diff --git a/package-lock.json b/package-lock.json index 349ebfb7d..79419343e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25139,54 +25139,16 @@ }, "packages/service-provider-sfa": { "name": "@tkey/service-provider-sfa", - "version": "12.0.0", + "version": "13.0.0-alpha.4", "license": "MIT", "dependencies": { - "@tkey/service-provider-base": "^12.0.0", - "@toruslabs/fetch-node-details": "^13.0.4", - "@toruslabs/torus.js": "^12.0.1", + "@tkey/service-provider-base": "^13.0.0-alpha.4", + "@toruslabs/fetch-node-details": "^13.4.0", + "@toruslabs/torus.js": "^12.3.6", "bn.js": "^5.2.1" }, "devDependencies": { - "@types/bn.js": "^5.1.2" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, - "packages/service-provider-sfa/node_modules/@tkey/common-types": { - "version": "12.1.2", - "resolved": "https://registry.npmjs.org/@tkey/common-types/-/common-types-12.1.2.tgz", - "integrity": "sha512-Cat3qT34AeiR8HvVYW+sAR5b9dlKlRUShsGThKoxe9u5ppZK4othNlnFZ7921+kclB/gLAtoAXQp9pjtQncJ3A==", - "dependencies": { - "@toruslabs/customauth": "^18.1.0", - "@toruslabs/eccrypto": "^4.0.0", - "@toruslabs/torus.js": "^12.3.6", - "bn.js": "^5.2.1", - "elliptic": "^6.5.5", - "serialize-error": "^8.1.0", - "ts-custom-error": "^3.3.1" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, - "packages/service-provider-sfa/node_modules/@tkey/service-provider-base": { - "version": "12.1.2", - "resolved": "https://registry.npmjs.org/@tkey/service-provider-base/-/service-provider-base-12.1.2.tgz", - "integrity": "sha512-mPY3AuyX4coNVWEu4echuI2brAAMCNv8dIM5+n8HUSNgOk0nOirzz2v0TqGs1BNWiYq2eKIBtpOuF5n2rFY7mw==", - "dependencies": { - "@tkey/common-types": "^12.1.2", - "bn.js": "^5.2.1", - "elliptic": "^6.5.5" + "@types/bn.js": "^5.1.5" }, "engines": { "node": ">=18.x", @@ -25196,41 +25158,6 @@ "@babel/runtime": "7.x" } }, - "packages/service-provider-sfa/node_modules/@toruslabs/customauth": { - "version": "18.1.0", - "resolved": "https://registry.npmjs.org/@toruslabs/customauth/-/customauth-18.1.0.tgz", - "integrity": "sha512-cXNXkv2DOZu+XQc0ZEqzPwQJWTXkdxTKz9/uzVsF1i9A6KyrTupX1yN8k/mXZybLJ2R3cz1/e9iezn1NPhUPiA==", - "dependencies": { - "@chaitanyapotti/register-service-worker": "^1.7.4", - "@toruslabs/broadcast-channel": "^10.0.2", - "@toruslabs/constants": "^13.2.0", - "@toruslabs/eccrypto": "^4.0.0", - "@toruslabs/fetch-node-details": "^13.2.0", - "@toruslabs/http-helpers": "^6.1.0", - "@toruslabs/metadata-helpers": "^5.1.0", - "@toruslabs/openlogin-session-manager": "^3.1.1", - "@toruslabs/torus.js": "^12.2.0", - "base64url": "^3.0.1", - "bowser": "^2.11.0", - "events": "^3.3.0", - "jwt-decode": "^4.0.0", - "lodash.merge": "^4.6.2", - "loglevel": "^1.9.1" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "^7.x", - "@sentry/types": "^7.x" - }, - "peerDependenciesMeta": { - "@sentry/types": { - "optional": true - } - } - }, "packages/service-provider-sfa/node_modules/@toruslabs/torus.js": { "version": "12.3.6", "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-12.3.6.tgz", diff --git a/packages/service-provider-sfa/package.json b/packages/service-provider-sfa/package.json index 20b07143f..d73874b4a 100644 --- a/packages/service-provider-sfa/package.json +++ b/packages/service-provider-sfa/package.json @@ -1,6 +1,6 @@ { "name": "@tkey/service-provider-sfa", - "version": "12.0.0", + "version": "13.0.0-alpha.4", "description": "TKey Torus Service Provider Module", "author": "Torus Labs", "homepage": "https://github.com/tkey/tkey#readme", @@ -38,13 +38,13 @@ "@babel/runtime": "7.x" }, "dependencies": { - "@tkey/service-provider-base": "^12.0.0", - "@toruslabs/fetch-node-details": "^13.0.4", - "@toruslabs/torus.js": "^12.0.1", + "@tkey/service-provider-base": "^13.0.0-alpha.4", + "@toruslabs/fetch-node-details": "^13.4.0", + "@toruslabs/torus.js": "^12.3.6", "bn.js": "^5.2.1" }, "devDependencies": { - "@types/bn.js": "^5.1.2" + "@types/bn.js": "^5.1.5" }, "bugs": { "url": "https://github.com/tkey/tkey/issues" From 32dc51f2b0d9ca82d6a3f2ab09d5720bb87896a3 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 10:54:19 +0530 Subject: [PATCH 16/22] linting fixes --- packages/core/src/core.ts | 8 ++++---- .../service-provider-torus/src/TorusServiceProvider.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index f98ba103e..2874ca3d3 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -281,10 +281,10 @@ class ThresholdKey implements ITKey { // restore manual sync flag this.manualSync = tempStateManualSync; } else { - await this._initializeNewKey({ - initializeModules: true, - importedKey: importKey, - delete1OutOf1: p.delete1OutOf1, + await this._initializeNewKey({ + initializeModules: true, + importedKey: importKey, + delete1OutOf1: p.delete1OutOf1, importEd25519Seed: params?.importEd25519Seed, }); } diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index c736b8c23..44a57b56c 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -58,11 +58,11 @@ class TorusServiceProvider extends ServiceProviderBase { this.torusKey = obj; const { finalKeyData, oAuthKeyData } = obj; const privKey = finalKeyData.privKey || oAuthKeyData.privKey; - + if (!obj.metadata.upgraded) { this.migratableKey = new BN(privKey, "hex"); } - + this.postboxKey = new BN(localPrivKey, "hex"); } From 1e256ad177aa7e5766d1e240a695e6668b20a3ee Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 10:54:42 +0530 Subject: [PATCH 17/22] SfaServiceProvider: Remove Legacy network fix --- packages/service-provider-sfa/src/SfaServiceProvider.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts index 422e61ff7..3f438a8fb 100644 --- a/packages/service-provider-sfa/src/SfaServiceProvider.ts +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -50,13 +50,13 @@ class SfaServiceProvider extends ServiceProviderBase { const verifierDetails = { verifier, verifierId }; // fetch node details. - const { torusNodeEndpoints, torusNodePub, torusIndexes } = await this.nodeDetailManagerInstance.getNodeDetails(verifierDetails); + const { torusNodeEndpoints, torusIndexes } = await this.nodeDetailManagerInstance.getNodeDetails(verifierDetails); if (params.serverTimeOffset) { this.authInstance.serverTimeOffset = params.serverTimeOffset; } // Does the key assign - if (this.authInstance.isLegacyNetwork) await this.authInstance.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }); + // if (this.authInstance.isLegacyNetwork) await this.authInstance.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }); let finalIdToken = idToken; let finalVerifierParams = { verifier_id: verifierId }; From be39fa4c820d54584ef3f1226615d4c13d87d422 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 12:08:32 +0530 Subject: [PATCH 18/22] Fix build --- packages/service-provider-sfa/test/.eslintrc.json | 7 +++++++ packages/service-provider-sfa/test/test.js | 0 test/setup.mjs | 11 +++++------ 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 packages/service-provider-sfa/test/.eslintrc.json create mode 100644 packages/service-provider-sfa/test/test.js diff --git a/packages/service-provider-sfa/test/.eslintrc.json b/packages/service-provider-sfa/test/.eslintrc.json new file mode 100644 index 000000000..955546bd7 --- /dev/null +++ b/packages/service-provider-sfa/test/.eslintrc.json @@ -0,0 +1,7 @@ +{ + "rules": { + "prefer-arrow-callback": "off", + "func-names": "off" + } + +} diff --git a/packages/service-provider-sfa/test/test.js b/packages/service-provider-sfa/test/test.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/setup.mjs b/test/setup.mjs index 2975e570b..97b565ccb 100644 --- a/test/setup.mjs +++ b/test/setup.mjs @@ -18,14 +18,13 @@ Register({ rootMode: "upward", }); - const storeFn = { getItem(key) { - return this[key] + return this[key]; }, setItem(key, value) { - this[key] = value + this[key] = value; }, -} -globalThis.localStorage = { ...storeFn } -globalThis.sessionStorage = { ...storeFn } \ No newline at end of file +}; +globalThis.localStorage = { ...storeFn }; +globalThis.sessionStorage = { ...storeFn }; From 3bd7214ca64e41fd187f0b62f5f7710d4ef522fa Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 12:28:21 +0530 Subject: [PATCH 19/22] Minor enhancements --- .../service-provider-sfa/src/SfaServiceProvider.ts | 6 +++--- .../service-provider-torus/src/TorusServiceProvider.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts index 3f438a8fb..1ed1e3295 100644 --- a/packages/service-provider-sfa/src/SfaServiceProvider.ts +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -13,7 +13,7 @@ class SfaServiceProvider extends ServiceProviderBase { public torusKey: TorusKey; - public migratableKey: BN | null = null; + public migratableKey: BN | null = null; // Migration of key from SFA to tKey private nodeDetailManagerInstance: NodeDetailManager; @@ -79,10 +79,10 @@ class SfaServiceProvider extends ServiceProviderBase { const torusKey = await this.authInstance.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, finalVerifierParams, finalIdToken); this.torusKey = torusKey; - const { finalKeyData, oAuthKeyData } = torusKey; - const privKey = finalKeyData.privKey || oAuthKeyData.privKey; if (!torusKey.metadata.upgraded) { + const { finalKeyData, oAuthKeyData } = torusKey; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; this.migratableKey = new BN(privKey, "hex"); } const postboxKey = Torus.getPostboxKey(torusKey); diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 44a57b56c..22ef68cbf 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -56,10 +56,10 @@ class TorusServiceProvider extends ServiceProviderBase { if (obj) { const localPrivKey = Torus.getPostboxKey(obj); this.torusKey = obj; - const { finalKeyData, oAuthKeyData } = obj; - const privKey = finalKeyData.privKey || oAuthKeyData.privKey; if (!obj.metadata.upgraded) { + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; this.migratableKey = new BN(privKey, "hex"); } @@ -78,10 +78,10 @@ class TorusServiceProvider extends ServiceProviderBase { if (obj) { const localPrivKey = Torus.getPostboxKey(obj); this.torusKey = obj; - const { finalKeyData, oAuthKeyData } = obj; - const privKey = finalKeyData.privKey || oAuthKeyData.privKey; if (!obj.metadata.upgraded) { + const { finalKeyData, oAuthKeyData } = obj; + const privKey = finalKeyData.privKey || oAuthKeyData.privKey; this.migratableKey = new BN(privKey, "hex"); } @@ -95,12 +95,12 @@ class TorusServiceProvider extends ServiceProviderBase { */ async triggerHybridAggregateLogin(params: HybridAggregateLoginParams): Promise { const obj = await this.customAuthInstance.triggerHybridAggregateLogin(params); + this.torusKey = null; // Since there are multiple keys, we don't set the torusKey here. // `obj` maybe `null` in redirect mode. if (obj) { const aggregateLoginKey = Torus.getPostboxKey(obj.aggregateLogins[0]); const singleLoginKey = Torus.getPostboxKey(obj.singleLogin); - this.torusKey = null; this.postboxKey = new BN(aggregateLoginKey, "hex"); this.singleLoginKey = new BN(singleLoginKey, "hex"); } From 2bd11adcc438c21727aa7be7307c31abe33035fd Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 12:29:24 +0530 Subject: [PATCH 20/22] Add comment for migratable key --- packages/service-provider-torus/src/TorusServiceProvider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/service-provider-torus/src/TorusServiceProvider.ts b/packages/service-provider-torus/src/TorusServiceProvider.ts index 22ef68cbf..7c47c827a 100644 --- a/packages/service-provider-torus/src/TorusServiceProvider.ts +++ b/packages/service-provider-torus/src/TorusServiceProvider.ts @@ -20,7 +20,7 @@ class TorusServiceProvider extends ServiceProviderBase { public torusKey: TorusKey; - public migratableKey: BN | null = null; + public migratableKey: BN | null = null; // Migration of key from SFA to tKey customAuthArgs: CustomAuthArgs; From c9811781ab2fa61638bfd2ef602f5e605d66cb45 Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 12:41:21 +0530 Subject: [PATCH 21/22] Fix: No sync metadata if manual sync is true --- packages/core/src/core.ts | 2 +- packages/service-provider-sfa/src/SfaServiceProvider.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 2874ca3d3..2466c495e 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -277,7 +277,7 @@ class ThresholdKey implements ITKey { const tempStateManualSync = this.manualSync; this.manualSync = true; await this._initializeNewKey({ initializeModules: true, importedKey: this.serviceProvider.migratableKey, delete1OutOf1: true }); - await this.syncLocalMetadataTransitions(); + if (!tempStateManualSync) await this.syncLocalMetadataTransitions(); // Only sync if we were not in manual sync mode // restore manual sync flag this.manualSync = tempStateManualSync; } else { diff --git a/packages/service-provider-sfa/src/SfaServiceProvider.ts b/packages/service-provider-sfa/src/SfaServiceProvider.ts index 1ed1e3295..2e15db3df 100644 --- a/packages/service-provider-sfa/src/SfaServiceProvider.ts +++ b/packages/service-provider-sfa/src/SfaServiceProvider.ts @@ -55,8 +55,6 @@ class SfaServiceProvider extends ServiceProviderBase { if (params.serverTimeOffset) { this.authInstance.serverTimeOffset = params.serverTimeOffset; } - // Does the key assign - // if (this.authInstance.isLegacyNetwork) await this.authInstance.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }); let finalIdToken = idToken; let finalVerifierParams = { verifier_id: verifierId }; From 479f66788c8c5e67812be301e89a9e2e271d2def Mon Sep 17 00:00:00 2001 From: Yashovardhan Agrawal Date: Wed, 12 Jun 2024 13:16:13 +0530 Subject: [PATCH 22/22] Add comments for manual sync changes in key import flow --- packages/core/src/core.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 2466c495e..2f12f2481 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -274,10 +274,10 @@ class ThresholdKey implements ITKey { // provided no importKey is provided ( importKey take precedent ) if (this.serviceProvider.migratableKey && !importKey) { // importkey from server provider need to be atomic, hence manual sync is required. - const tempStateManualSync = this.manualSync; - this.manualSync = true; + const tempStateManualSync = this.manualSync; // temp store manual sync flag + this.manualSync = true; // Setting this as true since _initializeNewKey has a check where for importkey from server provider need to be atomic, hence manual sync is required. await this._initializeNewKey({ initializeModules: true, importedKey: this.serviceProvider.migratableKey, delete1OutOf1: true }); - if (!tempStateManualSync) await this.syncLocalMetadataTransitions(); // Only sync if we were not in manual sync mode + if (!tempStateManualSync) await this.syncLocalMetadataTransitions(); // Only sync if we were not in manual sync mode, if manual sync is set by developer, they should handle it themselves // restore manual sync flag this.manualSync = tempStateManualSync; } else {