Skip to content

Commit

Permalink
chore(contract): remove keypackage refs, regen bindings (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc authored Jul 26, 2024
1 parent ebeabff commit f819a58
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 2,072 deletions.
12 changes: 1 addition & 11 deletions contracts/src/IScKeystore.sol
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

struct KeyPackage {
// store serialized onchain
bytes[] data;
}

struct UserInfo {
uint256[] keyPackageIndices;
bytes signaturePubKey;
}

interface IScKeystore {
function userExists(address user) external view returns (bool);

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

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

function addKeyPackage(KeyPackage calldata) external;

function getAvailableKeyPackage(address user) external view returns (KeyPackage memory);
}
41 changes: 3 additions & 38 deletions contracts/src/ScKeystore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,33 @@
pragma solidity 0.8.24;

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

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

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(address user, bytes calldata signaturePubKey, KeyPackage calldata keyPackage) external onlyOwner {
function addUser(address user, bytes calldata signaturePubKey) external onlyOwner {
if (signaturePubKey.length == 0) revert MalformedUserInfo();
if (keyPackage.data.length == 0) revert MalformedKeyPackage();
if (userExists(user)) revert UserAlreadyExists();

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

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

emit UserAdded(user, signaturePubKey);
}

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();

keyPackages.push(keyPackage);
uint256 keyPackageIndex = keyPackages.length - 1;
users[msg.sender].keyPackageIndices.push(keyPackageIndex);

emit UserKeyPackageAdded(msg.sender, keyPackageIndex);
}

function getAvailableKeyPackage(address user) external view returns (KeyPackage memory) {
UserInfo memory userInfo = users[user];
uint256 keyPackageIndex = userInfo.keyPackageIndices[userInfo.keyPackageIndices.length - 1];
return keyPackages[keyPackageIndex];
}

function getAllKeyPackagesForUser(address user) external view returns (KeyPackage[] memory) {
UserInfo memory userInfo = users[user];
KeyPackage[] memory userKeyPackages = new KeyPackage[](userInfo.keyPackageIndices.length);
for (uint256 i = 0; i < userInfo.keyPackageIndices.length; i++) {
userKeyPackages[i] = keyPackages[userInfo.keyPackageIndices[i]];
}
return userKeyPackages;
}
}
12 changes: 2 additions & 10 deletions contracts/test/ScKeystore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ contract ScKeystoreTest is Test {
}

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

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

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

function test__addUser__reverts__whenUserAlreadyExists() public {
Expand All @@ -57,12 +56,5 @@ contract ScKeystoreTest is Test {
addUser();
UserInfo memory userInfo = s.getUser(address(this));
assert(userInfo.signaturePubKey.length == 2);
assert(userInfo.keyPackageIndices.length == 1);
}

function test__getAllKeyPackagesForUser__returnsKeyPackages__whenUserExists() public {
addUser();
KeyPackage[] memory keyPackages = s.getAllKeyPackagesForUser(address(this));
assert(keyPackages.length == 1);
}
}
Loading

0 comments on commit f819a58

Please sign in to comment.