Check threashold against number of pubkeys

This commit is contained in:
Simon Warta 2021-03-22 18:45:31 +01:00
parent 7255af4186
commit 5fe0f5582e
2 changed files with 12 additions and 1 deletions

View File

@ -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,
);
});
});
});

View File

@ -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,
},
};