Skip to content

Commit

Permalink
add test to leverage is_signer
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrowana committed Dec 2, 2024
1 parent 40c048b commit d344355
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/declare-program/programs/declare-program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ no-idl = []
cpi = ["no-entrypoint"]
default = []
idl-build = ["anchor-lang/idl-build"]
test-sbf = []

[dependencies]
anchor-lang = { path = "../../../../lang" }

[dev-dependencies]
solana-sdk = "2"
solana-program-test = "2"
36 changes: 36 additions & 0 deletions tests/declare-program/programs/declare-program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ use external::program::External;
// Compilation check for legacy IDL (pre Anchor `0.30`)
declare_program!(external_legacy);

pub const GLOBAL: &[u8] = b"global";

#[program]
pub mod declare_program {
use anchor_lang::solana_program::{instruction::Instruction, program::invoke_signed};

use super::*;

pub fn cpi(ctx: Context<Cpi>, value: u32) -> Result<()> {
Expand Down Expand Up @@ -114,6 +118,33 @@ pub mod declare_program {

Ok(())
}

pub fn proxy(ctx: Context<Proxy>, data: Vec<u8>) -> Result<()> {
let (authority, bump) = Pubkey::find_program_address(&[GLOBAL], &ID);

let accounts = ctx
.remaining_accounts
.iter()
.map(|ra| AccountMeta {
pubkey: ra.key(),
is_signer: ra.is_signer || &authority == ra.key,
is_writable: ra.is_writable,
})
.collect();

let signer_seeds: &[&[&[u8]]] = &[&[GLOBAL, &[bump]]];
invoke_signed(
&Instruction {
program_id: ctx.accounts.program.key(),
accounts,
data,
},
ctx.remaining_accounts,
signer_seeds,
)?;

Ok(())
}
}

#[derive(Accounts)]
Expand All @@ -128,3 +159,8 @@ pub struct Cpi<'info> {
pub struct Utils<'info> {
pub authority: Signer<'info>,
}

#[derive(Accounts)]
pub struct Proxy<'info> {
pub program: Program<'info, External>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#![cfg(feature = "test-sbf")]

use solana_sdk::{
native_token::LAMPORTS_PER_SOL, signature::Signer, system_instruction, transaction::Transaction,
};
use {
anchor_lang::{
prelude::Pubkey,
solana_program::{instruction::Instruction, system_program},
InstructionData, ToAccountMetas,
},
declare_program::external,
solana_program_test::{tokio, ProgramTest},
};

#[tokio::test]
async fn proxy() {
let mut pt = ProgramTest::new("declare_program", declare_program::id(), None);
pt.add_program("external", external::ID, None);

let (banks_client, payer, recent_blockhash) = pt.start().await;

let authority =
Pubkey::find_program_address(&[declare_program::GLOBAL], &declare_program::ID).0;
let mut accounts = declare_program::accounts::Proxy {
program: external::ID,
}
.to_account_metas(None);
accounts.extend(
external::client::accounts::Init {
authority,
my_account: Pubkey::find_program_address(&[authority.as_ref()], &external::ID).0,
system_program: system_program::ID,
}
.to_account_metas(Some(false)), // Forward as remaining accounts but toggle authority not to be a signer
);

let data = declare_program::instruction::Proxy {
data: external::client::args::Init {}.data(),
}
.data();
let transaction = Transaction::new_signed_with_payer(
&[
system_instruction::transfer(&payer.pubkey(), &authority, LAMPORTS_PER_SOL),
Instruction {
program_id: declare_program::ID,
accounts,
data,
},
],
Some(&payer.pubkey()),
&[&payer],
recent_blockhash,
);

banks_client.process_transaction(transaction).await.unwrap();
}

0 comments on commit d344355

Please sign in to comment.