Skip to content

Commit

Permalink
fix(smart-contracts): renounce role after initialization when adding …
Browse files Browse the repository at this point in the history
…a new template (#15248)

* fix typo in initializer

* add a test

* release fixed unlock 14 contract

* add back `renounceLockManager`

* add back to iface
  • Loading branch information
clemsos authored Dec 6, 2024
1 parent 6f47f19 commit 715243a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 7 deletions.
11 changes: 9 additions & 2 deletions packages/contracts/src/abis/PublicLock/PublicLockV15.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/contracts/src/abis/Unlock/UnlockV14.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/contracts/src/contracts/PublicLock/PublicLockV15.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3146,6 +3146,10 @@ contract MixinRoles is AccessControlUpgradeable, MixinErrors {
return hasRole(LOCK_MANAGER_ROLE, account);
}

// kept for backward compat
function renounceLockManager() public {
renounceRole(LOCK_MANAGER_ROLE, msg.sender);
}

// added -1 slot for the onRoleHook address in v15
uint256[999] private __safe_upgrade_gap;
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/contracts/Unlock/UnlockV14.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2971,7 +2971,7 @@ contract Unlock is UnlockInitializable, UnlockOwnable {
// claim the template so that no-one else could
try IPublicLock(impl).initialize(address(this), 0, address(0), 0, 0, "") {
// renounce Unlock's lock manager role that was added during initialization
IPublicLock(impl).revokeRole(keccak256("LOCK_MANAGER"), address(this));
IPublicLock(impl).renounceRole(keccak256("LOCK_MANAGER"), address(this));
} catch {
// failure means that the template is already initialized
}
Expand Down
2 changes: 1 addition & 1 deletion smart-contracts/contracts/Unlock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ contract Unlock is UnlockInitializable, UnlockOwnable {
// claim the template so that no-one else could
try IPublicLock(impl).initialize(address(this), 0, address(0), 0, 0, "") {
// renounce Unlock's lock manager role that was added during initialization
IPublicLock(impl).revokeRole(keccak256("LOCK_MANAGER"), address(this));
IPublicLock(impl).renounceRole(keccak256("LOCK_MANAGER"), address(this));
} catch {
// failure means that the template is already initialized
}
Expand Down
3 changes: 3 additions & 0 deletions smart-contracts/contracts/interfaces/IPublicLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ interface IPublicLock {

function hasRole(bytes32 role, address account) external view returns (bool);

// helper function
function renounceLockManager() external;

/** `owner()` is provided as an helper to mimick the `Ownable` contract ABI.
* The `Ownable` logic is used by many 3rd party services to determine
* contract ownership - e.g. who is allowed to edit metadata on Opensea.
Expand Down
4 changes: 4 additions & 0 deletions smart-contracts/contracts/mixins/MixinRoles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ contract MixinRoles is AccessControlUpgradeable, MixinErrors {
return hasRole(LOCK_MANAGER_ROLE, account);
}

// kept for backward compat with Unlock prior v14
function renounceLockManager() public {
renounceRole(LOCK_MANAGER_ROLE, msg.sender);
}

// added -1 slot for the onRoleHook address in v15
uint256[999] private __safe_upgrade_gap;
Expand Down
8 changes: 7 additions & 1 deletion smart-contracts/test/Lock/initializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Lock / initializers', () => {
})

describe('initializing when setting as Unlock template', () => {
it('admin role is revoked when adding a template', async () => {
it('admin role is revoked when adding a template that hasnt been initialized', async () => {
// deploy contracts
const PublicLock = await ethers.getContractFactory(
'contracts/PublicLock.sol:PublicLock'
Expand All @@ -72,6 +72,12 @@ describe('Lock / initializers', () => {
false
)

// deployer should not be lock manager
await assert.equal(
await template.isLockManager(await deployer.getAddress()),
false
)

await reverts(
template.initialize(
await lockOwner.getAddress(),
Expand Down

0 comments on commit 715243a

Please sign in to comment.