Skip to content

Commit

Permalink
check accounts uniqueness without vec (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid authored Jan 4, 2022
1 parent b4a3bd5 commit 94726c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
12 changes: 7 additions & 5 deletions programs/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl From<&Transaction> for Instruction {
fn from(tx: &Transaction) -> Instruction {
Instruction {
program_id: tx.program_id,
accounts: tx.accounts.iter().map(AccountMeta::from).collect(),
accounts: tx.accounts.iter().map(Into::into).collect(),
data: tx.data.clone(),
}
}
Expand Down Expand Up @@ -300,10 +300,12 @@ impl From<&AccountMeta> for TransactionAccount {
}

fn assert_unique_owners(owners: &[Pubkey]) -> Result<()> {
let mut uniq_owners = owners.to_vec();
uniq_owners.sort();
uniq_owners.dedup();
require!(owners.len() == uniq_owners.len(), UniqueOwners);
for (i, owner) in owners.iter().enumerate() {
require!(
!owners.iter().skip(i + 1).any(|item| item == owner),
UniqueOwners
)
}
Ok(())
}

Expand Down
35 changes: 35 additions & 0 deletions tests/multisig.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,39 @@ describe("multisig", () => {
assert.deepStrictEqual(multisigAccount.owners, newOwners);
assert.ok(multisigAccount.ownerSetSeqno === 1);
});

it("Assert Unique Owners", async () => {
const multisig = anchor.web3.Keypair.generate();
const [_multisigSigner, nonce] =
await anchor.web3.PublicKey.findProgramAddress(
[multisig.publicKey.toBuffer()],
program.programId
);
const multisigSize = 200; // Big enough.

const ownerA = anchor.web3.Keypair.generate();
const ownerB = anchor.web3.Keypair.generate();
const owners = [ownerA.publicKey, ownerB.publicKey, ownerA.publicKey];

const threshold = new anchor.BN(2);
try {
await program.rpc.createMultisig(owners, threshold, nonce, {
accounts: {
multisig: multisig.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
instructions: [
await program.account.multisig.createInstruction(
multisig,
multisigSize
),
],
signers: [multisig],
});
assert.fail();
} catch (err) {
assert.equal(err.code, 308);
assert.equal(err.msg, "Owners must be unique");
}
});
});

0 comments on commit 94726c6

Please sign in to comment.