Skip to content

Commit

Permalink
chore(sc_keystore): add Ownable to the contracts for access control (#21
Browse files Browse the repository at this point in the history
)

* forge install: openzeppelin-contracts

v5.0.2

* chore(contracts): add ownable for access control on contract

* fix: add test

* fmt

* fix: add user address instead of defaulting to msg.sender
  • Loading branch information
rymnc authored Jul 24, 2024
1 parent a90fe0e commit 82ac601
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
branch = "v1"
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "contracts/lib/openzeppelin-contracts"]
path = contracts/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
1 change: 1 addition & 0 deletions contracts/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
1 change: 1 addition & 0 deletions contracts/remappings.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
forge-std/=lib/forge-std/src/
Openzeppelin/=lib/openzeppelin-contracts/contracts
8 changes: 6 additions & 2 deletions contracts/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import { BaseScript } from "./Base.s.sol";
import { DeploymentConfig } from "./DeploymentConfig.s.sol";

contract Deploy is BaseScript {
function run() public broadcast returns (ScKeystore scKeystore, DeploymentConfig deploymentConfig) {
function run(address initialOwner)
public
broadcast
returns (ScKeystore scKeystore, DeploymentConfig deploymentConfig)
{
deploymentConfig = new DeploymentConfig(broadcaster);
scKeystore = new ScKeystore();
scKeystore = new ScKeystore(initialOwner);
}
}
6 changes: 5 additions & 1 deletion contracts/src/IScKeystore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ struct UserInfo {

interface IScKeystore {
function userExists(address user) external view returns (bool);
function addUser(bytes calldata signaturePubKey, KeyPackage calldata keyPackage) external;

function addUser(address user, bytes calldata signaturePubKey, KeyPackage calldata keyPackage) external;

function getUser(address user) external view returns (UserInfo memory);

function addKeyPackage(KeyPackage calldata) external;

function getAvailableKeyPackage(address user) external view returns (KeyPackage memory);
}
17 changes: 10 additions & 7 deletions contracts/src/ScKeystore.sol
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.24;

import { Ownable } from "Openzeppelin/access/Ownable.sol";
import { IScKeystore, UserInfo, KeyPackage } from "./IScKeystore.sol";

error UserAlreadyExists();
error MalformedKeyPackage();
error MalformedUserInfo();
error UserDoesNotExist();

contract ScKeystore is IScKeystore {
contract ScKeystore is Ownable, IScKeystore {
event UserAdded(address user, bytes signaturePubKey);
event UserKeyPackageAdded(address indexed user, uint256 index);

mapping(address user => UserInfo userInfo) private users;
KeyPackage[] private keyPackages;

constructor(address initialOwner) Ownable(initialOwner) { }

function userExists(address user) public view returns (bool) {
return users[user].signaturePubKey.length > 0;
}

function addUser(bytes calldata signaturePubKey, KeyPackage calldata keyPackage) external {
function addUser(address user, bytes calldata signaturePubKey, KeyPackage calldata keyPackage) external onlyOwner {
if (signaturePubKey.length == 0) revert MalformedUserInfo();
if (keyPackage.data.length == 0) revert MalformedKeyPackage();
if (userExists(msg.sender)) revert UserAlreadyExists();
if (userExists(user)) revert UserAlreadyExists();

keyPackages.push(keyPackage);
uint256 keyPackageIndex = keyPackages.length - 1;

users[msg.sender] = UserInfo(new uint256[](0), signaturePubKey);
users[msg.sender].signaturePubKey = signaturePubKey;
users[msg.sender].keyPackageIndices.push(keyPackageIndex);
users[user] = UserInfo(new uint256[](0), signaturePubKey);
users[user].signaturePubKey = signaturePubKey;
users[user].keyPackageIndices.push(keyPackageIndex);

emit UserAdded(msg.sender, signaturePubKey);
emit UserAdded(user, signaturePubKey);
}

function getUser(address user) external view returns (UserInfo memory) {
Expand Down
18 changes: 15 additions & 3 deletions contracts/test/ScKeystore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity >=0.8.19 <0.9.0;
import { Test } from "forge-std/Test.sol";
import { Deploy } from "../script/Deploy.s.sol";
import { DeploymentConfig } from "../script/DeploymentConfig.s.sol";
import "forge-std/console.sol";
import "../src/ScKeystore.sol"; // solhint-disable-line

contract ScKeystoreTest is Test {
Expand All @@ -13,12 +14,16 @@ contract ScKeystoreTest is Test {

function setUp() public virtual {
Deploy deployment = new Deploy();
(s, deploymentConfig) = deployment.run();
(s, deploymentConfig) = deployment.run(address(this));
}

function addUser() internal {
KeyPackage memory keyPackage = KeyPackage({ data: new bytes[](1) });
s.addUser("0x", keyPackage);
s.addUser(address(this), "0x", keyPackage);
}

function test__owner() public view {
assert(s.owner() == address(this));
}

function test__userExists__returnsFalse__whenUserDoesNotExist() public view {
Expand All @@ -27,7 +32,7 @@ contract ScKeystoreTest is Test {

function test__addUser__reverts__whenUserInfoIsMalformed() public {
vm.expectRevert(MalformedUserInfo.selector);
s.addUser("", KeyPackage({ data: new bytes[](0) }));
s.addUser(address(this), "", KeyPackage({ data: new bytes[](0) }));
}

function test__addUser__reverts__whenUserAlreadyExists() public {
Expand All @@ -41,6 +46,13 @@ contract ScKeystoreTest is Test {
assert(s.userExists(address(this)));
}

function test__addUser__reverts__whenSenderIsNotOwner() public {
vm.prank(address(0));
vm.expectRevert();
addUser();
vm.stopPrank();
}

function test__getUser__returnsUserInfo__whenUserExists() public {
addUser();
UserInfo memory userInfo = s.getUser(address(this));
Expand Down

0 comments on commit 82ac601

Please sign in to comment.