Skip to content

Commit

Permalink
feat: auto attach self-minted badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Thegaram committed Mar 28, 2024
1 parent 9f00889 commit a63a07a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 40 deletions.
4 changes: 0 additions & 4 deletions src/interfaces/IScrollBadgeResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,4 @@ interface IScrollBadgeResolver {
/// @param uid The attestation UID.
/// @return The attestation.
function getAndValidateBadge(bytes32 uid) external view returns (Attestation memory);

/// @notice Mapping from badge address to auto attach whitelist status.
/// @param badge The address of the badge contract.
function isBadgeAutoAttach(address badge) external view returns (bool);
}
28 changes: 3 additions & 25 deletions src/resolver/ScrollBadgeResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ contract ScrollBadgeResolver is IScrollBadgeResolver, SchemaResolver, ScrollBadg
/// @inheritdoc IScrollBadgeResolver
address public immutable registry;

/**
*
* Variables *
*
*/

/// @inheritdoc IScrollBadgeResolver
mapping(address => bool) public isBadgeAutoAttach;

/**
*
* Constructor *
Expand Down Expand Up @@ -113,8 +104,9 @@ contract ScrollBadgeResolver is IScrollBadgeResolver, SchemaResolver, ScrollBadg
return false;
}

// auto-attach whitelisted badges
if (isBadgeAutoAttach[badge]) {
// auto-attach self-minted badges
// note: in some cases attestation.attester is a proxy, so we also check tx.origin.
if (attestation.recipient == attestation.attester || attestation.recipient == tx.origin) {
_autoAttach(attestation);
}

Expand Down Expand Up @@ -178,20 +170,6 @@ contract ScrollBadgeResolver is IScrollBadgeResolver, SchemaResolver, ScrollBadg
return attestation;
}

/**
*
* Restricted Functions *
*
*/

/// @notice Enable or disable auto-attach for a badge.
/// @param badge The address of the badge contract.
/// @param enable Enable auto-attach if true, disable if false.
function toggleBadgeAutoAttach(address badge, bool enable) external onlyOwner {
isBadgeAutoAttach[badge] = enable;
emit UpdateAutoAttachWhitelist(badge, enable);
}

/**
*
* Internal Functions *
Expand Down
10 changes: 7 additions & 3 deletions test/Profile.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ contract ProfileRegistryTest is Test {
error TokenNotOwnedByUser(address token, uint256 tokenId);
error Unauthorized();

address internal constant attester = address(1);

address private constant TREASURY_ADDRESS = 0x1000000000000000000000000000000000000000;

address private constant PROXY_ADMIN_ADDRESS = 0x2000000000000000000000000000000000000000;
Expand Down Expand Up @@ -144,15 +146,16 @@ contract ProfileRegistryTest is Test {
vm.prank(address(1));
vm.expectRevert(Unauthorized.selector);
_attachOne(bytes32(0));
vm.stopPrank();

// revert when invalid badge
bytes32 invalidUID = _attest(address(badge), "invalidUID", address(badge));
vm.expectRevert(abi.encodeWithSelector(AttestationOwnerMismatch.selector, invalidUID));
_attachOne(invalidUID);

vm.startPrank(attester);
bytes32 uid0 = _attest(address(badge), "1", address(this));
bytes32 uid1 = _attest(address(badge), "2", address(this));
vm.stopPrank();
// attach one badge
_attachOne(uid0);
bytes32[] memory badges = profile.getAttachedBadges();
Expand Down Expand Up @@ -182,7 +185,9 @@ contract ProfileRegistryTest is Test {

// attach 46 badges
for (uint256 i = 1; i <= 46; ++i) {
vm.startPrank(attester);
bytes32 uid = _attest(address(badge), abi.encodePacked(i), address(this));
vm.stopPrank();
_attachOne(uid);
}
badges = profile.getAttachedBadges();
Expand Down Expand Up @@ -377,8 +382,7 @@ contract ProfileRegistryTest is Test {
bytes32[] memory badges = profile.getAttachedBadges();
assertEq(badges.length, 0);

resolver.toggleBadgeAutoAttach(address(badge), true);

vm.prank(address(this));
_attest(address(badge), "1", address(this));

badges = profile.getAttachedBadges();
Expand Down
7 changes: 0 additions & 7 deletions test/ScrollBadgeResolver.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ contract ScrollBadgeTest is ScrollBadgeTestBase {
resolver.toggleWhitelist(enable);
}

function testResolverToggleAutoAttachOnlyOwner(address notOwner, address anyBadge, bool enable) external {
vm.assume(notOwner != address(this));
vm.prank(notOwner);
vm.expectRevert("Ownable: caller is not the owner");
resolver.toggleBadgeAutoAttach(anyBadge, enable);
}

function testGetBadge() external {
bytes32 uid = _attest(address(badge), "", alice);
Attestation memory attestation = resolver.getAndValidateBadge(uid);
Expand Down
5 changes: 4 additions & 1 deletion test/ScrollBadgeTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "@eas/contracts/IEAS.sol";

import {ScrollBadgeResolver} from "../src/resolver/ScrollBadgeResolver.sol";
import {ProfileRegistry} from "../src/profile/ProfileRegistry.sol";

contract ScrollBadgeTestBase is Test {
ISchemaRegistry internal registry;
Expand All @@ -35,7 +36,9 @@ contract ScrollBadgeTestBase is Test {
eas = new EAS(registry);

// Scroll components
address profileRegistry = address(0);
// no need to initialize the registry, since resolver
// only uses it to see if a profile has been minted or not.
address profileRegistry = address(new ProfileRegistry());
resolver = new ScrollBadgeResolver(address(eas), profileRegistry);
schema = resolver.schema();
}
Expand Down

0 comments on commit a63a07a

Please sign in to comment.