Skip to content

Commit

Permalink
multisig duplicates check and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ridev6 committed Mar 26, 2024
1 parent 05c7097 commit 9034d09
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ride/multisig.ride
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ func validatePublicKey(publicKey: String) = {
fromBase58String(publicKey).size() == publicKeySize
}

func validateOwner(owners: List[String], pk: String) = {
strict checks = [
validatePublicKey(pk) || throwErr("invalid owner public key"),
owners.indexOf(pk) == owners.lastIndexOf(pk) || throwErr("must not contain duplicates")
]

owners
}

@Callable(i)
func init(owners: List[String], quorum: Int) = {
func validateOwner(acc: Unit, pk: String) = validatePublicKey(pk) || throwErr("invalid owner public key")
strict checks = [
i.caller == this || throwErr("init: not allowed"),
!getString(kMultisig).isDefined() || throwErr("init: already initialized"),
(owners.size() > 0 && owners.size() <= maxOwners) || throwErr("init: invalid owners"),
(quorum > 0 && quorum <= owners.size()) || throwErr("init: invalid quorum"),
# maxOwners
FOLD<10>(owners, unit, validateOwner)
FOLD<10>(owners, owners, validateOwner)
]

([
Expand Down
34 changes: 33 additions & 1 deletion test/components/multisig/init.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe(`[${process.pid}] multisig: init`, () => {
})).to.be.rejectedWith('not allowed');
});

it('invalid owners', async () => {
it('should throw if owners size <= 0', async () => {
const owners = [];
const quorum = 0;

Expand All @@ -42,6 +42,38 @@ describe(`[${process.pid}] multisig: init`, () => {
})).to.be.rejectedWith('invalid owners');
});

it('should throw if owners size > maxOwners', async () => {
const maxOwners = 10;
const owners = Array(maxOwners + 1).fill(accounts.admin0.publicKey);
const quorum = 0;

return expect(init({
dApp: accounts.multisig.address,
caller: accounts.multisig.seed,
owners,
quorum,
additionalFee: 4e5,
})).to.be.rejectedWith('invalid owners');
});

it('should throw id there are duplicates in owners', async () => {
const owners = [
accounts.admin0.publicKey,
accounts.admin1.publicKey,
accounts.admin1.publicKey,
accounts.admin2.publicKey,
];
const quorum = 3;

return expect(init({
dApp: accounts.multisig.address,
caller: accounts.multisig.seed,
owners,
quorum,
additionalFee: 4e5,
})).to.be.rejectedWith('must not contain duplicates');
});

it('invalid quorum', async () => {
const owners = [accounts.admin0.publicKey];
const quorum = 0;
Expand Down

0 comments on commit 9034d09

Please sign in to comment.