From 34885fafef7b05bcb1a96d1f604ae80fd5d066b2 Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 26 Feb 2024 14:31:16 +0800 Subject: [PATCH] fix: make threshold in option optional --- packages/core/src/core.ts | 2 +- .../default/test/RefreshAndAccountIndex.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index c6829684..2b5ea3e5 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -394,7 +394,7 @@ class ThresholdKey implements ITKey { * getTSSShare accepts a factorKey and returns the TSS share based on the factor encrypted TSS shares in the metadata * @param factorKey - factor key */ - async getTSSShare(factorKey: BN, opts?: { threshold: number; accountIndex?: number }): Promise<{ tssIndex: number; tssShare: BN }> { + async getTSSShare(factorKey: BN, opts?: { threshold?: number; accountIndex?: number }): Promise<{ tssIndex: number; tssShare: BN }> { if (!this.privKey) throw CoreError.default("tss share cannot be returned until you've reconstructed tkey"); const factorPub = getPubKeyPoint(factorKey); const factorEncs = this.getFactorEncs(factorPub); diff --git a/packages/default/test/RefreshAndAccountIndex.js b/packages/default/test/RefreshAndAccountIndex.js index f9b652f0..7854f873 100644 --- a/packages/default/test/RefreshAndAccountIndex.js +++ b/packages/default/test/RefreshAndAccountIndex.js @@ -1,7 +1,7 @@ import { ecCurve, getPubKeyPoint } from "@tkey-mpc/common-types"; // eslint-disable-next-line import/no-extraneous-dependencies import { generatePrivate } from "@toruslabs/eccrypto"; -import { fail, notEqual, rejects, strictEqual } from "assert"; +import assert, { fail, notEqual, rejects, strictEqual } from "assert"; import BN from "bn.js"; import ThresholdKey from "../src/index"; @@ -131,6 +131,22 @@ export const refreshAndAccountIndex = (customSP, manualSync, accountIndexBackwar authSignatures: signatures, }); + { + // ensure tssShare is the master share + const { tssShare } = await tb2.getTSSShare(factorKey); + const { tssShare: tssShareIndex1 } = await tb2.getTSSShare(factorKey, { accountIndex: 1 }); + const { tssShare: tssShareIndex2 } = await tb2.getTSSShare(factorKey, { accountIndex: 2 }); + const { tssShare: tssShareIndex0 } = await tb2.getTSSShare(factorKey, { accountIndex: 0 }); + + const nonce1 = tb2.computeAccountNonce(1); + const nonce2 = tb2.computeAccountNonce(2); + + assert(tb2.computeAccountNonce(0).eq(new BN(0)), "nonce for account 0 should be 0"); + assert(tssShare.eq(tssShareIndex0), "tssShareIndex0 should be equal to tssShare"); + assert(tssShare.add(nonce1).umod(ecCurve.n).eq(tssShareIndex1), "tssShareIndex1 should be equal to tssShare + nonce1"); + assert(tssShare.add(nonce2).umod(ecCurve.n).eq(tssShareIndex2), "tssShareIndex2 should be equal to tssShare + nonce2"); + assert(!tssShareIndex1.add(nonce1).umod(ecCurve.n).eq(tssShareIndex2), "tssShareIndex2 should not be equal to tssShareIndex1 + nonce1"); + } // test case to ensure nonce mechanism { // make sure derived pub key is different from the index 0 key