From 19f276dc2d19208793806f1a6e8a2dee77ec7e85 Mon Sep 17 00:00:00 2001 From: guru-web3 <105355858+guru-web3@users.noreply.github.com> Date: Tue, 20 Feb 2024 17:31:57 +0530 Subject: [PATCH] fix: review comments --- packages/core/src/core.ts | 13 ++++++++----- packages/core/src/errors.ts | 4 ++-- packages/default/test/shared.js | 22 +++++++++++----------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 7ffe1865..d00ebe0f 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -479,17 +479,16 @@ class ThresholdKey implements ITKey { return tssPolyCommits; } - async getTSSPub(accountIndex?: number): Promise { + getTSSPub(accountIndex?: number): Point { const tssCommits = this.getTSSCommits(); if (accountIndex && accountIndex > 0) { - const nonce = await this.computeAccountNonce(accountIndex); + const nonce = this.computeAccountNonce(accountIndex); // we need to add the pub key nonce to the tssPub const noncePub = ecCurve.keyFromPrivate(nonce.toString("hex")).getPublic(); const pubKeyPoint = ecCurve.keyFromPublic({ x: tssCommits[0].x.toString("hex"), y: tssCommits[0].y.toString("hex") }).getPublic(); const dervicepubKeyPoint = pubKeyPoint.add(noncePub); return new Point(dervicepubKeyPoint.getX().toString("hex"), dervicepubKeyPoint.getY().toString("hex")); } - return tssCommits[0]; } /** @@ -620,6 +619,11 @@ class ThresholdKey implements ITKey { }) ); } + + // assign account salt from tKey store if it exists + const accountSalt = await this.getTKeyStoreItem(TSS_MODULE, "accountSalt"); + if (accountSalt && accountSalt?.value) this._accountSalt = accountSalt.value; + return { privKey, ...returnObject }; } @@ -2001,9 +2005,8 @@ class ThresholdKey implements ITKey { return Promise.all(Object.keys(this.modules).map((x) => this.modules[x].initialize())); } - private async computeAccountNonce(index: number) { + private computeAccountNonce(index: number) { // generation should occur during tkey.init, fails if accountSalt is absent - this._accountSalt = this._accountSalt || (await this.getTKeyStoreItem(TSS_MODULE, "accountSalt")).value; if (!this._accountSalt) { throw CoreError.accountSaltUndefined(); } diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index fdbf5c00..03dab9f1 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -32,8 +32,8 @@ class CoreError extends TkeyError { 1103: "setMetadata errored", 1104: "previouslyFetchedCloudMetadata provided in initialization is outdated", 1105: "previouslyFetchedCloudMetadata.nonce should never be higher than the latestShareDetails, please contact support", - 1106: "Account Salt is absent, required for nonce generation", - // tkeystore + 1106: "Account Salt is absent, required for nonce generation.Make sure key is reconstructed", + // tKeystore 1201: "Invalid tkeyStore", 1202: "Encryption failed", 1203: "Decryption failed", diff --git a/packages/default/test/shared.js b/packages/default/test/shared.js index e64be869..ec2d28c1 100644 --- a/packages/default/test/shared.js +++ b/packages/default/test/shared.js @@ -96,7 +96,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { // accountSalt is absent, required for nonce generation // can be only initialize with tkey.initialize(); rejects(async () => { - await tb1.computeAccountNonce(1); + tb1.computeAccountNonce(1); }); // factor key needs to passed from outside of tKey const factorKey = new BN(generatePrivate()); @@ -111,12 +111,12 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { } const { tssShare: retrievedTSS1, tssIndex: retrievedTSSIndex1 } = await tb1.getTSSShare(factorKey, { accountIndex: 1 }); const tssPrivKey1 = getLagrangeCoeffs([1, retrievedTSSIndex1], 1) - .mul(serverDKGPrivKeys[0].add(await tb1.computeAccountNonce(1))) + .mul(serverDKGPrivKeys[0].add(tb1.computeAccountNonce(1))) .add(getLagrangeCoeffs([1, retrievedTSSIndex1], retrievedTSSIndex1).mul(retrievedTSS1)) .umod(ecCurve.n); const tssPubKey1 = ecCurve.keyFromPrivate(tssPrivKey1).getPublic(); - const pubKey1 = await tb1.getTSSPub(1); + const pubKey1 = tb1.getTSSPub(1); strictEqual(tssPubKey1.x.toString(16, 64), pubKey1.x.toString(16, 64)); strictEqual(tssPubKey1.y.toString(16, 64), pubKey1.y.toString(16, 64)); @@ -141,12 +141,12 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { const { tssShare: retrievedTSS2, tssIndex: retrievedTSSIndex2 } = await tb2.getTSSShare(factorKey, { accountIndex: 2 }); const tssPrivKey2 = getLagrangeCoeffs([1, retrievedTSSIndex2], 1) - .mul(serverDKGPrivKeys[0].add(await tb1.computeAccountNonce(2))) + .mul(serverDKGPrivKeys[0].add(tb1.computeAccountNonce(2))) .add(getLagrangeCoeffs([1, retrievedTSSIndex2], retrievedTSSIndex2).mul(retrievedTSS2)) .umod(ecCurve.n); const tssPubKey2 = getPubKeyPoint(tssPrivKey2); - const pubKey2 = await tb1.getTSSPub(2); + const pubKey2 = tb1.getTSSPub(2); strictEqual(tssPubKey2.x.toString(16, 64), pubKey2.x.toString(16, 64)); strictEqual(tssPubKey2.y.toString(16, 64), pubKey2.y.toString(16, 64)); @@ -174,7 +174,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { const tssSharePub = ecCurve.keyFromPrivate(retrievedTSS.toString("hex")).getPublic(); const { tssShare: retrievedTSS2 } = await tb2.getTSSShare(factorKey, { accountIndex: 1 }); const tssSharePub2 = ecCurve.keyFromPrivate(retrievedTSS2.toString("hex")).getPublic(); - const nonce = await tb1.computeAccountNonce(1); + const nonce = tb1.computeAccountNonce(1); const noncePub = ecCurve.keyFromPrivate(nonce.toString("hex")).getPublic(); const tssShareDerived = tssSharePub.add(noncePub); strictEqual(tssShareDerived.getX().toString("hex"), tssSharePub2.getX().toString("hex")); @@ -182,7 +182,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { const { tssShare: retrievedTSS3 } = await tb2.getTSSShare(factorKey, { accountIndex: 2 }); const tssSharePub3 = ecCurve.keyFromPrivate(retrievedTSS3.toString("hex")).getPublic(); - const nonce2 = await tb1.computeAccountNonce(2); + const nonce2 = tb1.computeAccountNonce(2); const noncePub2 = ecCurve.keyFromPrivate(nonce2.toString("hex")).getPublic(); const tssShareDerived2 = tssSharePub.add(noncePub2); strictEqual(tssShareDerived2.getX().toString("hex"), tssSharePub3.getX().toString("hex")); @@ -192,7 +192,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { { const { tssShare: newTSS, tssIndex } = await tb1.getTSSShare(factorKey, { accountIndex: 1 }); const newTSSPrivKey = getLagrangeCoeffs([1, 2], 1) - .mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb1.computeAccountNonce(1))) + .mul(new BN(serverDKGPrivKeys[1], "hex").add(tb1.computeAccountNonce(1))) .add(getLagrangeCoeffs([1, 2], 2).mul(newTSS)) .umod(ecCurve.n); strictEqual(tssPrivKey1.toString(16, 64), newTSSPrivKey.toString(16, 64)); @@ -203,7 +203,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { { const { tssShare: newTSS2, tssIndex } = await tb2.getTSSShare(factorKey2, { accountIndex: 1 }); const newTSSPrivKey = getLagrangeCoeffs([1, 3], 1) - .mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb1.computeAccountNonce(1))) + .mul(new BN(serverDKGPrivKeys[1], "hex").add(tb1.computeAccountNonce(1))) .add(getLagrangeCoeffs([1, 3], 3).mul(newTSS2)) .umod(ecCurve.n); strictEqual(tssPrivKey1.toString(16, 64), newTSSPrivKey.toString(16, 64)); @@ -214,7 +214,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { { const { tssShare: newTSS, tssIndex } = await tb2.getTSSShare(factorKey, { accountIndex: 2 }); const newTSSPrivKey = getLagrangeCoeffs([1, 2], 1) - .mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb2.computeAccountNonce(2))) + .mul(new BN(serverDKGPrivKeys[1], "hex").add(tb2.computeAccountNonce(2))) .add(getLagrangeCoeffs([1, 2], 2).mul(newTSS)) .umod(ecCurve.n); strictEqual(tssPrivKey2.toString(16, 64), newTSSPrivKey.toString(16, 64)); @@ -225,7 +225,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => { { const { tssShare: newTSS2, tssIndex } = await tb2.getTSSShare(factorKey2, { accountIndex: 2 }); const newTSSPrivKey = getLagrangeCoeffs([1, 3], 1) - .mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb1.computeAccountNonce(2))) + .mul(new BN(serverDKGPrivKeys[1], "hex").add(tb1.computeAccountNonce(2))) .add(getLagrangeCoeffs([1, 3], 3).mul(newTSS2)) .umod(ecCurve.n); strictEqual(tssPrivKey2.toString(16, 64), newTSSPrivKey.toString(16, 64));