Skip to content

Commit

Permalink
Use fromUtf8 in web3.toHex (#1398)
Browse files Browse the repository at this point in the history
* always use fromUtf8 in toHex

* add korean test case

* treat 0 as 00 instead of end of string

* update test case for utf8

* chinese and korean utf8 cases
  • Loading branch information
wjmelements authored and frozeman committed Mar 13, 2018
1 parent 0299bc4 commit bd6a890
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,24 @@ var toAscii = function(hex) {
*
* @method fromUtf8
* @param {String} string
* @param {Number} optional padding
* @param {Boolean} allowZero to convert code point zero to 00 instead of end of string
* @returns {String} hex representation of input string
*/
var fromUtf8 = function(str) {
var fromUtf8 = function(str, allowZero) {
str = utf8.encode(str);
var hex = "";
for(var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
if (code === 0)
break;
var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n;
if (code === 0) {
if (allowZero) {
hex += '00';
} else {
break;
}
} else {
var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n;
}
}

return "0x" + hex;
Expand Down Expand Up @@ -276,7 +282,7 @@ var toHex = function (val) {
else if(val.indexOf('0x') === 0)
return val;
else if (!isFinite(val))
return fromAscii(val);
return fromUtf8(val,1);
}

return fromDecimal(val);
Expand Down
2 changes: 2 additions & 0 deletions test/utils.fromUtf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var assert = chai.assert;
var tests = [
{ value: 'myString', expected: '0x6d79537472696e67'},
{ value: 'myString\x00', expected: '0x6d79537472696e67'},
{ value: '我能吞下玻璃而不伤身体。', expected: '0xe68891e883bde5909ee4b88be78ebbe79283e8808ce4b88de4bca4e8baabe4bd93e38082'},
{ value: '나는 유리를 먹을 수 있어요. 그래도 아프지 않아요', expected: '0xeb8298eb8a9420ec9ca0eba6aceba5bc20eba8b9ec9d8420ec889820ec9e88ec96b4ec9a942e20eab7b8eb9e98eb8f8420ec9584ed9484eca78020ec958aec9584ec9a94' },
{ value: 'expected value\u0000\u0000\u0000', expected: '0x65787065637465642076616c7565'}
];

Expand Down
3 changes: 2 additions & 1 deletion test/utils.toHex.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ var tests = [
{ value: {test: 'test'}, expected: '0x7b2274657374223a2274657374227d'},
{ value: '{"test": "test"}', expected: '0x7b2274657374223a202274657374227d'},
{ value: 'myString', expected: '0x6d79537472696e67'},
{ value: '내가 제일 잘 나가', expected:'0xeb82b4eab08020eca09cec9dbc20ec9e9820eb8298eab080'},
{ value: new BigNumber(15), expected: '0xf'},
{ value: true, expected: '0x1'},
{ value: false, expected: '0x0'},
{ 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: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'}
expected: '0x0300000035c3a8c386c3954c5d127cc29dc38ec2bec29e1a37c2abc29b05321128c390c297590a3c100000000000006521c39f642fc3b1c3b5c3ac0c3a7ac2a6c38ec2a6c2b1c3a7c2b7c3b7c38dc2a2c38bc39f07362ac28508c28ec297c3b1c29ec3b94331c38955c380c3a9321ac393c28642c28c'}
];

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

0 comments on commit bd6a890

Please sign in to comment.