Skip to content

Commit

Permalink
fixed Web3Utils toHex() for Buffer input web3#3021 (web3#3076)
Browse files Browse the repository at this point in the history
* fixed Web3Utils toHex for Buffer input web3#3021

* updated toHex funcDoc and CHANGELOG.md

* used toString instead of bytesToHex

* updated packages/web3-utils/src/utils.js

Co-Authored-By: Samuel Furter <nivida@users.noreply.github.com>
  • Loading branch information
2 people authored and nachomazzara committed Jun 4, 2020
1 parent 688bc88 commit f89b3c4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Released with 1.0.0-beta.37 code base.
- Fix incorrectly populating chainId param with `net_version` when signing txs (#2378)
- regeneratorRuntime error fixed (#3058)
- Fix accessing event.name where event is undefined (#3014)
- fixed Web3Utils toHex() for Buffer input (#3021)
- Fix bubbling up tx signing errors (#2063, #3105)
- HttpProvider: CORS issue with Firefox and Safari (#2978)
- Ensure the immutability of the `tx` object passed to function `signTransaction` (#2190)
Expand Down
5 changes: 4 additions & 1 deletion packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ var hexToBytes = function(hex) {
* And even stringifys objects before.
*
* @method toHex
* @param {String|Number|BN|Object} value
* @param {String|Number|BN|Object|Buffer} value
* @param {Boolean} returnType
* @return {String}
*/
Expand All @@ -333,6 +333,9 @@ var toHex = function (value, returnType) {
return returnType ? 'bool' : value ? '0x01' : '0x00';
}

if (Buffer.isBuffer(value)) {
return '0x' + value.toString('hex');
}

if (_.isObject(value) && !isBigNumber(value) && !isBN(value)) {
return returnType ? 'string' : utf8ToHex(JSON.stringify(value));
Expand Down
1 change: 1 addition & 0 deletions test/utils.toHex.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var tests = [
{ value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ',
expected: '0x0300000035c3a8c386c3954c5d127cc29dc38ec2bec29e1a37c2abc29b05321128c390c297590a3c100000000000006521c39f642fc3b1c3b5c3ac0c3a7ac2a6c38ec2a6c2b1c3a7c2b7c3b7c38dc2a2c38bc39f07362ac28508c28ec297c3b1c29ec3b94331c38955c380c3a9321ac393c28642c28c'},
{ value: '내가 제일 잘 나가', expected:'0xeb82b4eab08020eca09cec9dbc20ec9e9820eb8298eab080'},
{ value: Buffer.from('100'), expected:'0x313030'}
];

describe('lib/utils/utils', function () {
Expand Down

0 comments on commit f89b3c4

Please sign in to comment.