Skip to content

Commit

Permalink
program: add update_authority instruction and test
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Aug 15, 2024
1 parent 479f94d commit 751b0d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ pub mod gateway {
Ok(())
}

pub fn update_authority(ctx: Context<UpdateAuthority>, new_authority_address: Pubkey) -> Result<()> {
let pda = &mut ctx.accounts.pda;
require!(
ctx.accounts.signer.key() == pda.authority,
Errors::SignerIsNotAuthority
);
pda.authority = new_authority_address;
Ok(())
}

pub fn deposit(ctx: Context<Deposit>, amount: u64, memo: Vec<u8>) -> Result<()> {
require!(memo.len() >= 20, Errors::MemoLengthTooShort);
require!(memo.len() <= 512, Errors::MemoLengthExceeded);
Expand Down Expand Up @@ -314,6 +324,14 @@ pub struct UpdateTss<'info> {
pub signer: Signer<'info>,
}

#[derive(Accounts)]
pub struct UpdateAuthority<'info> {
#[account(mut)]
pub pda: Account<'info, Pda>,
#[account(mut)]
pub signer: Signer<'info>,
}

#[derive(Accounts)]
pub struct UpdatePaused<'info> {
#[account(mut)]
Expand Down
20 changes: 20 additions & 0 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,26 @@ describe("some tests", () => {

});

it("update authority", async () => {
const newAuthority = anchor.web3.Keypair.generate();
await gatewayProgram.methods.updateAuthority(newAuthority.publicKey).accounts({
pda: pdaAccount,
}).rpc();
// const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount);
// expect(pdaAccountData.authority).to.be.eq(newAuthority.publicKey);

// now the old authority cannot update TSS address and will fail
try {
await gatewayProgram.methods.updateTss(Array.from(new Uint8Array(20))).accounts({
pda: pdaAccount,
}).rpc();
} catch (err) {
console.log("Error message: ", err.message);
expect(err).to.be.instanceof(anchor.AnchorError);
expect(err.message).to.include("SignerIsNotAuthority");
}
});


});

Expand Down

0 comments on commit 751b0d3

Please sign in to comment.