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

ERC-721: Check ownership in transfer_token_from #2093

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Fix the `StorageVec` type by excluding the `len_cached` field from its type info - [#2052](https://github.com/paritytech/ink/pull/2052)
- ERC-721: `transfer_token_from` now ensures the token owner is correct - [#2093](https://github.com/paritytech/ink/pull/2093)

## Version 5.0.0-rc

Expand Down
42 changes: 42 additions & 0 deletions integration-tests/erc721/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ mod erc721 {
if !self.approved_or_owner(Some(caller), id) {
return Err(Error::NotApproved)
};
if self.token_owner.get(id) != Some(*from) {
return Err(Error::NotOwner)
};
self.clear_approval(id);
self.remove_token_from(from, id)?;
self.add_token_to(to, id)?;
Expand Down Expand Up @@ -630,6 +633,45 @@ mod erc721 {
assert_eq!(erc721.burn(1), Err(Error::NotOwner));
}

#[ink::test]
fn transfer_from_fails_not_owner() {
let accounts =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
// Create a new contract instance.
let mut erc721 = Erc721::new();
// Create token Id 1 for Alice
assert_eq!(erc721.mint(1), Ok(()));
// Bob can transfer alice's tokens
assert_eq!(erc721.set_approval_for_all(accounts.bob, true), Ok(()));
// Set caller to Frank
set_caller(accounts.frank);
// Create token Id 2 for Frank
assert_eq!(erc721.mint(2), Ok(()));
// Set caller to Bob
set_caller(accounts.bob);
// Bob makes invalid call to transfer_from (Alice is token owner, not Frank)
assert_eq!(
erc721.transfer_from(accounts.frank, accounts.bob, 1),
Err(Error::NotOwner)
);
}

#[ink::test]
fn transfer_fails_not_owner() {
let accounts =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
// Create a new contract instance.
let mut erc721 = Erc721::new();
// Create token Id 1 for Alice
assert_eq!(erc721.mint(1), Ok(()));
// Bob can transfer alice's tokens
assert_eq!(erc721.set_approval_for_all(accounts.bob, true), Ok(()));
// Set caller to bob
set_caller(accounts.bob);
// Bob makes invalid call to transfer (he is not token owner, Alice is)
assert_eq!(erc721.transfer(accounts.bob, 1), Err(Error::NotOwner));
}

fn set_caller(sender: AccountId) {
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(sender);
}
Expand Down
Loading