From 751b0d3510f54e03ba526882a44481aef4e84507 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Thu, 15 Aug 2024 01:53:31 -0500 Subject: [PATCH] program: add update_authority instruction and test --- programs/protocol-contracts-solana/src/lib.rs | 18 +++++++++++++++++ tests/protocol-contracts-solana.ts | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/programs/protocol-contracts-solana/src/lib.rs b/programs/protocol-contracts-solana/src/lib.rs index ad19814..d11eef7 100644 --- a/programs/protocol-contracts-solana/src/lib.rs +++ b/programs/protocol-contracts-solana/src/lib.rs @@ -70,6 +70,16 @@ pub mod gateway { Ok(()) } + pub fn update_authority(ctx: Context, 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, amount: u64, memo: Vec) -> Result<()> { require!(memo.len() >= 20, Errors::MemoLengthTooShort); require!(memo.len() <= 512, Errors::MemoLengthExceeded); @@ -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)] diff --git a/tests/protocol-contracts-solana.ts b/tests/protocol-contracts-solana.ts index 5e07237..498b8de 100644 --- a/tests/protocol-contracts-solana.ts +++ b/tests/protocol-contracts-solana.ts @@ -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"); + } + }); + });