mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 11:38:12 +00:00
fixed fixed size bytes encoding and decoding
This commit is contained in:
parent
f8a43eddfb
commit
9fa9cc77f4
35
dist/web3-light.js
vendored
35
dist/web3-light.js
vendored
@ -433,8 +433,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
|
||||
}),
|
||||
@ -510,6 +517,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);
|
||||
};
|
||||
@ -620,6 +639,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);
|
||||
};
|
||||
@ -639,6 +670,7 @@ var formatOutputAddress = function (param) {
|
||||
module.exports = {
|
||||
formatInputInt: formatInputInt,
|
||||
formatInputBytes: formatInputBytes,
|
||||
formatInputDynamicBytes: formatInputDynamicBytes,
|
||||
formatInputBool: formatInputBool,
|
||||
formatInputReal: formatInputReal,
|
||||
formatOutputInt: formatOutputInt,
|
||||
@ -647,6 +679,7 @@ module.exports = {
|
||||
formatOutputUReal: formatOutputUReal,
|
||||
formatOutputBool: formatOutputBool,
|
||||
formatOutputBytes: formatOutputBytes,
|
||||
formatOutputDynamicBytes: formatOutputDynamicBytes,
|
||||
formatOutputAddress: formatOutputAddress
|
||||
};
|
||||
|
||||
|
6
dist/web3-light.js.map
vendored
6
dist/web3-light.js.map
vendored
File diff suppressed because one or more lines are too long
4
dist/web3-light.min.js
vendored
4
dist/web3-light.min.js
vendored
File diff suppressed because one or more lines are too long
35
dist/web3.js
vendored
35
dist/web3.js
vendored
@ -433,8 +433,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
|
||||
}),
|
||||
@ -510,6 +517,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);
|
||||
};
|
||||
@ -620,6 +639,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);
|
||||
};
|
||||
@ -639,6 +670,7 @@ var formatOutputAddress = function (param) {
|
||||
module.exports = {
|
||||
formatInputInt: formatInputInt,
|
||||
formatInputBytes: formatInputBytes,
|
||||
formatInputDynamicBytes: formatInputDynamicBytes,
|
||||
formatInputBool: formatInputBool,
|
||||
formatInputReal: formatInputReal,
|
||||
formatOutputInt: formatOutputInt,
|
||||
@ -647,6 +679,7 @@ module.exports = {
|
||||
formatOutputUReal: formatOutputUReal,
|
||||
formatOutputBool: formatOutputBool,
|
||||
formatOutputBytes: formatOutputBytes,
|
||||
formatOutputDynamicBytes: formatOutputDynamicBytes,
|
||||
formatOutputAddress: formatOutputAddress
|
||||
};
|
||||
|
||||
|
6
dist/web3.js.map
vendored
6
dist/web3.js.map
vendored
File diff suppressed because one or more lines are too long
4
dist/web3.min.js
vendored
4
dist/web3.min.js
vendored
File diff suppressed because one or more lines are too long
@ -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
|
||||
}),
|
||||
|
@ -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);
|
||||
};
|
||||
@ -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);
|
||||
};
|
||||
@ -179,6 +203,7 @@ var formatOutputAddress = function (param) {
|
||||
module.exports = {
|
||||
formatInputInt: formatInputInt,
|
||||
formatInputBytes: formatInputBytes,
|
||||
formatInputDynamicBytes: formatInputDynamicBytes,
|
||||
formatInputBool: formatInputBool,
|
||||
formatInputReal: formatInputReal,
|
||||
formatOutputInt: formatOutputInt,
|
||||
@ -187,6 +212,7 @@ module.exports = {
|
||||
formatOutputUReal: formatOutputUReal,
|
||||
formatOutputBool: formatOutputBool,
|
||||
formatOutputBytes: formatOutputBytes,
|
||||
formatOutputDynamicBytes: formatOutputDynamicBytes,
|
||||
formatOutputAddress: formatOutputAddress
|
||||
};
|
||||
|
||||
|
@ -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 () {
|
||||
|
@ -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 () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user