Skip to content

Commit

Permalink
solana: Add revertTokenAuthority to cancel set token authority flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
nvsriram committed Dec 12, 2024
1 parent 9a40f7f commit 496e315
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ pub struct SetTokenAuthority<'info> {
#[account(
seeds = [crate::TOKEN_AUTHORITY_SEED],
bump,
constraint = mint.mint_authority.unwrap() == token_authority.key() @ NTTError::InvalidMintAuthority
address = mint.mint_authority.unwrap() @ NTTError::InvalidMintAuthority
)]
/// CHECK: The constraints enforce this is valid mint authority
pub token_authority: UncheckedAccount<'info>,

/// CHECK: The rent payer of the [PendingTokenAuthority] storing this account will be the signer in the [claim_token_authority] instruction.
/// CHECK: This account will be the signer in the [claim_token_authority] instruction.
pub new_authority: UncheckedAccount<'info>,
}

Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn set_token_authority_one_step_unchecked(
}

#[derive(Accounts)]
pub struct ClaimTokenAuthority<'info> {
pub struct RevertTokenAuthority<'info> {
#[account(
constraint = config.paused @ NTTError::NotPaused,
)]
Expand All @@ -255,7 +255,8 @@ pub struct ClaimTokenAuthority<'info> {
mut,
address = pending_token_authority.rent_payer @ NTTError::IncorrectRentPayer,
)]
pub payer: Signer<'info>,
/// CHECK: the constraint enforces that this is the correct address
pub payer: UncheckedAccount<'info>,

#[account(
mut,
Expand All @@ -271,15 +272,6 @@ pub struct ClaimTokenAuthority<'info> {
/// CHECK: The seeds constraint enforces that this is the correct address
pub token_authority: UncheckedAccount<'info>,

#[account(
constraint = (
new_authority.key() == pending_token_authority.pending_authority
|| new_authority.key() == token_authority.key()
) @ NTTError::InvalidPendingTokenAuthority
)]
/// CHECK: constraint ensures that this is the correct address
pub new_authority: UncheckedAccount<'info>,

#[account(
mut,
seeds = [PendingTokenAuthority::SEED_PREFIX],
Expand All @@ -293,15 +285,32 @@ pub struct ClaimTokenAuthority<'info> {
pub system_program: Program<'info, System>,
}

pub fn revert_token_authority(_ctx: Context<RevertTokenAuthority>) -> Result<()> {
Ok(())
}

#[derive(Accounts)]
pub struct ClaimTokenAuthority<'info> {
pub common: RevertTokenAuthority<'info>,

#[account(
address = common.pending_token_authority.pending_authority @ NTTError::InvalidPendingTokenAuthority
)]
pub new_authority: Signer<'info>,
}

pub fn claim_token_authority(ctx: Context<ClaimTokenAuthority>) -> Result<()> {
token_interface::set_authority(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
ctx.accounts.common.token_program.to_account_info(),
token_interface::SetAuthority {
account_or_mint: ctx.accounts.mint.to_account_info(),
current_authority: ctx.accounts.token_authority.to_account_info(),
account_or_mint: ctx.accounts.common.mint.to_account_info(),
current_authority: ctx.accounts.common.token_authority.to_account_info(),
},
&[&[crate::TOKEN_AUTHORITY_SEED, &[ctx.bumps.token_authority]]],
&[&[
crate::TOKEN_AUTHORITY_SEED,
&[ctx.bumps.common.token_authority],
]],
),
AuthorityType::MintTokens,
Some(ctx.accounts.new_authority.key()),
Expand Down
4 changes: 4 additions & 0 deletions solana/programs/example-native-token-transfers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ pub mod example_native_token_transfers {
instructions::set_token_authority_one_step_unchecked(ctx)
}

pub fn revert_token_authority(ctx: Context<RevertTokenAuthority>) -> Result<()> {
instructions::revert_token_authority(ctx)
}

pub fn claim_token_authority(ctx: Context<ClaimTokenAuthority>) -> Result<()> {
instructions::claim_token_authority(ctx)
}
Expand Down
60 changes: 53 additions & 7 deletions solana/ts/idl/3_0_0/json/example_native_token_transfers.json
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@
"args": []
},
{
"name": "claimTokenAuthority",
"name": "revertTokenAuthority",
"accounts": [
{
"name": "config",
Expand All @@ -822,7 +822,7 @@
{
"name": "payer",
"isMut": true,
"isSigner": true
"isSigner": false
},
{
"name": "mint",
Expand All @@ -834,11 +834,6 @@
"isMut": false,
"isSigner": false
},
{
"name": "newAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pendingTokenAuthority",
"isMut": true,
Expand All @@ -857,6 +852,57 @@
],
"args": []
},
{
"name": "claimTokenAuthority",
"accounts": [
{
"name": "common",
"accounts": [
{
"name": "config",
"isMut": false,
"isSigner": false
},
{
"name": "payer",
"isMut": true,
"isSigner": false
},
{
"name": "mint",
"isMut": true,
"isSigner": false
},
{
"name": "tokenAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pendingTokenAuthority",
"isMut": true,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
]
},
{
"name": "newAuthority",
"isMut": false,
"isSigner": true
}
],
"args": []
},
{
"name": "setPaused",
"accounts": [
Expand Down
120 changes: 106 additions & 14 deletions solana/ts/idl/3_0_0/ts/example_native_token_transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ export type ExampleNativeTokenTransfers = {
"args": []
},
{
"name": "claimTokenAuthority",
"name": "revertTokenAuthority",
"accounts": [
{
"name": "config",
Expand All @@ -822,7 +822,7 @@ export type ExampleNativeTokenTransfers = {
{
"name": "payer",
"isMut": true,
"isSigner": true
"isSigner": false
},
{
"name": "mint",
Expand All @@ -834,11 +834,6 @@ export type ExampleNativeTokenTransfers = {
"isMut": false,
"isSigner": false
},
{
"name": "newAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pendingTokenAuthority",
"isMut": true,
Expand All @@ -857,6 +852,57 @@ export type ExampleNativeTokenTransfers = {
],
"args": []
},
{
"name": "claimTokenAuthority",
"accounts": [
{
"name": "common",
"accounts": [
{
"name": "config",
"isMut": false,
"isSigner": false
},
{
"name": "payer",
"isMut": true,
"isSigner": false
},
{
"name": "mint",
"isMut": true,
"isSigner": false
},
{
"name": "tokenAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pendingTokenAuthority",
"isMut": true,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
]
},
{
"name": "newAuthority",
"isMut": false,
"isSigner": true
}
],
"args": []
},
{
"name": "setPaused",
"accounts": [
Expand Down Expand Up @@ -3009,7 +3055,7 @@ export const IDL: ExampleNativeTokenTransfers = {
"args": []
},
{
"name": "claimTokenAuthority",
"name": "revertTokenAuthority",
"accounts": [
{
"name": "config",
Expand All @@ -3019,7 +3065,7 @@ export const IDL: ExampleNativeTokenTransfers = {
{
"name": "payer",
"isMut": true,
"isSigner": true
"isSigner": false
},
{
"name": "mint",
Expand All @@ -3031,11 +3077,6 @@ export const IDL: ExampleNativeTokenTransfers = {
"isMut": false,
"isSigner": false
},
{
"name": "newAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pendingTokenAuthority",
"isMut": true,
Expand All @@ -3054,6 +3095,57 @@ export const IDL: ExampleNativeTokenTransfers = {
],
"args": []
},
{
"name": "claimTokenAuthority",
"accounts": [
{
"name": "common",
"accounts": [
{
"name": "config",
"isMut": false,
"isSigner": false
},
{
"name": "payer",
"isMut": true,
"isSigner": false
},
{
"name": "mint",
"isMut": true,
"isSigner": false
},
{
"name": "tokenAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "pendingTokenAuthority",
"isMut": true,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
]
},
{
"name": "newAuthority",
"isMut": false,
"isSigner": true
}
],
"args": []
},
{
"name": "setPaused",
"accounts": [
Expand Down

0 comments on commit 496e315

Please sign in to comment.