Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Uniques: Reset approved account after transfer #12145

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
6 changes: 6 additions & 0 deletions frame/uniques/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Account::<T, I>::insert((&dest, &collection, &item), ());
let origin = details.owner;
details.owner = dest;

// The approved account has to be reset to None, because otherwise pre-approve attack would
// be possible, where the owner can approve his second account before making the transaction
// and then claiming the item back.
details.approved = None;

Item::<T, I>::insert(&collection, &item, &details);
ItemPriceOf::<T, I>::remove(&collection, &item);

Expand Down
4 changes: 4 additions & 0 deletions frame/uniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ pub mod pallet {

/// Move an item from the sender account to another.
///
/// This resets the approved account of the item.
///
/// Origin must be Signed and the signing account must be either:
/// - the Admin of the `collection`;
/// - the Owner of the `item`;
Expand Down Expand Up @@ -914,6 +916,8 @@ pub mod pallet {
/// - `item`: The item of the item to be approved for delegated transfer.
/// - `delegate`: The account to delegate permission to transfer the item.
///
/// Important NOTE: The `approved` account gets resest after each transfer.
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
///
/// Emits `ApprovedTransfer` on success.
///
/// Weight: `O(1)`
Expand Down
16 changes: 16 additions & 0 deletions frame/uniques/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,22 @@ fn approval_lifecycle_works() {
});
}

#[test]
fn approved_account_gets_reset_after_transfer() {
new_test_ext().execute_with(|| {
assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true));
assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2));

assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3));
assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 5));
Szegoo marked this conversation as resolved.
Show resolved Hide resolved

// this shouldn't work because we have just transfered the item to another account.
assert_noop!(Uniques::transfer(Origin::signed(3), 0, 42, 4), Error::<Test>::NoPermission);
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
// The new owner can transfer fine:
assert_ok!(Uniques::transfer(Origin::signed(5), 0, 42, 6));
});
}

#[test]
fn cancel_approval_works() {
new_test_ext().execute_with(|| {
Expand Down