Skip to content
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

solana: Handle transferring mint authority using SPL Multisig #587

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
bb7a3f4
Update `accept_token_authority` to account for multisig token authority
nvsriram Jan 31, 2025
e944553
Add `accept_token_authority_multisig` ix
nvsriram Jan 31, 2025
f269001
Update `set_token_authority_one_step_unchecked` to account for multis…
nvsriram Jan 31, 2025
960f080
Update `set_token_authority` to account for multisig token authority
nvsriram Jan 31, 2025
a31365a
Update `claim_token_authority`and add `claim_token_authority_to_multi…
nvsriram Jan 31, 2025
2fc9fcb
solana: Rename ix to `acceptTokenAuthorityToMultisig`
nvsriram Feb 3, 2025
69aa742
solana: Pass all remaining_accounts as required signers in `accept_to…
nvsriram Feb 3, 2025
f102b3a
solana: Fix lint
nvsriram Feb 3, 2025
d82f87e
solana: Add TS helper functions
nvsriram Feb 3, 2025
ad25ae1
solana: Update IDL
nvsriram Feb 3, 2025
fbda72a
solana: Add token authority transfer test cases
nvsriram Feb 3, 2025
3f20aa9
solana: Make comment/function syntax consistent
nvsriram Feb 3, 2025
6e5d603
solana: Fix unneeded borrow
nvsriram Feb 3, 2025
7932667
solana: Replace if let with match syntax
nvsriram Feb 5, 2025
08c750f
solana: Simplify constraint using map_or
nvsriram Feb 5, 2025
5e7b92c
solana: Add comment on lack of custom error thrown
nvsriram Feb 5, 2025
a33f8df
solana: Refactor out `claim_from_(multisig_)token_authority` fn's
nvsriram Feb 5, 2025
472fc49
solana: Make `additionalSigners` `readonly`
nvsriram Feb 6, 2025
98de06d
solana: Refactor out`transfer_[ownership | token_authority]` into sep…
nvsriram Feb 12, 2025
f1a084a
solana: Add test helper file and refactor code
nvsriram Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 241 additions & 44 deletions solana/programs/example-native-token-transfers/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
pending_token_authority::PendingTokenAuthority,
queue::{inbox::InboxRateLimit, outbox::OutboxRateLimit, rate_limit::RateLimitState},
registered_transceiver::RegisteredTransceiver,
spl_multisig::SplMultisig,
};

// * Transfer ownership
Expand Down Expand Up @@ -159,7 +160,7 @@ pub fn claim_ownership(ctx: Context<ClaimOwnership>) -> Result<()> {
// * Set token authority

#[derive(Accounts)]
pub struct AcceptTokenAuthority<'info> {
pub struct AcceptTokenAuthorityBase<'info> {
#[account(
has_one = mint,
constraint = config.paused @ NTTError::NotPaused,
Expand All @@ -176,25 +177,86 @@ pub struct AcceptTokenAuthority<'info> {
/// CHECK: The constraints enforce this is valid mint authority
pub token_authority: UncheckedAccount<'info>,

pub current_authority: Signer<'info>,
#[account(
constraint = multisig_token_authority.m == 1
&& multisig_token_authority.signers.contains(&token_authority.key())
@ NTTError::InvalidMultisig,
)]
pub multisig_token_authority: Option<InterfaceAccount<'info, SplMultisig>>,

pub token_program: Interface<'info, token_interface::TokenInterface>,
}

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

pub current_authority: Signer<'info>,
}

pub fn accept_token_authority(ctx: Context<AcceptTokenAuthority>) -> Result<()> {
token_interface::set_authority(
CpiContext::new(
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(),
account_or_mint: ctx.accounts.common.mint.to_account_info(),
current_authority: ctx.accounts.current_authority.to_account_info(),
},
),
AuthorityType::MintTokens,
Some(ctx.accounts.token_authority.key()),
if let Some(multisig_token_authority) = &ctx.accounts.common.multisig_token_authority {
Some(multisig_token_authority.key())
} else {
Some(ctx.accounts.common.token_authority.key())
},
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
)
}

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

/// CHECK: The remaining accounts are treated as required signers for the multisig
pub current_multisig_authority: InterfaceAccount<'info, SplMultisig>,
}

pub fn accept_token_authority_from_multisig<'info>(
ctx: Context<'_, '_, '_, 'info, AcceptTokenAuthorityFromMultisig<'info>>,
) -> Result<()> {
let new_authority =
if let Some(multisig_token_authority) = &ctx.accounts.common.multisig_token_authority {
multisig_token_authority.to_account_info()
} else {
ctx.accounts.common.token_authority.to_account_info()
};
nvsriram marked this conversation as resolved.
Show resolved Hide resolved

let mut signer_pubkeys: Vec<&Pubkey> = Vec::new();
let mut account_infos = vec![
ctx.accounts.common.mint.to_account_info(),
new_authority.clone(),
ctx.accounts.current_multisig_authority.to_account_info(),
];

// pass ctx.remaining_accounts as required signers
{
signer_pubkeys.extend(ctx.remaining_accounts.iter().map(|x| x.key));
account_infos.extend_from_slice(ctx.remaining_accounts);
}

solana_program::program::invoke(
&spl_token_2022::instruction::set_authority(
&ctx.accounts.common.token_program.key(),
&ctx.accounts.common.mint.key(),
Some(&new_authority.key()),
spl_token_2022::instruction::AuthorityType::MintTokens,
&ctx.accounts.current_multisig_authority.key(),
&signer_pubkeys,
)?,
account_infos.as_slice(),
)?;
Ok(())
}

#[derive(Accounts)]
pub struct SetTokenAuthority<'info> {
#[account(
Expand All @@ -216,14 +278,77 @@ pub struct SetTokenAuthority<'info> {
/// CHECK: The constraints enforce this is valid mint authority
pub token_authority: UncheckedAccount<'info>,

#[account(
constraint = multisig_token_authority.m == 1
&& multisig_token_authority.signers.contains(&token_authority.key())
@ NTTError::InvalidMultisig,
)]
pub multisig_token_authority: Option<InterfaceAccount<'info, SplMultisig>>,

/// CHECK: This account will be the signer in the [claim_token_authority] instruction.
pub new_authority: UncheckedAccount<'info>,
}

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

pub token_program: Interface<'info, token_interface::TokenInterface>,
}

pub fn set_token_authority_one_step_unchecked(
ctx: Context<SetTokenAuthorityUnchecked>,
) -> Result<()> {
if let Some(multisig_token_authority) = &ctx.accounts.common.multisig_token_authority {
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
solana_program::program::invoke_signed(
&spl_token_2022::instruction::set_authority(
&ctx.accounts.token_program.key(),
&ctx.accounts.common.mint.key(),
Some(&ctx.accounts.common.new_authority.key()),
spl_token_2022::instruction::AuthorityType::MintTokens,
&multisig_token_authority.key(),
&[&ctx.accounts.common.token_authority.key()],
)?,
&[
ctx.accounts.common.mint.to_account_info(),
ctx.accounts.common.new_authority.to_account_info(),
multisig_token_authority.to_account_info(),
ctx.accounts.common.token_authority.to_account_info(),
],
&[&[
crate::TOKEN_AUTHORITY_SEED,
&[ctx.bumps.common.token_authority],
]],
)?;
} else {
token_interface::set_authority(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
token_interface::SetAuthority {
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.common.token_authority],
]],
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
),
AuthorityType::MintTokens,
Some(ctx.accounts.common.new_authority.key()),
)?;
}
Ok(())
}

#[derive(Accounts)]
pub struct SetTokenAuthorityChecked<'info> {
#[account(
constraint = common.token_authority.key() == common.mint.mint_authority.unwrap() @ NTTError::InvalidMintAuthority
constraint =
(common.multisig_token_authority.is_some() &&
common.multisig_token_authority.clone().unwrap().key() == common.mint.mint_authority.unwrap()) ||
(common.multisig_token_authority.is_none() &&
common.token_authority.key() == common.mint.mint_authority.unwrap())
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
@ NTTError::InvalidMintAuthority
)]
pub common: SetTokenAuthority<'info>,

Expand Down Expand Up @@ -253,33 +378,6 @@ pub fn set_token_authority(ctx: Context<SetTokenAuthorityChecked>) -> Result<()>
Ok(())
}

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

pub token_program: Interface<'info, token_interface::TokenInterface>,
}

pub fn set_token_authority_one_step_unchecked(
ctx: Context<SetTokenAuthorityUnchecked>,
) -> Result<()> {
token_interface::set_authority(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
token_interface::SetAuthority {
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.common.token_authority],
]],
),
AuthorityType::MintTokens,
Some(ctx.accounts.common.new_authority.key()),
)
}

// * Claim token authority

#[derive(Accounts)]
Expand All @@ -300,6 +398,13 @@ pub struct ClaimTokenAuthorityBase<'info> {
/// CHECK: The seeds constraint enforces that this is the correct address
pub token_authority: UncheckedAccount<'info>,

#[account(
constraint = multisig_token_authority.m == 1
&& multisig_token_authority.signers.contains(&token_authority.key())
@ NTTError::InvalidMultisig,
)]
pub multisig_token_authority: Option<InterfaceAccount<'info, SplMultisig>>,

#[account(mut)]
/// CHECK: the `pending_token_authority` constraint enforces that this is the correct address
pub rent_payer: UncheckedAccount<'info>,
Expand Down Expand Up @@ -343,21 +448,113 @@ pub struct ClaimTokenAuthority<'info> {
}

pub fn claim_token_authority(ctx: Context<ClaimTokenAuthority>) -> Result<()> {
token_interface::set_authority(
CpiContext::new_with_signer(
ctx.accounts.common.token_program.to_account_info(),
token_interface::SetAuthority {
account_or_mint: ctx.accounts.common.mint.to_account_info(),
current_authority: ctx.accounts.common.token_authority.to_account_info(),
},
if let Some(multisig_token_authority) = &ctx.accounts.common.multisig_token_authority {
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
solana_program::program::invoke_signed(
&spl_token_2022::instruction::set_authority(
&ctx.accounts.common.token_program.key(),
&ctx.accounts.common.mint.key(),
Some(&ctx.accounts.new_authority.key()),
spl_token_2022::instruction::AuthorityType::MintTokens,
&multisig_token_authority.key(),
&[&ctx.accounts.common.token_authority.key()],
)?,
&[
ctx.accounts.common.mint.to_account_info(),
ctx.accounts.new_authority.to_account_info(),
multisig_token_authority.to_account_info(),
ctx.accounts.common.token_authority.to_account_info(),
],
&[&[
crate::TOKEN_AUTHORITY_SEED,
&[ctx.bumps.common.token_authority],
]],
),
AuthorityType::MintTokens,
Some(ctx.accounts.new_authority.key()),
)
)?;
} else {
token_interface::set_authority(
CpiContext::new_with_signer(
ctx.accounts.common.token_program.to_account_info(),
token_interface::SetAuthority {
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.common.token_authority],
]],
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
),
AuthorityType::MintTokens,
Some(ctx.accounts.new_authority.key()),
)?;
}
Ok(())
}

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

#[account(
address = common.pending_token_authority.pending_authority @ NTTError::InvalidPendingTokenAuthority
)]
/// CHECK: The remaining accounts are treated as required signers for the multisig to be validated
pub new_multisig_authority: InterfaceAccount<'info, SplMultisig>,
}

pub fn claim_token_authority_to_multisig(
ctx: Context<ClaimTokenAuthorityToMultisig>,
) -> Result<()> {
// SPL Multisig cannot be a Signer so we simulate multisig signing using ctx.remaining_accounts as
// required signers to validate it
{
let multisig = ctx.accounts.new_multisig_authority.to_account_info();
token_interface::spl_token_2022::processor::Processor::validate_owner(
&ctx.accounts.common.token_program.key(),
&multisig.key(),
&multisig,
multisig.data_len(),
ctx.remaining_accounts,
)?;
}

if let Some(multisig_token_authority) = &ctx.accounts.common.multisig_token_authority {
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
solana_program::program::invoke_signed(
&spl_token_2022::instruction::set_authority(
&ctx.accounts.common.token_program.key(),
&ctx.accounts.common.mint.key(),
Some(&ctx.accounts.new_multisig_authority.key()),
spl_token_2022::instruction::AuthorityType::MintTokens,
&multisig_token_authority.key(),
&[&ctx.accounts.common.token_authority.key()],
)?,
&[
ctx.accounts.common.mint.to_account_info(),
ctx.accounts.new_multisig_authority.to_account_info(),
multisig_token_authority.to_account_info(),
ctx.accounts.common.token_authority.to_account_info(),
],
&[&[
crate::TOKEN_AUTHORITY_SEED,
&[ctx.bumps.common.token_authority],
]],
)?;
} else {
token_interface::set_authority(
CpiContext::new_with_signer(
ctx.accounts.common.token_program.to_account_info(),
token_interface::SetAuthority {
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.common.token_authority],
]],
nvsriram marked this conversation as resolved.
Show resolved Hide resolved
),
AuthorityType::MintTokens,
Some(ctx.accounts.new_multisig_authority.key()),
)?;
}
Ok(())
}

// * Set peers
Expand Down
16 changes: 14 additions & 2 deletions solana/programs/example-native-token-transfers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ pub mod example_native_token_transfers {
instructions::accept_token_authority(ctx)
}

pub fn set_token_authority(ctx: Context<SetTokenAuthorityChecked>) -> Result<()> {
instructions::set_token_authority(ctx)
pub fn accept_token_authority_from_multisig<'info>(
ctx: Context<'_, '_, '_, 'info, AcceptTokenAuthorityFromMultisig<'info>>,
) -> Result<()> {
instructions::accept_token_authority_from_multisig(ctx)
}

pub fn set_token_authority_one_step_unchecked(
Expand All @@ -156,6 +158,10 @@ pub mod example_native_token_transfers {
instructions::set_token_authority_one_step_unchecked(ctx)
}

pub fn set_token_authority(ctx: Context<SetTokenAuthorityChecked>) -> Result<()> {
instructions::set_token_authority(ctx)
}

pub fn revert_token_authority(ctx: Context<RevertTokenAuthority>) -> Result<()> {
instructions::revert_token_authority(ctx)
}
Expand All @@ -164,6 +170,12 @@ pub mod example_native_token_transfers {
instructions::claim_token_authority(ctx)
}

pub fn claim_token_authority_to_multisig(
ctx: Context<ClaimTokenAuthorityToMultisig>,
) -> Result<()> {
instructions::claim_token_authority_to_multisig(ctx)
}

pub fn set_paused(ctx: Context<SetPaused>, pause: bool) -> Result<()> {
instructions::set_paused(ctx, pause)
}
Expand Down
Loading
Loading