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

Use TextEncoder to encode UTF-8 #3461

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 5 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ function getTestFiles(){
switch (process.env.BROWSER_BUNDLE_TEST){
case 'publishedDist': return ["packages/web3/dist/web3.min.js", "test/e2e.minified.js"]
case 'gitRepoDist': return ["dist/web3.min.js", "test/e2e.minified.js"]
default: return ["test/**/e2e*.js"]
default: return ["test/**/e2e*.js", "test/**/*tf8*.js"]
}
}

// Only loads browserified preprocessor for the logic unit tests so we can `require` stuff.
function getPreprocessors(){
if (!process.env.BROWSER_BUNDLE_TEST){
return { 'test/**/e2e*.js': [ 'browserify' ] }
return {
'test/**/e2e*.js': [ 'browserify' ],
'test/**/*tf8*.js': [ 'browserify' ]
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/web3-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"ethjs-unit": "0.1.6",
"number-to-bn": "1.7.0",
"randombytes": "^2.1.0",
"underscore": "1.9.1",
"utf8": "3.0.0"
"underscore": "1.9.1"
},
"devDependencies": {
"definitelytyped-header-parser": "^1.0.1",
Expand Down
34 changes: 21 additions & 13 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@
var _ = require('underscore');
var BN = require('bn.js');
var numberToBN = require('number-to-bn');
var utf8 = require('utf8');
var Hash = require("eth-lib/lib/hash");
var ethereumBloomFilters = require('ethereum-bloom-filters');

var utf8Encoder;
var utf8Decoder;
if (typeof TextEncoder !== 'undefined' && typeof TextDecoder !== 'undefined') {
utf8Encoder = new TextEncoder();
utf8Decoder = new TextDecoder();
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a review note: have added utf8 tests to the headless browser suite and manually verified code passes through this block. (The browser tests don't get captured in the coverage report).

var util = require('util');
utf8Encoder = new util.TextEncoder();
utf8Decoder = new util.TextDecoder();
}


/**
Expand Down Expand Up @@ -166,23 +175,22 @@ var rightPad = function (string, chars, sign) {
* @returns {String} hex representation of input string
*/
var utf8ToHex = function(str) {
str = utf8.encode(str);
var bytes = utf8Encoder.encode(str);
var hex = "";

// remove \u0000 padding from either side
str = str.replace(/^(?:\u0000)*/,'');
str = str.split("").reverse().join("");
str = str.replace(/^(?:\u0000)*/,'');
str = str.split("").reverse().join("");

for(var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
for(var i = 0; i < bytes.length; i++) {
var code = bytes[i];
// if (code !== 0) {
var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n;
// }
}

hex = hex.replace(/^(?:00)*/,'');
hex = hex.split("").reverse().join("");
hex = hex.replace(/^(?:00)*/,'');
hex = hex.split("").reverse().join("");

return "0x" + hex;
};

Expand All @@ -197,7 +205,7 @@ var hexToUtf8 = function(hex) {
if (!isHexStrict(hex))
throw new Error('The parameter "'+ hex +'" must be a valid HEX string.');

var str = "";
var bytes = [];
var code = 0;
hex = hex.replace(/^0x/i,'');

Expand All @@ -212,11 +220,11 @@ var hexToUtf8 = function(hex) {
for (var i=0; i < l; i+=2) {
code = parseInt(hex.substr(i, 2), 16);
// if (code !== 0) {
str += String.fromCharCode(code);
bytes.push(code);
// }
}

return utf8.decode(str);
return utf8Decoder.decode(Uint8Array.from(bytes));
};


Expand Down
3 changes: 2 additions & 1 deletion test/utils.toUtf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var tests = [
{ value: '0x6d79537472696e67', expected: 'myString'},
{ value: '0x6d79537472696e6700', expected: 'myString'},
{ value: '0x65787065637465642076616c7565000000000000000000000000000000000000', expected: 'expected value'},
{ value: '0x000000000000000000000000000000000000657870656374000065642076616c7565', expected: 'expect\u0000\u0000ed value'}
{ value: '0x000000000000000000000000000000000000657870656374000065642076616c7565', expected: 'expect\u0000\u0000ed value'},
{ value: '0x9f90e274657374', expected: '���test' }
];

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