diff --git a/lib/solidity/coder.js b/lib/solidity/coder.js index 59b001f..a0119dd 100644 --- a/lib/solidity/coder.js +++ b/lib/solidity/coder.js @@ -219,70 +219,115 @@ SolidityCoder.prototype.decodeParams = function (types, bytes) { }); }; +var SolidityTypeAddress = function () { + this._name = 'address'; + this._match = 'strict'; + this._mode = 'value'; + this._inputFormatter = f.formatInputInt; + this._outputFormatter = f.formatOutputAddress; +}; + +SolidityTypeAddress.prototype = new SolidityType({}); +SolidityTypeAddress.prototype.constructor = SolidityTypeAddress; + +var SolidityTypeBool = function () { + this._name = 'bool'; + this._match = 'strict'; + this._mode = 'value'; + this._inputFormatter = f.formatInputBool; + this._outputFormatter = f.formatOutputBool; +}; + +SolidityTypeBool.prototype = new SolidityType({}); +SolidityTypeBool.prototype.constructor = SolidityTypeBool; + +var SolidityTypeInt = function () { + this._name = 'int'; + this._match = 'prefix'; + this._mode = 'value'; + this._inputFormatter = f.formatInputInt; + this._outputFormatter = f.formatOutputInt; +}; + +SolidityTypeInt.prototype = new SolidityType({}); +SolidityTypeInt.prototype.constructor = SolidityTypeInt; + +var SolidityTypeUInt = function () { + this._name = 'uint'; + this._match = 'prefix'; + this._mode = 'value'; + this._inputFormatter = f.formatInputInt; + this._outputFormatter = f.formatOutputUInt; +}; + +SolidityTypeUInt.prototype = new SolidityType({}); +SolidityTypeUInt.prototype.constructor = SolidityTypeUInt; + +var SolidityTypeDynamicBytes = function () { + this._name = 'bytes'; + this._match = 'strict'; + this._mode = 'bytes'; + this._inputFormatter = f.formatInputDynamicBytes; + this._outputFormatter = f.formatOutputDynamicBytes; +}; + +SolidityTypeDynamicBytes.prototype = new SolidityType({}); +SolidityTypeDynamicBytes.prototype.constructor = SolidityTypeDynamicBytes; + +var SolidityTypeBytes = function () { + this._name = 'bytes'; + this._match = 'prefix'; + this._mode = 'value'; + this._inputFormatter = f.formatInputBytes; + this._outputFormatter = f.formatOutputBytes; +}; + +SolidityTypeBytes.prototype = new SolidityType({}); +SolidityTypeBytes.prototype.constructor = SolidityTypeBytes; + +var SolidityTypeString = function () { + this._name = 'string'; + this._match = 'strict'; + this._mode = 'bytes'; + this._inputFormatter = f.formatInputString; + this._outputFormatter = f.formatOutputString; +}; + +SolidityTypeString.prototype = new SolidityType({}); +SolidityTypeString.prototype.constructor = SolidityTypeString; + +var SolidityTypeReal = function () { + this._name = 'real'; + this._match = 'prefix'; + this._mode = 'value'; + this._inputFormatter = f.formatInputReal; + this._outputFormatter = f.formatOutputReal; +}; + +SolidityTypeReal.prototype = new SolidityType({}); +SolidityTypeReal.prototype.constructor = SolidityTypeReal; + +var SolidityTypeUReal = function () { + this._name = 'ureal'; + this._match = 'prefix'; + this._mode = 'value'; + this._inputFormatter = f.formatInputReal; + this._outputFormatter = f.formatOutputUReal; +}; + +SolidityTypeUReal.prototype = new SolidityType({}); +SolidityTypeUReal.prototype.constructor = SolidityTypeUReal; + var coder = new SolidityCoder([ - new SolidityType({ - name: 'address', - match: 'strict', - mode: 'value', - inputFormatter: f.formatInputInt, - outputFormatter: f.formatOutputAddress - }), - new SolidityType({ - name: 'bool', - match: 'strict', - mode: 'value', - inputFormatter: f.formatInputBool, - outputFormatter: f.formatOutputBool - }), - new SolidityType({ - name: 'int', - match: 'prefix', - mode: 'value', - inputFormatter: f.formatInputInt, - outputFormatter: f.formatOutputInt, - }), - new SolidityType({ - name: 'uint', - match: 'prefix', - mode: 'value', - inputFormatter: f.formatInputInt, - outputFormatter: f.formatOutputUInt - }), - new SolidityType({ - name: 'bytes', - match: 'strict', - mode: 'bytes', - inputFormatter: f.formatInputDynamicBytes, - outputFormatter: f.formatOutputDynamicBytes - }), - new SolidityType({ - name: 'bytes', - match: 'prefix', - mode: 'value', - inputFormatter: f.formatInputBytes, - outputFormatter: f.formatOutputBytes - }), - new SolidityType({ - name: 'string', - match: 'strict', - mode: 'bytes', - inputFormatter: f.formatInputString, - outputFormatter: f.formatOutputString - }), - new SolidityType({ - name: 'real', - match: 'prefix', - mode: 'value', - inputFormatter: f.formatInputReal, - outputFormatter: f.formatOutputReal - }), - new SolidityType({ - name: 'ureal', - match: 'prefix', - mode: 'value', - inputFormatter: f.formatInputReal, - outputFormatter: f.formatOutputUReal - }) + new SolidityTypeAddress(), + new SolidityTypeBool(), + new SolidityTypeInt(), + new SolidityTypeUInt(), + new SolidityTypeDynamicBytes(), + new SolidityTypeBytes(), + new SolidityTypeString(), + new SolidityTypeReal(), + new SolidityTypeUReal() ]); module.exports = coder; diff --git a/test/coder.decodeParam.js b/test/coder.decodeParam.js index ff92fa6..e0dd3fd 100644 --- a/test/coder.decodeParam.js +++ b/test/coder.decodeParam.js @@ -32,6 +32,10 @@ describe('lib/solidity/coder', function () { '6761766f66796f726b0000000000000000000000000000000000000000000000'}); test({ type: 'bytes32', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', value: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'}); + //test({ type: 'bytes64', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' + + //'c3a40000c3a40000000000000000000000000000000000000000000000000000', + //value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' + + //'c3a40000c3a40000000000000000000000000000000000000000000000000000'}); test({ type: 'bytes', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', value: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000020' +