From e61342ea3ba2efd602da04b2362421f4f4b8486e Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 7 Feb 2025 19:51:19 +0800 Subject: [PATCH 1/3] feat: initial commit integrate multicurve tkey tests passed --- package-lock.json | 201 +++++++++++++++---------------- package.json | 10 +- src/interfaces.ts | 9 ++ src/mpcCoreKit.ts | 190 ++++++++++++++++++++--------- tests/backwardCompatible.spec.ts | 5 +- tests/bip340.spec.ts | 17 ++- tests/ed25519.spec.ts | 18 +-- tests/factors.spec.ts | 5 +- tests/importRecovery.spec.ts | 3 +- tests/login.spec.ts | 10 +- tests/securityQuestion.spec.ts | 10 +- tests/sessionTime.spec.ts | 2 + tests/setup.ts | 12 +- 13 files changed, 282 insertions(+), 210 deletions(-) diff --git a/package-lock.json b/package-lock.json index d2fa201f..660ddb6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,11 +9,11 @@ "version": "4.1.7-alpha.0", "license": "ISC", "dependencies": { - "@tkey/common-types": "^15.1.0", - "@tkey/core": "^15.1.0", + "@tkey/common-types": "file:../tkey/packages/common-types", + "@tkey/core": "file:../tkey/packages/core/tkey-core-15.2.1-alpha.0.tgz", "@tkey/share-serialization": "^15.1.0", - "@tkey/storage-layer-torus": "^15.1.0", - "@tkey/tss": "^15.1.0", + "@tkey/storage-layer-torus": "file:../tkey/packages/storage-layer-torus/tkey-storage-layer-torus-15.2.1-alpha.0.tgz", + "@tkey/tss": "file:../tkey/packages/tss/tkey-tss-15.2.1-alpha.0.tgz", "@toruslabs/constants": "^14.2.0", "@toruslabs/customauth": "^20.3.0", "@toruslabs/elliptic-wrapper": "^0.1.1", @@ -84,6 +84,30 @@ } } }, + "../tkey/packages/common-types": { + "name": "@tkey/common-types", + "version": "15.2.1-alpha.0", + "license": "MIT", + "dependencies": { + "@toruslabs/customauth": "^20.3.0", + "@toruslabs/eccrypto": "^5.0.4", + "@toruslabs/torus.js": "^15.1.0", + "bn.js": "^5.2.1", + "elliptic": "^6.5.5", + "ts-custom-error": "^3.3.1" + }, + "devDependencies": { + "@types/bn.js": "^5.1.5", + "@types/elliptic": "^6.4.18" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, "node_modules/@ampproject/remapping": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz", @@ -3754,17 +3778,23 @@ } }, "node_modules/@tkey/common-types": { - "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/common-types/-/common-types-15.1.0.tgz", - "integrity": "sha512-oA5gLoyhNNMgCKcjvwLyU31TVS5KMT+lotRrjjoBdDvS0keZwzSLrtHWbXj8jZDlSZaqbd3VlPbCoHcqpk1irA==", + "resolved": "../tkey/packages/common-types", + "link": true + }, + "node_modules/@tkey/core": { + "version": "15.2.1-alpha.0", + "resolved": "file:../tkey/packages/core/tkey-core-15.2.1-alpha.0.tgz", + "integrity": "sha512-oR27rVrBUjhhABEsnclpa/z13C7jAl/QTkBthivDgnGCC8+CQ59uEaiBrD3LmVmrkwBJ31+y4g7cqJJJc0Wz7g==", "license": "MIT", "dependencies": { - "@toruslabs/customauth": "^20.3.0", + "@tkey/common-types": "^15.2.1-alpha.0", "@toruslabs/eccrypto": "^5.0.4", + "@toruslabs/http-helpers": "^7.0.0", "@toruslabs/torus.js": "^15.1.0", "bn.js": "^5.2.1", "elliptic": "^6.5.5", - "ts-custom-error": "^3.3.1" + "ethereum-cryptography": "^2.1.3", + "json-stable-stringify": "^1.1.1" }, "engines": { "node": ">=18.x", @@ -3774,7 +3804,7 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/common-types/node_modules/@toruslabs/torus.js": { + "node_modules/@tkey/core/node_modules/@toruslabs/torus.js": { "version": "15.1.1", "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", @@ -3798,20 +3828,36 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/core": { - "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/core/-/core-15.1.0.tgz", - "integrity": "sha512-JaFprczHR8fBEw1LrwKs87ASgpZagxQ9VZ6lAfAAI8jEh1yhz8djh9l2wzJbaFuLEOQskh7GoxpKfgB+YtBSmw==", + "node_modules/@tkey/service-provider-base": { + "version": "15.2.1-alpha.0", + "resolved": "https://registry.npmjs.org/@tkey/service-provider-base/-/service-provider-base-15.2.1-alpha.0.tgz", + "integrity": "sha512-8CTHuAoglCfDhxk3U0VkWJTl6XcL5UXIQAjtRTo/fbFClsHZ1lfQREXe24m+185XwBsZySrgE+UpL/Qvrq9vow==", "license": "MIT", "dependencies": { - "@tkey/common-types": "^15.1.0", - "@toruslabs/eccrypto": "^5.0.4", - "@toruslabs/http-helpers": "^7.0.0", + "@tkey/common-types": "^15.2.1-alpha.0", + "bn.js": "^5.2.1", + "elliptic": "^6.5.5" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, + "node_modules/@tkey/service-provider-torus": { + "version": "15.2.1-alpha.0", + "resolved": "https://registry.npmjs.org/@tkey/service-provider-torus/-/service-provider-torus-15.2.1-alpha.0.tgz", + "integrity": "sha512-ZkVaZvf0S+wrjN7nVAwq87+X02XJSCal2aCEYoRtoYQAcqp0wgTMPzBB6r4Jo1VyWIYbXS9zl6fZ90h1I86YVQ==", + "license": "MIT", + "dependencies": { + "@tkey/common-types": "^15.2.1-alpha.0", + "@tkey/service-provider-base": "^15.2.1-alpha.0", + "@toruslabs/customauth": "^20.3.0", "@toruslabs/torus.js": "^15.1.0", "bn.js": "^5.2.1", - "elliptic": "^6.5.5", - "ethereum-cryptography": "^2.1.3", - "json-stable-stringify": "^1.1.1" + "elliptic": "^6.5.5" }, "engines": { "node": ">=18.x", @@ -3821,7 +3867,7 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/core/node_modules/@toruslabs/torus.js": { + "node_modules/@tkey/service-provider-torus/node_modules/@toruslabs/torus.js": { "version": "15.1.1", "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", @@ -3845,15 +3891,15 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/service-provider-base": { + "node_modules/@tkey/share-serialization": { "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/service-provider-base/-/service-provider-base-15.1.0.tgz", - "integrity": "sha512-MruUxiWwyRczZ8KlhhGJ2TQ/p+VFPMOQZ089B5SIi7UsTOBMlzRqJWP3lM2fBSyQsfJCzpzXkj9a29ecpRZe0g==", + "resolved": "https://registry.npmjs.org/@tkey/share-serialization/-/share-serialization-15.1.0.tgz", + "integrity": "sha512-n8jf6WDAWcWKFUr/nSnBURxX8vlXCQjJOfQUchiqvvUFlExeDqXqJq8I5icn2hjxKqANNcXVf1vKSvOCxPCAng==", "license": "MIT", "dependencies": { "@tkey/common-types": "^15.1.0", "bn.js": "^5.2.1", - "elliptic": "^6.5.5" + "ethereum-cryptography": "^2.2.1" }, "engines": { "node": ">=18.x", @@ -3863,18 +3909,18 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/service-provider-torus": { + "node_modules/@tkey/share-serialization/node_modules/@tkey/common-types": { "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/service-provider-torus/-/service-provider-torus-15.1.0.tgz", - "integrity": "sha512-7tA/1ALPo4ToXvwTwMj9OF0wh97S3p1sCeilwRcfyxBMJGpaDW8MSbiAbPqaSkK/DT3AFxlkHwAXWYYZ4+ZueQ==", + "resolved": "https://registry.npmjs.org/@tkey/common-types/-/common-types-15.1.0.tgz", + "integrity": "sha512-oA5gLoyhNNMgCKcjvwLyU31TVS5KMT+lotRrjjoBdDvS0keZwzSLrtHWbXj8jZDlSZaqbd3VlPbCoHcqpk1irA==", "license": "MIT", "dependencies": { - "@tkey/common-types": "^15.1.0", - "@tkey/service-provider-base": "^15.1.0", "@toruslabs/customauth": "^20.3.0", + "@toruslabs/eccrypto": "^5.0.4", "@toruslabs/torus.js": "^15.1.0", "bn.js": "^5.2.1", - "elliptic": "^6.5.5" + "elliptic": "^6.5.5", + "ts-custom-error": "^3.3.1" }, "engines": { "node": ">=18.x", @@ -3884,7 +3930,7 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/service-provider-torus/node_modules/@toruslabs/torus.js": { + "node_modules/@tkey/share-serialization/node_modules/@toruslabs/torus.js": { "version": "15.1.1", "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", @@ -3908,31 +3954,13 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/share-serialization": { - "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/share-serialization/-/share-serialization-15.1.0.tgz", - "integrity": "sha512-n8jf6WDAWcWKFUr/nSnBURxX8vlXCQjJOfQUchiqvvUFlExeDqXqJq8I5icn2hjxKqANNcXVf1vKSvOCxPCAng==", - "license": "MIT", - "dependencies": { - "@tkey/common-types": "^15.1.0", - "bn.js": "^5.2.1", - "ethereum-cryptography": "^2.2.1" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, "node_modules/@tkey/storage-layer-torus": { - "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/storage-layer-torus/-/storage-layer-torus-15.1.0.tgz", - "integrity": "sha512-ZRk1FclOEomd7KAE3evKc/4Nk9I//RW20Bq2UrSPyKGf5YeTbDTaglAK0GrcG5CPtCpVLqh348Bw6hvGzjArag==", + "version": "15.2.1-alpha.0", + "resolved": "file:../tkey/packages/storage-layer-torus/tkey-storage-layer-torus-15.2.1-alpha.0.tgz", + "integrity": "sha512-5f3N6JoAFAqEGoEm/Qpuc5+ncxnXsuZMCCAjV4yokBJHVEbFLcYnW5LPWqbnWULavGMDHjnfpy49bBR/5UG3Uw==", "license": "MIT", "dependencies": { - "@tkey/common-types": "^15.1.0", + "@tkey/common-types": "^15.2.1-alpha.0", "@toruslabs/http-helpers": "^7.0.0", "base64url": "3.0.1", "bn.js": "^5.2.1", @@ -3948,17 +3976,18 @@ } }, "node_modules/@tkey/tss": { - "version": "15.1.0", - "resolved": "https://registry.npmjs.org/@tkey/tss/-/tss-15.1.0.tgz", - "integrity": "sha512-UcbJbWscIL83Zh1/i6M+X/xhN4EOimGV8JoWZ3D23Ji2pHPo8BgveUZA9i1DK4Y3YqqZ9aS8PvhSHt+KVnNluw==", + "version": "15.2.1-alpha.0", + "resolved": "file:../tkey/packages/tss/tkey-tss-15.2.1-alpha.0.tgz", + "integrity": "sha512-KncB4Ws/+Sj0dy/3fh+0dv299E9i7Rrkf4VeYeeV0D5ErlqCze8SWXoX9eQXdUgdF0ryxUHNLckjg6ljCcmXMg==", "license": "ISC", "dependencies": { - "@tkey/common-types": "^15.1.0", - "@tkey/core": "^15.1.0", - "@tkey/service-provider-torus": "^15.1.0", + "@tkey/common-types": "^15.2.1-alpha.0", + "@tkey/core": "^15.2.1-alpha.0", + "@tkey/service-provider-torus": "^15.2.1-alpha.0", "@toruslabs/customauth": "^20.3.0", + "@toruslabs/http-helpers": "^7.0.0", "@toruslabs/rss-client": "^2.0.1", - "@toruslabs/torus.js": "^15.1.0", + "@toruslabs/torus.js": "file:../../../torus.js/toruslabs-torus.js-15.1.1.tgz", "@types/bn.js": "^5.1.5", "bn.js": "^5.2.1", "elliptic": "^6.5.5", @@ -3967,8 +3996,8 @@ }, "node_modules/@tkey/tss/node_modules/@toruslabs/torus.js": { "version": "15.1.1", - "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", - "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", + "resolved": "file:../torus.js/toruslabs-torus.js-15.1.1.tgz", + "integrity": "sha512-kkLuZ9vpmWZTklva+RQFKozDMCR2ydhR5WBn9bbgJBZqVP75FSzLZL0LQYrFqBPMgKgV22uOQ8quXW6T+Kko/g==", "license": "MIT", "dependencies": { "@toruslabs/bs58": "^1.0.0", @@ -4515,16 +4544,15 @@ } }, "node_modules/@toruslabs/rss-client": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@toruslabs/rss-client/-/rss-client-2.0.1.tgz", - "integrity": "sha512-EBqAX7LK8ZBy4fiGTGQhI3wVvLvfUyfwREbNkfAxP/iwevgCA2EKLXGWtVsvJj/wXF8HkfjqOcorVeHwP2vvWw==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@toruslabs/rss-client/-/rss-client-2.0.2.tgz", + "integrity": "sha512-kmtl7KaxTOdrftxtv/hTzz6k2RJbVFq6oPs5Vg4u4iybdQYdRzyP1HqQ3wMu9ADhiItbZAg80VP4gOCCmjao4w==", "license": "MIT", "dependencies": { "@toruslabs/eccrypto": "^5.0.4", "@toruslabs/http-helpers": "^7.0.0", "bn.js": "^5.2.1", "elliptic": "^6.5.7", - "fetch": "^1.1.0", "loglevel": "^1.9.2" }, "engines": { @@ -6236,18 +6264,6 @@ "@noble/hashes": "^1.2.0" } }, - "node_modules/biskviit": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/biskviit/-/biskviit-1.0.1.tgz", - "integrity": "sha512-VGCXdHbdbpEkFgtjkeoBN8vRlbj1ZRX2/mxhE8asCCRalUx2nBzOomLJv8Aw/nRt5+ccDb+tPKidg4XxcfGW4w==", - "license": "MIT", - "dependencies": { - "psl": "^1.1.7" - }, - "engines": { - "node": ">=1.0.0" - } - }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -7858,15 +7874,6 @@ "typedarray-to-buffer": "3.1.5" } }, - "node_modules/encoding": { - "version": "0.1.12", - "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", - "integrity": "sha512-bl1LAgiQc4ZWr++pNYUdRe/alecaHFeHxIJ/pNciqGdKXghaTCOwKkbKp6ye7pKZGu/GcaSXFk8PBVhgs+dJdA==", - "license": "MIT", - "dependencies": { - "iconv-lite": "~0.4.13" - } - }, "node_modules/end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -9138,16 +9145,6 @@ "reusify": "^1.0.4" } }, - "node_modules/fetch": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/fetch/-/fetch-1.1.0.tgz", - "integrity": "sha512-5O8TwrGzoNblBG/jtK4NFuZwNCkZX6s5GfRNOaGtm+QGJEuNakSC/i2RW0R93KX6E0jVjNXm6O3CRN4Ql3K+yA==", - "license": "MIT", - "dependencies": { - "biskviit": "1.0.1", - "encoding": "0.1.12" - } - }, "node_modules/fetch-blob": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", @@ -10185,6 +10182,7 @@ "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dev": true, "license": "MIT", "dependencies": { "safer-buffer": ">= 2.1.2 < 3" @@ -13717,12 +13715,6 @@ "dev": true, "license": "MIT" }, - "node_modules/psl": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", - "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==", - "license": "MIT" - }, "node_modules/public-encrypt": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/public-encrypt/-/public-encrypt-4.0.3.tgz", @@ -14818,6 +14810,7 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "dev": true, "license": "MIT" }, "node_modules/schema-utils": { diff --git a/package.json b/package.json index 15a15e16..b150dc06 100644 --- a/package.json +++ b/package.json @@ -47,11 +47,11 @@ } }, "dependencies": { - "@tkey/common-types": "^15.1.0", - "@tkey/core": "^15.1.0", + "@tkey/common-types": "file:../tkey/packages/common-types", + "@tkey/core": "file:../tkey/packages/core/tkey-core-15.2.1-alpha.0.tgz", "@tkey/share-serialization": "^15.1.0", - "@tkey/storage-layer-torus": "^15.1.0", - "@tkey/tss": "^15.1.0", + "@tkey/storage-layer-torus": "file:../tkey/packages/storage-layer-torus/tkey-storage-layer-torus-15.2.1-alpha.0.tgz", + "@tkey/tss": "file:../tkey/packages/tss/tkey-tss-15.2.1-alpha.0.tgz", "@toruslabs/constants": "^14.2.0", "@toruslabs/customauth": "^20.3.0", "@toruslabs/elliptic-wrapper": "^0.1.1", @@ -59,8 +59,8 @@ "@toruslabs/fnd-base": "^14.2.0", "@toruslabs/metadata-helpers": "^6.0.0", "@toruslabs/openlogin-utils": "^8.2.1", - "@toruslabs/torus.js": "15.2.0-alpha.0", "@toruslabs/session-manager": "^3.1.0", + "@toruslabs/torus.js": "15.2.0-alpha.0", "@toruslabs/tss-client": "^3.3.0-alpha.0", "@toruslabs/tss-frost-client": "^1.0.1-alpha.0", "@toruslabs/tss-frost-common": "^1.0.2-alpha.0", diff --git a/src/interfaces.ts b/src/interfaces.ts index ccfb484d..3b19cb64 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -333,6 +333,15 @@ export interface Web3AuthOptions { * only scalar will be exported, scalar can be used for signing outside of this sdk but not for importing the key in other wallets. */ useClientGeneratedTSSKey?: boolean; + + /** + * @defaultValue `false` + * Set this flag to true to use the legacy flag for signing + * legacy flag do not support multicurve mode + * legacy ed25519 customAuth is only supported in legacy mode + * Note: This option is set to false by default. + */ + legacyFlag?: boolean; } export type Web3AuthOptionsWithDefaults = Required; diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 44b7fc1d..dddf41f3 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -2,7 +2,7 @@ import { BNString, KeyType, ONE_KEY_DELETE_NONCE, Point, secp256k1, SHARE_DELETE import { CoreError } from "@tkey/core"; import { ShareSerializationModule } from "@tkey/share-serialization"; import { TorusStorageLayer } from "@tkey/storage-layer-torus"; -import { factorKeyCurve, getPubKeyPoint, lagrangeInterpolation, TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss"; +import { factorKeyCurve, getPubKeyPoint, lagrangeInterpolation, TKeyTSS, TSS_TAG_DEFAULT, TSSTorusServiceProvider } from "@tkey/tss"; import { SIGNER_MAP } from "@toruslabs/constants"; import { AGGREGATE_VERIFIER, TORUS_METHOD, TorusAggregateLoginResponse, TorusLoginResponse, UX_MODE } from "@toruslabs/customauth"; import type { UX_MODE_TYPE } from "@toruslabs/customauth/dist/types/utils/enums"; @@ -10,7 +10,7 @@ import { Ed25519Curve, Secp256k1Curve } from "@toruslabs/elliptic-wrapper"; import { fetchLocalConfig } from "@toruslabs/fnd-base"; import { keccak256 } from "@toruslabs/metadata-helpers"; import { SessionManager } from "@toruslabs/session-manager"; -import { Torus as TorusUtils, TorusKey } from "@toruslabs/torus.js"; +import { getKeyCurve, Torus as TorusUtils, TorusKey } from "@toruslabs/torus.js"; import { Client, getDKLSCoeff, setupSockets } from "@toruslabs/tss-client"; import type { WasmLib as DKLSWasmLib } from "@toruslabs/tss-dkls-lib"; import { sign as signFrost } from "@toruslabs/tss-frost-client"; @@ -136,6 +136,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (!options.disableHashedFactorKey) options.disableHashedFactorKey = false; if (!options.hashedFactorNonce) options.hashedFactorNonce = options.web3AuthClientId; if (options.disableSessionManager === undefined) options.disableSessionManager = false; + if (!options.legacyFlag) options.legacyFlag = false; + this.sessionSigGenerator = new DefaultSessionSigGeneratorPlugin(this); this.options = options as Web3AuthOptionsWithDefaults; @@ -221,6 +223,27 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { return this.sessionSigGenerator.getSessionSigs(); } + public setTkeyType(tkeyType: KeyType) { + // check tkeyType is supported by tssLib + this._keyType = tkeyType; + } + + public getTssShare(factorkey: BN, accountIndex?: number) { + return this.tkey.getTSSShare(factorkey, { + tssTag: TSS_TAG_DEFAULT, + keyType: this.keyType, + accountIndex: accountIndex === undefined ? this.state.accountIndex : accountIndex, + }); + } + + public getTssData(args: { skipThrow: boolean } = { skipThrow: false }) { + const result = this.tkey.metadata.getTssData(this.keyType, TSS_TAG_DEFAULT); + if (!result && !args.skipThrow) { + throw CoreKitError.noMetadataFound(); + } + return result; + } + // 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 @@ -234,7 +257,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { const tssIndexesBN: BN[] = []; for (let i = 0; i < factorKey.length; i++) { const factorKeyBNInput = new BN(factorKey[i], "hex"); - const { tssIndex, tssShare } = await this.tKey.getTSSShare(factorKeyBNInput); + const { tssIndex, tssShare } = await this.getTssShare(factorKeyBNInput); if (tssIndexes.includes(tssIndex)) { // reset instance before throw error await this.init(); @@ -245,7 +268,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { tssShares.push(tssShare); } - const finalKey = lagrangeInterpolation(this.tkey.tssCurve, tssShares, tssIndexesBN); + const tssCurve = getKeyCurve(this.keyType); + const finalKey = lagrangeInterpolation(tssCurve, tssShares, tssIndexesBN); // reset instance after recovery completed await this.init(); return finalKey.toString("hex", 64); @@ -261,6 +285,14 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { throw CoreKitError.invalidConfig("DKG is not supported for ed25519 signature type"); } + // multicurve only support for secp256k1 torus/ sss + let keyType = KeyType.secp256k1; + if (this.options.legacyFlag) { + // check for added supported keyType + // if keyType is ed25519, + keyType = this.keyType; + } + this.torusSp = new TSSTorusServiceProvider({ customAuthArgs: { web3AuthClientId: this.options.web3AuthClientId, @@ -270,7 +302,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { redirectPathName: this.options.redirectPathName, locationReplaceOnRedirect: true, serverTimeOffset: this.options.serverTimeOffset, - keyType: this.keyType, + keyType, useDkg: this.options.useDKG, }, }); @@ -290,7 +322,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { modules: { shareSerialization: shareSerializationModule, }, - tssKeyType: this.keyType, + legacyMetadataFlag: this.options.legacyFlag, }); if (this.isRedirectMode) { @@ -424,7 +456,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { // prefetch tss pub keys. const prefetchTssPubs = []; for (let i = 0; i < prefetchTssPublicKeys; i++) { - prefetchTssPubs.push(this.torusSp.getTSSPubKey(this.tkey.tssTag, i)); + prefetchTssPubs.push(this.torusSp.getTSSPubKey(this.tkey.tssTag, i, this.keyType)); } // get postbox key. @@ -546,7 +578,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } public setTssWalletIndex(accountIndex: number) { - this.updateState({ tssPubKey: this.tKey.getTSSPub(accountIndex).toSEC1(this.tkey.tssCurve, false), accountIndex }); + this.updateState({ tssPubKey: this.getPubKey(false, accountIndex), accountIndex }); } public getCurrentFactorKey(): IFactorKey { @@ -628,7 +660,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (!this.state.factorKey) { throw CoreKitError.factorKeyNotPresent("factorKey not present in state when getting tss factor public key."); } - const factorPubsList = this.tKey.metadata.factorPubs[this.tKey.tssTag]; + const tssData = this.getTssData(); + const factorPubsList = tssData.factorPubs; return factorPubsList.map((factorPub) => factorPub.toSEC1(factorKeyCurve, true).toString("hex")); }; @@ -675,17 +708,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { /** * Get public key point in SEC1 format. */ - public getPubKey(): Buffer { - const { tssPubKey } = this.state; - return Buffer.from(tssPubKey); + public getPubKey(compressed: boolean = true, accountIndex?: number): Buffer { + const tssPubKey = this.getPubKeyPoint(accountIndex); + const tssCurve = getKeyCurve(this.keyType); + return tssPubKey.toSEC1(tssCurve, compressed); } /** * Get public key point. */ - public getPubKeyPoint(): Point { - const { tssPubKey } = this.state; - return Point.fromSEC1(this.tkey.tssCurve, tssPubKey.toString("hex")); + public getPubKeyPoint(accountIndex?: number): Point { + const tssPubKey = this.tkey.getTSSPub(this.keyType, TSS_TAG_DEFAULT, accountIndex === undefined ? this.state.accountIndex : accountIndex); + return tssPubKey; } /** @@ -698,7 +732,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { throw CoreKitError.default(`getPubKeyEd25519 not supported for signature type ${this.sigType}`); } - const p = this.tkey.tssCurve.keyFromPublic(this.getPubKey()).getPublic(); + const tssCurve = getKeyCurve(this.keyType); + const p = this.getPubKeyPoint().toEllipticPoint(tssCurve); return ed25519().keyFromPublic(p).getPublic(); } @@ -712,7 +747,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { throw CoreKitError.default(`getPubKeyBip340 not supported for signature type ${this.sigType}`); } - const p = this.tkey.tssCurve.keyFromPublic(this.getPubKey()).getPublic(); + const tssCurve = getKeyCurve(this.keyType); + const p = this.getPubKeyPoint().toEllipticPoint(tssCurve); return p.getX().toBuffer("be", 32); } @@ -732,9 +768,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (!this.state.factorKey) { throw CoreKitError.factorKeyNotPresent("factorKey not present in state when signing."); } - const { tssShare } = await this.tKey.getTSSShare(this.state.factorKey, { - accountIndex: 0, - }); + const { tssShare } = await this.getTssShare(this.state.factorKey, 0); const tssNonce = this.getTssNonce(); if (!tssPubKey || !torusNodeTSSEndpoints) { @@ -749,7 +783,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { const clientIndex = parties - 1; // 1. setup // generate endpoints for servers - const { nodeIndexes } = await this.torusSp.getTSSPubKey(this.tKey.tssTag, this.tKey.metadata.tssNonces[this.tKey.tssTag]); + const tssData = this.getTssData(); + const { nodeIndexes } = await this.torusSp.getTSSPubKey(this.tKey.tssTag, tssData.tssNonce, this.keyType); const { endpoints, tssWSEndpoints, @@ -846,12 +881,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (!this.state.factorKey) { throw CoreKitError.factorKeyNotPresent("factorKey not present in state when deleting a factor."); } - if (!this.tKey.metadata.factorPubs) { + const tssData = this.getTssData(); + if (!tssData.factorPubs) { throw CoreKitError.factorPubsMissing(); } await this.atomicSync(async () => { - const remainingFactors = this.tKey.metadata.factorPubs[this.tKey.tssTag].length || 0; + const remainingFactors = tssData.factorPubs.length || 0; if (remainingFactors <= 1) { throw CoreKitError.cannotDeleteLastFactor("Cannot delete last factor"); } @@ -862,9 +898,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } const authSignatures = await this.getSessionSignatures(); - await this.tKey.deleteFactorPub({ factorKey: this.state.factorKey, deleteFactorPub: factorPub, authSignatures }); + await this.tKey.deleteFactorPub({ factorKey: this.state.factorKey, deleteFactorPub: factorPub, authSignatures, tssTag: TSS_TAG_DEFAULT }); const factorPubHex = fpp.toSEC1(factorKeyCurve, true).toString("hex"); - const allDesc = this.tKey.metadata.getShareDescription(); + const allDesc = this.tkey.metadata.getShareDescription(); const keyDesc = allDesc[factorPubHex]; if (keyDesc) { await Promise.all(keyDesc.map(async (desc) => this.tKey?.metadata.deleteShareDescription(factorPubHex, desc))); @@ -903,10 +939,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { public getKeyDetails(): MPCKeyDetails { this.checkReady(); + const tssData = this.getTssData({ skipThrow: true }); const tkeyDetails = this.tKey.getKeyDetails(); - const tssPubKey = this.state.tssPubKey ? Point.fromSEC1(this.tkey.tssCurve, this.state.tssPubKey.toString("hex")) : undefined; + const tssCurve = getKeyCurve(this.keyType); + // TODO: fix me, should remove tssPubKey from state + const tssPubKey = this.state.tssPubKey ? Point.fromSEC1(tssCurve, this.state.tssPubKey.toString("hex")) : undefined; - const factors = this.tKey.metadata.factorPubs ? this.tKey.metadata.factorPubs[this.tKey.tssTag] : []; + const factors = tssData?.factorPubs ? tssData.factorPubs : []; const keyDetails: MPCKeyDetails = { // use tkey's for now requiredFactors: tkeyDetails.requiredShares, @@ -992,10 +1031,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { const exportTssKey0 = await this.tKey._UNSAFE_exportTssKey({ factorKey: this.state.factorKey, authSignatures: this.state.signatures, + keyType: this.keyType, + tssTag: TSS_TAG_DEFAULT, }); const accountNonce = this.getAccountNonce(); - const tssKey = exportTssKey0.add(accountNonce).umod(this.tKey.tssCurve.n); + const tssCurve = getKeyCurve(this.keyType); + const tssKey = exportTssKey0.add(accountNonce).umod(tssCurve.n); return tssKey.toString("hex", FIELD_ELEMENT_HEX_LEN); } @@ -1017,6 +1059,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { const exportEd25519Seed = await this.tKey._UNSAFE_exportTssEd25519Seed({ factorKey: this.state.factorKey, authSignatures: this.state.signatures, + tssTag: TSS_TAG_DEFAULT, }); return exportEd25519Seed; @@ -1107,22 +1150,24 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } } - private async importTssKey(tssKey: string, factorPub: Point, newTSSIndex: TssShareType = TssShareType.DEVICE): Promise { - if (!this.state.signatures) { - throw CoreKitError.signaturesNotPresent("Signatures not present in state when importing tss key."); - } + // private async importTssKey(tssKey: string, factorPub: Point, newTSSIndex: TssShareType = TssShareType.DEVICE): Promise { + // if (!this.state.signatures) { + // throw CoreKitError.signaturesNotPresent("Signatures not present in state when importing tss key."); + // } - await this.tKey.importTssKey( - { tag: this.tKey.tssTag, importKey: Buffer.from(tssKey, "hex"), factorPub, newTSSIndex }, - { authSignatures: this.state.signatures } - ); - } + // const keyType = this._keyType + // await this.tKey.importTssKey( + // { tssTag: this.tKey.tssTag, importKey: Buffer.from(tssKey, "hex"), factorPubs: [factorPub], newTSSIndexes: [newTSSIndex], tssKeyType: keyType }, + // { authSignatures: this.state.signatures } + // ); + // } private getTssNonce(): number { - if (!this.tKey.metadata.tssNonces || this.tKey.metadata.tssNonces[this.tKey.tssTag] === undefined) { + const tssData = this.getTssData(); + if (tssData.tssNonce === undefined) { throw CoreKitError.tssNoncesMissing(`tssNonce not present for tag ${this.tKey.tssTag}`); } - const tssNonce = this.tKey.metadata.tssNonces[this.tKey.tssTag]; + const { tssNonce } = tssData; return tssNonce; } @@ -1141,15 +1186,37 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } const deviceTSSIndex = TssShareType.DEVICE; const factorPub = getPubKeyPoint(factorKey, factorKeyCurve); - if (!importTssKey) { - const ec = new EC(this.keyType); - const deviceTSSShare = ec.genKeyPair().getPrivate(); - await this.tKey.initialize({ factorPub, deviceTSSShare, deviceTSSIndex }); - } else { - await this.tKey.initialize({ skipTssInit: true }); - await this.tKey.reconstructKey(); - await this.importTssKey(importTssKey, factorPub, deviceTSSIndex); - } + + const ec = new EC(this.keyType); + const deviceTSSShare = ec.genKeyPair().getPrivate(); + await this.tKey.initialize(); + + // if both keyType library is avaiable, initialize secp256k1 first as secp256k1 is initialize offline + + // check if key is in the tsslib and keytype exists + await this.tKey.initializeTss({ + importKey: importTssKey ? Buffer.from(importTssKey, "hex") : undefined, + factorPub, + deviceTSSShare, + deviceTSSIndex, + tssKeyType: this._keyType, + serverOpts: { + // selectedServers: [], + authSignatures: this.state.signatures, + }, + }); + + //TODO resolve this + // if both key type available + // if () { + // await this.tKey.initializeTss({ + // importKey: importTssKey? Buffer.from(importTssKey) : undefined, tssKeyType: this._keyType, + // serverOpts: { + // // selectedServers: [], + // authSignatures: this.state.signatures + // } + // }); + // } // Finalize initialization. await this.tKey.reconstructKey(); @@ -1212,8 +1279,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { this.state.accountIndex = 0; } // Read tss meta data. - const { tssIndex: tssShareIndex } = await this.tKey.getTSSShare(factorKey); - const tssPubKey = this.tKey.getTSSPub().toSEC1(this.tkey.tssCurve, false); + const { tssIndex: tssShareIndex } = await this.getTssShare(factorKey); + const tssCurve = getKeyCurve(this._keyType); + const tssPubKey = this.tKey.getTSSPub(this.keyType, TSS_TAG_DEFAULT).toSEC1(tssCurve, false); this.updateState({ tssShareIndex, tssPubKey, factorKey }); @@ -1246,12 +1314,16 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { await this.tKey.inputShareStoreSafe(factorKeyMetadata, true); await this.tKey.reconstructKey(); + const tssCurve = getKeyCurve(this._keyType); + this.updateState({ factorKey: new BN(result.factorKey, "hex"), postBoxKey, postboxKeyNodeIndexes: result.postboxKeyNodeIndexes || [], tssShareIndex: result.tssShareIndex, - tssPubKey: this.tkey.getTSSPub().toSEC1(this.tKey.tssCurve, false), + // still need tssPubkey on the state? + // should compute from tkey as we support the account index + tssPubKey: this.tkey.getTSSPub(this.keyType, TSS_TAG_DEFAULT).toSEC1(tssCurve, false), signatures: result.signatures, userInfo: result.userInfo, }); @@ -1275,9 +1347,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (!this.state.factorKey) { throw CoreKitError.factorKeyNotPresent("factorKey not present in state when creating session."); } - const { tssShare } = await this.tKey.getTSSShare(this.state.factorKey, { - accountIndex: this.state.accountIndex, - }); + const { tssShare } = await this.getTssShare(this.state.factorKey); if (!postBoxKey || !factorKey || !tssShare || !tssPubKey || !userInfo) { throw CoreKitError.userNotLoggedIn(); } @@ -1334,10 +1404,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { */ private async copyOrCreateShare(newFactorTSSIndex: number, newFactorPub: Point) { this.checkReady(); - if (!this.tKey.metadata.factorPubs || !Array.isArray(this.tKey.metadata.factorPubs[this.tKey.tssTag])) { + const tssData = this.getTssData(); + if (!tssData.factorPubs || !Array.isArray(tssData.factorPubs)) { throw CoreKitError.factorPubsMissing("'factorPubs' is missing in the metadata. Failed to copy factor public key."); } - if (!this.tKey.metadata.factorEncs || typeof this.tKey.metadata.factorEncs[this.tKey.tssTag] !== "object") { + if (!tssData.factorEncs || typeof tssData.factorEncs !== "object") { throw CoreKitError.factorEncsMissing("'factorEncs' is missing in the metadata. Failed to copy factor public key."); } if (!this.state.factorKey) { @@ -1346,7 +1417,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (VALID_SHARE_INDICES.indexOf(newFactorTSSIndex) === -1) { throw CoreKitError.newShareIndexInvalid(`Invalid share type provided (${newFactorTSSIndex}). Valid share types are ${VALID_SHARE_INDICES}.`); } - if (this.tKey.metadata.factorPubs[this.tKey.tssTag].length >= MAX_FACTORS) { + if (tssData.factorPubs.length >= MAX_FACTORS) { throw CoreKitError.maximumFactorsReached(`The maximum number of allowable factors (${MAX_FACTORS}) has been reached.`); } const authSignatures = await this.getSessionSignatures(); @@ -1357,6 +1428,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { newFactorPub, newTSSIndex: newFactorTSSIndex, refreshShares: this.state.tssShareIndex !== newFactorTSSIndex, // Refresh shares if we have a new factor key index. + tssTag: TSS_TAG_DEFAULT, }); } @@ -1410,7 +1482,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { additionalMetadata = {}; } - const { tssIndex } = await this.tKey.getTSSShare(factorKey); + const { tssIndex } = await this.getTssShare(factorKey); const factorPoint = getPubKeyPoint(factorKey, factorKeyCurve); const factorPub = factorPoint.toSEC1(factorKeyCurve, true).toString("hex"); @@ -1544,7 +1616,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (this._sigType === "ed25519" && this.state.accountIndex !== 0) { throw CoreKitError.default("Account index not supported for ed25519"); } - const { tssShare } = await this.tKey.getTSSShare(this.state.factorKey); + const { tssShare } = await this.getTssShare(this.state.factorKey); const clientShareAdjusted = tssShare.mul(clientCoefficient).umod(ec.n); const clientShareAdjustedHex = ec.scalarToBuffer(clientShareAdjusted, Buffer).toString("hex"); diff --git a/tests/backwardCompatible.spec.ts b/tests/backwardCompatible.spec.ts index 5c244c6d..94c0249a 100644 --- a/tests/backwardCompatible.spec.ts +++ b/tests/backwardCompatible.spec.ts @@ -33,7 +33,7 @@ const checkLogin = async (coreKitInstance: Web3AuthMPCCoreKit) => { assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.LOGGED_IN); assert.strictEqual(keyDetails.requiredFactors, 0); const factorkey = coreKitInstance.getCurrentFactorKey(); - await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex")); + await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); }; variable.forEach((testVariable) => { @@ -49,6 +49,7 @@ variable.forEach((testVariable) => { tssLib, storage: storageInstance, manualSync, + legacyFlag: true, }); const coreKitInstance = newCoreKitInstance(); @@ -69,6 +70,8 @@ variable.forEach((testVariable) => { idToken, }); + // console.log(coreKitInstance.tKey.metadata) + // console.log(coreKitInstance.state); // get key details await checkLogin(coreKitInstance); diff --git a/tests/bip340.spec.ts b/tests/bip340.spec.ts index 298d1009..39ec07d4 100644 --- a/tests/bip340.spec.ts +++ b/tests/bip340.spec.ts @@ -9,6 +9,7 @@ import { schnorr as bip340 } from '@noble/curves/secp256k1'; import { AsyncStorage, COREKIT_STATUS, MemoryStorage, WEB3AUTH_NETWORK, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit } from "../src"; import { bufferToElliptic, criticalResetAccount, mockLogin, mockLogin2 } from "./setup"; +import { getKeyCurve } from "@toruslabs/torus.js"; type TestVariable = { web3AuthNetwork: WEB3AUTH_NETWORK_TYPE; @@ -31,9 +32,7 @@ const checkLogin = async (coreKitInstance: Web3AuthMPCCoreKit, accountIndex = 0) assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.LOGGED_IN); assert.strictEqual(keyDetails.requiredFactors, 0); const factorkey = coreKitInstance.getCurrentFactorKey(); - await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { - accountIndex, - }); + await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex"), accountIndex ); }; const storageInstance = new MemoryStorage(); @@ -85,11 +84,10 @@ variable.forEach((testVariable) => { // get key details await checkLogin(coreKitInstance); - checkPubKey = bufferToElliptic(coreKitInstance.getPubKey(), coreKitInstance.tKey.tssCurve); + const tssCurve = getKeyCurve(coreKitInstance.keyType) + checkPubKey = coreKitInstance.getPubKeyPoint().toEllipticPoint(tssCurve); const factorkey = coreKitInstance.getCurrentFactorKey(); - const { tssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { - threshold: 0, - }); + const { tssShare } = await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); checkTssShare = tssShare; if (manualSync) { @@ -125,9 +123,10 @@ variable.forEach((testVariable) => { // get key details await checkLogin(coreKitInstance); - const newPubKey = bufferToElliptic(coreKitInstance.getPubKey(), coreKitInstance.tKey.tssCurve); + const tssCurve = getKeyCurve(coreKitInstance.keyType) + const newPubKey = coreKitInstance.getPubKeyPoint().toEllipticPoint(tssCurve); const factorkey = coreKitInstance.getCurrentFactorKey(); - const { tssShare: newTssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex")); + const { tssShare: newTssShare } = await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); assert(checkPubKey.eq(newPubKey)); assert(checkTssShare.eq(newTssShare)); }); diff --git a/tests/ed25519.spec.ts b/tests/ed25519.spec.ts index 208beb5a..40efc6ad 100644 --- a/tests/ed25519.spec.ts +++ b/tests/ed25519.spec.ts @@ -8,6 +8,7 @@ import BN from "bn.js"; import { AsyncStorage, COREKIT_STATUS, ed25519, MemoryStorage, WEB3AUTH_NETWORK, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit } from "../src"; import { bufferToElliptic, criticalResetAccount, mockLogin, mockLogin2 } from "./setup"; +import { getKeyCurve } from "@toruslabs/torus.js"; type TestVariable = { web3AuthNetwork: WEB3AUTH_NETWORK_TYPE; @@ -30,9 +31,7 @@ const checkLogin = async (coreKitInstance: Web3AuthMPCCoreKit, accountIndex = 0) assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.LOGGED_IN); assert.strictEqual(keyDetails.requiredFactors, 0); const factorkey = coreKitInstance.getCurrentFactorKey(); - await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { - accountIndex, - }); + await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex") ,accountIndex); }; const storageInstance = new MemoryStorage(); @@ -48,6 +47,7 @@ variable.forEach((testVariable) => { tssLib, storage: storageInstance, manualSync, + legacyFlag: true, }); async function resetAccount() { @@ -84,11 +84,10 @@ variable.forEach((testVariable) => { // get key details await checkLogin(coreKitInstance); - checkPubKey = bufferToElliptic(coreKitInstance.getPubKey(), coreKitInstance.tKey.tssCurve); + const tssCurve = getKeyCurve(coreKitInstance.keyType) + checkPubKey = bufferToElliptic(coreKitInstance.getPubKey(), tssCurve); const factorkey = coreKitInstance.getCurrentFactorKey(); - const { tssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { - threshold: 0, - }); + const { tssShare } = await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); checkTssShare = tssShare; if (manualSync) { @@ -124,9 +123,10 @@ variable.forEach((testVariable) => { // get key details await checkLogin(coreKitInstance); - const newPubKey = bufferToElliptic(coreKitInstance.getPubKey(), coreKitInstance.tKey.tssCurve); + const tssCurve = getKeyCurve(coreKitInstance.keyType) + const newPubKey = bufferToElliptic(coreKitInstance.getPubKey(), tssCurve); const factorkey = coreKitInstance.getCurrentFactorKey(); - const { tssShare: newTssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex")); + const { tssShare: newTssShare } = await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); assert(checkPubKey.eq(newPubKey)); assert(checkTssShare.eq(newTssShare)); }); diff --git a/tests/factors.spec.ts b/tests/factors.spec.ts index e3a75069..aada6af3 100644 --- a/tests/factors.spec.ts +++ b/tests/factors.spec.ts @@ -9,6 +9,7 @@ import BN from "bn.js"; import { COREKIT_STATUS, IAsyncStorage, IStorage, MemoryStorage, TssLibType, TssShareType, WEB3AUTH_NETWORK, Web3AuthMPCCoreKit } from "../src"; import { AsyncMemoryStorage, bufferToElliptic, criticalResetAccount, mockLogin } from "./setup"; +import { getKeyCurve } from "@toruslabs/torus.js"; type FactorTestVariable = { manualSync?: boolean; @@ -21,9 +22,9 @@ function getPubKeys(kit: Web3AuthMPCCoreKit, indices: number[]): EllipticPoint[] if (!kit.supportsAccountIndex) { indices = indices.filter((i) => i === 0); } + const tssCurve = getKeyCurve(kit.keyType) const pubKeys = indices.map((i) => { - kit.setTssWalletIndex(i); - return bufferToElliptic(kit.getPubKey()); + return kit.getPubKeyPoint( i).toEllipticPoint(tssCurve); }); return pubKeys; } diff --git a/tests/importRecovery.spec.ts b/tests/importRecovery.spec.ts index 8d3bbc8e..6964a4f9 100644 --- a/tests/importRecovery.spec.ts +++ b/tests/importRecovery.spec.ts @@ -6,6 +6,7 @@ import { tssLib as tssLibFROST } from "@toruslabs/tss-frost-lib"; import { AsyncStorage, MemoryStorage, TssLibType, TssShareType, WEB3AUTH_NETWORK } from "../src"; import { bufferToElliptic, criticalResetAccount, newCoreKitLogInInstance } from "./setup"; +import { getKeyCurve } from "@toruslabs/torus.js"; type ImportKeyTestVariable = { manualSync?: boolean; @@ -81,7 +82,7 @@ export const ImportTest = async (testVariable: ImportKeyTestVariable) => { const tssPubkey = bufferToElliptic(coreKitInstance3.getPubKey()); const exportedTssKey3 = await coreKitInstance3._UNSAFE_exportTssKey(); - const tssCurve = coreKitInstance3.tKey.tssCurve; + const tssCurve = getKeyCurve(coreKitInstance.keyType); const exportedPub = tssCurve.keyFromPrivate(exportedTssKey3).getPublic(); assert(tssPubkey.eq(exportedPub)); diff --git a/tests/login.spec.ts b/tests/login.spec.ts index 33a45ef3..fd0a4375 100644 --- a/tests/login.spec.ts +++ b/tests/login.spec.ts @@ -33,9 +33,7 @@ const checkLogin = async (coreKitInstance: Web3AuthMPCCoreKit, accountIndex = 0) assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.LOGGED_IN); assert.strictEqual(keyDetails.requiredFactors, 0); const factorkey = coreKitInstance.getCurrentFactorKey(); - await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { - accountIndex, - }); + await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex"), accountIndex ); }; const storageInstance = new MemoryStorage(); @@ -97,9 +95,7 @@ variable.forEach((testVariable) => { checkPubKey = bufferToElliptic(coreKitInstance.getPubKey()); const factorkey = coreKitInstance.getCurrentFactorKey(); - const { tssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { - threshold: 0, - }); + const { tssShare } = await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); checkTssShare = tssShare; if (manualSync) { @@ -137,7 +133,7 @@ variable.forEach((testVariable) => { await checkLogin(coreKitInstance); const newPubKey = bufferToElliptic(coreKitInstance.getPubKey()); const factorkey = coreKitInstance.getCurrentFactorKey(); - const { tssShare: newTssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex")); + const { tssShare: newTssShare } = await coreKitInstance.getTssShare(new BN(factorkey.factorKey, "hex")); assert(checkPubKey.eq(newPubKey)); assert(checkTssShare.eq(newTssShare)); }); diff --git a/tests/securityQuestion.spec.ts b/tests/securityQuestion.spec.ts index 3f1d788b..df74cc2c 100644 --- a/tests/securityQuestion.spec.ts +++ b/tests/securityQuestion.spec.ts @@ -63,7 +63,7 @@ export const TssSecurityQuestionsTest = async (newInstance: () => Promise securityQuestion.recoverFactor(instance, "wrong answer")); @@ -77,26 +77,26 @@ export const TssSecurityQuestionsTest = async (newInstance: () => Promise instance.tKey.getTSSShare(new BN(factor, "hex"))); + await assert.rejects(() => instance.getTssShare(new BN(factor, "hex"))); // recover factor // check wrong answer diff --git a/tests/sessionTime.spec.ts b/tests/sessionTime.spec.ts index f649fbdc..7175e4a4 100644 --- a/tests/sessionTime.spec.ts +++ b/tests/sessionTime.spec.ts @@ -75,6 +75,7 @@ variable.forEach(async (testVariable) => { manualSync, sessionTime, disableSessionManager, + legacyFlag: false, }); if (coreKitInstance.status === COREKIT_STATUS.INITIALIZED) await criticalResetAccount(coreKitInstance); } @@ -96,6 +97,7 @@ variable.forEach(async (testVariable) => { } }); + console.log(coreKitInstance.keyType) await coreKitInstance.loginWithJWT({ verifier: "torus-test-health", verifierId: parsedToken.email, diff --git a/tests/setup.ts b/tests/setup.ts index 8b6adc5f..7a66c35a 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -31,14 +31,10 @@ export const criticalResetAccount = async (coreKitInstance: Web3AuthMPCCoreKit): throw new Error("coreKitInstance is not set"); } - if (coreKitInstance.tKey.secp256k1Key) { - await coreKitInstance.tKey.CRITICAL_deleteTkey(); - } else { - await coreKitInstance.tKey.storageLayer.setMetadata({ - privKey: new BN(coreKitInstance.state.postBoxKey!, "hex"), - input: { message: "KEY_NOT_FOUND" }, - }); - } + await coreKitInstance.tKey.storageLayer.setMetadata({ + privKey: new BN(coreKitInstance.state.postBoxKey!, "hex"), + input: { message: "KEY_NOT_FOUND" }, + }); }; const privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCD7oLrcKae+jVZPGx52Cb/lKhdKxpXjl9eGNa1MlY57A=="; From 9a25f60fe84abf38d7a3e30174a7192970ebf378 Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 10 Feb 2025 15:21:00 +0800 Subject: [PATCH 2/3] fix: add randomId verifier testcases --- package-lock.json | 130 ++---------------------------------------- package.json | 2 +- src/mpcCoreKit.ts | 6 +- tests/factors.spec.ts | 11 +++- 4 files changed, 19 insertions(+), 130 deletions(-) diff --git a/package-lock.json b/package-lock.json index 660ddb6e..933e5d5a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "@toruslabs/metadata-helpers": "^6.0.0", "@toruslabs/openlogin-utils": "^8.2.1", "@toruslabs/session-manager": "^3.1.0", - "@toruslabs/torus.js": "15.2.0-alpha.0", + "@toruslabs/torus.js": "file:../torus.js/toruslabs-torus.js-15.1.1.tgz", "@toruslabs/tss-client": "^3.3.0-alpha.0", "@toruslabs/tss-frost-client": "^1.0.1-alpha.0", "@toruslabs/tss-frost-common": "^1.0.2-alpha.0", @@ -3804,30 +3804,6 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/core/node_modules/@toruslabs/torus.js": { - "version": "15.1.1", - "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", - "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", - "license": "MIT", - "dependencies": { - "@toruslabs/bs58": "^1.0.0", - "@toruslabs/constants": "^14.0.0", - "@toruslabs/eccrypto": "^5.0.4", - "@toruslabs/http-helpers": "^7.0.0", - "bn.js": "^5.2.1", - "elliptic": "^6.5.7", - "ethereum-cryptography": "^2.2.1", - "json-stable-stringify": "^1.1.1", - "loglevel": "^1.9.2" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, "node_modules/@tkey/service-provider-base": { "version": "15.2.1-alpha.0", "resolved": "https://registry.npmjs.org/@tkey/service-provider-base/-/service-provider-base-15.2.1-alpha.0.tgz", @@ -3867,30 +3843,6 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/service-provider-torus/node_modules/@toruslabs/torus.js": { - "version": "15.1.1", - "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", - "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", - "license": "MIT", - "dependencies": { - "@toruslabs/bs58": "^1.0.0", - "@toruslabs/constants": "^14.0.0", - "@toruslabs/eccrypto": "^5.0.4", - "@toruslabs/http-helpers": "^7.0.0", - "bn.js": "^5.2.1", - "elliptic": "^6.5.7", - "ethereum-cryptography": "^2.2.1", - "json-stable-stringify": "^1.1.1", - "loglevel": "^1.9.2" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, "node_modules/@tkey/share-serialization": { "version": "15.1.0", "resolved": "https://registry.npmjs.org/@tkey/share-serialization/-/share-serialization-15.1.0.tgz", @@ -3930,30 +3882,6 @@ "@babel/runtime": "7.x" } }, - "node_modules/@tkey/share-serialization/node_modules/@toruslabs/torus.js": { - "version": "15.1.1", - "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", - "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", - "license": "MIT", - "dependencies": { - "@toruslabs/bs58": "^1.0.0", - "@toruslabs/constants": "^14.0.0", - "@toruslabs/eccrypto": "^5.0.4", - "@toruslabs/http-helpers": "^7.0.0", - "bn.js": "^5.2.1", - "elliptic": "^6.5.7", - "ethereum-cryptography": "^2.2.1", - "json-stable-stringify": "^1.1.1", - "loglevel": "^1.9.2" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, "node_modules/@tkey/storage-layer-torus": { "version": "15.2.1-alpha.0", "resolved": "file:../tkey/packages/storage-layer-torus/tkey-storage-layer-torus-15.2.1-alpha.0.tgz", @@ -3978,7 +3906,7 @@ "node_modules/@tkey/tss": { "version": "15.2.1-alpha.0", "resolved": "file:../tkey/packages/tss/tkey-tss-15.2.1-alpha.0.tgz", - "integrity": "sha512-KncB4Ws/+Sj0dy/3fh+0dv299E9i7Rrkf4VeYeeV0D5ErlqCze8SWXoX9eQXdUgdF0ryxUHNLckjg6ljCcmXMg==", + "integrity": "sha512-qc+aZ3PnAkbUZlcbKAcFJ5rVmnqZuJnFhCp/y031BOJWmCH+Lw4wwlSAqrzH9Z9NEi+D7QnOS8r2eay4dTd56g==", "license": "ISC", "dependencies": { "@tkey/common-types": "^15.2.1-alpha.0", @@ -3994,30 +3922,6 @@ "ethereum-cryptography": "^2.1.3" } }, - "node_modules/@tkey/tss/node_modules/@toruslabs/torus.js": { - "version": "15.1.1", - "resolved": "file:../torus.js/toruslabs-torus.js-15.1.1.tgz", - "integrity": "sha512-kkLuZ9vpmWZTklva+RQFKozDMCR2ydhR5WBn9bbgJBZqVP75FSzLZL0LQYrFqBPMgKgV22uOQ8quXW6T+Kko/g==", - "license": "MIT", - "dependencies": { - "@toruslabs/bs58": "^1.0.0", - "@toruslabs/constants": "^14.0.0", - "@toruslabs/eccrypto": "^5.0.4", - "@toruslabs/http-helpers": "^7.0.0", - "bn.js": "^5.2.1", - "elliptic": "^6.5.7", - "ethereum-cryptography": "^2.2.1", - "json-stable-stringify": "^1.1.1", - "loglevel": "^1.9.2" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, "node_modules/@tootallnate/quickjs-emscripten": { "version": "0.23.0", "resolved": "https://registry.npmjs.org/@tootallnate/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz", @@ -4118,30 +4022,6 @@ } } }, - "node_modules/@toruslabs/customauth/node_modules/@toruslabs/torus.js": { - "version": "15.1.1", - "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.1.1.tgz", - "integrity": "sha512-sLaXA1/R8KTTjU4t+teL3PPaJr2+j01QLYn5IY/t5uTD+1G2nzzfVWpkMDYrk9EfQYw0u4aKJ1lT7j9uKafMlg==", - "license": "MIT", - "dependencies": { - "@toruslabs/bs58": "^1.0.0", - "@toruslabs/constants": "^14.0.0", - "@toruslabs/eccrypto": "^5.0.4", - "@toruslabs/http-helpers": "^7.0.0", - "bn.js": "^5.2.1", - "elliptic": "^6.5.7", - "ethereum-cryptography": "^2.2.1", - "json-stable-stringify": "^1.1.1", - "loglevel": "^1.9.2" - }, - "engines": { - "node": ">=18.x", - "npm": ">=9.x" - }, - "peerDependencies": { - "@babel/runtime": "7.x" - } - }, "node_modules/@toruslabs/eccrypto": { "version": "5.0.4", "resolved": "https://registry.npmjs.org/@toruslabs/eccrypto/-/eccrypto-5.0.4.tgz", @@ -4701,9 +4581,9 @@ } }, "node_modules/@toruslabs/torus.js": { - "version": "15.2.0-alpha.0", - "resolved": "https://registry.npmjs.org/@toruslabs/torus.js/-/torus.js-15.2.0-alpha.0.tgz", - "integrity": "sha512-W0HXmffYTbA9pFC3gdj6ON+FsmzJlSksT6hW5mDkRLZp1qSprYppjHnnnelq5n46yOs+290En8LP5s/U5viluw==", + "version": "15.1.1", + "resolved": "file:../torus.js/toruslabs-torus.js-15.1.1.tgz", + "integrity": "sha512-kkLuZ9vpmWZTklva+RQFKozDMCR2ydhR5WBn9bbgJBZqVP75FSzLZL0LQYrFqBPMgKgV22uOQ8quXW6T+Kko/g==", "license": "MIT", "dependencies": { "@toruslabs/bs58": "^1.0.0", diff --git a/package.json b/package.json index b150dc06..76b760ba 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "@toruslabs/metadata-helpers": "^6.0.0", "@toruslabs/openlogin-utils": "^8.2.1", "@toruslabs/session-manager": "^3.1.0", - "@toruslabs/torus.js": "15.2.0-alpha.0", + "@toruslabs/torus.js": "file:../torus.js/toruslabs-torus.js-15.1.1.tgz", "@toruslabs/tss-client": "^3.3.0-alpha.0", "@toruslabs/tss-frost-client": "^1.0.1-alpha.0", "@toruslabs/tss-frost-common": "^1.0.2-alpha.0", diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index dddf41f3..1e19003a 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -828,8 +828,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { // Suppress client logs if logging is disabled. client.log = (msg: string) => { if (!this.enableLogging) return; - // eslint-disable-next-line no-console - console.log(msg); + log.debug(msg); }; const serverCoeffs: Record = {}; @@ -1193,9 +1192,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { // if both keyType library is avaiable, initialize secp256k1 first as secp256k1 is initialize offline + const importTssBuf = importTssKey ? Buffer.from(importTssKey, "hex") : undefined; // check if key is in the tsslib and keytype exists await this.tKey.initializeTss({ - importKey: importTssKey ? Buffer.from(importTssKey, "hex") : undefined, + importKey: importTssBuf, factorPub, deviceTSSShare, deviceTSSIndex, diff --git a/tests/factors.spec.ts b/tests/factors.spec.ts index aada6af3..2397a8ed 100644 --- a/tests/factors.spec.ts +++ b/tests/factors.spec.ts @@ -8,14 +8,17 @@ import { tssLib as tssLibFROST } from "@toruslabs/tss-frost-lib"; import BN from "bn.js"; import { COREKIT_STATUS, IAsyncStorage, IStorage, MemoryStorage, TssLibType, TssShareType, WEB3AUTH_NETWORK, Web3AuthMPCCoreKit } from "../src"; -import { AsyncMemoryStorage, bufferToElliptic, criticalResetAccount, mockLogin } from "./setup"; +import { AsyncMemoryStorage, criticalResetAccount, mockLogin } from "./setup"; import { getKeyCurve } from "@toruslabs/torus.js"; +import { randomId } from "@toruslabs/customauth"; +import log from "loglevel"; type FactorTestVariable = { manualSync?: boolean; storage?: IAsyncStorage | IStorage; email: string; tssLib?: TssLibType; + resetAccount? : false }; function getPubKeys(kit: Web3AuthMPCCoreKit, indices: number[]): EllipticPoint[] { @@ -53,7 +56,12 @@ export const FactorManipulationTest = async (testVariable: FactorTestVariable) = }; async function beforeTest() { + if (testVariable.resetAccount === false) { + log.debug("skipping reset account"); + return ; + } const resetInstance = await newInstance(); + await criticalResetAccount(resetInstance); await resetInstance.logout(); } @@ -182,6 +190,7 @@ const variable: FactorTestVariable[] = [ { manualSync: false, storage: new AsyncMemoryStorage(), email: "testmail1015" }, { manualSync: true, storage: new MemoryStorage(), email: "testmail1012ed25519", tssLib: tssLibFROST }, + { manualSync: true, storage: new MemoryStorage(), email: randomId() , tssLib: tssLibFROST , resetAccount: false}, ]; variable.forEach(async (testVariable) => { From b80ad8aff110321b98bdfae09af8d2619aa03c43 Mon Sep 17 00:00:00 2001 From: ieow Date: Tue, 11 Feb 2025 19:48:28 +0800 Subject: [PATCH 3/3] fix: enable multicurve fix tests add multicurve tests --- src/interfaces.ts | 12 +- src/mpcCoreKit.ts | 197 +++++++++++++++++++++---------- tests/backwardCompatible.spec.ts | 2 +- tests/bip340.spec.ts | 2 +- tests/ed25519.spec.ts | 2 +- tests/factors.spec.ts | 3 +- tests/gating.spec.ts | 2 +- tests/login.spec.ts | 4 +- tests/multiCurveTest.ts | 129 ++++++++++++++++++++ tests/securityQuestion.spec.ts | 2 +- tests/sessionTime.spec.ts | 2 +- tests/setup.ts | 14 ++- 12 files changed, 292 insertions(+), 79 deletions(-) create mode 100644 tests/multiCurveTest.ts diff --git a/src/interfaces.ts b/src/interfaces.ts index 3b19cb64..a3aee429 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -95,7 +95,9 @@ export type OAuthLoginParams = (SubVerifierDetailsParams | AggregateVerifierLogi /** * Key to import key into Tss during first time login. */ - importTssKey?: string; + importTssKey?: { + [K in KeyType]: string; + }; /** * For new users, use SFA key if user was registered with SFA before. @@ -161,7 +163,9 @@ export interface JWTLoginParams { /** * Key to import key into Tss during first time login. */ - importTssKey?: string; + importTssKey?: { + [K in KeyType]?: string; + }; /** * For new users, use SFA key if user was registered with SFA before. @@ -201,7 +205,7 @@ export interface Web3AuthOptions { /** * The threshold signing library to use. */ - tssLib: TssLibType; + tssLibs: TssLibType[]; /** * @defaultValue `false` @@ -354,7 +358,7 @@ export interface IMPCContext { updateState: (newState: Partial) => void; getUserInfo: () => UserInfo; setupTkey: (params?: { - providedImportKey?: string; + providedImportKey?: { [key in KeyType]?: string }; sfaLoginResponse?: TorusKey | TorusLoginResponse | TorusAggregateLoginResponse; userInfo?: UserInfo; importingSFAKey?: boolean; diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 1e19003a..7be699b7 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -3,7 +3,7 @@ import { CoreError } from "@tkey/core"; import { ShareSerializationModule } from "@tkey/share-serialization"; import { TorusStorageLayer } from "@tkey/storage-layer-torus"; import { factorKeyCurve, getPubKeyPoint, lagrangeInterpolation, TKeyTSS, TSS_TAG_DEFAULT, TSSTorusServiceProvider } from "@tkey/tss"; -import { SIGNER_MAP } from "@toruslabs/constants"; +import { SIG_TYPE, SIGNER_MAP, WEB3AUTH_SIG_TYPE } from "@toruslabs/constants"; import { AGGREGATE_VERIFIER, TORUS_METHOD, TorusAggregateLoginResponse, TorusLoginResponse, UX_MODE } from "@toruslabs/customauth"; import type { UX_MODE_TYPE } from "@toruslabs/customauth/dist/types/utils/enums"; import { Ed25519Curve, Secp256k1Curve } from "@toruslabs/elliptic-wrapper"; @@ -12,7 +12,7 @@ import { keccak256 } from "@toruslabs/metadata-helpers"; import { SessionManager } from "@toruslabs/session-manager"; import { getKeyCurve, Torus as TorusUtils, TorusKey } from "@toruslabs/torus.js"; import { Client, getDKLSCoeff, setupSockets } from "@toruslabs/tss-client"; -import type { WasmLib as DKLSWasmLib } from "@toruslabs/tss-dkls-lib"; +import { type WasmLib as DKLSWasmLib } from "@toruslabs/tss-dkls-lib"; import { sign as signFrost } from "@toruslabs/tss-frost-client"; import type { WasmLib as FrostWasmLibEd25519 } from "@toruslabs/tss-frost-lib"; import type { WasmLib as FrostWasmLibBip340 } from "@toruslabs/tss-frost-lib-bip340"; @@ -98,9 +98,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { private ready = false; - private _tssLib: TssLibType; + private _tssLibs: TssLibType[]; - private wasmLib: DKLSWasmLib | FrostWasmLibEd25519 | FrostWasmLibBip340; + private wasmLib: { + [SIG_TYPE.ECDSA_SECP256K1]?: DKLSWasmLib; + [SIG_TYPE.ED25519]?: FrostWasmLibEd25519; + [SIG_TYPE.BIP340]?: FrostWasmLibBip340; + } = {}; private _keyType: KeyType; @@ -110,14 +114,28 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { private sessionSigGenerator: ISessionSigGenerator; + private supportedCurveKeyTypes: Set = new Set(); + + private supportedSigTypes: Set = new Set(); + constructor(options: Web3AuthOptions) { if (!options.web3AuthClientId) { throw CoreKitError.clientIdInvalid(); } - this._tssLib = options.tssLib; - this._keyType = options.tssLib.keyType as KeyType; - this._sigType = options.tssLib.sigType as SigType; + options.tssLibs.forEach((tssLibItem) => { + this.supportedCurveKeyTypes.add(tssLibItem.keyType as KeyType); + this.supportedSigTypes.add(tssLibItem.sigType as SigType); + }); + this._keyType = options.tssLibs[0].keyType as KeyType; + this._sigType = options.tssLibs[0].sigType as SigType; + this._tssLibs = options.tssLibs; + + if (!options.legacyFlag) { + options.legacyFlag = false; + } else if (this.supportedCurveKeyTypes.size > 1) { + throw CoreKitError.invalidConfig("Legacy flag is not supported for multiple curves"); + } const isNodejsOrRN = this.isNodejsOrRN(options.uxMode); @@ -136,7 +154,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { if (!options.disableHashedFactorKey) options.disableHashedFactorKey = false; if (!options.hashedFactorNonce) options.hashedFactorNonce = options.web3AuthClientId; if (options.disableSessionManager === undefined) options.disableSessionManager = false; - if (!options.legacyFlag) options.legacyFlag = false; this.sessionSigGenerator = new DefaultSessionSigGeneratorPlugin(this); this.options = options as Web3AuthOptionsWithDefaults; @@ -225,9 +242,16 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { public setTkeyType(tkeyType: KeyType) { // check tkeyType is supported by tssLib + if (!this.supportedCurveKeyTypes.has(tkeyType)) throw CoreError.default("KeyType not supported, please provide valid tssLib"); this._keyType = tkeyType; } + public setSigType(sigType: WEB3AUTH_SIG_TYPE) { + // check tkeyType is supported by tssLib + if (!this.supportedSigTypes.has(sigType)) throw CoreError.default("SigType not supported, please provide valid tssLib"); + this._sigType = sigType; + } + public getTssShare(factorkey: BN, accountIndex?: number) { return this.tkey.getTSSShare(factorkey, { tssTag: TSS_TAG_DEFAULT, @@ -236,8 +260,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { }); } - public getTssData(args: { skipThrow: boolean } = { skipThrow: false }) { - const result = this.tkey.metadata.getTssData(this.keyType, TSS_TAG_DEFAULT); + public getTssData(args: { skipThrow: boolean; keyType?: KeyType } = { skipThrow: false }) { + const result = this.tkey.metadata.getTssData(args.keyType ?? this.keyType, TSS_TAG_DEFAULT); if (!result && !args.skipThrow) { throw CoreKitError.noMetadataFound(); } @@ -411,7 +435,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } const existingSFAKey = loginResponse.finalKeyData.privKey.padStart(64, "0"); await this.setupTkey({ - providedImportKey: existingSFAKey, + providedImportKey: { secp256k1: existingSFAKey }, sfaLoginResponse: loginResponse, userInfo, importingSFAKey: true, @@ -448,7 +472,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { this.torusSp.verifierName = verifier; this.torusSp.verifierId = verifierId; - if (registerExistingSFAKey && importTssKey) { + if (registerExistingSFAKey && importTssKey && (importTssKey.secp256k1 || this.options.legacyFlag)) { throw CoreKitError.invalidConfig("Cannot import TSS key and register SFA key at the same time."); } @@ -483,7 +507,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } const existingSFAKey = loginResponse.finalKeyData.privKey.padStart(64, "0"); await this.setupTkey({ - providedImportKey: existingSFAKey, + providedImportKey: { [this._keyType]: existingSFAKey }, importingSFAKey: true, sfaLoginResponse: loginResponse, userInfo: { ...parseToken(idToken), verifier, verifierId }, @@ -555,6 +579,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { public async inputFactorKey(factorKey: BN): Promise { this.checkReady(); + + if (this.options.legacyFlag) { + // Check for existing curve in tssData for legacy mode + const tssData = this.getTssData({ skipThrow: true }); + if (!tssData) throw CoreKitError.default("Legacy mode only support single curve, please congfiure with correct keyType"); + } + try { // input tkey device share when required share > 0 ( or not reconstructed ) // assumption tkey shares will not changed @@ -758,7 +789,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { signatures: string[]; }> { const { sessionSignatures } = params || {}; - this.wasmLib = await this.loadTssWasm(); + + await this.loadTssWasm(SIG_TYPE.ECDSA_SECP256K1); + const dklsWasm = this.wasmLib[SIG_TYPE.ECDSA_SECP256K1]; + // PreSetup const { tssShareIndex } = this.state; const tssPubKey = this.getPubKeyPoint(); @@ -813,17 +847,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { // Client lib expects pub key in XY-format, base64-encoded. const tssPubKeyBase64 = Buffer.from(tssPubKey.toSEC1(secp256k1).subarray(1)).toString("base64"); - const client = new Client( - currentSession, - clientIndex, - partyIndexes, - endpoints, - sockets, - share, - tssPubKeyBase64, - true, - this.wasmLib as DKLSWasmLib - ); + const client = new Client(currentSession, clientIndex, partyIndexes, endpoints, sockets, share, tssPubKeyBase64, true, dklsWasm); // Suppress client logs if logging is disabled. client.log = (msg: string) => { @@ -856,7 +880,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { keyTweak?: BN; } ): Promise { - this.wasmLib = await this.loadTssWasm(); + // this.wasmLib = await this.loadTssWasm(); if (this._sigType === "ecdsa-secp256k1") { if (opts?.keyTweak) { throw CoreKitError.default("key tweaking not supported for ecdsa-secp256k1"); @@ -1072,7 +1096,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } public async setupTkey(params?: { - providedImportKey?: string; + providedImportKey?: { [k in KeyType]?: string }; sfaLoginResponse?: TorusKey | TorusLoginResponse | TorusAggregateLoginResponse; userInfo?: UserInfo; importingSFAKey?: boolean; @@ -1102,15 +1126,15 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } const existingUser = await this.isMetadataPresent(this.state.postBoxKey); - let importKey = providedImportKey; + const importKey = providedImportKey ?? {}; if (!existingUser) { if (!importKey && this.useClientGeneratedTSSKey) { if (this.keyType === KeyType.ed25519) { const k = generateEd25519Seed(); - importKey = k.toString("hex"); + importKey.ed25519 = k.toString("hex"); } else if (this.keyType === KeyType.secp256k1) { const k = secp256k1.genKeyPair().getPrivate(); - importKey = scalarBNToBufferSEC1(k).toString("hex"); + importKey.secp256k1 = scalarBNToBufferSEC1(k).toString("hex"); } else { throw CoreKitError.default(`Unsupported key type and sig type combination: ${this.keyType}, ${this._sigType}`); } @@ -1120,7 +1144,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } await this.handleNewUser(importKey, importingSFAKey); } else { - if (importKey) { + if (importKey && Object.keys(importKey).length > 0) { throw CoreKitError.tssKeyImportNotAllowed(); } await this.handleExistingUser(); @@ -1171,7 +1195,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { } // mutation function - private async handleNewUser(importTssKey?: string, isSfaKey?: boolean) { + private async handleNewUser(importTssKey?: { [k in KeyType]?: string }, isSfaKey?: boolean) { await this.atomicSync(async () => { // Generate or use hash factor and initialize tkey with it. let factorKey: BN; @@ -1190,33 +1214,37 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { const deviceTSSShare = ec.genKeyPair().getPrivate(); await this.tKey.initialize(); - // if both keyType library is avaiable, initialize secp256k1 first as secp256k1 is initialize offline + // only consider 2 types of curve, will change the logic in the future if there are more key types curves - const importTssBuf = importTssKey ? Buffer.from(importTssKey, "hex") : undefined; - // check if key is in the tsslib and keytype exists - await this.tKey.initializeTss({ - importKey: importTssBuf, - factorPub, - deviceTSSShare, - deviceTSSIndex, - tssKeyType: this._keyType, - serverOpts: { - // selectedServers: [], - authSignatures: this.state.signatures, - }, - }); + // if both keyType library is avaiable, initialize secp256k1 first as secp256k1 is initialize offline - //TODO resolve this - // if both key type available - // if () { - // await this.tKey.initializeTss({ - // importKey: importTssKey? Buffer.from(importTssKey) : undefined, tssKeyType: this._keyType, - // serverOpts: { - // // selectedServers: [], - // authSignatures: this.state.signatures - // } - // }); - // } + if (this.supportedCurveKeyTypes.has(KeyType.secp256k1)) { + const importTssBuf = importTssKey.secp256k1 ? Buffer.from(importTssKey.secp256k1, "hex") : undefined; + // check if key is in the tsslib and keytype exists + await this.tKey.initializeTss({ + importKey: importTssBuf, + factorPub, + deviceTSSShare, + deviceTSSIndex, + tssKeyType: KeyType.secp256k1, + serverOpts: { + // selectedServers: [], + authSignatures: this.state.signatures, + }, + }); + } + if (this.supportedCurveKeyTypes.has(KeyType.ed25519)) { + const importTssBuf = importTssKey.ed25519 ? Buffer.from(importTssKey.ed25519, "hex") : undefined; + // check if key is in the tsslib and keytype exists + await this.tKey.initializeTss({ + importKey: importTssBuf, + tssKeyType: KeyType.ed25519, + serverOpts: { + // selectedServers: [], + authSignatures: this.state.signatures, + }, + }); + } // Finalize initialization. await this.tKey.reconstructKey(); @@ -1248,6 +1276,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { private async handleExistingUser() { await this.tKey.initialize({ neverInitializeNewKey: true }); + + if (this.options.legacyFlag) { + // Check for existing curve in tssData for legacy mode + const tssData = this.getTssData({ skipThrow: true }); + if (!tssData) throw CoreKitError.default("Legacy mode only support single curve, please congfiure with correct keyType"); + } + if (this.options.disableHashedFactorKey) { return; } @@ -1278,6 +1313,32 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { log.warn("AccountIndex should be 0"); this.state.accountIndex = 0; } + + let newCurveKeyType = false; + for (const keyType of this.supportedCurveKeyTypes) { + const tssData = this.getTssData({ skipThrow: true, keyType }); + if (!tssData) { + newCurveKeyType = true; + } + } + + if (newCurveKeyType) { + this.atomicSync(async () => { + // check for missing curve and initialize it + for (const keyType of this.supportedCurveKeyTypes) { + const tssData = this.getTssData({ skipThrow: true, keyType }); + if (!tssData) { + await this.tKey.initializeTss({ + tssKeyType: keyType, + serverOpts: { + authSignatures: this.state.signatures, + }, + }); + } + } + }); + } + // Read tss meta data. const { tssIndex: tssShareIndex } = await this.getTssShare(factorKey); const tssCurve = getKeyCurve(this._keyType); @@ -1630,8 +1691,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { const pubKeyHex = ec.pointToBuffer(tssPubKeyPoint, Buffer).toString("hex"); const serverCoefficientsHex = serverCoefficients.map((c) => ec.scalarToBuffer(c, Buffer).toString("hex")); const authSignatures = await this.getSessionSignatures(); + + await this.loadTssWasm(this._sigType); + + const frostlib = this.sigType === SIG_TYPE.BIP340 ? this.wasmLib[SIG_TYPE.BIP340] : this.wasmLib[SIG_TYPE.ED25519]; const signature = await signFrost( - this.wasmLib as FrostWasmLibEd25519 | FrostWasmLibBip340, + frostlib, session, authSignatures, serverXCoords, @@ -1648,8 +1713,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext { return Buffer.from(signature, "hex"); } - private async loadTssWasm() { - if (this.wasmLib) return this.wasmLib; - return this._tssLib.load(); + private async loadTssWasm(sigType: WEB3AUTH_SIG_TYPE) { + if (this.wasmLib[sigType]) { + return this.wasmLib[sigType]; + } + + if (this.supportedSigTypes.has(sigType)) { + const matchLibs = this._tssLibs.find((x) => x.sigType === sigType); + if (sigType === SIG_TYPE.ECDSA_SECP256K1) { + this.wasmLib[sigType] = (await matchLibs?.load()) as DKLSWasmLib; + } + if (sigType === SIG_TYPE.ED25519 || sigType === SIG_TYPE.BIP340) this.wasmLib[sigType] = (await matchLibs?.load()) as FrostWasmLibEd25519; + } + return this.wasmLib; } } diff --git a/tests/backwardCompatible.spec.ts b/tests/backwardCompatible.spec.ts index 94c0249a..65084bcf 100644 --- a/tests/backwardCompatible.spec.ts +++ b/tests/backwardCompatible.spec.ts @@ -46,7 +46,7 @@ variable.forEach((testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs: [tssLib], storage: storageInstance, manualSync, legacyFlag: true, diff --git a/tests/bip340.spec.ts b/tests/bip340.spec.ts index 39ec07d4..db8bb961 100644 --- a/tests/bip340.spec.ts +++ b/tests/bip340.spec.ts @@ -45,7 +45,7 @@ variable.forEach((testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs : [tssLib], storage: storageInstance, manualSync, }); diff --git a/tests/ed25519.spec.ts b/tests/ed25519.spec.ts index 40efc6ad..11e9fbfc 100644 --- a/tests/ed25519.spec.ts +++ b/tests/ed25519.spec.ts @@ -44,7 +44,7 @@ variable.forEach((testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs: [tssLib], storage: storageInstance, manualSync, legacyFlag: true, diff --git a/tests/factors.spec.ts b/tests/factors.spec.ts index 2397a8ed..6973fa4b 100644 --- a/tests/factors.spec.ts +++ b/tests/factors.spec.ts @@ -34,13 +34,14 @@ function getPubKeys(kit: Web3AuthMPCCoreKit, indices: number[]): EllipticPoint[] export const FactorManipulationTest = async (testVariable: FactorTestVariable) => { const { email, tssLib } = testVariable; + const tsslibs = tssLib ? [tssLib] : [tssLibDKLS]; const newInstance = async () => { const instance = new Web3AuthMPCCoreKit({ web3AuthClientId: "torus-key-test", web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, baseUrl: "http://localhost:3000", uxMode: "nodejs", - tssLib: tssLib || tssLibDKLS, + tssLibs: tsslibs, storage: testVariable.storage, manualSync: testVariable.manualSync, }); diff --git a/tests/gating.spec.ts b/tests/gating.spec.ts index 1cb68682..4b786eca 100644 --- a/tests/gating.spec.ts +++ b/tests/gating.spec.ts @@ -52,7 +52,7 @@ variable.forEach((testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs: [tssLib], storage: new MemoryStorage(), manualSync, }); diff --git a/tests/login.spec.ts b/tests/login.spec.ts index fd0a4375..57a311d8 100644 --- a/tests/login.spec.ts +++ b/tests/login.spec.ts @@ -46,7 +46,7 @@ variable.forEach((testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs: [tssLib], storage: storageInstance, manualSync, }); @@ -63,7 +63,7 @@ variable.forEach((testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs: [tssLib], storage: storageInstance, manualSync, }); diff --git a/tests/multiCurveTest.ts b/tests/multiCurveTest.ts new file mode 100644 index 00000000..e4433bcb --- /dev/null +++ b/tests/multiCurveTest.ts @@ -0,0 +1,129 @@ +import { COREKIT_STATUS } from './../src/interfaces'; +import dklslib from "@toruslabs/tss-dkls-lib"; +import frostLib from "@toruslabs/tss-frost-lib"; +import frostBip340lib from "@toruslabs/tss-frost-lib-bip340"; + +import { expect } from "chai"; +import { describe, it } from "node:test"; +import { MemoryStorage, sigToRSV, WEB3AUTH_NETWORK, Web3AuthMPCCoreKit } from "src"; +import { mockLogin2 } from "./setup"; +import { KeyType } from '@tkey/common-types'; +import { BN } from 'bn.js'; +import { MockStorageLayer } from '@tkey/storage-layer-torus'; +import { secp256k1 } from '@noble/curves/secp256k1'; +import { ed25519 } from '@noble/curves/ed25519'; +import { bytesToHex, utf8ToBytes, bytesToNumberBE } from '@noble/curves/abstract/utils'; +import { keccak_256 } from '@noble/hashes/sha3'; + +import { schnorr as bip340 } from '@noble/curves/secp256k1'; + + +const web3AuthNetwork = WEB3AUTH_NETWORK.DEVNET; +const manualSync = false; +const storageInstance = new MemoryStorage(); +const verifierId = "multicurvetest" + +const mockSL = new MockStorageLayer({ + dataMap: {}, + lockMap: {}, +}); + +describe("multiCurveTest", () => { + // + const newCoreKitInstance = () => + new Web3AuthMPCCoreKit({ + web3AuthClientId: "torus-key-test", + web3AuthNetwork, + baseUrl: "http://localhost:3000", + uxMode: "nodejs", + tssLibs : [dklslib, frostLib, frostBip340lib], + storage: storageInstance, + manualSync, + }); + + + const testAllSigning = async (instance: Web3AuthMPCCoreKit) => { + const message = "message to sign"; + + + instance.setTkeyType(KeyType.secp256k1) + instance.setSigType("ecdsa-secp256k1") + + const hash = keccak_256(message); + const result = await instance.sign(Buffer.from(hash), {hashed: true}) + + const {r, s } = sigToRSV(result); + + // new secp256k1.Signature() + const validsecp256k1 = secp256k1.verify({ + r: bytesToNumberBE(r), + s: bytesToNumberBE(s), + }, bytesToHex(hash), bytesToHex(instance.getPubKey(false))) + expect(validsecp256k1).eq(true); + + instance.setSigType("bip340"); + const result2 = await instance.sign( Buffer.from(utf8ToBytes(message)), {hashed: false}) + + const validb340 = bip340.verify(bytesToHex(result2), bytesToHex(utf8ToBytes(message)), bytesToHex(instance.getPubKeyBip340())); + expect(validb340).eq(true); + + + instance.setSigType("ed25519") + instance.setTkeyType(KeyType.ed25519) + const result3 = await instance.sign(Buffer.from(message), { hashed: false }) + const valided25519 = ed25519.verify(bytesToHex(result3), bytesToHex(Buffer.from(message)), bytesToHex( new Uint8Array(instance.getPubKeyEd25519()) ) ) + expect(valided25519).eq(true); + + } + + it("should able to initialize with multiple curve/ tsslib", async () => { + const instance = newCoreKitInstance(); + await instance.init({ handleRedirectResult: false, rehydrate: false }); + + // mock storage layer + instance.tKey.storageLayer = mockSL; + + + const { idToken, parsedToken } = await mockLogin2(verifierId); + + await instance.loginWithJWT({ + verifier: "torus-test-health", + verifierId: parsedToken.email, + idToken, + }); + + expect(instance.status).eq("LOGGED_IN"); + + + await testAllSigning(instance); + + + const recoverFactor = await instance.enableMFA({}) + console.log(recoverFactor); + + await testAllSigning(instance); + + + const instance2 = newCoreKitInstance(); + await instance2.init({ handleRedirectResult: false, rehydrate: false }); + + // mock storage layer + instance2.tKey.storageLayer = mockSL; + + const { idToken: idToken2, parsedToken: parsedToken2 } = await mockLogin2(verifierId); + await instance2.loginWithJWT({ + verifier: "torus-test-health", + verifierId: parsedToken2.email, + idToken: idToken2, + }); + + expect(instance2.status).eq(COREKIT_STATUS.REQUIRED_SHARE); + + await instance2.inputFactorKey(new BN(recoverFactor, "hex")); + expect(instance2.status).eq(COREKIT_STATUS.LOGGED_IN); + + await testAllSigning(instance2); + + }) + +}); \ No newline at end of file diff --git a/tests/securityQuestion.spec.ts b/tests/securityQuestion.spec.ts index df74cc2c..ae24366a 100644 --- a/tests/securityQuestion.spec.ts +++ b/tests/securityQuestion.spec.ts @@ -132,7 +132,7 @@ variable.forEach(async (testVariable) => { web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, baseUrl: "http://localhost:3000", uxMode: "nodejs", - tssLib, + tssLibs: [tssLib], storage: storageInstance, manualSync: testVariable.manualSync, }); diff --git a/tests/sessionTime.spec.ts b/tests/sessionTime.spec.ts index 7175e4a4..0fae1a58 100644 --- a/tests/sessionTime.spec.ts +++ b/tests/sessionTime.spec.ts @@ -70,7 +70,7 @@ variable.forEach(async (testVariable) => { web3AuthNetwork, baseUrl: "http://localhost:3000", uxMode, - tssLib, + tssLibs : [tssLib], storage: new MemoryStorage(), manualSync, sessionTime, diff --git a/tests/setup.ts b/tests/setup.ts index 7a66c35a..9a05553c 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -99,13 +99,14 @@ export const newCoreKitLogInInstance = async ({ importTssKey?: string; registerExistingSFAKey?: boolean; login?: LoginFunc; -}) => { + }) => { + const tssLibs = [tssLib || tssLibDKLS]; const instance = new Web3AuthMPCCoreKit({ web3AuthClientId: "torus-key-test", web3AuthNetwork: network, baseUrl: "http://localhost:3000", uxMode: "nodejs", - tssLib: tssLib || tssLibDKLS, + tssLibs: tssLibs, storage: storageInstance, manualSync, }); @@ -116,7 +117,7 @@ export const newCoreKitLogInInstance = async ({ verifier: "torus-test-health", verifierId: parsedToken.email, idToken, - importTssKey, + importTssKey: importTssKey ? { [tssLibs[0].keyType] : importTssKey} : undefined, registerExistingSFAKey }); @@ -136,13 +137,16 @@ export const loginWithSFA = async ({ storageInstance: IStorage | IAsyncStorage; tssLib?: TssLibType; login?: LoginFunc; -}): Promise => { + }): Promise => { + + const tssLibs = [tssLib || tssLibDKLS] + const instance = new Web3AuthMPCCoreKit({ web3AuthClientId: "torus-key-test", web3AuthNetwork: network, baseUrl: "http://localhost:3000", uxMode: "nodejs", - tssLib: tssLib || tssLibDKLS, + tssLibs: tssLibs, storage: storageInstance, manualSync, });