Skip to content

Commit b29cda6

Browse files
committed
♻️ Hardcoding known CPI Auth PDA
1 parent 3aee912 commit b29cda6

File tree

5 files changed

+85
-54
lines changed

5 files changed

+85
-54
lines changed

clients/typescript/src/generated/idl/world.json

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,26 @@
118118
"accounts": [
119119
{
120120
"name": "buffer",
121-
"writable": true
121+
"writable": true,
122+
"pda": {
123+
"seeds": [
124+
{
125+
"kind": "const",
126+
"value": [
127+
98,
128+
117,
129+
102,
130+
102,
131+
101,
132+
114
133+
]
134+
},
135+
{
136+
"kind": "account",
137+
"path": "authority"
138+
}
139+
]
140+
}
122141
},
123142
{
124143
"name": "bolt_system"
@@ -161,7 +180,26 @@
161180
"accounts": [
162181
{
163182
"name": "buffer",
164-
"writable": true
183+
"writable": true,
184+
"pda": {
185+
"seeds": [
186+
{
187+
"kind": "const",
188+
"value": [
189+
98,
190+
117,
191+
102,
192+
102,
193+
101,
194+
114
195+
]
196+
},
197+
{
198+
"kind": "account",
199+
"path": "authority"
200+
}
201+
]
202+
}
165203
},
166204
{
167205
"name": "bolt_system"

clients/typescript/src/generated/types/world.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ export type World = {
9292
{
9393
name: "buffer";
9494
writable: true;
95+
pda: {
96+
seeds: [
97+
{
98+
kind: "const";
99+
value: [98, 117, 102, 102, 101, 114];
100+
},
101+
{
102+
kind: "account";
103+
path: "authority";
104+
},
105+
];
106+
};
95107
},
96108
{
97109
name: "boltSystem";
@@ -126,6 +138,18 @@ export type World = {
126138
{
127139
name: "buffer";
128140
writable: true;
141+
pda: {
142+
seeds: [
143+
{
144+
kind: "const";
145+
value: [98, 117, 102, 102, 101, 114];
146+
},
147+
{
148+
kind: "account";
149+
path: "authority";
150+
},
151+
];
152+
};
129153
},
130154
{
131155
name: "boltSystem";

crates/bolt-lang/src/cpi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::prelude::*;
22
use crate::BoltError;
33

4+
#[inline(always)]
45
pub fn checker<'info>(cpi_auth: &AccountInfo<'info>) -> Result<()> {
5-
if !cpi_auth.is_signer || cpi_auth.key != &crate::world::World::cpi_auth_address() {
6+
if !cpi_auth.is_signer || cpi_auth.key != crate::world::World::cpi_auth_address() {
67
return Err(BoltError::InvalidCaller.into());
78
}
89
Ok(())
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::prelude::*;
22

3+
#[inline(always)]
34
pub fn set_data<'info>(cpi_auth: AccountInfo<'info>, buffer: AccountInfo<'info>, component: AccountInfo<'info>) -> Result<()> {
45
crate::cpi::checker(&cpi_auth)?;
56
let buffer_data = buffer.try_borrow_data()?;
@@ -8,4 +9,3 @@ pub fn set_data<'info>(cpi_auth: AccountInfo<'info>, buffer: AccountInfo<'info>,
89
component_data.copy_from_slice(&buffer_data);
910
Ok(())
1011
}
11-

crates/programs/world/src/lib.rs

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use anchor_lang::{prelude::*, system_program};
33
use error::WorldError;
44
use std::collections::BTreeSet;
55

6+
static CPI_AUTH_ADDRESS: Pubkey = Pubkey::from_str_const("B2f2y3QTBv346wE6nWKor72AUhUvFF6mPk7TWCF2QVhi");
7+
68
#[cfg(not(feature = "no-entrypoint"))]
79
use solana_security_txt::security_txt;
810

@@ -260,12 +262,12 @@ pub mod world {
260262
if !ctx.accounts.authority.is_signer && ctx.accounts.authority.key != &ID {
261263
return Err(WorldError::InvalidAuthority.into());
262264
}
263-
bolt_component::cpi::initialize(ctx.accounts.build(&[World::cpi_auth_seeds().as_array().as_slice()]))?;
265+
bolt_component::cpi::initialize(ctx.accounts.build(&[World::cpi_auth_seeds().as_slice()]))?;
264266
Ok(())
265267
}
266268

267269
pub fn destroy_component(ctx: Context<DestroyComponent>) -> Result<()> {
268-
bolt_component::cpi::destroy(ctx.accounts.build(&[World::cpi_auth_seeds().as_array().as_slice()]))?;
270+
bolt_component::cpi::destroy(ctx.accounts.build(&[World::cpi_auth_seeds().as_slice()]))?;
269271
Ok(())
270272
}
271273

@@ -275,6 +277,7 @@ pub mod world {
275277
) -> Result<()> {
276278
apply_impl(
277279
&ctx.accounts.buffer,
280+
ctx.bumps.buffer,
278281
&ctx.accounts.authority,
279282
&ctx.accounts.world,
280283
&ctx.accounts.bolt_system,
@@ -291,7 +294,7 @@ pub mod world {
291294
#[derive(Accounts)]
292295
pub struct Apply<'info> {
293296
/// CHECK: buffer data check
294-
#[account(mut)]
297+
#[account(mut, seeds = [b"buffer", authority.key.as_ref()], bump)]
295298
pub buffer: AccountInfo<'info>,
296299
/// CHECK: bolt system program check
297300
#[account()]
@@ -325,6 +328,7 @@ pub mod world {
325328
) -> Result<()> {
326329
apply_impl(
327330
&ctx.accounts.buffer,
331+
ctx.bumps.buffer,
328332
&ctx.accounts.authority,
329333
&ctx.accounts.world,
330334
&ctx.accounts.bolt_system,
@@ -341,7 +345,7 @@ pub mod world {
341345
#[derive(Accounts)]
342346
pub struct ApplyWithSession<'info> {
343347
/// CHECK: buffer data check
344-
#[account(mut)]
348+
#[account(mut, seeds = [b"buffer", authority.key.as_ref()], bump)]
345349
pub buffer: AccountInfo<'info>,
346350
/// CHECK: bolt system program check
347351
#[account()]
@@ -375,6 +379,7 @@ pub mod world {
375379
#[allow(clippy::type_complexity)]
376380
fn apply_impl<'info>(
377381
buffer: &AccountInfo<'info>,
382+
buffer_bump: u8,
378383
authority: &Signer<'info>,
379384
world: &Account<'info, World>,
380385
bolt_system: &UncheckedAccount<'info>,
@@ -458,7 +463,7 @@ fn apply_impl<'info>(
458463
from: authority.to_account_info(),
459464
to: buffer.to_account_info(),
460465
},
461-
&[World::buffer_seeds(authority.key).as_array().as_slice()],
466+
&[&[b"buffer", authority.key.as_ref(), &[buffer_bump]]],
462467
),
463468
lamports,
464469
size as u64,
@@ -480,7 +485,7 @@ fn apply_impl<'info>(
480485
cpi_auth: cpi_auth.to_account_info(),
481486
component: component.to_account_info(),
482487
},
483-
&[World::cpi_auth_seeds().as_array().as_slice()],
488+
&[World::cpi_auth_seeds().as_slice()],
484489
),
485490
*bolt_system.key,
486491
)?;
@@ -493,7 +498,7 @@ fn apply_impl<'info>(
493498
buffer: buffer.to_account_info(),
494499
component: component.to_account_info(),
495500
},
496-
&[World::cpi_auth_seeds().as_array().as_slice()],
501+
&[World::cpi_auth_seeds().as_slice()],
497502
),
498503
)?;
499504
}
@@ -517,7 +522,7 @@ fn apply_impl<'info>(
517522
cpi_auth: cpi_auth.to_account_info(),
518523
component: component.to_account_info(),
519524
},
520-
&[World::cpi_auth_seeds().as_array().as_slice()],
525+
&[World::cpi_auth_seeds().as_slice()],
521526
),
522527
program.key(),
523528
)?;
@@ -534,7 +539,7 @@ fn apply_impl<'info>(
534539
buffer: buffer.to_account_info(),
535540
component: component.to_account_info(),
536541
},
537-
&[World::cpi_auth_seeds().as_array().as_slice()],
542+
&[World::cpi_auth_seeds().as_slice()],
538543
),
539544
)?;
540545
}
@@ -777,37 +782,6 @@ pub struct WorldSystems {
777782
pub approved_systems: BTreeSet<Pubkey>,
778783
}
779784

780-
pub struct BufferSeeds<'buffer, 'owner> {
781-
pub buffer: &'buffer [u8],
782-
pub owner: &'owner [u8],
783-
pub bump: [u8; 1],
784-
}
785-
786-
impl<'buffer, 'owner> BufferSeeds<'buffer, 'owner> {
787-
pub fn new(buffer: &'buffer [u8], owner: &'owner [u8], bump: u8) -> Self {
788-
Self { buffer, owner, bump: [bump] }
789-
}
790-
791-
pub fn as_array(&self) -> [&[u8]; 3] {
792-
[&self.buffer, &self.owner, &self.bump]
793-
}
794-
}
795-
796-
pub struct CpiAuthSeeds<'cpi_auth> {
797-
pub cpi_auth: &'cpi_auth [u8],
798-
pub bump: [u8; 1],
799-
}
800-
801-
impl<'cpi_auth> CpiAuthSeeds<'cpi_auth> {
802-
pub fn new(cpi_auth: &'cpi_auth [u8], bump: u8) -> Self {
803-
Self { cpi_auth, bump: [bump] }
804-
}
805-
806-
pub fn as_array(&self) -> [&[u8]; 2] {
807-
[&self.cpi_auth, &self.bump]
808-
}
809-
}
810-
811785
impl World {
812786
pub fn seed() -> &'static [u8] {
813787
b"world"
@@ -821,18 +795,12 @@ impl World {
821795
Pubkey::find_program_address(&[World::seed(), &self.id.to_be_bytes()], &crate::ID)
822796
}
823797

824-
pub fn buffer_seeds<'buffer, 'owner>(owner: &'owner Pubkey) -> BufferSeeds<'buffer, 'owner> {
825-
let (_, bump) = Pubkey::find_program_address(&[b"buffer", owner.as_ref()], &crate::ID);
826-
BufferSeeds::new(b"buffer", owner.as_ref(), bump)
827-
}
828-
829-
pub fn cpi_auth_seeds<'cpi_auth>() -> CpiAuthSeeds<'cpi_auth> {
830-
let (_, bump) = Pubkey::find_program_address(&[b"cpi_auth"], &crate::ID);
831-
CpiAuthSeeds::new(b"cpi_auth", bump)
798+
pub const fn cpi_auth_seeds() -> [&'static [u8]; 2] {
799+
[b"cpi_auth", &[251]] // 251 is the pre-computed bump for cpi_auth.
832800
}
833801

834-
pub fn cpi_auth_address() -> Pubkey {
835-
Pubkey::find_program_address(&[b"cpi_auth"], &crate::ID).0
802+
pub const fn cpi_auth_address() -> &'static Pubkey {
803+
&CPI_AUTH_ADDRESS // This is the pre-computed address for cpi_auth.
836804
}
837805
}
838806

0 commit comments

Comments
 (0)