Skip to content

Commit 976c553

Browse files
feat: Use const-crypto to compute event auth and do address comparison (solana-foundation#3986)
* feat: Use const-crypto to compute event auth and do address comparison * event auth const is computed and present only when event-cpi feature is enabled * chore: update lockfiles --------- Co-authored-by: Arrowana <Arrowana@users.noreply.github.com>
1 parent e8495d9 commit 976c553

File tree

10 files changed

+90
-10
lines changed

10 files changed

+90
-10
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lang/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ anchor-debug = [
2323
"anchor-derive-accounts/anchor-debug",
2424
]
2525
derive = []
26-
event-cpi = ["anchor-attribute-event/event-cpi"]
26+
event-cpi = ["anchor-attribute-event/event-cpi","anchor-attribute-account/event-cpi"]
2727
idl-build = [
2828
"anchor-attribute-account/idl-build",
2929
"anchor-attribute-constant/idl-build",
@@ -56,6 +56,7 @@ base64 = "0.21"
5656
bincode = "1"
5757
borsh = "0.10.3"
5858
bytemuck = { version = "1", features = ["derive"] }
59+
const-crypto = "0.3.0"
5960
solana-account-info = "2"
6061
solana-clock = "2"
6162
solana-cpi = "2"

lang/attribute/account/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ proc-macro = true
1414
anchor-debug = ["anchor-syn/anchor-debug"]
1515
idl-build = ["anchor-syn/idl-build"]
1616
lazy-account = []
17+
event-cpi = []
1718

1819
[dependencies]
1920
anchor-syn = { path = "../../syn", version = "0.32.1", features = ["hash"] }

lang/attribute/account/src/id.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ fn id_to_tokens(
3939
pubkey_type: proc_macro2::TokenStream,
4040
tokens: &mut proc_macro2::TokenStream,
4141
) {
42+
let event_authority_and_bump = {
43+
#[cfg(feature = "event-cpi")]
44+
quote! {
45+
pub const EVENT_AUTHORITY_AND_BUMP: (#pubkey_type, u8) = {
46+
let (address, bump) = anchor_lang::derive_program_address(&[b"__event_authority"], &ID_CONST.to_bytes());
47+
(#pubkey_type::new_from_array(address), bump)
48+
};
49+
}
50+
#[cfg(not(feature = "event-cpi"))]
51+
quote! {}
52+
};
4253
tokens.extend(quote! {
4354
/// The static program ID
4455
pub static ID: #pubkey_type = #id;
@@ -61,6 +72,8 @@ fn id_to_tokens(
6172
ID_CONST
6273
}
6374

75+
#event_authority_and_bump
76+
6477
#[cfg(test)]
6578
#[test]
6679
fn test_id() {

lang/attribute/event/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
164164
proc_macro::TokenStream::from(quote! {
165165
{
166166
let authority_info = ctx.accounts.#authority_name.to_account_info();
167-
let authority_bump = ctx.bumps.#authority_name;
168167

169168
let disc = anchor_lang::event::EVENT_IX_TAG_LE;
170169
let inner_data = anchor_lang::Event::data(&#event_struct);
@@ -187,7 +186,7 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
187186
anchor_lang::solana_program::program::invoke_signed(
188187
&ix,
189188
&[authority_info],
190-
&[&[#authority_seeds, &[authority_bump]]],
189+
&[&[#authority_seeds, &[crate::EVENT_AUTHORITY_AND_BUMP.1]]],
191190
)
192191
.map_err(anchor_lang::error::Error::from)?;
193192
}

lang/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub use anchor_attribute_program::{declare_program, instruction, program};
5959
pub use anchor_derive_accounts::Accounts;
6060
pub use anchor_derive_serde::{AnchorDeserialize, AnchorSerialize};
6161
pub use anchor_derive_space::InitSpace;
62+
pub use const_crypto::ed25519::derive_program_address;
6263

6364
/// Borsh is the default serialization format for instructions and accounts.
6465
pub use borsh::de::BorshDeserialize as AnchorDeserialize;

lang/syn/src/codegen/program/handlers.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ fn generate_event_cpi_mod() -> proc_macro2::TokenStream {
197197
{
198198
let authority = crate::parser::accounts::event_cpi::EventAuthority::get();
199199
let authority_name = authority.name;
200-
let authority_seeds = authority.seeds;
201200

202201
quote! {
203202
/// __events mod defines handler for self-cpi based event logging
@@ -218,14 +217,12 @@ fn generate_event_cpi_mod() -> proc_macro2::TokenStream {
218217
.with_account_name(#authority_name));
219218
}
220219

221-
let (expected_event_authority, _) =
222-
Pubkey::find_program_address(&[#authority_seeds], &program_id);
223-
if given_event_authority.key() != expected_event_authority {
220+
if given_event_authority.key() != crate::EVENT_AUTHORITY_AND_BUMP.0 {
224221
return Err(anchor_lang::error::Error::from(
225222
anchor_lang::error::ErrorCode::ConstraintSeeds,
226223
)
227224
.with_account_name(#authority_name)
228-
.with_pubkeys((given_event_authority.key(), expected_event_authority)));
225+
.with_pubkeys((given_event_authority.key(), crate::EVENT_AUTHORITY_AND_BUMP.0)));
229226
}
230227

231228
Ok(())

lang/syn/src/parser/accounts/event_cpi.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ pub fn add_event_cpi_accounts(
5252

5353
let authority = EventAuthority::get();
5454
let authority_name = authority.name_token_stream();
55-
let authority_seeds = authority.seeds;
5655

5756
let accounts_struct = quote! {
5857
#(#attrs)*
5958
#vis #struct_token #ident #generics {
6059
#(#fields,)*
6160

6261
/// CHECK: Only the event authority can invoke self-CPI
63-
#[account(seeds = [#authority_seeds], bump)]
62+
#[account(address = crate::EVENT_AUTHORITY_AND_BUMP.0)]
6463
pub #authority_name: AccountInfo<#info_lifetime>,
6564
/// CHECK: Self-CPI will fail if the program is not the current program
6665
pub program: AccountInfo<#info_lifetime>,

tests/auction-house/Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/spl/metadata/Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)