fixed utf8 encoding/decoding in solidity params

This commit is contained in:
Marek Kotewicz 2015-07-21 11:35:52 +02:00
parent d163c9dee4
commit 4a7edb5ad8
4 changed files with 29 additions and 18 deletions

View File

@ -62,10 +62,10 @@ var formatInputBytes = function (value) {
* @returns {SolidityParam}
*/
var formatInputDynamicBytes = function (value) {
value = utils.toHex(value).substr(2);
var l = Math.floor((value.length + 63) / 64);
var result = utils.padRight(value, l * 64);
var length = Math.floor(value.length / 2);
var result = utils.toHex(value).substr(2);
var length = result.length / 2;
var l = Math.floor((result.length + 63) / 64);
var result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(length).value + result, 32);
};
@ -78,9 +78,10 @@ var formatInputDynamicBytes = function (value) {
*/
var formatInputString = function (value) {
var result = utils.fromAscii(value).substr(2);
var length = result.length / 2;
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(value.length).value + result, 32);
return new SolidityParam(formatInputInt(length).value + result, 32);
};
/**

View File

@ -90,6 +90,7 @@ var padRight = function (string, chars, sign) {
/**
* Should be called to get sting from it's hex representation
* TODO: it should be called toUTF8
*
* @method toAscii
* @param {String} string in hex
@ -107,7 +108,7 @@ var toAscii = function(hex) {
str += String.fromCharCode(code);
}
return str;
return decodeURIComponent(escape(str));
};
/**
@ -118,6 +119,7 @@ var toAscii = function(hex) {
* @returns {String} hex representation of input string
*/
var toHexNative = function(str) {
str = unescape(encodeURIComponent(str));
var hex = "";
for(var i = 0; i < str.length; i++) {
var n = str.charCodeAt(i).toString(16);

View File

@ -53,14 +53,18 @@ describe('lib/solidity/coder', function () {
test({ type: 'string', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: '\xc3\xa4\x00\x00\xc3\xa4',
test({ type: 'string', expected: 'ää',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: '\xc3',
'0000000000000000000000000000000000000000000000000000000000000008' +
'c383c2a4c383c2a4000000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: 'ü',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'c300000000000000000000000000000000000000000000000000000000000000'});
'0000000000000000000000000000000000000000000000000000000000000002' +
'c3bc000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: 'Ã',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'c383000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes', expected: '0xc3a40000c3a4',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +

View File

@ -43,14 +43,18 @@ describe('lib/solidity/coder', function () {
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0xc3a40000c3a4',
expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: '\xc3\xa4\x00\x00\xc3\xa4',
test({ type: 'string', value: 'ää',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: '\xc3',
'0000000000000000000000000000000000000000000000000000000000000008' +
'c383c2a4c383c2a4000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: 'ü',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'c300000000000000000000000000000000000000000000000000000000000000'});
'0000000000000000000000000000000000000000000000000000000000000002' +
'c3bc000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: 'Ã',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'c383000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +