diff --git a/programs/multisig/src/lib.rs b/programs/multisig/src/lib.rs index 2abba30..768d504 100644 --- a/programs/multisig/src/lib.rs +++ b/programs/multisig/src/lib.rs @@ -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(), } } @@ -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(()) } diff --git a/tests/multisig.js b/tests/multisig.js index da068a0..af84186 100644 --- a/tests/multisig.js +++ b/tests/multisig.js @@ -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"); + } + }); });