Skip to content

Commit

Permalink
fix: add user address instead of defaulting to msg.sender
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jul 23, 2024
1 parent 9b23215 commit 062c06a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
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);
}
12 changes: 6 additions & 6 deletions contracts/src/ScKeystore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ contract ScKeystore is Ownable, IScKeystore {
return users[user].signaturePubKey.length > 0;
}

function addUser(bytes calldata signaturePubKey, KeyPackage calldata keyPackage) external onlyOwner {
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
4 changes: 2 additions & 2 deletions contracts/test/ScKeystore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract ScKeystoreTest is Test {

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 {
Expand All @@ -32,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 Down

0 comments on commit 062c06a

Please sign in to comment.