Skip to content

Commit

Permalink
Added function to generate CREATE2 addresses (#697).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 6, 2020
1 parent a648f2b commit eb26a6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/address/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// We use this for base 36 maths
import { BN } from "bn.js";

import { arrayify, hexDataSlice, isHexString, stripZeros } from "@ethersproject/bytes";
import { arrayify, BytesLike, concat, hexDataLength, hexDataSlice, isHexString, stripZeros } from "@ethersproject/bytes";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { keccak256 } from "@ethersproject/keccak256";
import { encode } from "@ethersproject/rlp";
Expand Down Expand Up @@ -143,3 +143,12 @@ export function getContractAddress(transaction: { from: string, nonce: BigNumber
return getAddress(hexDataSlice(keccak256(encode([ from, nonce ])), 12));
}

export function getCreate2Address(from: string, salt: BytesLike, initCodeHash: BytesLike): string {
if (hexDataLength(salt) !== 32) {
logger.throwArgumentError("salt must be 32 bytes", "salt", salt);
}
if (hexDataLength(initCodeHash) !== 32) {
logger.throwArgumentError("initCodeHash must be 32 bytes", "initCodeHash", initCodeHash);
}
return getAddress(hexDataSlice(keccak256(concat([ "0xff", getAddress(from), salt, initCodeHash ])), 12))
}
3 changes: 2 additions & 1 deletion packages/ethers/src.ts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import { AbiCoder, defaultAbiCoder, EventFragment, FormatTypes, Fragment, FunctionFragment, Indexed, Interface, ParamType } from "@ethersproject/abi";
import { getAddress, getContractAddress, getIcapAddress, isAddress } from "@ethersproject/address";
import { getAddress, getCreate2Address, getContractAddress, getIcapAddress, isAddress } from "@ethersproject/address";
import * as base64 from "@ethersproject/base64";
import { arrayify, concat, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexValue, hexZeroPad, isHexString, joinSignature, zeroPad, splitSignature, stripZeros } from "@ethersproject/bytes";
import { hashMessage, id, isValidName, namehash } from "@ethersproject/hash";
Expand Down Expand Up @@ -102,6 +102,7 @@ export {
getAddress,
getIcapAddress,
getContractAddress,
getCreate2Address,
isAddress,

formatEther,
Expand Down

0 comments on commit eb26a6d

Please sign in to comment.