Skip to content

Commit

Permalink
6327 soliditySha3 with BigInt (#6422)
Browse files Browse the repository at this point in the history
* added support of bigint solidity encode packed

* fixture data for test with bigint

* unit test

* changelog
  • Loading branch information
jdevcs authored Sep 15, 2023
1 parent 054033f commit e4ba45c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,7 @@ Documentation:
- Dependencies updated

## [Unreleased]

### Fixed

- `soliditySha3()` with BigInt support
8 changes: 6 additions & 2 deletions packages/web3-utils/src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,12 @@ const getType = (arg: Sha3Input): [string, EncodingTypes] => {
type = 't' in arg ? arg.t : arg.type;
value = 'v' in arg ? arg.v : arg.value;

// otherwise try to guess the type
} else {
type = type.toLowerCase() === 'bigint' ? 'int' : type;
} else if (typeof arg === 'bigint') {
return ['int', arg];
}
// otherwise try to guess the type
else {
type = toHex(arg, true);
value = toHex(arg);

Expand Down
47 changes: 46 additions & 1 deletion packages/web3-utils/test/fixtures/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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/>.
*/

import { TypedObject, TypedObjectAbbreviated, Bytes } from 'web3-types';
import { TypedObject, TypedObjectAbbreviated, Bytes, Sha3Input } from 'web3-types';
import { hexToBytes } from '../../src/converters';

export const sha3Data: [Bytes, string | undefined][] = [
Expand Down Expand Up @@ -304,3 +304,48 @@ export const elementaryNameValidData: [any, string][] = [
['uint128', '128'],
['int256', '256'],
];

export const soliditySha3BigIntValidData: [Sha3Input[], string][] = [
[[3434], '0xf219fa5590f999dc677e94dd9cf99cf14103d2f4323898edb31db982d5909687'],
[[BigInt(3434)], '0xf219fa5590f999dc677e94dd9cf99cf14103d2f4323898edb31db982d5909687'],
[
[{ t: 'bigint', v: BigInt(3434) }],
'0xf219fa5590f999dc677e94dd9cf99cf14103d2f4323898edb31db982d5909687',
],

[[0], '0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563'],
[[BigInt(0)], '0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563'],
[
[{ t: 'bigint', v: BigInt(0) }],
'0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563',
],

[[90071992547409], '0x290998ceba657b344f8ee112246f32b20ecaad06d8d9ad09748de1821b9ef73e'],
[
[BigInt(90071992547409)],
'0x290998ceba657b344f8ee112246f32b20ecaad06d8d9ad09748de1821b9ef73e',
],
[
[{ t: 'bigint', v: BigInt(90071992547409) }],
'0x290998ceba657b344f8ee112246f32b20ecaad06d8d9ad09748de1821b9ef73e',
],

[['0x70696e67', 0], '0xe54a278c69f07b6b4f0736dc55c389cd2d3f31365b090c9f76a414fb51552c53'],
[
['0x70696e67', BigInt(0)],
'0xe54a278c69f07b6b4f0736dc55c389cd2d3f31365b090c9f76a414fb51552c53',
],

[['0x70696e67', 10], '0x418e921c5c859d5f560b89ad03a6672907d66de336392a5178ea69281feff40a'],
[
['0x70696e67', BigInt(10)],
'0x418e921c5c859d5f560b89ad03a6672907d66de336392a5178ea69281feff40a',
],

/*
//These hash values are generated using contract with function like:
function func90071992547409() external pure returns (bytes32) {
return keccak256(abi.encodePacked(int(90071992547409))) ;}
*/
];
7 changes: 7 additions & 0 deletions packages/web3-utils/test/unit/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
encodePackData,
encodePackedInvalidData,
keccak256ValidData,
soliditySha3BigIntValidData,
} from '../fixtures/hash';

describe('hash', () => {
Expand Down Expand Up @@ -141,5 +142,11 @@ describe('hash', () => {
);
expect(res).toBe('0x46e99868594ceb46b7cd37e4b33d635f12a7751671f8c51dd8218fa0dcf82901');
});

describe('BigInt soliditySha3', () => {
it.each(soliditySha3BigIntValidData)('%s', (input, output) => {
expect(soliditySha3(...input)).toEqual(output);
});
});
});
});

0 comments on commit e4ba45c

Please sign in to comment.