Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Auto-detect hex encoded bytes in sha3 #4108

Merged
merged 3 commits into from
Jan 10, 2017
Merged

Auto-detect hex encoded bytes in sha3 #4108

merged 3 commits into from
Jan 10, 2017

Conversation

tomusdrw
Copy link
Collaborator

Closes #4081

@gavofyork TBH I find previous behaviour more sensible, we have couple of contexts where we explicitly don't want to interpret strings starting with 0x as bytes (e-mail addresses, solidity function names, etc.).

It would make sense if sha3 accepted ONLY 0x-prefixed, hex-encoded strings, then in most context we would use it as: sha3(asciiToHex("asdf")), right now we kind of mix both approaches.

@tomusdrw tomusdrw added A0-pleasereview 🤓 Pull request needs code review. M7-ui labels Jan 10, 2017
@@ -67,3 +68,7 @@ export function toHex (str) {

return `0x${(str || '').toLowerCase()}`;
}

export function isHex(str) {
Copy link
Contributor

Choose a reason for hiding this comment

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

util/types.js has an isHex - it is very confusing having the 2 now, not sure what the differences are and what the various uses would be - especially from an external point. In addition, this file is for formatting functions, util/types would be for testing.

@@ -28,3 +30,4 @@ export function sha3 (value, options) {

return `0x${hash}`;
}
sha3.text = (val) => sha3(val, { encoding: 'raw' });
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra line break required before, no squashing.

@jacogr jacogr added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jan 10, 2017
@tomusdrw tomusdrw added A0-pleasereview 🤓 Pull request needs code review. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Jan 10, 2017
@@ -29,6 +29,10 @@ export function isFunction (test) {
}

export function isHex (_test) {
if (!isString(_test)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 Was just wondering about this. (Apart from the use-case, makes it more robust overall)

@@ -15,6 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { range } from 'lodash';
import { isString } from './types';
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused.

@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { bytesToHex, hexToBytes, hexToAscii, bytesToAscii, asciiToHex } from './format';
import { bytesToHex, hexToBytes, hexToAscii, bytesToAscii, asciiToHex, isHex } from './format';
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused.

@gavofyork
Copy link
Contributor

gavofyork commented Jan 10, 2017

It would make sense if sha3 accepted ONLY 0x-prefixed, hex-encoded strings, then in most context we would use it as: sha3(asciiToHex("asdf")), right now we kind of mix both approaches

This was the original approach of web3 before Fabian unilaterally changed it. It is fully consistent with our RPC and I'm happy with it.

That said, I don't see any huge issue with allowing a second {encoding: 'raw'} argument to force raw ASCII/raw interpretation.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 63225b2 on js-sha3 into ** on master**.

@@ -91,7 +91,7 @@ export default class Registry {

lookupAddress (_name) {
const name = _name.toLowerCase();
const sha3 = this._api.util.sha3(name);
const sha3 = this._api.util.sha3.text(name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ugly as sin, but ok, I don't have any other suggestions atm ;)

@gavofyork
Copy link
Contributor

tests failing

@gavofyork
Copy link
Contributor

fwiw i don't think solidity function names are much of an issue since they can't start with 0.

@jacogr jacogr added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jan 10, 2017
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling a491c92 on js-sha3 into ** on master**.

@gavofyork gavofyork merged commit 7aa2af3 into master Jan 10, 2017
@gavofyork gavofyork deleted the js-sha3 branch January 10, 2017 17:56
arkpar pushed a commit that referenced this pull request Jan 10, 2017
* Auto-detect hex encoded bytes in sha3

* Using types/isHex

* Removing unused imports
gavofyork pushed a commit that referenced this pull request Jan 11, 2017
* Ignore get_price_info test by default. (#4112)

* Auto-detect hex encoded bytes in sha3 (#4108)

* Auto-detect hex encoded bytes in sha3

* Using types/isHex

* Removing unused imports

* Use binary chop to estimate gas accurately (#4100)

* Initial sketch.

* Building.

* Fix a few things.

* Fix issue, add tracing.

* Address grumbles

* Raise upper limit if needed

* Fix test.

* Fixing decoding API with signatures in names (#4125)

* Fix call/estimate_gas (#4121)

* Return 0 instead of error with out of gas on estimate_gas

* Fix stuff up.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SHA3 stuck on flawed "binary" encoding
4 participants