diff --git a/lib/solidity/coder.js b/lib/solidity/coder.js index d55312c..d5d4851 100644 --- a/lib/solidity/coder.js +++ b/lib/solidity/coder.js @@ -132,69 +132,50 @@ SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, return result; }; -// TODO: refactor whole encoding! SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) { var self = this; - if (solidityType.isDynamicArray(type)) { - return (function () { - // offset was already set - var nestedName = solidityType.nestedName(type); - var nestedStaticPartLength = solidityType.staticPartLength(nestedName); - var result = encoded[0]; + var encodingMode={dynamic:1,static:2,other:3}; + + var mode=(solidityType.isDynamicArray(type)?encodingMode.dynamic:(solidityType.isStaticArray(type)?encodingMode.static:encodingMode.other)); - (function () { - var previousLength = 2; // in int - if (solidityType.isDynamicArray(nestedName)) { - for (var i = 1; i < encoded.length; i++) { - previousLength += +(encoded[i - 1])[0] || 0; - result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); - } + if(mode!=encodingMode.other){ + var nestedName = solidityType.nestedName(type); + var nestedStaticPartLength = solidityType.staticPartLength(nestedName); + var result = (mode==encodingMode.dynamic?encoded[0]:""); + + if (solidityType.isDynamicArray(nestedName)) { + var previousLength = (mode==encodingMode.dynamic?2:0); + + for (var i = 0; i < encoded.length; i++) { + // calculate length of previous item + if(mode==encodingMode.dynamic){ + previousLength += +(encoded[i - 1])[0] || 0; } - })(); - - // first element is length, skip it - (function () { - for (var i = 0; i < encoded.length - 1; i++) { - var additionalOffset = result / 2; - result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset); + else if(mode==encodingMode.static){ + previousLength += +(encoded[i - 1] || [])[0] || 0; } - })(); - - return result; - })(); - - } else if (solidityType.isStaticArray(type)) { - return (function () { - var nestedName = solidityType.nestedName(type); - var nestedStaticPartLength = solidityType.staticPartLength(nestedName); - var result = ""; - - - if (solidityType.isDynamicArray(nestedName)) { - (function () { - var previousLength = 0; // in int - for (var i = 0; i < encoded.length; i++) { - // calculate length of previous item - previousLength += +(encoded[i - 1] || [])[0] || 0; - result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); - } - })(); + result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); } + } - (function () { - for (var i = 0; i < encoded.length; i++) { - var additionalOffset = result / 2; - result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset); - } - })(); + var len=(mode==encodingMode.dynamic?encoded.length-1:encoded.length); + for (var i = 0; i < len; i++) { + var additionalOffset = result / 2; + if(mode==encodingMode.dynamic){ + result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset); + } + else if(mode==encodingMode.static){ + result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset); + } + } - return result; - })(); + return result; } return encoded; }; + /** * Should be used to decode bytes to plain param *