-
Notifications
You must be signed in to change notification settings - Fork 25
/
ModuleStorageLib.sol
62 lines (55 loc) · 2.27 KB
/
ModuleStorageLib.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;
type StoragePointer is bytes32;
/// @title Module Storage Library
/// @notice Library for allocating and accessing ERC-4337 address-associated storage within modules.
library ModuleStorageLib {
/// @notice Allocates a memory buffer for an associated storage key, and sets the associated address and batch
/// index.
/// @param addr The address to associate with the storage key.
/// @param batchIndex The batch index to associate with the storage key.
/// @param keySize The size of the key in words, where each word is 32 bytes. Not inclusive of the address and
/// batch index.
/// @return key The allocated memory buffer.
function allocateAssociatedStorageKey(address addr, uint256 batchIndex, uint8 keySize)
internal
pure
returns (bytes memory key)
{
/// @solidity memory-safe-assembly
assembly {
// Clear any dirty upper bits of keySize to prevent overflow
keySize := and(keySize, 0xff)
// compute the total size of the buffer, include the address and batch index
let totalSize := add(64, mul(32, keySize))
// Allocate memory for the key
key := mload(0x40)
mstore(0x40, add(add(key, totalSize), 32))
mstore(key, totalSize)
// Clear any dirty upper bits of address
addr := and(addr, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
// Store the address and batch index in the key buffer
mstore(add(key, 32), addr)
mstore(add(key, 64), batchIndex)
}
}
function associatedStorageLookup(bytes memory key, bytes32 input) internal pure returns (StoragePointer ptr) {
/// @solidity memory-safe-assembly
assembly {
mstore(add(key, 96), input)
ptr := keccak256(add(key, 32), mload(key))
}
}
function associatedStorageLookup(bytes memory key, bytes32 input1, bytes32 input2)
internal
pure
returns (StoragePointer ptr)
{
/// @solidity memory-safe-assembly
assembly {
mstore(add(key, 96), input1)
mstore(add(key, 128), input2)
ptr := keccak256(add(key, 32), mload(key))
}
}
}