Skip to content

Commit

Permalink
chore(sc_keystore): initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jun 26, 2024
1 parent c14b0a1 commit 3dd1496
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 37 deletions.
6 changes: 3 additions & 3 deletions contracts/script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19 <=0.9.0;

import { Foo } from "../src/Foo.sol";
import { ScKeystore } from "../src/ScKeystore.sol";
import { BaseScript } from "./Base.s.sol";
import { DeploymentConfig } from "./DeploymentConfig.s.sol";

contract Deploy is BaseScript {
function run() public returns (Foo foo, DeploymentConfig deploymentConfig) {
function run() public returns (ScKeystore scKeystore, DeploymentConfig deploymentConfig) {
deploymentConfig = new DeploymentConfig(broadcaster);
foo = new Foo();
scKeystore = new ScKeystore();
}
}
8 changes: 0 additions & 8 deletions contracts/src/Foo.sol

This file was deleted.

40 changes: 40 additions & 0 deletions contracts/src/ScKeystore.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.24;

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

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

contract ScKeystore is IScKeystore {
mapping(address user => UserInfo userInfo) private users;

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

function addUser(UserInfo calldata userInfo) external {
if(userInfo.signaturePubKey.length == 0) revert MalformedUserInfo();
if(userInfo.keyPackages.length != 1) revert MalformedUserInfo();
if(userExists(msg.sender)) revert UserAlreadyExists();

users[msg.sender] = userInfo;
}

function getUser(address user) external view returns (UserInfo memory) {
return users[user];
}

function addKeyPackage(KeyPackage calldata keyPackage) external {
if(keyPackage.data.length == 0) revert MalformedKeyPackage();
if(!userExists(msg.sender)) revert UserDoesNotExist();

users[msg.sender].keyPackages.push(keyPackage);
}

function getAvailableKeyPackage(address user) external view returns (KeyPackage memory) {
return users[user].keyPackages[users[user].keyPackages.length - 1];
}
}
26 changes: 0 additions & 26 deletions contracts/test/Foo.t.sol

This file was deleted.

56 changes: 56 additions & 0 deletions contracts/test/ScKeystore.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: UNLICENSED
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 "../src/ScKeystore.sol"; // solhint-disable-line

contract ScKeystoreTest is Test {
ScKeystore internal s;
DeploymentConfig internal deploymentConfig;
address internal deployer;

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

function addUser() internal {
KeyPackage[] memory keyPackages = new KeyPackage[](1);
keyPackages[0] = KeyPackage({data: new bytes[](0)});
UserInfo memory userInfo = UserInfo({
signaturePubKey: "0x",
keyPackages: keyPackages
});
s.addUser(userInfo);
}

function test__userExists__returnsFalse__whenUserDoesNotExist() public view {
assert(!s.userExists(address(this)));
}

function test__addUser__reverts__whenUserInfoIsMalformed() public {
UserInfo memory userInfo;
vm.expectRevert(MalformedUserInfo.selector);
s.addUser(userInfo);
}

function test__addUser__reverts__whenUserAlreadyExists() public {
addUser();
vm.expectRevert(UserAlreadyExists.selector);
addUser();
}

function test__addUser__addsUser__whenUserInfoIsValid() public {
addUser();
assert(s.userExists(address(this)));
}

function test__getUser__returnsUserInfo__whenUserExists() public {
addUser();
UserInfo memory userInfo = s.getUser(address(this));
assert(userInfo.signaturePubKey.length == 2);
assert(userInfo.keyPackages.length == 1);
}
}

0 comments on commit 3dd1496

Please sign in to comment.