forked from dragonfly-xyz/useful-solidity-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryProofs.sol
102 lines (100 loc) · 4.3 KB
/
FactoryProofs.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract FactoryProofs {
// Validate that `deployed` was deployed by `deployer` using regular create opcode
// given the `deployNonce` it was deployed with.
function verifyDeployedBy(address deployed, address deployer, uint32 deployNonce)
external
pure
returns (bool)
{
// The address a contract will be deployed at with the create opcode is
// simply the hash of the RLP encoded deployer address + deployer nonce.
// For EOA deployers, the deploy nonce is the account's nonce
// (number of txs it has executed) when it deployed the contract.
// For contract deployers, the deploy nonce is 1 + the number of other contracts
// that contract has deployed (using regular create opcode).
address expected;
assembly {
mstore(0x02, shl(96, deployer))
let rlpNonceLength
switch gt(deployNonce, 0xFFFFFF)
case 1 { // 4 byte nonce
rlpNonceLength := 5
mstore8(0x00, 0xD8)
mstore8(0x16, 0x84)
mstore(0x17, shl(224, deployNonce))
}
default {
switch gt(deployNonce, 0xFFFF)
case 1 {
// 3 byte nonce
rlpNonceLength := 4
mstore8(0x16, 0x83)
mstore(0x17, shl(232, deployNonce))
}
default {
switch gt(deployNonce, 0xFF)
case 1 {
// 2 byte nonce
rlpNonceLength := 3
mstore8(0x16, 0x82)
mstore(0x17, shl(240, deployNonce))
}
default {
switch gt(deployNonce, 0x7F)
case 1 {
// 1 byte nonce >= 0x80
rlpNonceLength := 2
mstore8(0x16, 0x81)
mstore8(0x17, deployNonce)
}
default {
rlpNonceLength := 1
switch iszero(deployNonce)
case 1 {
// zero nonce
mstore8(0x16, 0x80)
}
default {
// 1 byte nonce < 0x80
mstore8(0x16, deployNonce)
}
}
}
}
}
mstore8(0x00, add(0xD5, rlpNonceLength))
mstore8(0x01, 0x94)
expected := and(
keccak256(0x00, add(0x16, rlpNonceLength)),
0xffffffffffffffffffffffffffffffffffffffff
)
}
return deployed == expected;
}
// Validate that `deployed` was deployed by `deployer` using create2 opcode
// given the `initCodeHash` (hash of the deployed contract's `creationCode`)
// and the `deploySalt` it was deployed with.
function verifySaltedDeployedBy(
address deployed,
address deployer,
bytes32 initCodeHash,
bytes32 deploySalt
)
external
pure
returns (bool)
{
// The address a contract will be deployed at with the create2 opcode is
// the hash of:
// '\xff' + deployer address + salt + keccak256(type(DEPLOYED_CONTRACT).creationCode)
address expected = address(uint160(uint256(keccak256(abi.encodePacked(
hex'ff',
deployer,
deploySalt,
initCodeHash
)))));
return expected == deployed;
}
}