Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed Web3Utils toHex() for Buffer input #3021 #3076

Merged
merged 7 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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