Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

add generateAddress2 for CREATE2 #146

Merged
merged 3 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- "4"
- "6"
- "8"
- "10"
env:
- CXX=g++-4.8
addons:
Expand Down
111 changes: 63 additions & 48 deletions docs/index.md

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,31 @@ exports.generateAddress = function (from, nonce) {
return exports.rlphash([from, nonce]).slice(-20)
}

/**
* Generates an address for a contract created using CREATE2
* @param {Buffer} from the address which is creating this new address
* @param {Buffer} salt a salt
* @param {Buffer} initCode the init code of the contract being created
* @return {Buffer}
*/
exports.generateAddress2 = function (from, salt, initCode) {
from = exports.toBuffer(from)
salt = exports.toBuffer(salt)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the length of the buffers be checked (as they should be 20 and 32, else the hashes would differ, right?)

initCode = exports.toBuffer(initCode)

assert(from.length === 20)
assert(salt.length === 32)

let address = exports.keccak256(Buffer.concat([
Buffer.from('ff', 'hex'),
from,
salt,
exports.keccak256(initCode)
]))

return address.slice(-20)
}

/**
* Returns true if the supplied address belongs to a precompiled account (Byzantium)
* @param {Buffer|String} address
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"coverage": "npm run build:dist && istanbul cover _mocha",
"coveralls": "npm run coverage && coveralls <coverage/lcov.info",
"lint": "standard",
"prepublishOnly": "npm run lint && npm run build:dist && npm run test",
"test": "npm run test:node && npm run test:browser",
"prepublishOnly": "npm run test && npm run build:dist",
"test": "npm run lint && npm run test:node && npm run test:browser",
"test:browser": "npm run build:dist && karma start karma.conf.js",
"test:node": "npm run build:dist && istanbul test mocha -- --reporter spec",
"build:dist": "babel index.js --source-root ./ -d ./dist",
Expand Down Expand Up @@ -128,7 +128,7 @@
"karma-firefox-launcher": "^1.0.0",
"karma-mocha": "^1.3.0",
"mocha": "^4.0.0",
"standard": "*"
"standard": "^10.0.0"
},
"standard": {
"globals": [
Expand Down
Loading