Added function to generate CREATE2 addresses (#697).

This commit is contained in:
Richard Moore 2020-01-06 18:51:36 -05:00
parent a648f2bd1e
commit eb26a6d950
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 12 additions and 2 deletions

@ -3,7 +3,7 @@
// We use this for base 36 maths // We use this for base 36 maths
import { BN } from "bn.js"; 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 { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { keccak256 } from "@ethersproject/keccak256"; import { keccak256 } from "@ethersproject/keccak256";
import { encode } from "@ethersproject/rlp"; import { encode } from "@ethersproject/rlp";
@ -143,3 +143,12 @@ export function getContractAddress(transaction: { from: string, nonce: BigNumber
return getAddress(hexDataSlice(keccak256(encode([ from, nonce ])), 12)); 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))
}

@ -1,7 +1,7 @@
"use strict"; "use strict";
import { AbiCoder, defaultAbiCoder, EventFragment, FormatTypes, Fragment, FunctionFragment, Indexed, Interface, ParamType } from "@ethersproject/abi"; 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 * as base64 from "@ethersproject/base64";
import { arrayify, concat, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexValue, hexZeroPad, isHexString, joinSignature, zeroPad, splitSignature, stripZeros } from "@ethersproject/bytes"; 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"; import { hashMessage, id, isValidName, namehash } from "@ethersproject/hash";
@ -102,6 +102,7 @@ export {
getAddress, getAddress,
getIcapAddress, getIcapAddress,
getContractAddress, getContractAddress,
getCreate2Address,
isAddress, isAddress,
formatEther, formatEther,