contracts/lib/ERC6551AccountLib.sol 1.4 K raw
1
// SPDX-License-Identifier: MIT
2
pragma solidity ^0.8.20;
3
4
import "@openzeppelin/contracts/utils/Create2.sol";
5
import "./ERC6551BytecodeLib.sol";
6
7
library ERC6551AccountLib {
8
    function computeAddress(
9
        address registry,
10
        address implementation,
11
        uint256 chainId,
12
        address tokenContract,
13
        uint256 tokenId,
14
        uint256 _salt
15
    ) internal pure returns (address) {
16
        bytes32 bytecodeHash = keccak256(
17
            ERC6551BytecodeLib.getCreationCode(
18
                implementation,
19
                chainId,
20
                tokenContract,
21
                tokenId,
22
                _salt
23
            )
24
        );
25
26
        return Create2.computeAddress(bytes32(_salt), bytecodeHash, registry);
27
    }
28
29
    function token() internal view returns (uint256, address, uint256) {
30
        bytes memory footer = new bytes(0x60);
31
32
        assembly {
33
            // copy 0x60 bytes from end of footer
34
            extcodecopy(address(), add(footer, 0x20), 0x4d, 0x60)
35
        }
36
37
        return abi.decode(footer, (uint256, address, uint256));
38
    }
39
40
    function salt() internal view returns (uint256) {
41
        bytes memory footer = new bytes(0x20);
42
43
        assembly {
44
            // copy 0x20 bytes from beginning of footer
45
            extcodecopy(address(), add(footer, 0x20), 0x2d, 0x20)
46
        }
47
48
        return abi.decode(footer, (uint256));
49
    }
50
}