Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Client is_signer usage in to_account_metas #3322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lang/syn/src/codegen/accounts/__client_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn generate(
AccountField::CompositeField(s) => {
let name = &s.ident;
quote! {
account_metas.extend(self.#name.to_account_metas(None));
account_metas.extend(self.#name.to_account_metas(is_signer));
}
}
AccountField::Field(f) => {
Expand All @@ -94,7 +94,7 @@ pub fn generate(
};
let is_signer = match is_signer {
false => quote! {false},
true => quote! {true},
true => quote! {is_signer.unwrap_or(true)},
};
let meta = match f.constraints.is_mutable() {
false => quote! { anchor_lang::solana_program::instruction::AccountMeta::new_readonly },
Expand Down
4 changes: 2 additions & 2 deletions lang/syn/src/codegen/accounts/__cpi_client_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn generate(
AccountField::CompositeField(s) => {
let name = &s.ident;
quote! {
account_metas.extend(self.#name.to_account_metas(None));
account_metas.extend(self.#name.to_account_metas(is_signer));
}
}
AccountField::Field(f) => {
Expand All @@ -95,7 +95,7 @@ pub fn generate(
};
let is_signer = match is_signer {
false => quote! {false},
true => quote! {true},
true => quote! {is_signer.unwrap_or(true)},
};
let meta = match f.constraints.is_mutable() {
false => quote! { anchor_lang::solana_program::instruction::AccountMeta::new_readonly },
Expand Down
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();
}