diff --git a/.travis.yml b/.travis.yml index bb8143e9..fb6f93ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: node_js node_js: - - "4" - "6" - "8" + - "10" env: - CXX=g++-4.8 addons: diff --git a/docs/index.md b/docs/index.md index d8de4a3c..79072aea 100644 --- a/docs/index.md +++ b/docs/index.md @@ -13,6 +13,7 @@ - [fromRpcSig](#fromrpcsig) - [fromSigned](#fromsigned) - [generateAddress](#generateaddress) +- [generateAddress2](#generateaddress2) - [hashPersonalMessage](#hashpersonalmessage) - [importPublic](#importpublic) - [isPrecompiled](#isprecompiled) @@ -53,7 +54,7 @@ ## addHexPrefix -[index.js:550-556](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L550-L556 "Source code on GitHub") +[index.js:571-577](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L571-L577 "Source code on GitHub") Adds "0x" to a given `String` if it does not already start with "0x" @@ -65,7 +66,7 @@ Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## baToJSON -[index.js:599-609](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L599-L609 "Source code on GitHub") +[index.js:620-630](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L620-L630 "Source code on GitHub") Converts a `Buffer` or `Array` to JSON @@ -77,7 +78,7 @@ Returns **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## BN -[index.js:68-68](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L68-L68 "Source code on GitHub") +[index.js:68-68](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L68-L68 "Source code on GitHub") [`BN`](https://github.com/indutny/bn.js) @@ -85,7 +86,7 @@ Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Sta ## bufferToHex -[index.js:200-203](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L200-L203 "Source code on GitHub") +[index.js:200-203](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L200-L203 "Source code on GitHub") Converts a `Buffer` into a hex `String` @@ -97,7 +98,7 @@ Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## bufferToInt -[index.js:191-193](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L191-L193 "Source code on GitHub") +[index.js:191-193](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L191-L193 "Source code on GitHub") Converts a `Buffer` to a `Number` @@ -112,7 +113,7 @@ Returns **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## defineProperties -[index.js:621-714](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L621-L714 "Source code on GitHub") +[index.js:642-735](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L642-L735 "Source code on GitHub") Defines properties on a `Object`. It make the assumption that underlying data is binary. @@ -127,7 +128,7 @@ Defines properties on a `Object`. It make the assumption that underlying data is ## ecrecover -[index.js:395-403](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L395-L403 "Source code on GitHub") +[index.js:395-403](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L395-L403 "Source code on GitHub") ECDSA public key recovery from signature @@ -142,7 +143,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** publicKey ## ecsign -[index.js:364-372](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L364-L372 "Source code on GitHub") +[index.js:364-372](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L364-L372 "Source code on GitHub") ECDSA sign @@ -155,7 +156,7 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## fromRpcSig -[index.js:433-452](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L433-L452 "Source code on GitHub") +[index.js:432-451](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L432-L451 "Source code on GitHub") Convert signature format of the `eth_sign` RPC method to signature parameters NOTE: all because of a bug in geth: @@ -168,7 +169,7 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## fromSigned -[index.js:210-212](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L210-L212 "Source code on GitHub") +[index.js:210-212](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L210-L212 "Source code on GitHub") Interprets a `Buffer` as a signed integer and returns a `BN`. Assumes 256-bit numbers. @@ -180,7 +181,7 @@ Returns **BN** ## generateAddress -[index.js:519-533](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L519-L533 "Source code on GitHub") +[index.js:518-532](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L518-L532 "Source code on GitHub") Generates an address of a newly created contract @@ -191,9 +192,23 @@ Generates an address of a newly created contract Returns **[Buffer](https://nodejs.org/api/buffer.html)** +## generateAddress2 + +[index.js:541-554](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L541-L554 "Source code on GitHub") + +Generates an address for a contract created using CREATE2 + +**Parameters** + +- `from` **[Buffer](https://nodejs.org/api/buffer.html)** the address which is creating this new address +- `salt` **[Buffer](https://nodejs.org/api/buffer.html)** a salt +- `initCode` **[Buffer](https://nodejs.org/api/buffer.html)** the init code of the contract being created + +Returns **[Buffer](https://nodejs.org/api/buffer.html)** + ## hashPersonalMessage -[index.js:382-385](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L382-L385 "Source code on GitHub") +[index.js:382-385](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L382-L385 "Source code on GitHub") Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call. The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign` @@ -208,7 +223,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** hash ## importPublic -[index.js:350-356](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L350-L356 "Source code on GitHub") +[index.js:350-356](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L350-L356 "Source code on GitHub") Converts a public key to the Ethereum format. @@ -220,7 +235,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## isPrecompiled -[index.js:540-543](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L540-L543 "Source code on GitHub") +[index.js:561-564](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L561-L564 "Source code on GitHub") Returns true if the supplied address belongs to a precompiled account (Byzantium) @@ -232,7 +247,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## isValidAddress -[index.js:468-470](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L468-L470 "Source code on GitHub") +[index.js:467-469](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L467-L469 "Source code on GitHub") Checks if the address is a valid. Accepts checksummed addresses too @@ -244,7 +259,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## isValidChecksumAddress -[index.js:509-511](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L509-L511 "Source code on GitHub") +[index.js:508-510](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L508-L510 "Source code on GitHub") Checks if the address is a valid checksummed address @@ -256,7 +271,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## isValidPrivate -[index.js:293-295](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L293-L295 "Source code on GitHub") +[index.js:293-295](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L293-L295 "Source code on GitHub") Checks if the private key satisfies the rules of the curve secp256k1. @@ -268,7 +283,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## isValidPublic -[index.js:304-315](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L304-L315 "Source code on GitHub") +[index.js:304-315](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L304-L315 "Source code on GitHub") Checks if the public key satisfies the rules of the curve secp256k1 and the requirements of Ethereum. @@ -282,7 +297,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## keccak -[index.js:229-234](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L229-L234 "Source code on GitHub") +[index.js:229-234](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L229-L234 "Source code on GitHub") Creates Keccak hash of the input @@ -295,7 +310,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## keccak256 -[index.js:241-243](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L241-L243 "Source code on GitHub") +[index.js:241-243](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L241-L243 "Source code on GitHub") Creates Keccak-256 hash of the input, alias for keccak(a, 256) @@ -307,7 +322,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## privateToAddress -[index.js:459-461](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L459-L461 "Source code on GitHub") +[index.js:458-460](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L458-L460 "Source code on GitHub") Returns the ethereum address of a given private key @@ -319,7 +334,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## pubToAddress -[index.js:324-332](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L324-L332 "Source code on GitHub") +[index.js:324-332](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L324-L332 "Source code on GitHub") Returns the ethereum address of a given public key. Accepts "Ethereum public keys" and SEC1 encoded keys. @@ -333,7 +348,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## ripemd160 -[index.js:269-277](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L269-L277 "Source code on GitHub") +[index.js:269-277](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L269-L277 "Source code on GitHub") Creates RIPEMD160 hash of the input @@ -346,7 +361,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## rlp -[index.js:74-74](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L74-L74 "Source code on GitHub") +[index.js:74-74](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L74-L74 "Source code on GitHub") [`rlp`](https://github.com/ethereumjs/rlp) @@ -354,7 +369,7 @@ Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Sta ## rlphash -[index.js:284-286](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L284-L286 "Source code on GitHub") +[index.js:284-286](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L284-L286 "Source code on GitHub") Creates SHA-3 hash of the RLP encoded version of the input @@ -366,7 +381,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## secp256k1 -[index.js:80-80](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L80-L80 "Source code on GitHub") +[index.js:80-80](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L80-L80 "Source code on GitHub") [`secp256k1`](https://github.com/cryptocoinjs/secp256k1-node/) @@ -374,7 +389,7 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa ## setLengthRight -[index.js:137-139](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L137-L139 "Source code on GitHub") +[index.js:137-139](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L137-L139 "Source code on GitHub") Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes. Or it truncates the beginning if it exceeds. @@ -388,7 +403,7 @@ Returns **([Buffer](https://nodejs.org/api/buffer.html) \| [Array](https://devel ## sha256 -[index.js:258-261](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L258-L261 "Source code on GitHub") +[index.js:258-261](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L258-L261 "Source code on GitHub") Creates SHA256 hash of the input @@ -400,7 +415,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## sha3 -[index.js:251-251](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L251-L251 "Source code on GitHub") +[index.js:251-251](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L251-L251 "Source code on GitHub") Creates SHA-3 (Keccak) hash of the input [OBSOLETE] @@ -413,7 +428,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## toBuffer -[index.js:159-183](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L159-L183 "Source code on GitHub") +[index.js:159-183](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L159-L183 "Source code on GitHub") Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method. @@ -423,7 +438,7 @@ Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String ## toChecksumAddress -[index.js:488-502](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L488-L502 "Source code on GitHub") +[index.js:487-501](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L487-L501 "Source code on GitHub") Returns a checksummed address @@ -435,7 +450,7 @@ Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## toRpcSig -[index.js:412-425](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L412-L425 "Source code on GitHub") +[index.js:412-424](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L412-L424 "Source code on GitHub") Convert signature parameters into the format of `eth_sign` RPC method @@ -449,7 +464,7 @@ Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## toUnsigned -[index.js:219-221](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L219-L221 "Source code on GitHub") +[index.js:219-221](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L219-L221 "Source code on GitHub") Converts a `BN` to an unsigned integer and returns it as a `Buffer`. Assumes 256-bit numbers. @@ -461,7 +476,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## unpad -[index.js:146-154](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L146-L154 "Source code on GitHub") +[index.js:146-154](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L146-L154 "Source code on GitHub") Trims leading zeros from a `Buffer` or an `Array` @@ -473,7 +488,7 @@ Returns **([Buffer](https://nodejs.org/api/buffer.html) \| [Array](https://devel ## isValidSignature -[index.js:568-592](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L568-L592 "Source code on GitHub") +[index.js:589-613](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L589-L613 "Source code on GitHub") Validate ECDSA signature @@ -488,7 +503,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## isZeroAddress -[index.js:478-481](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L478-L481 "Source code on GitHub") +[index.js:477-480](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L477-L480 "Source code on GitHub") Checks if a given address is a zero address @@ -500,7 +515,7 @@ Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/ ## KECCAK256_NULL -[index.js:33-33](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L33-L33 "Source code on GitHub") +[index.js:33-33](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L33-L33 "Source code on GitHub") Keccak-256 hash of null (a `Buffer`) @@ -508,7 +523,7 @@ Type: [Buffer](https://nodejs.org/api/buffer.html) ## KECCAK256_NULL_S -[index.js:26-26](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L26-L26 "Source code on GitHub") +[index.js:26-26](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L26-L26 "Source code on GitHub") Keccak-256 hash of null (a `String`) @@ -516,7 +531,7 @@ Type: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa ## KECCAK256_RLP -[index.js:61-61](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L61-L61 "Source code on GitHub") +[index.js:61-61](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L61-L61 "Source code on GitHub") Keccak-256 hash of the RLP of null (a `Buffer`) @@ -524,7 +539,7 @@ Type: [Buffer](https://nodejs.org/api/buffer.html) ## KECCAK256_RLP_ARRAY -[index.js:47-47](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L47-L47 "Source code on GitHub") +[index.js:47-47](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L47-L47 "Source code on GitHub") Keccak-256 of an RLP of an empty array (a `Buffer`) @@ -532,7 +547,7 @@ Type: [Buffer](https://nodejs.org/api/buffer.html) ## KECCAK256_RLP_ARRAY_S -[index.js:40-40](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L40-L40 "Source code on GitHub") +[index.js:40-40](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L40-L40 "Source code on GitHub") Keccak-256 of an RLP of an empty array (a `String`) @@ -540,7 +555,7 @@ Type: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa ## KECCAK256_RLP_S -[index.js:54-54](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L54-L54 "Source code on GitHub") +[index.js:54-54](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L54-L54 "Source code on GitHub") Keccak-256 hash of the RLP of null (a `String`) @@ -548,7 +563,7 @@ Type: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa ## lsetLength -[index.js:112-128](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L112-L128 "Source code on GitHub") +[index.js:112-128](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L112-L128 "Source code on GitHub") Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes. Or it truncates the beginning if it exceeds. @@ -563,7 +578,7 @@ Returns **([Buffer](https://nodejs.org/api/buffer.html) \| [Array](https://devel ## MAX_INTEGER -[index.js:14-14](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L14-L14 "Source code on GitHub") +[index.js:14-14](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L14-L14 "Source code on GitHub") the max integer that this VM can handle (a `BN`) @@ -571,7 +586,7 @@ Type: BN ## privateToPublic -[index.js:339-343](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L339-L343 "Source code on GitHub") +[index.js:339-343](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L339-L343 "Source code on GitHub") Returns the ethereum public key of a given private key @@ -583,7 +598,7 @@ Returns **[Buffer](https://nodejs.org/api/buffer.html)** ## TWO_POW256 -[index.js:20-20](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L20-L20 "Source code on GitHub") +[index.js:20-20](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L20-L20 "Source code on GitHub") 2^256 (a `BN`) @@ -591,7 +606,7 @@ Type: BN ## zeroAddress -[index.js:97-101](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L97-L101 "Source code on GitHub") +[index.js:97-101](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L97-L101 "Source code on GitHub") Returns a zero address @@ -599,7 +614,7 @@ Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## zeros -[index.js:88-90](https://github.com/ethereumjs/ethereumjs-util/blob/54aa41ccb5c09de626ee358499e2efab535bc0bf/index.js#L88-L90 "Source code on GitHub") +[index.js:88-90](https://github.com/ethereumjs/ethereumjs-util/blob/00706865a1b64f1342022a4ef719df0b4beb3896/index.js#L88-L90 "Source code on GitHub") Returns a buffer filled with 0s diff --git a/index.js b/index.js index 61db6f38..c1c9e85e 100644 --- a/index.js +++ b/index.js @@ -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) + 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 diff --git a/package.json b/package.json index 725fdf79..ef09ea64 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "coverage": "npm run build:dist && istanbul cover _mocha", "coveralls": "npm run coverage && coveralls = N)', function () { - var tmp = SECP256K1_N.addn(1).toString(16) + const tmp = SECP256K1_N.addn(1).toString(16) assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), false) }) it('should work otherwise (< N)', function () { - var tmp = SECP256K1_N.subn(1).toString(16) + const tmp = SECP256K1_N.subn(1).toString(16) assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), true) }) }) describe('isValidPublic', function () { it('should fail on too short input', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), false) }) it('should fail on too big input', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d00' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d00', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), false) }) it('should fail on SEC1 key', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), false) }) it('shouldn\'t fail on SEC1 key with sanitize enabled', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), true) }) it('should fail with an invalid SEC1 public key', function () { - var pubKey = '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), false) }) it('should work with compressed keys with sanitize enabled', function () { - var pubKey = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), true) }) it('should work with sanitize enabled', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), true) }) it('should work otherwise', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), true) }) }) describe('importPublic', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' it('should work with an Ethereum public key', function () { - var tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' assert.equal(ethUtils.importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) }) it('should work with uncompressed SEC1 keys', function () { - var tmp = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const tmp = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' assert.equal(ethUtils.importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) }) it('should work with compressed SEC1 keys', function () { - var tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' + const tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' assert.equal(ethUtils.importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) }) }) describe('publicToAddress', function () { it('should produce an address given a public key', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - pubKey = Buffer.from(pubKey, 'hex') - var r = ethUtils.publicToAddress(pubKey) + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const r = ethUtils.publicToAddress(pubKey) assert.equal(r.toString('hex'), address) }) it('should produce an address given a SEC1 public key', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - pubKey = Buffer.from(pubKey, 'hex') - var r = ethUtils.publicToAddress(pubKey, true) + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const r = ethUtils.publicToAddress(pubKey, true) assert.equal(r.toString('hex'), address) }) it('shouldn\'t produce an address given an invalid SEC1 public key', function () { - var pubKey = '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.throws(function () { ethUtils.publicToAddress(pubKey, true) }) }) it('shouldn\'t produce an address given an invalid public key', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex') assert.throws(function () { ethUtils.publicToAddress(pubKey) }) @@ -347,23 +336,23 @@ describe('publicToAddress', function () { describe('publicToAddress 0x', function () { it('should produce an address given a public key', function () { - var pubKey = '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - var r = ethUtils.publicToAddress(pubKey) + const pubKey = '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const r = ethUtils.publicToAddress(pubKey) assert.equal(r.toString('hex'), address) }) }) describe('privateToPublic', function () { it('should produce a public key given a private key', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) - var r = ethUtils.privateToPublic(privateKey).toString('hex') + const pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) + const r = ethUtils.privateToPublic(privateKey).toString('hex') assert.equal(r.toString('hex'), pubKey) }) it('shouldn\'t produce a public key given an invalid private key', function () { - var privateKey1 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95, 42]) - var privateKey2 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28]) + const privateKey1 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95, 42]) + const privateKey2 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28]) assert.throws(function () { ethUtils.privateToPublic(privateKey1) }) @@ -375,37 +364,47 @@ describe('privateToPublic', function () { describe('privateToAddress', function () { it('should produce an address given a private key', function () { - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' // Our private key - var privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) - var r = ethUtils.privateToAddress(privateKey).toString('hex') + const privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) + const r = ethUtils.privateToAddress(privateKey).toString('hex') assert.equal(r.toString('hex'), address) }) }) describe('generateAddress', function () { it('should produce an address given a public key', function () { - var add = ethUtils.generateAddress('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') + const add = ethUtils.generateAddress('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') assert.equal(add.toString('hex'), '936a4295d8d74e310c0c95f0a63e53737b998d12') }) }) describe('generateAddress with hex prefix', function () { it('should produce an address given a public key', function () { - var add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') + const add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') assert.equal(add.toString('hex'), 'd658a4b8247c14868f3c512fa5cbb6e458e4a989') }) }) describe('generateAddress with nonce 0 (special case)', function () { it('should produce an address given a public key', function () { - var add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 0).toString('hex') + const add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 0).toString('hex') assert.equal(add.toString('hex'), 'bfa69ba91385206bfdd2d8b9c1a5d6c10097a85b') }) }) +describe('generateAddress2: EIP-1014 testdata examples', function () { + for (let i = 0; i <= 6; i++) { + let e = eip1014Testdata[i] + it(`${e['comment']}: should generate the addresses provided`, function () { + let result = ethUtils.generateAddress2(e['address'], e['salt'], e['initCode']) + assert.equal('0x' + result.toString('hex'), e['result']) + }) + } +}) + describe('hex prefix', function () { - var string = 'd658a4b8247c14868f3c512fa5cbb6e458e4a989' + const string = 'd658a4b8247c14868f3c512fa5cbb6e458e4a989' it('should add', function () { assert.equal(ethUtils.addHexPrefix(string), '0x' + string) }) @@ -465,7 +464,7 @@ describe('toBuffer', function () { describe('baToJSON', function () { it('should turn a array of buffers into a pure json object', function () { - var ba = [Buffer.from([0]), Buffer.from([1]), [Buffer.from([2])]] + const ba = [Buffer.from([0]), Buffer.from([1]), [Buffer.from([2])]] assert.deepEqual(ethUtils.baToJSON(ba), ['0x00', '0x01', ['0x02']]) }) it('should turn a buffers into string', function () { @@ -473,20 +472,20 @@ describe('baToJSON', function () { }) }) -var echash = Buffer.from('82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', 'hex') -var ecprivkey = Buffer.from('3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', 'hex') -var chainId = 3 // ropsten +const echash = Buffer.from('82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', 'hex') +const ecprivkey = Buffer.from('3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', 'hex') +const chainId = 3 // ropsten describe('ecsign', function () { it('should produce a signature', function () { - var sig = ethUtils.ecsign(echash, ecprivkey) + const sig = ethUtils.ecsign(echash, ecprivkey) assert.deepEqual(sig.r, Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')) assert.deepEqual(sig.s, Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')) assert.equal(sig.v, 27) }) it('should produce a signature for Ropsten testnet', function () { - var sig = ethUtils.ecsign(echash, ecprivkey, chainId) + const sig = ethUtils.ecsign(echash, ecprivkey, chainId) assert.deepEqual(sig.r, Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')) assert.deepEqual(sig.s, Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')) assert.equal(sig.v, 41) @@ -495,36 +494,36 @@ describe('ecsign', function () { describe('ecrecover', function () { it('should recover a public key', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - var v = 27 - var pubkey = ethUtils.ecrecover(echash, v, r, s) + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const v = 27 + const pubkey = ethUtils.ecrecover(echash, v, r, s) assert.deepEqual(pubkey, ethUtils.privateToPublic(ecprivkey)) }) it('should recover a public key (chainId = 3)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - var v = 41 - var pubkey = ethUtils.ecrecover(echash, v, r, s, chainId) + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const v = 41 + const pubkey = ethUtils.ecrecover(echash, v, r, s, chainId) assert.deepEqual(pubkey, ethUtils.privateToPublic(ecprivkey)) }) it('should fail on an invalid signature (v = 21)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.throws(function () { ethUtils.ecrecover(echash, 21, r, s) }) }) it('should fail on an invalid signature (v = 29)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.throws(function () { ethUtils.ecrecover(echash, 29, r, s) }) }) it('should fail on an invalid signature (swapped points)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.throws(function () { ethUtils.ecrecover(echash, 27, s, r) }) @@ -533,48 +532,48 @@ describe('ecrecover', function () { describe('hashPersonalMessage', function () { it('should produce a deterministic hash', function () { - var h = ethUtils.hashPersonalMessage(Buffer.from('Hello world')) + const h = ethUtils.hashPersonalMessage(Buffer.from('Hello world')) assert.deepEqual(h, Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex')) }) }) describe('isValidSignature', function () { it('should fail on an invalid signature (shorter r))', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(27, r, s), false) }) it('should fail on an invalid signature (shorter s))', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca', 'hex') assert.equal(ethUtils.isValidSignature(27, r, s), false) }) it('should fail on an invalid signature (v = 21)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(21, r, s), false) }) it('should fail on an invalid signature (v = 29)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(29, r, s), false) }) it('should work otherwise', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - var v = 27 + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const v = 27 assert.equal(ethUtils.isValidSignature(v, r, s), true) }) it('should work otherwise(chainId=3)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - var v = 41 + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const v = 41 assert.equal(ethUtils.isValidSignature(v, r, s, false, chainId), true) }) // FIXME: add homestead test }) -var checksumAddresses = [ +const checksumAddresses = [ // All caps '0x52908400098527886E0F7030069857D2E4169EE7', '0x8617E340B3D01FA5F11F306F4090FD50E238070D', @@ -590,8 +589,8 @@ var checksumAddresses = [ describe('.toChecksumAddress()', function () { it('should work', function () { - for (var i = 0; i < checksumAddresses.length; i++) { - var tmp = checksumAddresses[i] + for (let i = 0; i < checksumAddresses.length; i++) { + let tmp = checksumAddresses[i] assert.equal(ethUtils.toChecksumAddress(tmp.toLowerCase()), tmp) } }) @@ -599,7 +598,7 @@ describe('.toChecksumAddress()', function () { describe('.isValidChecksumAddress()', function () { it('should return true', function () { - for (var i = 0; i < checksumAddresses.length; i++) { + for (let i = 0; i < checksumAddresses.length; i++) { assert.equal(ethUtils.isValidChecksumAddress(checksumAddresses[i]), true) } }) diff --git a/test/testdata/eip1014Examples.json b/test/testdata/eip1014Examples.json new file mode 100644 index 00000000..fa66f446 --- /dev/null +++ b/test/testdata/eip1014Examples.json @@ -0,0 +1,58 @@ +[ + { + "comment": "Example 0", + "address": "0x0000000000000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0x00", + "gas": "32006", + "result": "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38" + }, + { + "comment": "Example 1", + "address": "0xdeadbeef00000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0x00", + "gas": "32006", + "result": "0xb928f69bb1d91cd65274e3c79d8986362984fda3" + }, + { + "comment": "Example 2", + "address": "0xdeadbeef00000000000000000000000000000000", + "salt": "0x000000000000000000000000feed000000000000000000000000000000000000", + "initCode": "0x00", + "gas": "32006", + "result": "0xd04116cdd17bebe565eb2422f2497e06cc1c9833" + }, + { + "comment": "Example 3", + "address": "0x0000000000000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0xdeadbeef", + "gas": "32006", + "result": "0x70f2b2914a2a4b783faefb75f459a580616fcb5e" + }, + { + "comment": "Example 4", + "address": "0x00000000000000000000000000000000deadbeef", + "salt": "0x00000000000000000000000000000000000000000000000000000000cafebabe", + "initCode": "0xdeadbeef", + "gas": "32006", + "result": "0x60f3f640a8508fc6a86d45df051962668e1e8ac7" + }, + { + "comment": "Example 5", + "address": "0x00000000000000000000000000000000deadbeef", + "salt": "0x00000000000000000000000000000000000000000000000000000000cafebabe", + "initCode": "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", + "gas": "32012", + "result": "0x1d8bfdc5d46dc4f61d6b6115972536ebe6a8854c" + }, + { + "comment": "Example 6", + "address": "0x0000000000000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0x", + "gas": "32000", + "result": "0xe33c0c7f7df4809055c3eba6c09cfe4baf1bd9e0" + } +] \ No newline at end of file