From 5fe0f5582e69f48fa37f1afbe2605c3beb87d723 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 22 Mar 2021 18:45:31 +0100 Subject: [PATCH] Check threashold against number of pubkeys --- packages/amino/src/multisig.spec.ts | 6 ++++++ packages/amino/src/multisig.ts | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/amino/src/multisig.spec.ts b/packages/amino/src/multisig.spec.ts index ce239345..758e1f64 100644 --- a/packages/amino/src/multisig.spec.ts +++ b/packages/amino/src/multisig.spec.ts @@ -59,5 +59,11 @@ describe("multisig", () => { it("works with nosort", () => { expect(createMultisigThresholdPubkey([test3, test1], 2, true)).toEqual(testgroup4); }); + + it("throws for threshold lager than number of keys", () => { + expect(() => createMultisigThresholdPubkey([test1, test2, test3], 5)).toThrowError( + /threshold k = 5 exceeds number of keys n = 3/i, + ); + }); }); }); diff --git a/packages/amino/src/multisig.ts b/packages/amino/src/multisig.ts index 51203f87..795b5caf 100644 --- a/packages/amino/src/multisig.ts +++ b/packages/amino/src/multisig.ts @@ -20,6 +20,11 @@ export function createMultisigThresholdPubkey( threshold: number, nosort = false, ): MultisigThresholdPubkey { + const uintThreshold = new Uint53(threshold); + if (uintThreshold.toNumber() > pubkeys.length) { + throw new Error(`Threshold k = ${uintThreshold.toNumber()} exceeds number of keys n = ${pubkeys.length}`); + } + const outPubkeys = nosort ? pubkeys : Array.from(pubkeys).sort((lhs, rhs) => { @@ -31,7 +36,7 @@ export function createMultisigThresholdPubkey( return { type: "tendermint/PubKeyMultisigThreshold", value: { - threshold: new Uint53(threshold).toString(), + threshold: uintThreshold.toString(), pubkeys: outPubkeys, }, };