Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit a37fb00

Browse files
committed
remove mutable signer in CreateRegistry instruction
1 parent 6ad43a1 commit a37fb00

File tree

3 files changed

+43
-66
lines changed

3 files changed

+43
-66
lines changed

token/confidential-transfer/elgamal-registry/src/instruction.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ use {
1717
pub enum RegistryInstruction {
1818
/// Initialize an ElGamal public key registry.
1919
///
20-
/// 0. `[writable, signer]` The funding account (must be a system account)
21-
/// 1. `[writable]` The account to be created
22-
/// 2. `[signer]` The wallet address (will also be the owner address for the
20+
/// 0. `[writable]` The account to be created
21+
/// 1. `[signer]` The wallet address (will also be the owner address for the
2322
/// registry account)
24-
/// 3. `[]` System program
25-
/// 4. `[]` Instructions sysvar if `VerifyPubkeyValidity` is included in the
23+
/// 2. `[]` System program
24+
/// 3. `[]` Instructions sysvar if `VerifyPubkeyValidity` is included in the
2625
/// same transaction or context state account if `VerifyPubkeyValidity`
2726
/// is pre-verified into a context state account.
28-
/// 5. `[]` (Optional) Record account if the accompanying proof is to be
27+
/// 4. `[]` (Optional) Record account if the accompanying proof is to be
2928
/// read from a record account.
3029
CreateRegistry {
3130
/// Relative location of the `ProofInstruction::PubkeyValidityProof`
@@ -101,14 +100,12 @@ impl RegistryInstruction {
101100

102101
/// Create a `RegistryInstruction::CreateRegistry` instruction
103102
pub fn create_registry(
104-
funding_address: &Pubkey,
105103
owner_address: &Pubkey,
106104
proof_location: ProofLocation<PubkeyValidityProofData>,
107105
) -> Result<Vec<Instruction>, ProgramError> {
108106
let elgamal_registry_address = get_elgamal_registry_address(owner_address, &id());
109107

110108
let mut accounts = vec![
111-
AccountMeta::new(*funding_address, true),
112109
AccountMeta::new(elgamal_registry_address, false),
113110
AccountMeta::new_readonly(*owner_address, true),
114111
AccountMeta::new_readonly(system_program::id(), false),

token/confidential-transfer/elgamal-registry/src/processor.rs

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use {
99
account_info::{next_account_info, AccountInfo},
1010
entrypoint::ProgramResult,
1111
msg,
12-
program::{invoke, invoke_signed},
12+
program::invoke_signed,
1313
program_error::ProgramError,
1414
pubkey::Pubkey,
1515
rent::Rent,
@@ -30,7 +30,6 @@ pub fn process_create_registry_account(
3030
proof_instruction_offset: i64,
3131
) -> ProgramResult {
3232
let account_info_iter = &mut accounts.iter();
33-
let funding_account_info = next_account_info(account_info_iter)?;
3433
let elgamal_registry_account_info = next_account_info(account_info_iter)?;
3534
let wallet_account_info = next_account_info(account_info_iter)?;
3635
let system_program_info = next_account_info(account_info_iter)?;
@@ -60,7 +59,6 @@ pub fn process_create_registry_account(
6059
let rent = Rent::get()?;
6160

6261
create_pda_account(
63-
funding_account_info,
6462
&rent,
6563
ELGAMAL_REGISTRY_ACCOUNT_LEN,
6664
program_id,
@@ -136,59 +134,33 @@ fn validate_owner(owner_info: &AccountInfo, expected_owner: &Pubkey) -> ProgramR
136134
Ok(())
137135
}
138136

139-
/// Creates ElGamal registry account using Program Derived Address for the given
140-
/// seeds
137+
/// Allocate ElGamal registry account using Program Derived Address for the
138+
/// given seeds
141139
pub fn create_pda_account<'a>(
142-
payer: &AccountInfo<'a>,
143140
rent: &Rent,
144141
space: usize,
145142
owner: &Pubkey,
146143
system_program: &AccountInfo<'a>,
147144
new_pda_account: &AccountInfo<'a>,
148145
new_pda_signer_seeds: &[&[u8]],
149146
) -> ProgramResult {
150-
if new_pda_account.lamports() > 0 {
151-
let required_lamports = rent
152-
.minimum_balance(space)
153-
.saturating_sub(new_pda_account.lamports());
154-
155-
if required_lamports > 0 {
156-
invoke(
157-
&system_instruction::transfer(payer.key, new_pda_account.key, required_lamports),
158-
&[
159-
payer.clone(),
160-
new_pda_account.clone(),
161-
system_program.clone(),
162-
],
163-
)?;
164-
}
147+
let required_lamports = rent
148+
.minimum_balance(space)
149+
.saturating_sub(new_pda_account.lamports());
165150

166-
invoke_signed(
167-
&system_instruction::allocate(new_pda_account.key, space as u64),
168-
&[new_pda_account.clone(), system_program.clone()],
169-
&[new_pda_signer_seeds],
170-
)?;
171-
172-
invoke_signed(
173-
&system_instruction::assign(new_pda_account.key, owner),
174-
&[new_pda_account.clone(), system_program.clone()],
175-
&[new_pda_signer_seeds],
176-
)
177-
} else {
178-
invoke_signed(
179-
&system_instruction::create_account(
180-
payer.key,
181-
new_pda_account.key,
182-
rent.minimum_balance(space),
183-
space as u64,
184-
owner,
185-
),
186-
&[
187-
payer.clone(),
188-
new_pda_account.clone(),
189-
system_program.clone(),
190-
],
191-
&[new_pda_signer_seeds],
192-
)
151+
if required_lamports > 0 {
152+
return Err(ProgramError::AccountNotRentExempt);
193153
}
154+
155+
invoke_signed(
156+
&system_instruction::allocate(new_pda_account.key, space as u64),
157+
&[new_pda_account.clone(), system_program.clone()],
158+
&[new_pda_signer_seeds],
159+
)?;
160+
161+
invoke_signed(
162+
&system_instruction::assign(new_pda_account.key, owner),
163+
&[new_pda_account.clone(), system_program.clone()],
164+
&[new_pda_signer_seeds],
165+
)
194166
}

token/program-2022-test/tests/confidential_transfer.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ use {
1313
pubkey::Pubkey,
1414
signature::Signer,
1515
signer::{keypair::Keypair, signers::Signers},
16+
system_instruction,
1617
transaction::{Transaction, TransactionError},
1718
transport::TransportError,
1819
},
20+
spl_elgamal_registry::state::ELGAMAL_REGISTRY_ACCOUNT_LEN,
1921
spl_record::state::RecordData,
2022
spl_token_2022::{
2123
error::TokenError,
@@ -2853,12 +2855,23 @@ async fn confidential_transfer_configure_token_account_with_registry() {
28532855
ProofData::InstructionData(&proof_data),
28542856
);
28552857

2856-
let instructions = spl_elgamal_registry::instruction::create_registry(
2857-
&ctx.payer.pubkey(),
2858+
let elgamal_registry_address = spl_elgamal_registry::get_elgamal_registry_address(
28582859
&alice.pubkey(),
2859-
proof_location,
2860-
)
2861-
.unwrap();
2860+
&spl_elgamal_registry::id(),
2861+
);
2862+
2863+
let rent = ctx.banks_client.get_rent().await.unwrap();
2864+
let space = ELGAMAL_REGISTRY_ACCOUNT_LEN;
2865+
let system_instruction = system_instruction::transfer(
2866+
&ctx.payer.pubkey(),
2867+
&elgamal_registry_address,
2868+
rent.minimum_balance(space),
2869+
);
2870+
let create_registry_instructions =
2871+
spl_elgamal_registry::instruction::create_registry(&alice.pubkey(), proof_location)
2872+
.unwrap();
2873+
2874+
let instructions = [&[system_instruction], &create_registry_instructions[..]].concat();
28622875
let tx = Transaction::new_signed_with_payer(
28632876
&instructions,
28642877
Some(&ctx.payer.pubkey()),
@@ -2879,11 +2892,6 @@ async fn confidential_transfer_configure_token_account_with_registry() {
28792892
ProofData::InstructionData(&proof_data),
28802893
);
28812894

2882-
let elgamal_registry_address = spl_elgamal_registry::get_elgamal_registry_address(
2883-
&alice.pubkey(),
2884-
&spl_elgamal_registry::id(),
2885-
);
2886-
28872895
let instructions =
28882896
spl_elgamal_registry::instruction::update_registry(&alice.pubkey(), proof_location)
28892897
.unwrap();

0 commit comments

Comments
 (0)