Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 9b52455

Browse files
authored
token-2022: Add delegate self-revoke (#2872)
* token-2022: Add delegate self-revoke * Address feedback
1 parent 0a0a6d9 commit 9b52455

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

token/program-2022/src/instruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ pub enum TokenInstruction {
152152
///
153153
/// * Single owner
154154
/// 0. `[writable]` The source account.
155-
/// 1. `[signer]` The source account owner.
155+
/// 1. `[signer]` The source account owner or current delegate.
156156
///
157157
/// * Multisignature owner
158158
/// 0. `[writable]` The source account.
159-
/// 1. `[]` The source account's multisignature owner.
159+
/// 1. `[]` The source account's multisignature owner or current delegate.
160160
/// 2. ..2+M `[signer]` M signer accounts
161161
Revoke,
162162
/// Sets a new authority of a mint or account.

token/program-2022/src/processor.rs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ impl Processor {
471471
pub fn process_revoke(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
472472
let account_info_iter = &mut accounts.iter();
473473
let source_account_info = next_account_info(account_info_iter)?;
474-
let owner_info = next_account_info(account_info_iter)?;
475-
let owner_info_data_len = owner_info.data_len();
474+
let authority_info = next_account_info(account_info_iter)?;
475+
let authority_info_data_len = authority_info.data_len();
476476

477477
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
478478
if source_account.is_frozen() {
@@ -481,9 +481,14 @@ impl Processor {
481481

482482
Self::validate_owner(
483483
program_id,
484-
&source_account.owner,
485-
owner_info,
486-
owner_info_data_len,
484+
match source_account.delegate {
485+
COption::Some(ref delegate) if cmp_pubkeys(authority_info.key, delegate) => {
486+
delegate
487+
}
488+
_ => &source_account.owner,
489+
},
490+
authority_info,
491+
authority_info_data_len,
487492
account_info_iter.as_slice(),
488493
)?;
489494

@@ -3248,6 +3253,36 @@ mod tests {
32483253
],
32493254
)
32503255
.unwrap();
3256+
3257+
// approve to source
3258+
do_process_instruction_dups(
3259+
approve_checked(
3260+
&program_id,
3261+
&account2_key,
3262+
&mint_key,
3263+
&account2_key,
3264+
&owner_key,
3265+
&[],
3266+
500,
3267+
2,
3268+
)
3269+
.unwrap(),
3270+
vec![
3271+
account2_info.clone(),
3272+
mint_info.clone(),
3273+
account2_info.clone(),
3274+
owner_info.clone(),
3275+
],
3276+
)
3277+
.unwrap();
3278+
3279+
// source-delegate revoke, force account2 to be a signer
3280+
let account2_info: AccountInfo = (&account2_key, true, &mut account2_account).into();
3281+
do_process_instruction_dups(
3282+
revoke(&program_id, &account2_key, &account2_key, &[]).unwrap(),
3283+
vec![account2_info.clone(), account2_info.clone()],
3284+
)
3285+
.unwrap();
32513286
}
32523287

32533288
#[test]
@@ -3453,6 +3488,44 @@ mod tests {
34533488
vec![&mut account_account, &mut owner_account],
34543489
)
34553490
.unwrap();
3491+
3492+
// approve delegate 3
3493+
do_process_instruction(
3494+
approve_checked(
3495+
&program_id,
3496+
&account_key,
3497+
&mint_key,
3498+
&delegate_key,
3499+
&owner_key,
3500+
&[],
3501+
100,
3502+
2,
3503+
)
3504+
.unwrap(),
3505+
vec![
3506+
&mut account_account,
3507+
&mut mint_account,
3508+
&mut delegate_account,
3509+
&mut owner_account,
3510+
],
3511+
)
3512+
.unwrap();
3513+
3514+
// revoke by delegate
3515+
do_process_instruction(
3516+
revoke(&program_id, &account_key, &delegate_key, &[]).unwrap(),
3517+
vec![&mut account_account, &mut delegate_account],
3518+
)
3519+
.unwrap();
3520+
3521+
// fails the second time
3522+
assert_eq!(
3523+
Err(TokenError::OwnerMismatch.into()),
3524+
do_process_instruction(
3525+
revoke(&program_id, &account_key, &delegate_key, &[]).unwrap(),
3526+
vec![&mut account_account, &mut delegate_account],
3527+
)
3528+
);
34563529
}
34573530

34583531
#[test]

0 commit comments

Comments
 (0)