Skip to content

Commit

Permalink
fixed fixed size bytes encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
debris committed Apr 24, 2015
1 parent f8a43ed commit 9fa9cc7
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 19 deletions.
35 changes: 34 additions & 1 deletion dist/web3-light.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/web3-light.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3-light.min.js

Large diffs are not rendered by default.

35 changes: 34 additions & 1 deletion dist/web3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/web3.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3.min.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion lib/solidity/coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,15 @@ var coder = new SolidityCoder([
}),
new SolidityType({
name: 'bytes',
match: 'prefix',
match: 'strict',
mode: 'bytes',
inputFormatter: f.formatInputDynamicBytes,
outputFormatter: f.formatOutputDynamicBytes
}),
new SolidityType({
name: 'bytes',
match: 'prefix',
mode: 'value',
inputFormatter: f.formatInputBytes,
outputFormatter: f.formatOutputBytes
}),
Expand Down
26 changes: 26 additions & 0 deletions lib/solidity/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ var formatInputInt = function (value) {
* @returns {SolidityParam}
*/
var formatInputBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
return new SolidityParam(result);
};

/**
* Formats input value to byte representation of string
*
* @method formatInputDynamicBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputDynamicBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
return new SolidityParam('', formatInputInt(value.length).value, result);
};
Expand Down Expand Up @@ -160,6 +172,18 @@ var formatOutputBool = function (param) {
* @returns {String} ascii string
*/
var formatOutputBytes = function (param) {
// length might also be important!
return utils.toAscii(param.value);
};

/**
* Should be used to format output string
*
* @method formatOutputDynamicBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
*/
var formatOutputDynamicBytes = function (param) {
// length might also be important!
return utils.toAscii(param.suffix);
};
Expand All @@ -179,6 +203,7 @@ var formatOutputAddress = function (param) {
module.exports = {
formatInputInt: formatInputInt,
formatInputBytes: formatInputBytes,
formatInputDynamicBytes: formatInputDynamicBytes,
formatInputBool: formatInputBool,
formatInputReal: formatInputReal,
formatOutputInt: formatOutputInt,
Expand All @@ -187,6 +212,7 @@ module.exports = {
formatOutputUReal: formatOutputUReal,
formatOutputBool: formatOutputBool,
formatOutputBytes: formatOutputBytes,
formatOutputDynamicBytes: formatOutputDynamicBytes,
formatOutputAddress: formatOutputAddress
};

9 changes: 6 additions & 3 deletions test/coder.decodeParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ var assert = chai.assert;
var coder = require('../lib/solidity/coder');

var tests = [
{ type: 'int', value: '0000000000000000000000000000000000000000000000000000000000000001', expected: 1},
{ type: 'int', value: '0000000000000000000000000000000000000000000000000000000000000010', expected: 16},
{ type: 'int', value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: -1}
{ type: 'int', value: '0000000000000000000000000000000000000000000000000000000000000001', expected: 1},
{ type: 'int', value: '0000000000000000000000000000000000000000000000000000000000000010', expected: 16},
{ type: 'int', value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: -1},
{ type: 'bytes32', value: '6761766f66796f726b0000000000000000000000000000000000000000000000', expected: 'gavofyork'},
{ type: 'bytes', value: '0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000', expected: 'gavofyork'}
];

describe('lib/solidity/coder', function () {
Expand Down
9 changes: 6 additions & 3 deletions test/coder.encodeParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ var assert = chai.assert;
var coder = require('../lib/solidity/coder');

var tests = [
{ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'},
{ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'},
{ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}
{ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'},
{ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'},
{ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ type: 'bytes32', value: 'gavofyork', expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'},
{ type: 'bytes', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'}
];

describe('lib/solidity/coder', function () {
Expand Down

0 comments on commit 9fa9cc7

Please sign in to comment.