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
use sha3::{Digest, Keccak256};

/// Deterministically calculates the address a contract will be deployed to using `CREATE2`.
///
/// This implements the following formula https://eips.ethereum.org/EIPS/eip-1014
pub fn calc_addr(address: &[u8; 20], salt: &[u8; 32], init_code: &[u8]) -> [u8; 20] {
    let mut hasher = Keccak256::new();
    hasher.input(init_code);

    let mut code_hash = [0; 32];
    code_hash.copy_from_slice(&hasher.result());
    calc_addr_with_hash(address, salt, &code_hash)
}

pub fn calc_addr_with_hash(address: &[u8; 20], salt: &[u8; 32], code_hash: &[u8; 32]) -> [u8; 20] {
    let mut buf = [0; 85];

    buf[0] = 0xFF;
    buf[1..21].copy_from_slice(address);
    buf[21..53].copy_from_slice(salt);
    buf[53..85].copy_from_slice(code_hash);

    let mut hasher = Keccak256::new();
    hasher.input(&buf[..]);

    let mut ret = [0; 20];
    ret.copy_from_slice(&hasher.result()[12..32]);
    ret
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn simple() {
        let addr = calc_addr(&[0; 20], &[0; 32], &[0; 1]);

        assert_eq!(
            addr.to_vec(),
            hex::decode("4D1A2e2bB4F88F0250f26Ffff098B0b30B26BF38").expect("valid addr")
        )
    }

    #[test]
    fn more_complex() {
        let mut addr = [0; 20];
        let mut salt = [0; 32];
        let init_code = hex::decode("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef").expect("valid code");

        hex::decode_to_slice("deadbeef", &mut addr[16..]).expect("valid addr");
        hex::decode_to_slice("cafebabe", &mut salt[28..]).expect("valid salt");

        let addr = calc_addr(&addr, &salt, &init_code);

        assert_eq!(
            addr.to_vec(),
            hex::decode("1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C").expect("valid addr")
        )
    }
}