Skip to content

Commit

Permalink
Add libraries and basic example in MinimalDelegation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric committed Feb 18, 2025
1 parent 22a7349 commit 8142cba
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
19 changes: 6 additions & 13 deletions src/MinimalDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@
pragma solidity ^0.8.23;

import {IMinimalDelegation} from "./interfaces/IMinimalDelegation.sol";
import {Key, KeyLib} from "./lib/KeyLib.sol";
import {MinimalDelegationStorageLib} from "./lib/MinimalDelegationStorageLib.sol";

contract MinimalDelegation {
constructor() {}
using KeyLib for Key;

struct MinimalDelegationStorage {
mapping(bytes32 keyHash => bytes encodedKey) keyStorage;
}

// keccak256(abi.encode(uint256(keccak256("Uniswap.MinimalDelegation")) - 1)) & ~bytes32(uint256(0xff));
bytes32 private constant MINIMAL_DELEGATION_STORAGE_LOCATION =
0x21f3d48e9724698d61a2dadd352c365013ee5d0f841f7fc54fb8a78301ee0c00;

function _getMinimalDelegationStorage() private pure returns (MinimalDelegationStorage storage $) {
assembly {
$.slot := MINIMAL_DELEGATION_STORAGE_LOCATION
}
function authorize(Key memory key) external returns (bytes32 keyHash) {
keyHash = key.hash();
MinimalDelegationStorageLib.setKeyStorage(keyHash, abi.encode(key));
}
}
22 changes: 2 additions & 20 deletions src/interfaces/IKeyManagement.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IKeyManagement {
/// @dev The type of key.
enum KeyType {
P256,
WebAuthnP256,
Secp256k1
}

struct Key {
/// @dev Unix timestamp at which the key expires (0 = never).
uint40 expiry;
/// @dev Type of key. See the {KeyType} enum.
KeyType keyType;
/// @dev Whether the key is a super admin key.
/// Super admin keys are allowed to call into super admin functions such as
/// `authorize` and `revoke` via `execute`.
bool isSuperAdmin;
/// @dev Public key in encoded form.
bytes publicKey;
}
import {Key} from "../lib/KeyLib.sol";

interface IKeyManagement {
function authorize(Key memory key) external returns (bytes32 keyHash);
function revoke(bytes32 keyHash) external;
function keyCount() external view returns (uint256);
Expand Down
28 changes: 28 additions & 0 deletions src/lib/KeyLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;

/// @dev The type of key.
enum KeyType {
P256,
WebAuthnP256,
Secp256k1
}

struct Key {
/// @dev Unix timestamp at which the key expires (0 = never).
uint40 expiry;
/// @dev Type of key. See the {KeyType} enum.
KeyType keyType;
/// @dev Whether the key is a super admin key.
/// Super admin keys are allowed to call into super admin functions such as
/// `authorize` and `revoke` via `execute`.
bool isSuperAdmin;
/// @dev Public key in encoded form.
bytes publicKey;
}

library KeyLib {
function hash(Key memory key) internal pure returns (bytes32) {
return keccak256(abi.encode(key.keyType, keccak256(key.publicKey)));
}
}
25 changes: 25 additions & 0 deletions src/lib/MinimalDelegationStorageLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;

library MinimalDelegationStorageLib {
struct MinimalDelegationStorage {
mapping(bytes32 keyHash => bytes encodedKey) keyStorage;
}

bytes32 private constant MINIMAL_DELEGATION_STORAGE_LOCATION =
0x21f3d48e9724698d61a2dadd352c365013ee5d0f841f7fc54fb8a78301ee0c00;

function _getMinimalDelegationStorage() private pure returns (MinimalDelegationStorage storage $) {
assembly {
$.slot := MINIMAL_DELEGATION_STORAGE_LOCATION
}
}

function getKeyStorage(bytes32 keyHash) internal view returns (bytes memory) {
return _getMinimalDelegationStorage().keyStorage[keyHash];
}

function setKeyStorage(bytes32 keyHash, bytes memory encodedKey) internal {
_getMinimalDelegationStorage().keyStorage[keyHash] = encodedKey;
}
}

0 comments on commit 8142cba

Please sign in to comment.