Skip to content

Commit

Permalink
Merge pull request #2710 from princesinha19/keccack256
Browse files Browse the repository at this point in the history
Renaming sha3 to keccak256
  • Loading branch information
nivida authored Apr 23, 2019
2 parents f223499 + 2fbb438 commit 9707590
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 68 deletions.
20 changes: 10 additions & 10 deletions docs/web3-utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ Example
------------------------------------------------------------------------------

sha3
keccak256
=====================

.. code-block:: javascript
web3.utils.sha3(string)
web3.utils.keccak256(string) // ALIAS
web3.utils.keccak256(string)
web3.utils.sha3(string) // ALIAS
Will calculate the sha3 of the input.
Will calculate the keccak256 of the input.

.. note:: To mimic the sha3 behaviour of solidity use :ref:`soliditySha3 <utils-soliditysha3>`
.. note:: To mimic the keccak256 behaviour of solidity use :ref:`soliditySha3 <utils-soliditysha3>`

----------
Parameters
Expand All @@ -207,19 +207,19 @@ Example

.. code-block:: javascript
web3.utils.sha3('234'); // taken as string
web3.utils.keccak256('234'); // taken as string
> "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79"
web3.utils.sha3(new BN('234'));
web3.utils.keccak256(new BN('234'));
> "0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a"
web3.utils.sha3(234);
web3.utils.keccak256(234);
> null // can't calculate the hash of a number
web3.utils.sha3(0xea); // same as above, just the HEX representation of the number
web3.utils.keccak256(0xea); // same as above, just the HEX representation of the number
> null
web3.utils.sha3('0xea'); // will be converted to a byte array first, and then hashed
web3.utils.keccak256('0xea'); // will be converted to a byte array first, and then hashed
> "0x2f20677459120677484f7104c76deb6846a2c071f9b3152c103bb12cd54d1a4a"
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core-helpers/src/Formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export const outputLogFormatter = (log) => {
typeof log.transactionHash === 'string' &&
typeof log.logIndex === 'string'
) {
const shaId = Utils.sha3(
const shaId = Utils.keccak256(
log.blockHash.replace('0x', '') + log.transactionHash.replace('0x', '') + log.logIndex.replace('0x', '')
);

Expand Down
8 changes: 4 additions & 4 deletions packages/web3-eth-abi/src/AbiCoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class AbiCoder {
}

/**
* Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types.
* Encodes the function name to its ABI representation, which are the first 4 bytes of the keccak256 of the function name including types.
*
* @method encodeFunctionSignature
*
Expand All @@ -51,11 +51,11 @@ export default class AbiCoder {
functionName = this.utils.jsonInterfaceMethodToString(functionName);
}

return this.utils.sha3(functionName).slice(0, 10);
return this.utils.keccak256(functionName).slice(0, 10);
}

/**
* Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types.
* Encodes the function name to its ABI representation, which are the first 4 bytes of the keccak256 of the function name including types.
*
* @method encodeEventSignature
*
Expand All @@ -68,7 +68,7 @@ export default class AbiCoder {
functionName = this.utils.jsonInterfaceMethodToString(functionName);
}

return this.utils.sha3(functionName);
return this.utils.keccak256(functionName);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions packages/web3-eth-abi/tests/AbiCoderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,51 @@ describe('AbiCoderTest', () => {
});

it('calls encodeFunctionSignature with a string as parameter', () => {
Utils.sha3 = jest.fn(() => {
Utils.keccak256 = jest.fn(() => {
return '0x000000000';
});

expect(abiCoder.encodeFunctionSignature('functionName')).toEqual('0x00000000');

expect(Utils.sha3).toHaveBeenCalledWith('functionName');
expect(Utils.keccak256).toHaveBeenCalledWith('functionName');
});

it('calls encodeFunctionSignature with a object as parameter', () => {
Utils.jsonInterfaceMethodToString.mockReturnValueOnce('0x000000000');

Utils.sha3 = jest.fn(() => {
Utils.keccak256 = jest.fn(() => {
return '0x000000000';
});

expect(abiCoder.encodeFunctionSignature({})).toEqual('0x00000000');

expect(Utils.jsonInterfaceMethodToString).toHaveBeenCalledWith({});

expect(Utils.sha3).toHaveBeenCalledWith('0x000000000');
expect(Utils.keccak256).toHaveBeenCalledWith('0x000000000');
});

it('calls encodeEventSignature with a object as parameter', () => {
Utils.jsonInterfaceMethodToString.mockReturnValueOnce('0x000000000');

Utils.sha3 = jest.fn(() => {
Utils.keccak256 = jest.fn(() => {
return '0x000000000';
});

expect(abiCoder.encodeEventSignature({})).toEqual('0x000000000');

expect(Utils.jsonInterfaceMethodToString).toHaveBeenCalledWith({});

expect(Utils.sha3).toHaveBeenCalledWith('0x000000000');
expect(Utils.keccak256).toHaveBeenCalledWith('0x000000000');
});

it('calls encodeEventSignature with a string as parameter', () => {
Utils.sha3 = jest.fn(() => {
Utils.keccak256 = jest.fn(() => {
return '0x000000000';
});

expect(abiCoder.encodeEventSignature('functionName')).toEqual('0x000000000');

expect(Utils.sha3).toHaveBeenCalledWith('functionName');
expect(Utils.keccak256).toHaveBeenCalledWith('functionName');
});

it('calls encodeParameters', () => {
Expand All @@ -90,7 +90,7 @@ describe('AbiCoderTest', () => {
});

it('calls encodeFunctionCall and returns the expected string', () => {
Utils.sha3 = jest.fn(() => {
Utils.keccak256 = jest.fn(() => {
return '0x000000000';
});

Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-accounts/src/models/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import isObject from 'lodash/isObject';
import * as EthLibAccount from 'eth-lib/lib/account'; // TODO: Remove this dependency
import uuid from 'uuid';
import Hash from 'eth-lib/lib/hash';
import {isHexStrict, hexToBytes, randomHex, sha3} from 'web3-utils'; // TODO: Use the VO's of a web3-types module.
import {isHexStrict, hexToBytes, randomHex, keccak256} from 'web3-utils'; // TODO: Use the VO's of a web3-types module.
const crypto = typeof global === 'undefined' ? require('crypto-browserify') : require('crypto');

export default class Account {
Expand Down Expand Up @@ -169,7 +169,7 @@ export default class Account {
cipher.final()
]);

const mac = sha3(Buffer.concat([derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex')])).replace('0x', '');
const mac = keccak256(Buffer.concat([derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex')])).replace('0x', '');

return {
version: 3,
Expand Down Expand Up @@ -249,7 +249,7 @@ export default class Account {

const ciphertext = Buffer.from(json.crypto.ciphertext, 'hex');

const mac = sha3(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).replace('0x', '');
const mac = keccak256(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).replace('0x', '');
if (mac !== json.crypto.mac) {
throw new Error('Key derivation failed - possibly wrong password');
}
Expand Down
24 changes: 12 additions & 12 deletions packages/web3-eth-accounts/tests/src/models/AccountTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import crypto from 'crypto';
import uuid from 'uuid';
import Hash from 'eth-lib/lib/hash';
import {fromPrivate, sign, decodeSignature} from 'eth-lib/lib/account';
import {hexToBytes, isHexStrict, sha3} from 'web3-utils';
import {hexToBytes, isHexStrict, keccak256} from 'web3-utils';
import TransactionSigner from '../../__mocks__/TransactionSigner';
import Accounts from '../../../src/Accounts';
import Account from '../../../src/models/Account';
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('AccountTest', () => {

scryptsy.mockReturnValueOnce(Buffer.from('00000000000000000000000000000000'));

sha3.mockReturnValueOnce('0xmac');
keccak256.mockReturnValueOnce('0xmac');

const decipher = {
update: jest.fn(),
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('AccountTest', () => {
'dklen'
);

expect(sha3).toHaveBeenCalledWith(
expect(keccak256).toHaveBeenCalledWith(
Buffer.concat([Buffer.from('0000000000000000'), Buffer.from(json.crypto.ciphertext, 'hex')])
);

Expand Down Expand Up @@ -213,7 +213,7 @@ describe('AccountTest', () => {
privateKey: '0x0'
});

sha3.mockReturnValueOnce('0xmac');
keccak256.mockReturnValueOnce('0xmac');

const decipher = {
update: jest.fn(),
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('AccountTest', () => {

expect(crypto.pbkdf2Sync).toHaveBeenCalled();

expect(sha3).toHaveBeenCalledWith(
expect(keccak256).toHaveBeenCalledWith(
Buffer.concat([Buffer.from('0000000000000000'), Buffer.from(json.crypto.ciphertext, 'hex')])
);

Expand Down Expand Up @@ -311,7 +311,7 @@ describe('AccountTest', () => {
}
};

sha3.mockReturnValueOnce('0xmac');
keccak256.mockReturnValueOnce('0xmac');

crypto.pbkdf2Sync = jest.fn((password, salt, c, dklen, sha256) => {
expect(password).toEqual(Buffer.from(password));
Expand All @@ -333,7 +333,7 @@ describe('AccountTest', () => {

expect(crypto.pbkdf2Sync).toHaveBeenCalled();

expect(sha3).toHaveBeenCalledWith(
expect(keccak256).toHaveBeenCalledWith(
Buffer.concat([Buffer.from('0000000000000000'), Buffer.from(json.crypto.ciphertext, 'hex')])
);
});
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('AccountTest', () => {

scryptsy.mockReturnValueOnce(Buffer.from('0000000000000000'));

sha3.mockReturnValueOnce('0xmac');
keccak256.mockReturnValueOnce('0xmac');

uuid.v4.mockReturnValueOnce(0);

Expand Down Expand Up @@ -405,7 +405,7 @@ describe('AccountTest', () => {

expect(cipher.final).toHaveBeenCalled();

expect(sha3).toHaveBeenCalledWith(
expect(keccak256).toHaveBeenCalledWith(
Buffer.concat([
Buffer.from('0000000000000000').slice(16, 32),
Buffer.from(Buffer.concat([Buffer.from('0'), Buffer.from('0')]), 'hex')
Expand Down Expand Up @@ -440,7 +440,7 @@ describe('AccountTest', () => {
return Buffer.from('0000000000000000');
});

sha3.mockReturnValueOnce('0xmac');
keccak256.mockReturnValueOnce('0xmac');

uuid.v4.mockReturnValueOnce(0);

Expand Down Expand Up @@ -489,7 +489,7 @@ describe('AccountTest', () => {

expect(cipher.final).toHaveBeenCalled();

expect(sha3).toHaveBeenCalledWith(
expect(keccak256).toHaveBeenCalledWith(
Buffer.concat([
Buffer.from('0000000000000000').slice(16, 32),
Buffer.from(Buffer.concat([Buffer.from('0'), Buffer.from('0')]), 'hex')
Expand Down Expand Up @@ -534,7 +534,7 @@ describe('AccountTest', () => {
return Buffer.from('0000000000000000');
});

sha3.mockReturnValueOnce('0xmac');
keccak256.mockReturnValueOnce('0xmac');

expect(() => {
Account.fromPrivateKey('pk').toV3Keystore('password', options);
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/src/signers/TransactionSigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class TransactionSigner {

const rlpEncoded = ethTx.serialize().toString('hex');
const rawTransaction = '0x' + rlpEncoded;
const transactionHash = this.utils.sha3(rawTransaction);
const transactionHash = this.utils.keccak256(rawTransaction);

return {
messageHash: Buffer.from(ethTx.hash(false)).toString('hex'),
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is a sub package of [web3.js][repo]

This contains useful utility functions for Dapp developers.
This contains useful utility functions for Dapp developers.
Please read the [documentation][docs] for more.

## Installation
Expand All @@ -20,7 +20,7 @@ import * as Utils from 'web3-utils';

console.log(Utils);
> {
sha3: Function,
keccak256: Function,
soliditySha3: Function,
isAddress: Function,
...
Expand All @@ -36,7 +36,7 @@ console.log(asciiToHex('I have 100!'));
> "0x49206861766520313030e282ac"
```

## Types
## Types

All the typescript typings are placed in the types folder.

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-utils/src/BloomFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function codePointToInt(codePoint) {
* @returns {Boolean}
*/
function testBytes(bloom, bytes) {
const hash = utils.sha3(bytes).replace('0x', '');
const hash = utils.keccak256(bytes).replace('0x', '');

for (let i = 0; i < 12; i += 4) {
// calculate bit position in bloom filter that must be active
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-utils/src/SoliditySha3.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,5 @@ export const soliditySha3 = function() {

const hexArguments = map(arguments_, _processSoliditySha3Arguments);

return utils.sha3(`0x${hexArguments.join('')}`);
return utils.keccak256(`0x${hexArguments.join('')}`);
};
17 changes: 9 additions & 8 deletions packages/web3-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/**
* @file Utils.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @author Prince Sinha <sinhaprince013@gmail.com>
* @date 2017
*/

Expand Down Expand Up @@ -122,7 +123,7 @@ export const isAddress = (address) => {
export const checkAddressChecksum = (address) => {
// Check each case
address = address.replace(/^0x/i, '');
const addressHash = sha3(address.toLowerCase()).replace(/^0x/i, '');
const addressHash = keccak256(address.toLowerCase()).replace(/^0x/i, '');

for (let i = 0; i < 40; i++) {
// the nth letter should be uppercase if the nth digit of casemap is 1
Expand Down Expand Up @@ -466,30 +467,30 @@ export const isTopic = (topic) => {
};

/**
* Hashes values to a sha3 hash using keccak 256
* Hashes values to a keccak256 hash using keccak 256
*
* To hash a HEX string the hex must have 0x in front.
*
* @method sha3
* @return {String} the sha3 string
* @method keccak256
* @return {String} the keccak256 string
*/
const SHA3_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
const KECCAK256_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';

export const sha3 = (value) => {
export const keccak256 = (value) => {
if (isHexStrict(value) && /^0x/i.test(value.toString())) {
value = hexToBytes(value);
}

const returnValue = Hash.keccak256(value); // jshint ignore:line

if (returnValue === SHA3_NULL_S) {
if (returnValue === KECCAK256_NULL_S) {
return null;
} else {
return returnValue;
}
};
// expose the under the hood keccak256
sha3._Hash = Hash;
keccak256._Hash = Hash;

/**
* Gets the r,s,v values from a signature
Expand Down
Loading

0 comments on commit 9707590

Please sign in to comment.