-
Notifications
You must be signed in to change notification settings - Fork 8
Introduce canonical pointer PDA #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| use { | ||
| crate::{ | ||
| error::TokenWrapError, | ||
| get_canonical_pointer_address_signer_seeds, get_canonical_pointer_address_with_seed, | ||
| get_wrapped_mint_address, get_wrapped_mint_address_with_seed, get_wrapped_mint_authority, | ||
| get_wrapped_mint_authority_signer_seeds, get_wrapped_mint_authority_with_seed, | ||
| get_wrapped_mint_backpointer_address_signer_seeds, | ||
|
|
@@ -13,7 +14,7 @@ use { | |
| mint_customizer::{ | ||
| default_token_2022::DefaultToken2022Customizer, interface::MintCustomizer, | ||
| }, | ||
| state::Backpointer, | ||
| state::{Backpointer, CanonicalDeploymentPointer}, | ||
| }, | ||
| mpl_token_metadata::{ | ||
| accounts::Metadata as MetaplexMetadata, | ||
|
|
@@ -48,7 +49,7 @@ use { | |
| instruction::{initialize as initialize_token_metadata, remove_key, update_field}, | ||
| state::{Field, TokenMetadata}, | ||
| }, | ||
| std::collections::HashMap, | ||
| std::{collections::HashMap, mem}, | ||
| }; | ||
|
|
||
| /// Processes [`CreateMint`](enum.TokenWrapInstruction.html) instruction. | ||
|
|
@@ -793,6 +794,85 @@ pub fn process_sync_metadata_to_spl_token(accounts: &[AccountInfo]) -> ProgramRe | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Processes [`SetCanonicalPointer`](enum.TokenWrapInstruction.html) | ||
| /// instruction. | ||
| pub fn process_set_canonical_pointer( | ||
| program_id: &Pubkey, | ||
| accounts: &[AccountInfo], | ||
| new_program_id: Pubkey, | ||
| ) -> ProgramResult { | ||
| let account_info_iter = &mut accounts.iter(); | ||
| let unwrapped_mint_authority_info = next_account_info(account_info_iter)?; | ||
| let canonical_pointer_info = next_account_info(account_info_iter)?; | ||
| let unwrapped_mint_info = next_account_info(account_info_iter)?; | ||
| let _system_program_info = next_account_info(account_info_iter)?; | ||
|
|
||
| if !unwrapped_mint_authority_info.is_signer { | ||
| return Err(ProgramError::MissingRequiredSignature); | ||
| } | ||
|
|
||
| let mint_data = unwrapped_mint_info.try_borrow_data()?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be checking the mint's owner here? Maybe it's inconsequential, since the mint address can't be duplicated.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing, will add a check+test |
||
| let mint_state = PodStateWithExtensions::<PodMint>::unpack(&mint_data)?; | ||
| let mint_authority = mint_state | ||
| .base | ||
| .mint_authority | ||
| .ok_or(ProgramError::InvalidAccountData) | ||
| .inspect_err(|_| { | ||
| msg!("Cannot create/update pointer for unwrapped mint if does not have an authority"); | ||
| })?; | ||
|
|
||
| if mint_authority != *unwrapped_mint_authority_info.key { | ||
| return Err(ProgramError::IncorrectAuthority); | ||
| } | ||
|
|
||
| let (expected_pointer_address, bump) = | ||
| get_canonical_pointer_address_with_seed(unwrapped_mint_info.key); | ||
| if *canonical_pointer_info.key != expected_pointer_address { | ||
| msg!( | ||
| "Error: canonical pointer address {} does not match expected address {}", | ||
| canonical_pointer_info.key, | ||
| expected_pointer_address | ||
| ); | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
|
|
||
| // If pointer does not exist, initialize it | ||
| if canonical_pointer_info.data_is_empty() { | ||
| let space = mem::size_of::<CanonicalDeploymentPointer>(); | ||
| let rent_required = Rent::get()?.minimum_balance(space); | ||
|
|
||
| if canonical_pointer_info.lamports() < rent_required { | ||
| msg!( | ||
| "Error: canonical pointer PDA requires pre-funding of {} lamports", | ||
| rent_required | ||
| ); | ||
| Err(ProgramError::AccountNotRentExempt)? | ||
| } | ||
|
|
||
| let bump_seed = [bump]; | ||
| let signer_seeds = | ||
| get_canonical_pointer_address_signer_seeds(unwrapped_mint_info.key, &bump_seed); | ||
| invoke_signed( | ||
| &allocate(canonical_pointer_info.key, space as u64), | ||
| &[canonical_pointer_info.clone()], | ||
| &[&signer_seeds], | ||
| )?; | ||
| invoke_signed( | ||
| &assign(canonical_pointer_info.key, program_id), | ||
| &[canonical_pointer_info.clone()], | ||
| &[&signer_seeds], | ||
| )?; | ||
| } | ||
|
|
||
| // Set data within canonical pointer PDA | ||
|
|
||
| let mut pointer_data = canonical_pointer_info.try_borrow_mut_data()?; | ||
| let state = bytemuck::from_bytes_mut::<CanonicalDeploymentPointer>(&mut pointer_data); | ||
| state.program_id = new_program_id; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Instruction processor | ||
| pub fn process_instruction( | ||
| program_id: &Pubkey, | ||
|
|
@@ -826,5 +906,11 @@ pub fn process_instruction( | |
| msg!("Instruction: SyncMetadataToSplToken"); | ||
| process_sync_metadata_to_spl_token(accounts) | ||
| } | ||
| TokenWrapInstruction::SetCanonicalPointer { | ||
| program_id: new_program_id, | ||
| } => { | ||
| msg!("Instruction: SetCanonicalPointer"); | ||
| process_set_canonical_pointer(program_id, accounts, new_program_id) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,13 @@ pub struct Backpointer { | |
| /// Address that the wrapped mint is wrapping | ||
| pub unwrapped_mint: Pubkey, | ||
| } | ||
|
|
||
| /// The mint authority of an unwrapped mint's on-chain signal another token-wrap | ||
| /// deployment is the "canonical" one. | ||
|
||
| #[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)] | ||
| #[repr(C)] | ||
| pub struct CanonicalDeploymentPointer { | ||
| /// The program ID of the canonical token-wrap deployment as determined by | ||
| /// the unwrapped mint authority. | ||
| pub program_id: Pubkey, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a
_for_program()variants to allow for these forks to utilize our package for their address lookupsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh I'd just have them all just take
program_idand not support duplicate versions of all of the helpers.If this is easier for SemVer, though, nbd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id()is so deeply embedded into the program+clients that requiring the program_id is a far reaching change. I think I'll keep this forward compatible for now and revisit for a later breaking change if we find lots of folks forking and wanting the simpler name.