浏览代码

:sparkles: Propagate signing authority to the systems (#31)

* :sparkles: Propagate signing authority to the systems

* :bug: Use authority in the generated tests
Gabriele Picco 1 年之前
父节点
当前提交
ec463da4ba

+ 1 - 0
cli/src/rust_template.rs

@@ -385,6 +385,7 @@ describe("{}", () => {{
           componentProgram: positionComponent.programId,
           componentProgram: positionComponent.programId,
           boltSystem: systemMovement.programId,
           boltSystem: systemMovement.programId,
           boltComponent: positionComponentPda,
           boltComponent: positionComponentPda,
+          authority: provider.wallet.publicKey,
       }}, {{args: new Uint8Array()}});
       }}, {{args: new Uint8Array()}});
 
 
       const tx = new anchor.web3.Transaction().add(applySystemIx);
       const tx = new anchor.web3.Transaction().add(applySystemIx);

+ 2 - 0
crates/bolt-helpers/attribute/system-template/src/lib.rs

@@ -54,6 +54,8 @@ pub fn system_template(attr: TokenStream, item: TokenStream) -> TokenStream {
             #[derive(Accounts)]
             #[derive(Accounts)]
             pub struct #data_struct<'info> {
             pub struct #data_struct<'info> {
                 #(#fields)*
                 #(#fields)*
+                #[account()]
+                pub authority: Signer<'info>,
             }
             }
         };
         };
         quote! {
         quote! {

+ 2 - 1
crates/bolt-helpers/attribute/world-apply/src/lib.rs

@@ -92,7 +92,7 @@ pub fn apply_system(attr: TokenStream, item: TokenStream) -> TokenStream {
                 pub bolt_system: UncheckedAccount<'info>,
                 pub bolt_system: UncheckedAccount<'info>,
                 #(#fields)*
                 #(#fields)*
                  /// CHECK: authority check
                  /// CHECK: authority check
-                pub authority: UncheckedAccount<'info>,
+                pub authority: Signer<'info>,
                 #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
                 #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
                 /// CHECK: instruction sysvar check
                 /// CHECK: instruction sysvar check
                 pub instruction_sysvar_account: UncheckedAccount<'info>,
                 pub instruction_sysvar_account: UncheckedAccount<'info>,
@@ -119,6 +119,7 @@ pub fn apply_system(attr: TokenStream, item: TokenStream) -> TokenStream {
                     let cpi_program = self.bolt_system.to_account_info();
                     let cpi_program = self.bolt_system.to_account_info();
                     let cpi_accounts = bolt_system::cpi::accounts::#set_data_struct {
                     let cpi_accounts = bolt_system::cpi::accounts::#set_data_struct {
                         #(#fields)*
                         #(#fields)*
+                        authority: self.authority.to_account_info(),
                     };
                     };
                     CpiContext::new(cpi_program, cpi_accounts)
                     CpiContext::new(cpi_program, cpi_accounts)
                 }
                 }

+ 1 - 1
crates/bolt-lang/attribute/bolt-program/src/lib.rs

@@ -156,7 +156,7 @@ fn generate_update(component_type: &Type) -> (TokenStream2, TokenStream2) {
                     return Err(BoltError::InvalidCaller.into());
                     return Err(BoltError::InvalidCaller.into());
                 }
                 }
                 // Check if the authority is authorized to modify the data
                 // Check if the authority is authorized to modify the data
-                if ctx.accounts.bolt_component.bolt_metadata.authority != World::id() && ctx.accounts.bolt_component.bolt_metadata.authority != *ctx.accounts.authority.key {
+                if ctx.accounts.bolt_component.bolt_metadata.authority != World::id() && (ctx.accounts.bolt_component.bolt_metadata.authority != *ctx.accounts.authority.key || !ctx.accounts.authority.is_signer) {
                     return Err(BoltError::InvalidAuthority.into());
                     return Err(BoltError::InvalidAuthority.into());
                 }
                 }
 
 

+ 1 - 0
crates/bolt-lang/attribute/system-input/src/lib.rs

@@ -76,6 +76,7 @@ pub fn system_input(_attr: TokenStream, item: TokenStream) -> TokenStream {
         #[derive(Accounts)]
         #[derive(Accounts)]
         pub struct #name<'info> {
         pub struct #name<'info> {
             #(#transformed_fields)*
             #(#transformed_fields)*
+            pub authority: Signer<'info>,
         }
         }
     };
     };
 
 

+ 1 - 0
examples/system-apply-velocity/src/lib.rs

@@ -20,6 +20,7 @@ pub mod system_apply_velocity {
         msg!("last applied: {}", ctx.accounts.velocity.last_applied);
         msg!("last applied: {}", ctx.accounts.velocity.last_applied);
         msg!("Position: {}", ctx.accounts.position.x);
         msg!("Position: {}", ctx.accounts.position.x);
         msg!("Remaining accounts: {}", ctx.remaining_accounts.len());
         msg!("Remaining accounts: {}", ctx.remaining_accounts.len());
+        msg!("Authority: {}", ctx.accounts.authority.key);
         Ok(ctx.accounts)
         Ok(ctx.accounts)
     }
     }
 
 

+ 2 - 0
programs/bolt-component/src/lib.rs

@@ -30,6 +30,7 @@ pub mod bolt_component {
         pub bolt_component: Account<'info, Component>,
         pub bolt_component: Account<'info, Component>,
         /// CHECK: The system can modify the data of the component
         /// CHECK: The system can modify the data of the component
         pub bolt_system: UncheckedAccount<'info>,
         pub bolt_system: UncheckedAccount<'info>,
+        pub authority: Signer<'info>,
     }
     }
 
 
     impl<'info> Apply<'info> {
     impl<'info> Apply<'info> {
@@ -39,6 +40,7 @@ pub mod bolt_component {
             let cpi_program = self.bolt_system.to_account_info();
             let cpi_program = self.bolt_system.to_account_info();
             let cpi_accounts = bolt_system::cpi::accounts::SetData {
             let cpi_accounts = bolt_system::cpi::accounts::SetData {
                 component: self.bolt_component.to_account_info().clone(),
                 component: self.bolt_component.to_account_info().clone(),
+                authority: self.authority.to_account_info(),
             };
             };
             CpiContext::new(cpi_program, cpi_accounts)
             CpiContext::new(cpi_program, cpi_accounts)
         }
         }

+ 2 - 0
programs/bolt-system/src/lib.rs

@@ -18,4 +18,6 @@ pub struct SetData<'info> {
     #[account()]
     #[account()]
     /// CHECK: unchecked account
     /// CHECK: unchecked account
     pub component: UncheckedAccount<'info>,
     pub component: UncheckedAccount<'info>,
+    #[account()]
+    pub authority: Signer<'info>,
 }
 }

+ 3 - 2
programs/world/src/lib.rs

@@ -87,7 +87,7 @@ pub mod world {
         /// CHECK: component account
         /// CHECK: component account
         pub bolt_component: UncheckedAccount<'info>,
         pub bolt_component: UncheckedAccount<'info>,
         /// CHECK: authority check
         /// CHECK: authority check
-        pub authority: UncheckedAccount<'info>,
+        pub authority: Signer<'info>,
         #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
         #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
         /// CHECK: instruction sysvar check
         /// CHECK: instruction sysvar check
         pub instruction_sysvar_account: UncheckedAccount<'info>,
         pub instruction_sysvar_account: UncheckedAccount<'info>,
@@ -100,6 +100,7 @@ pub mod world {
             let cpi_program = self.bolt_system.to_account_info();
             let cpi_program = self.bolt_system.to_account_info();
             let cpi_accounts = bolt_system::cpi::accounts::SetData {
             let cpi_accounts = bolt_system::cpi::accounts::SetData {
                 component: self.bolt_component.to_account_info(),
                 component: self.bolt_component.to_account_info(),
+                authority: self.authority.to_account_info(),
             };
             };
             CpiContext::new(cpi_program, cpi_accounts)
             CpiContext::new(cpi_program, cpi_accounts)
         }
         }
@@ -240,7 +241,7 @@ impl Entity {
 pub fn build_update_context<'info>(
 pub fn build_update_context<'info>(
     component_program: UncheckedAccount<'info>,
     component_program: UncheckedAccount<'info>,
     component: UncheckedAccount<'info>,
     component: UncheckedAccount<'info>,
-    authority: UncheckedAccount<'info>,
+    authority: Signer<'info>,
     instruction_sysvar_account: UncheckedAccount<'info>,
     instruction_sysvar_account: UncheckedAccount<'info>,
 ) -> CpiContext<'info, 'info, 'info, 'info, bolt_component::cpi::accounts::Update<'info>> {
 ) -> CpiContext<'info, 'info, 'info, 'info, bolt_component::cpi::accounts::Update<'info>> {
     let cpi_program = component_program.to_account_info();
     let cpi_program = component_program.to_account_info();

+ 7 - 7
tests/bolt.ts

@@ -335,9 +335,9 @@ describe("bolt", () => {
         boltSystem: systemSimpleMovement,
         boltSystem: systemSimpleMovement,
         boltComponent: componentPositionEntity1,
         boltComponent: componentPositionEntity1,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
-        authority: worldProgram.programId,
+        authority: provider.wallet.publicKey,
       })
       })
-      .rpc({ skipPreflight: true });
+      .rpc();
 
 
     expect(
     expect(
       (
       (
@@ -381,7 +381,7 @@ describe("bolt", () => {
         boltSystem: systemSimpleMovement,
         boltSystem: systemSimpleMovement,
         boltComponent: componentPositionEntity1,
         boltComponent: componentPositionEntity1,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
-        authority: worldProgram.programId,
+        authority: provider.wallet.publicKey,
       })
       })
       .rpc();
       .rpc();
 
 
@@ -430,7 +430,7 @@ describe("bolt", () => {
         boltSystem: systemFly,
         boltSystem: systemFly,
         boltComponent: componentPositionEntity1,
         boltComponent: componentPositionEntity1,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
-        authority: worldProgram.programId,
+        authority: provider.wallet.publicKey,
       })
       })
       .rpc();
       .rpc();
 
 
@@ -474,7 +474,7 @@ describe("bolt", () => {
         boltComponent1: componentVelocityEntity1,
         boltComponent1: componentVelocityEntity1,
         boltComponent2: componentPositionEntity1,
         boltComponent2: componentPositionEntity1,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
-        authority: worldProgram.programId,
+        authority: provider.wallet.publicKey,
       })
       })
       .rpc();
       .rpc();
 
 
@@ -535,7 +535,7 @@ describe("bolt", () => {
         boltComponent1: componentVelocityEntity1,
         boltComponent1: componentVelocityEntity1,
         boltComponent2: componentPositionEntity1,
         boltComponent2: componentPositionEntity1,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
         instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
-        authority: worldProgram.programId,
+        authority: provider.wallet.publicKey,
       })
       })
       .remainingAccounts([
       .remainingAccounts([
         {
         {
@@ -571,7 +571,7 @@ describe("bolt", () => {
           boltSystem: systemFly,
           boltSystem: systemFly,
           boltComponent: componentPositionEntity5,
           boltComponent: componentPositionEntity5,
           instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
           instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
-          authority: worldProgram.programId,
+          authority: provider.wallet.publicKey,
         })
         })
         .rpc();
         .rpc();
     } catch (e) {
     } catch (e) {