-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3226 from ethereum/issue/1961
Implements sha3Raw and soliditySha3Raw
- Loading branch information
Showing
11 changed files
with
234 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* @file sha3Raw-tests.ts | ||
* @author Samuel Furter <[email protected]> | ||
* @date 2019 | ||
*/ | ||
|
||
import BN = require('bn.js'); | ||
import {sha3Raw} from 'web3-utils'; | ||
|
||
// $ExpectType string | ||
sha3Raw('234'); | ||
// $ExpectType string | ||
sha3Raw(new BN(3)); | ||
|
||
// $ExpectError | ||
sha3Raw(['string']); | ||
// $ExpectError | ||
sha3Raw(234); | ||
// $ExpectError | ||
sha3Raw([4]); | ||
// $ExpectError | ||
sha3Raw({}); | ||
// $ExpectError | ||
sha3Raw(true); | ||
// $ExpectError | ||
sha3Raw(null); | ||
// $ExpectError | ||
sha3Raw(undefined); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* @file solidity-sha3-raw-test.ts | ||
* @author Josh Stevens <[email protected]> | ||
* @date 2018 | ||
*/ | ||
|
||
import BN = require('bn.js'); | ||
import {soliditySha3Raw} from 'web3-utils'; | ||
|
||
// $ExpectType string | ||
soliditySha3Raw('234564535', '0xfff23243', true, -10); | ||
// $ExpectType string | ||
soliditySha3Raw('Hello!%'); | ||
// $ExpectType string | ||
soliditySha3Raw('234'); | ||
// $ExpectType string | ||
soliditySha3Raw(0xea); | ||
// $ExpectType string | ||
soliditySha3Raw(new BN(3)); | ||
// $ExpectType string | ||
soliditySha3Raw({type: 'uint256', value: '234'}); | ||
// $ExpectType string | ||
soliditySha3Raw({t: 'uint', v: new BN('234')}); | ||
// $ExpectType string | ||
soliditySha3Raw({t: 'string', v: 'Hello!%'}, {t: 'int8', v: -23}, {t: 'address', v: '0x85F43D8a49eeB85d32Cf465507DD71d507100C1d'}); | ||
// $ExpectType string | ||
soliditySha3Raw('0x407D73d8a49eeb85D32Cf465507dd71d507100c1'); | ||
|
||
// $ExpectError | ||
soliditySha3Raw(['hey']); | ||
// $ExpectError | ||
soliditySha3Raw([34]); | ||
// $ExpectError | ||
soliditySha3Raw(null); | ||
// $ExpectError | ||
soliditySha3Raw(undefined); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var chai = require('chai'); | ||
var assert = chai.assert; | ||
var cjsSha3 = require('crypto-js/sha3'); | ||
var sha3Raw = require('../packages/web3-utils').sha3Raw; | ||
|
||
describe('web3.sha3Raw', function () { | ||
it('should return the sha3 hash with hex prefix', function() { | ||
assert.deepEqual(sha3Raw(''), '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'); | ||
}); | ||
|
||
it('should return sha3 with hex prefix', function() { | ||
assert.deepEqual( | ||
sha3Raw('test123'), | ||
'0x' + cjsSha3('test123', {outputLength: 256}).toString() | ||
); | ||
|
||
assert.deepEqual( | ||
sha3Raw('test(int)'), | ||
'0x' + cjsSha3('test(int)', {outputLength: 256}).toString() | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var chai = require('chai'); | ||
var assert = chai.assert; | ||
var soliditySha3Raw = require('../packages/web3-utils').soliditySha3Raw; | ||
|
||
describe('web3.soliditySha3Raw', function () { | ||
it('should return the sha3 hash of a empty string with hex prefix', function () { | ||
assert.deepEqual( | ||
soliditySha3Raw( | ||
{t: 'string', v: ''} | ||
), | ||
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' | ||
); | ||
}); | ||
|
||
it('should return the expected sha3 hash with hex prefix', function () { | ||
assert.deepEqual(soliditySha3Raw( | ||
'Hello!%', | ||
2345676856, | ||
'2342342342342342342345676856', | ||
'Hello!%', | ||
false | ||
), '0x7eb45eb9a0e1f6904514bc34c8b43e71c2e1f96f21b45ea284a0418cb351ec69'); | ||
}); | ||
}); |