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
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions js/src/api/util/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.


export function bytesToHex (bytes) {
return '0x' + bytes.map((b) => ('0' + b.toString(16)).slice(-2)).join('');
Expand Down
2 changes: 1 addition & 1 deletion js/src/api/util/format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.


describe('api/util/format', () => {
describe('bytesToHex', () => {
Expand Down
7 changes: 6 additions & 1 deletion js/src/api/util/sha3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import { keccak_256 } from 'js-sha3'; // eslint-disable-line

import { hexToBytes } from './format';
import { isHex } from './types';

export function sha3 (value, options) {
if (options && options.encoding === 'hex') {
const forceHex = options && options.encoding === 'hex';

if (forceHex || (!options && isHex(value))) {
const bytes = hexToBytes(value);
return sha3(bytes);
}
Expand All @@ -28,3 +31,5 @@ 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.

9 changes: 9 additions & 0 deletions js/src/api/util/sha3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ describe('api/util/sha3', () => {
expect(sha3('01020304', { encoding: 'hex' })).to.equal('0xa6885b3731702da62e8e4a8f584ac46a7f6822f4e2ba50fba902f67b1588d23b');
expect(sha3(Uint8Array.from([1, 2, 3, 4]))).to.equal('0xa6885b3731702da62e8e4a8f584ac46a7f6822f4e2ba50fba902f67b1588d23b');
});

it('should interpret as bytes by default', () => {
expect(sha3('0x01020304')).to.equal('0xa6885b3731702da62e8e4a8f584ac46a7f6822f4e2ba50fba902f67b1588d23b');
});

it('should force text if option is passed', () => {
expect(sha3('0x01020304', { encoding: 'raw' })).to.equal('0x16bff43de576d28857dcba65a56fc17c5e93c09bd6a709268eff8e62025ae869');
expect(sha3.text('0x01020304')).to.equal('0x16bff43de576d28857dcba65a56fc17c5e93c09bd6a709268eff8e62025ae869');
});
});
});
4 changes: 4 additions & 0 deletions js/src/api/util/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

return false;
}

if (_test.substr(0, 2) === '0x') {
return isHex(_test.slice(2));
}
Expand Down
6 changes: 6 additions & 0 deletions js/src/api/util/types.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ describe('api/util/types', () => {
it('correctly identifies non-hex values', () => {
expect(isHex('123j')).to.be.false;
});

it('correctly indentifies non-string values', () => {
expect(isHex(false)).to.be.false;
expect(isHex()).to.be.false;
expect(isHex([1, 2, 3])).to.be.false;
});
});

describe('isInstanceOf', () => {
Expand Down
2 changes: 1 addition & 1 deletion js/src/contracts/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;)


return this.getInstance().then((instance) => {
return instance.getAddress.call({}, [sha3, 'A']);
Expand Down
2 changes: 1 addition & 1 deletion js/src/dapps/registry/Lookup/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const lookup = (name, key) => (dispatch, getState) => {
name = name.toLowerCase();
dispatch(lookupStart(name, key));

getAddress.call({}, [ sha3(name), key ])
getAddress.call({}, [ sha3.text(name), key ])
.then((address) => dispatch(success('lookup', address)))
.catch((err) => {
console.error(`could not lookup ${key} for ${name}`);
Expand Down
4 changes: 2 additions & 2 deletions js/src/dapps/registry/Names/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const reserve = (name) => (dispatch, getState) => {
value: fee
};
const values = [
sha3(name)
sha3.text(name)
];

return postTx(api, reserve, options, values);
Expand Down Expand Up @@ -116,7 +116,7 @@ export const drop = (name) => (dispatch, getState) => {
};

const values = [
sha3(name)
sha3.text(name)
];

return postTx(api, drop, options, values);
Expand Down
2 changes: 1 addition & 1 deletion js/src/dapps/registry/Records/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const update = (name, key, value) => (dispatch, getState) => {
};

const values = [
sha3(name),
sha3.text(name),
key,
value
];
Expand Down
2 changes: 1 addition & 1 deletion js/src/dapps/registry/util/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
export const getOwner = (contract, name) => {
const { address, api } = contract;

const key = api.util.sha3(name) + '0000000000000000000000000000000000000000000000000000000000000001';
const key = api.util.sha3.text(name) + '0000000000000000000000000000000000000000000000000000000000000001';
const position = api.util.sha3(key, { encoding: 'hex' });

return api
Expand Down
6 changes: 3 additions & 3 deletions js/src/modals/ExecuteContract/executeContract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ const STORE = {
};

function createApi (result = true) {
const sha3 = sinon.stub().resolves('0x0000000000000000000000000000000000000000');
sha3.text = sha3;
return {
parity: {
registryAddress: sinon.stub().resolves('0x0000000000000000000000000000000000000000')
},
util: {
sha3: sinon.stub().resolves('0x0000000000000000000000000000000000000000')
}
util: { sha3 }
};
}

Expand Down
2 changes: 1 addition & 1 deletion js/src/modals/Verification/email-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class EmailVerificationStore extends VerificationStore {
super(api, EmailVerificationABI, EMAIL_VERIFICATION, account, isTestnet);
}

requestValues = () => [ sha3(this.email) ]
requestValues = () => [ sha3.text(this.email) ]

@action setEmail = (email) => {
this.email = email;
Expand Down
4 changes: 2 additions & 2 deletions js/src/modals/Verification/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class VerificationStore {

const confirm = contract.functions.find((fn) => fn.name === 'confirm');
const options = { from: account };
const values = [ sha3(code) ];
const values = [ sha3.text(code) ];

this.code = code;
this.isCodeValid = null;
Expand Down Expand Up @@ -192,7 +192,7 @@ export default class VerificationStore {

@action sendConfirmation = () => {
const { api, account, contract, code } = this;
const token = sha3(code);
const token = sha3.text(code);

const confirm = contract.functions.find((fn) => fn.name === 'confirm');
const options = { from: account };
Expand Down
4 changes: 2 additions & 2 deletions js/src/ui/Form/AddressSelect/addressSelectStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class AddressSelectStore {
return emailVerification
.instance
.reverse
.call({}, [ sha3(email) ])
.call({}, [ sha3.text(email) ])
.then((address) => {
return {
address,
Expand All @@ -68,7 +68,7 @@ export default class AddressSelectStore {
this.regLookups.push((name) => {
return registryInstance
.getAddress
.call({}, [ sha3(name), 'A' ])
.call({}, [ sha3.text(name), 'A' ])
.then((address) => {
return {
address,
Expand Down
2 changes: 1 addition & 1 deletion js/src/views/Settings/Background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Background extends Component {
generateSeed () {
const { api, muiTheme } = this.context;

return api.util.sha3(`${muiTheme.backgroundSeed}${Math.random()}${counter++}`);
return api.util.sha3.text(`${muiTheme.backgroundSeed}${Math.random()}${counter++}`);
}
}

Expand Down