From 496e3156e86f965bd2ba854559daaefc97e4cea6 Mon Sep 17 00:00:00 2001 From: nvsriram Date: Wed, 11 Dec 2024 20:02:25 -0500 Subject: [PATCH] solana: Add `revertTokenAuthority` to cancel set token authority flow. --- .../src/instructions/admin.rs | 43 ++++--- .../example-native-token-transfers/src/lib.rs | 4 + .../json/example_native_token_transfers.json | 60 ++++++++- .../ts/example_native_token_transfers.ts | 120 ++++++++++++++++-- 4 files changed, 189 insertions(+), 38 deletions(-) diff --git a/solana/programs/example-native-token-transfers/src/instructions/admin.rs b/solana/programs/example-native-token-transfers/src/instructions/admin.rs index b0ed2344a..e59c098dd 100644 --- a/solana/programs/example-native-token-transfers/src/instructions/admin.rs +++ b/solana/programs/example-native-token-transfers/src/instructions/admin.rs @@ -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>, } @@ -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, )] @@ -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, @@ -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], @@ -293,15 +285,32 @@ pub struct ClaimTokenAuthority<'info> { pub system_program: Program<'info, System>, } +pub fn revert_token_authority(_ctx: Context) -> 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) -> 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()), diff --git a/solana/programs/example-native-token-transfers/src/lib.rs b/solana/programs/example-native-token-transfers/src/lib.rs index 69fd2af76..9f71a747a 100644 --- a/solana/programs/example-native-token-transfers/src/lib.rs +++ b/solana/programs/example-native-token-transfers/src/lib.rs @@ -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) -> Result<()> { + instructions::revert_token_authority(ctx) + } + pub fn claim_token_authority(ctx: Context) -> Result<()> { instructions::claim_token_authority(ctx) } diff --git a/solana/ts/idl/3_0_0/json/example_native_token_transfers.json b/solana/ts/idl/3_0_0/json/example_native_token_transfers.json index 0a65e1d52..07a2f80e4 100644 --- a/solana/ts/idl/3_0_0/json/example_native_token_transfers.json +++ b/solana/ts/idl/3_0_0/json/example_native_token_transfers.json @@ -812,7 +812,7 @@ "args": [] }, { - "name": "claimTokenAuthority", + "name": "revertTokenAuthority", "accounts": [ { "name": "config", @@ -822,7 +822,7 @@ { "name": "payer", "isMut": true, - "isSigner": true + "isSigner": false }, { "name": "mint", @@ -834,11 +834,6 @@ "isMut": false, "isSigner": false }, - { - "name": "newAuthority", - "isMut": false, - "isSigner": false - }, { "name": "pendingTokenAuthority", "isMut": true, @@ -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": [ diff --git a/solana/ts/idl/3_0_0/ts/example_native_token_transfers.ts b/solana/ts/idl/3_0_0/ts/example_native_token_transfers.ts index 0572264e4..2ebe1a693 100644 --- a/solana/ts/idl/3_0_0/ts/example_native_token_transfers.ts +++ b/solana/ts/idl/3_0_0/ts/example_native_token_transfers.ts @@ -812,7 +812,7 @@ export type ExampleNativeTokenTransfers = { "args": [] }, { - "name": "claimTokenAuthority", + "name": "revertTokenAuthority", "accounts": [ { "name": "config", @@ -822,7 +822,7 @@ export type ExampleNativeTokenTransfers = { { "name": "payer", "isMut": true, - "isSigner": true + "isSigner": false }, { "name": "mint", @@ -834,11 +834,6 @@ export type ExampleNativeTokenTransfers = { "isMut": false, "isSigner": false }, - { - "name": "newAuthority", - "isMut": false, - "isSigner": false - }, { "name": "pendingTokenAuthority", "isMut": true, @@ -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": [ @@ -3009,7 +3055,7 @@ export const IDL: ExampleNativeTokenTransfers = { "args": [] }, { - "name": "claimTokenAuthority", + "name": "revertTokenAuthority", "accounts": [ { "name": "config", @@ -3019,7 +3065,7 @@ export const IDL: ExampleNativeTokenTransfers = { { "name": "payer", "isMut": true, - "isSigner": true + "isSigner": false }, { "name": "mint", @@ -3031,11 +3077,6 @@ export const IDL: ExampleNativeTokenTransfers = { "isMut": false, "isSigner": false }, - { - "name": "newAuthority", - "isMut": false, - "isSigner": false - }, { "name": "pendingTokenAuthority", "isMut": true, @@ -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": [