Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 2e84152

Browse files
akhil2015nivida
andcommitted
fixed Web3Utils toHex() for Buffer input #3021 (#3076)
* fixed Web3Utils toHex for Buffer input #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>
1 parent 02c0346 commit 2e84152

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Released with 1.0.0-beta.37 code base.
6767
- Fix incorrectly populating chainId param with `net_version` when signing txs (#2378)
6868
- regeneratorRuntime error fixed (#3058)
6969
- Fix accessing event.name where event is undefined (#3014)
70+
- fixed Web3Utils toHex() for Buffer input (#3021)
7071
- Fix bubbling up tx signing errors (#2063, #3105)
7172
- HttpProvider: CORS issue with Firefox and Safari (#2978)
7273
- Ensure the immutability of the `tx` object passed to function `signTransaction` (#2190)

packages/web3-utils/src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ var hexToBytes = function(hex) {
318318
* And even stringifys objects before.
319319
*
320320
* @method toHex
321-
* @param {String|Number|BN|Object} value
321+
* @param {String|Number|BN|Object|Buffer} value
322322
* @param {Boolean} returnType
323323
* @return {String}
324324
*/
@@ -333,6 +333,9 @@ var toHex = function (value, returnType) {
333333
return returnType ? 'bool' : value ? '0x01' : '0x00';
334334
}
335335

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

337340
if (_.isObject(value) && !isBigNumber(value) && !isBN(value)) {
338341
return returnType ? 'string' : utf8ToHex(JSON.stringify(value));

test/utils.toHex.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var tests = [
4242
{ 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Œ',
4343
expected: '0x0300000035c3a8c386c3954c5d127cc29dc38ec2bec29e1a37c2abc29b05321128c390c297590a3c100000000000006521c39f642fc3b1c3b5c3ac0c3a7ac2a6c38ec2a6c2b1c3a7c2b7c3b7c38dc2a2c38bc39f07362ac28508c28ec297c3b1c29ec3b94331c38955c380c3a9321ac393c28642c28c'},
4444
{ value: '내가 제일 잘 나가', expected:'0xeb82b4eab08020eca09cec9dbc20ec9e9820eb8298eab080'},
45+
{ value: Buffer.from('100'), expected:'0x313030'}
4546
];
4647

4748
describe('lib/utils/utils', function () {

0 commit comments

Comments
 (0)