-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScrollBadgeTokenOwner.sol
77 lines (60 loc) · 2.86 KB
/
ScrollBadgeTokenOwner.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {Attestation} from "@eas/contracts/IEAS.sol";
import {IERC721Metadata} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ScrollBadgeCustomPayload} from "../extensions/ScrollBadgeCustomPayload.sol";
import {ScrollBadgeSelfAttest} from "../extensions/ScrollBadgeSelfAttest.sol";
import {ScrollBadgeSingleton} from "../extensions/ScrollBadgeSingleton.sol";
import {ScrollBadge} from "../ScrollBadge.sol";
import {Unauthorized} from "../../Errors.sol";
string constant SCROLL_BADGE_NFT_OWNER_SCHEMA = "address tokenAddress, uint256 tokenId";
function decodePayloadData(bytes memory data) pure returns (address, uint256) {
return abi.decode(data, (address, uint256));
}
/// @title ScrollBadgeTokenOwner
/// @notice A simple badge that attests that the user owns a specific NFT.
contract ScrollBadgeTokenOwner is ScrollBadgeCustomPayload, ScrollBadgeSelfAttest, ScrollBadgeSingleton {
error IncorrectBadgeOwner();
mapping(address => bool) public isTokenAllowed;
constructor(address resolver_, address[] memory tokens_) ScrollBadge(resolver_) {
for (uint256 i = 0; i < tokens_.length; ++i) {
isTokenAllowed[tokens_[i]] = true;
}
}
/// @inheritdoc ScrollBadge
function onIssueBadge(
Attestation calldata attestation
) internal override(ScrollBadgeCustomPayload, ScrollBadgeSelfAttest, ScrollBadgeSingleton) returns (bool) {
if (!super.onIssueBadge(attestation)) {
return false;
}
// check that badge payload attestation is correct
bytes memory payload = getPayload(attestation);
(address tokenAddress, uint256 tokenId) = decodePayloadData(payload);
if (!isTokenAllowed[tokenAddress]) {
revert Unauthorized();
}
if (IERC721(tokenAddress).ownerOf(tokenId) != attestation.recipient) {
revert IncorrectBadgeOwner();
}
return true;
}
/// @inheritdoc ScrollBadge
function onRevokeBadge(
Attestation calldata attestation
) internal override(ScrollBadgeCustomPayload, ScrollBadgeSelfAttest, ScrollBadgeSingleton) returns (bool) {
return super.onRevokeBadge(attestation);
}
/// @inheritdoc ScrollBadge
function badgeTokenURI(bytes32 uid) public view override returns (string memory) {
Attestation memory attestation = getAndValidateBadge(uid);
bytes memory payload = getPayload(attestation);
(address tokenAddress, uint256 tokenId) = decodePayloadData(payload);
return IERC721Metadata(tokenAddress).tokenURI(tokenId);
}
/// @inheritdoc ScrollBadgeCustomPayload
function getSchema() public pure override returns (string memory) {
return SCROLL_BADGE_NFT_OWNER_SCHEMA;
}
}