From aaeb386c53d2e13c878c593628213b2886765332 Mon Sep 17 00:00:00 2001 From: William Morriss Date: Sat, 24 Feb 2018 04:28:17 -0800 Subject: [PATCH 1/5] always use fromUtf8 in toHex --- lib/utils/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 686a2d8f27f..f1c2455a589 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -269,7 +269,7 @@ var toHex = function (val) { else if(val.indexOf('0x') === 0) return val; else if (!isFinite(val)) - return fromAscii(val); + return fromUtf8(val); } return fromDecimal(val); From 2d39172a24b35cbea1088ce5a8b34d780979102d Mon Sep 17 00:00:00 2001 From: William Morriss Date: Sat, 24 Feb 2018 04:44:37 -0800 Subject: [PATCH 2/5] add korean test case --- test/utils.toHex.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/utils.toHex.js b/test/utils.toHex.js index 04483306c35..2deb980b11e 100644 --- a/test/utils.toHex.js +++ b/test/utils.toHex.js @@ -28,6 +28,7 @@ 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'}, From 47ef5412a848491c90e6fd5804ee94bdf5aee11b Mon Sep 17 00:00:00 2001 From: William Morriss Date: Sat, 24 Feb 2018 06:41:32 -0800 Subject: [PATCH 3/5] treat 0 as 00 instead of end of string --- lib/utils/utils.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index f1c2455a589..88ccb3ea7cf 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -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; @@ -269,7 +275,7 @@ var toHex = function (val) { else if(val.indexOf('0x') === 0) return val; else if (!isFinite(val)) - return fromUtf8(val); + return fromUtf8(val,1); } return fromDecimal(val); From 10dd2005b65ecb422e3405c8906d38fe987083ea Mon Sep 17 00:00:00 2001 From: William Morriss Date: Sat, 24 Feb 2018 06:44:11 -0800 Subject: [PATCH 4/5] update test case for utf8 --- test/utils.toHex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils.toHex.js b/test/utils.toHex.js index 2deb980b11e..41f6478d40e 100644 --- a/test/utils.toHex.js +++ b/test/utils.toHex.js @@ -33,7 +33,7 @@ var tests = [ { 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 () { From b41cdd6b6ed6e7a57ae39492909a58c12cc1a86a Mon Sep 17 00:00:00 2001 From: William Morriss Date: Sat, 24 Feb 2018 06:49:53 -0800 Subject: [PATCH 5/5] chinese and korean utf8 cases --- test/utils.fromUtf8.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/utils.fromUtf8.js b/test/utils.fromUtf8.js index f21a3b831d5..73bcad92d54 100644 --- a/test/utils.fromUtf8.js +++ b/test/utils.fromUtf8.js @@ -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'} ];