Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
remove mutable signer in CreateRegistry instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Oct 23, 2024
1 parent 6ad43a1 commit a37fb00
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 66 deletions.
13 changes: 5 additions & 8 deletions token/confidential-transfer/elgamal-registry/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ use {
pub enum RegistryInstruction {
/// Initialize an ElGamal public key registry.
///
/// 0. `[writable, signer]` The funding account (must be a system account)
/// 1. `[writable]` The account to be created
/// 2. `[signer]` The wallet address (will also be the owner address for the
/// 0. `[writable]` The account to be created
/// 1. `[signer]` The wallet address (will also be the owner address for the
/// registry account)
/// 3. `[]` System program
/// 4. `[]` Instructions sysvar if `VerifyPubkeyValidity` is included in the
/// 2. `[]` System program
/// 3. `[]` Instructions sysvar if `VerifyPubkeyValidity` is included in the
/// same transaction or context state account if `VerifyPubkeyValidity`
/// is pre-verified into a context state account.
/// 5. `[]` (Optional) Record account if the accompanying proof is to be
/// 4. `[]` (Optional) Record account if the accompanying proof is to be
/// read from a record account.
CreateRegistry {
/// Relative location of the `ProofInstruction::PubkeyValidityProof`
Expand Down Expand Up @@ -101,14 +100,12 @@ impl RegistryInstruction {

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

let mut accounts = vec![
AccountMeta::new(*funding_address, true),
AccountMeta::new(elgamal_registry_address, false),
AccountMeta::new_readonly(*owner_address, true),
AccountMeta::new_readonly(system_program::id(), false),
Expand Down
68 changes: 20 additions & 48 deletions token/confidential-transfer/elgamal-registry/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::{invoke, invoke_signed},
program::invoke_signed,
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
Expand All @@ -30,7 +30,6 @@ pub fn process_create_registry_account(
proof_instruction_offset: i64,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let funding_account_info = next_account_info(account_info_iter)?;
let elgamal_registry_account_info = next_account_info(account_info_iter)?;
let wallet_account_info = next_account_info(account_info_iter)?;
let system_program_info = next_account_info(account_info_iter)?;
Expand Down Expand Up @@ -60,7 +59,6 @@ pub fn process_create_registry_account(
let rent = Rent::get()?;

create_pda_account(
funding_account_info,
&rent,
ELGAMAL_REGISTRY_ACCOUNT_LEN,
program_id,
Expand Down Expand Up @@ -136,59 +134,33 @@ fn validate_owner(owner_info: &AccountInfo, expected_owner: &Pubkey) -> ProgramR
Ok(())
}

/// Creates ElGamal registry account using Program Derived Address for the given
/// seeds
/// Allocate ElGamal registry account using Program Derived Address for the
/// given seeds
pub fn create_pda_account<'a>(
payer: &AccountInfo<'a>,
rent: &Rent,
space: usize,
owner: &Pubkey,
system_program: &AccountInfo<'a>,
new_pda_account: &AccountInfo<'a>,
new_pda_signer_seeds: &[&[u8]],
) -> ProgramResult {
if new_pda_account.lamports() > 0 {
let required_lamports = rent
.minimum_balance(space)
.saturating_sub(new_pda_account.lamports());

if required_lamports > 0 {
invoke(
&system_instruction::transfer(payer.key, new_pda_account.key, required_lamports),
&[
payer.clone(),
new_pda_account.clone(),
system_program.clone(),
],
)?;
}
let required_lamports = rent
.minimum_balance(space)
.saturating_sub(new_pda_account.lamports());

invoke_signed(
&system_instruction::allocate(new_pda_account.key, space as u64),
&[new_pda_account.clone(), system_program.clone()],
&[new_pda_signer_seeds],
)?;

invoke_signed(
&system_instruction::assign(new_pda_account.key, owner),
&[new_pda_account.clone(), system_program.clone()],
&[new_pda_signer_seeds],
)
} else {
invoke_signed(
&system_instruction::create_account(
payer.key,
new_pda_account.key,
rent.minimum_balance(space),
space as u64,
owner,
),
&[
payer.clone(),
new_pda_account.clone(),
system_program.clone(),
],
&[new_pda_signer_seeds],
)
if required_lamports > 0 {
return Err(ProgramError::AccountNotRentExempt);
}

invoke_signed(
&system_instruction::allocate(new_pda_account.key, space as u64),
&[new_pda_account.clone(), system_program.clone()],
&[new_pda_signer_seeds],
)?;

invoke_signed(
&system_instruction::assign(new_pda_account.key, owner),
&[new_pda_account.clone(), system_program.clone()],
&[new_pda_signer_seeds],
)
}
28 changes: 18 additions & 10 deletions token/program-2022-test/tests/confidential_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use {
pubkey::Pubkey,
signature::Signer,
signer::{keypair::Keypair, signers::Signers},
system_instruction,
transaction::{Transaction, TransactionError},
transport::TransportError,
},
spl_elgamal_registry::state::ELGAMAL_REGISTRY_ACCOUNT_LEN,
spl_record::state::RecordData,
spl_token_2022::{
error::TokenError,
Expand Down Expand Up @@ -2853,12 +2855,23 @@ async fn confidential_transfer_configure_token_account_with_registry() {
ProofData::InstructionData(&proof_data),
);

let instructions = spl_elgamal_registry::instruction::create_registry(
&ctx.payer.pubkey(),
let elgamal_registry_address = spl_elgamal_registry::get_elgamal_registry_address(
&alice.pubkey(),
proof_location,
)
.unwrap();
&spl_elgamal_registry::id(),
);

let rent = ctx.banks_client.get_rent().await.unwrap();
let space = ELGAMAL_REGISTRY_ACCOUNT_LEN;
let system_instruction = system_instruction::transfer(
&ctx.payer.pubkey(),
&elgamal_registry_address,
rent.minimum_balance(space),
);
let create_registry_instructions =
spl_elgamal_registry::instruction::create_registry(&alice.pubkey(), proof_location)
.unwrap();

let instructions = [&[system_instruction], &create_registry_instructions[..]].concat();
let tx = Transaction::new_signed_with_payer(
&instructions,
Some(&ctx.payer.pubkey()),
Expand All @@ -2879,11 +2892,6 @@ async fn confidential_transfer_configure_token_account_with_registry() {
ProofData::InstructionData(&proof_data),
);

let elgamal_registry_address = spl_elgamal_registry::get_elgamal_registry_address(
&alice.pubkey(),
&spl_elgamal_registry::id(),
);

let instructions =
spl_elgamal_registry::instruction::update_registry(&alice.pubkey(), proof_location)
.unwrap();
Expand Down

0 comments on commit a37fb00

Please sign in to comment.