2022-09-16 05:58:45 +03:00
|
|
|
import { keccak256 } from "../crypto/index.js";
|
2022-11-09 10:57:02 +03:00
|
|
|
import { concat, dataSlice, getBigInt, getBytes, encodeRlp, assertArgument } from "../utils/index.js";
|
2022-09-05 23:57:11 +03:00
|
|
|
import { getAddress } from "./address.js";
|
|
|
|
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
2022-11-30 23:44:23 +03:00
|
|
|
/**
|
|
|
|
* Returns the address that would result from a ``CREATE`` for %%tx%%.
|
|
|
|
*
|
|
|
|
* This can be used to compute the address a contract will be
|
|
|
|
* deployed to by an EOA when sending a deployment transaction (i.e.
|
|
|
|
* when the ``to`` address is ``null``).
|
|
|
|
*
|
|
|
|
* This can also be used to compute the address a contract will be
|
|
|
|
* deployed to by a contract, by using the contract's address as the
|
|
|
|
* ``to`` and the contract's nonce.
|
2022-12-10 02:24:58 +03:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72";
|
|
|
|
* nonce = 5;
|
|
|
|
*
|
|
|
|
* getCreateAddress({ from, nonce });
|
|
|
|
* //_result:
|
2022-11-30 23:44:23 +03:00
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
export function getCreateAddress(tx) {
|
|
|
|
const from = getAddress(tx.from);
|
2022-09-16 05:58:45 +03:00
|
|
|
const nonce = getBigInt(tx.nonce, "tx.nonce");
|
2022-09-05 23:57:11 +03:00
|
|
|
let nonceHex = nonce.toString(16);
|
|
|
|
if (nonceHex === "0") {
|
|
|
|
nonceHex = "0x";
|
|
|
|
}
|
|
|
|
else if (nonceHex.length % 2) {
|
|
|
|
nonceHex = "0x0" + nonceHex;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nonceHex = "0x" + nonceHex;
|
|
|
|
}
|
|
|
|
return getAddress(dataSlice(keccak256(encodeRlp([from, nonceHex])), 12));
|
|
|
|
}
|
2022-11-30 23:44:23 +03:00
|
|
|
/**
|
|
|
|
* Returns the address that would result from a ``CREATE2`` operation
|
|
|
|
* with the given %%from%%, %%salt%% and %%initCodeHash%%.
|
|
|
|
*
|
|
|
|
* To compute the %%initCodeHash%% from a contract's init code, use
|
|
|
|
* the [[keccak256]] function.
|
2022-12-10 02:24:58 +03:00
|
|
|
*
|
|
|
|
* For a quick overview and example of ``CREATE2``, see [[link-ricmoo-wisps]].
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // The address of the contract
|
|
|
|
* from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72"
|
|
|
|
*
|
|
|
|
* // The salt
|
|
|
|
* salt = id("HelloWorld")
|
|
|
|
*
|
|
|
|
* // The hash of the initCode
|
|
|
|
* initCode = "0x6394198df16000526103ff60206004601c335afa6040516060f3";
|
|
|
|
* initCodeHash = keccak256(initCode)
|
|
|
|
*
|
|
|
|
* getCreate2Address(from, salt, initCodeHash)
|
|
|
|
* //_result:
|
2022-11-30 23:44:23 +03:00
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
export function getCreate2Address(_from, _salt, _initCodeHash) {
|
|
|
|
const from = getAddress(_from);
|
2022-09-16 05:58:45 +03:00
|
|
|
const salt = getBytes(_salt, "salt");
|
|
|
|
const initCodeHash = getBytes(_initCodeHash, "initCodeHash");
|
2022-11-09 10:57:02 +03:00
|
|
|
assertArgument(salt.length === 32, "salt must be 32 bytes", "salt", _salt);
|
|
|
|
assertArgument(initCodeHash.length === 32, "initCodeHash must be 32 bytes", "initCodeHash", _initCodeHash);
|
2022-09-05 23:57:11 +03:00
|
|
|
return getAddress(dataSlice(keccak256(concat(["0xff", from, salt, initCodeHash])), 12));
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=contract-address.js.map
|