-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/ir_compiler
- Loading branch information
Showing
16 changed files
with
326 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Optional, Any | ||
import eth_typing | ||
from vyper.utils import keccak256 | ||
from eth_utils import to_canonical_address, to_checksum_address | ||
|
||
|
||
# TODO replace return type with upcoming AddressType wrapper | ||
def get_create2_address(blueprint_bytecode: bytes, deployer_address: Any, salt: bytes) -> str: | ||
_, _, initcode = parse_erc5202(blueprint_bytecode) | ||
|
||
initcode_hash = keccak256(initcode) | ||
|
||
prefix = b"\xFF" | ||
addr = to_canonical_address(deployer_address) | ||
if len(salt) != 32: | ||
raise ValueError(f"bad salt (must be bytes32): {salt}") | ||
|
||
create2_hash = keccak256(prefix + addr + salt + initcode_hash) | ||
|
||
return to_checksum_address(create2_hash[12:]) | ||
|
||
|
||
# basically copied from ERC5202 reference implementation | ||
def parse_erc5202(blueprint_bytecode: bytes) -> tuple[int, Optional[bytes], bytes]: | ||
""" | ||
Given bytecode as a sequence of bytes, parse the blueprint preamble and | ||
deconstruct the bytecode into: | ||
the ERC version, preamble data and initcode. | ||
Raises an exception if the bytecode is not a valid blueprint contract | ||
according to this ERC. | ||
arguments: | ||
blueprint_bytecode: a `bytes` object representing the blueprint bytecode | ||
returns: | ||
(version, | ||
None if <length encoding bits> is 0, otherwise the bytes of the data section, | ||
the bytes of the initcode, | ||
) | ||
""" | ||
if blueprint_bytecode[:2] != b"\xFE\x71": | ||
raise ValueError("Not a blueprint!") | ||
|
||
erc_version = (blueprint_bytecode[2] & 0b11111100) >> 2 | ||
|
||
n_length_bytes = blueprint_bytecode[2] & 0b11 | ||
if n_length_bytes == 0b11: | ||
raise ValueError("Reserved bits are set") | ||
|
||
data_length = int.from_bytes(blueprint_bytecode[3 : 3 + n_length_bytes], byteorder="big") | ||
|
||
if n_length_bytes == 0: | ||
preamble_data = None | ||
else: | ||
data_start = 3 + n_length_bytes | ||
preamble_data = blueprint_bytecode[data_start : data_start + data_length] | ||
|
||
initcode = blueprint_bytecode[3 + n_length_bytes + data_length :] | ||
|
||
if len(initcode) == 0: | ||
raise ValueError("Empty initcode!") | ||
|
||
return erc_version, preamble_data, initcode |
Oops, something went wrong.