fixed most of jshint issues

This commit is contained in:
debris 2015-08-03 15:14:54 +02:00
parent e02a96528e
commit e6c31f7558
2 changed files with 109 additions and 75 deletions

View File

@ -126,49 +126,63 @@ SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes,
return result; return result;
}; };
// TODO: refactor whole encoding!
SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) { SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) {
var self = this;
if (solidityType.isDynamicArray(type)) { if (solidityType.isDynamicArray(type)) {
// offset was already set return (function () {
var nestedName = solidityType.nestedName(type); // offset was already set
var nestedStaticPartLength = solidityType.staticPartLength(nestedName); var nestedName = solidityType.nestedName(type);
var result = encoded[0]; var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = encoded[0];
var previousLength = 2; // in int
if (solidityType.isDynamicArray(nestedName)) { (function () {
for (var i = 1; i < encoded.length; i++) { var previousLength = 2; // in int
previousLength += +(encoded[i - 1] || {})[0] || 0; if (solidityType.isDynamicArray(nestedName)) {
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); for (var i = 1; i < encoded.length; i++) {
} previousLength += +(encoded[i - 1])[0] || 0;
} result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
// first element is length, skip it }
for (var i = 0; i < encoded.length - 1; i++) { })();
var additionalOffset = result / 2;
result += this.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset); // 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);
}
})();
return result; return result;
})();
} else if (solidityType.isStaticArray(type)) { } else if (solidityType.isStaticArray(type)) {
var nestedName = solidityType.nestedName(type); return (function () {
var nestedStaticPartLength = solidityType.staticPartLength(nestedName); var nestedName = solidityType.nestedName(type);
var result = ""; var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = "";
var previousLength = 0; // in int (function () {
if (solidityType.isDynamicArray(nestedName)) { var previousLength = 0; // in int
for (var i = 0; i < encoded.length; i++) { if (solidityType.isDynamicArray(nestedName)) {
// calculate length of previous item for (var i = 0; i < encoded.length; i++) {
previousLength += +(encoded[i - 1] || {})[0] || 0; // calculate length of previous item
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); previousLength += +(encoded[i - 1] || [])[0] || 0;
} result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
} }
}
})();
for (var i = 0; i < encoded.length; i++) { (function () {
var additionalOffset = result / 2; for (var i = 0; i < encoded.length; i++) {
result += this.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset); var additionalOffset = result / 2;
} result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset);
}
})();
return result; return result;
})();
} }
return encoded; return encoded;

View File

@ -143,29 +143,37 @@ SolidityType.prototype.nestedTypes = function (name) {
* @return {String} encoded value * @return {String} encoded value
*/ */
SolidityType.prototype.encode = function (value, name) { SolidityType.prototype.encode = function (value, name) {
var self = this;
if (this.isDynamicArray(name)) { if (this.isDynamicArray(name)) {
var length = value.length; // in int
var nestedName = this.nestedName(name);
var result = []; return (function () {
result.push(f.formatInputInt(length).encode()); var length = value.length; // in int
var nestedName = self.nestedName(name);
var self = this;
value.forEach(function (v) { var result = [];
result.push(self.encode(v, nestedName)); result.push(f.formatInputInt(length).encode());
});
value.forEach(function (v) {
result.push(self.encode(v, nestedName));
});
return result;
})();
return result;
} else if (this.isStaticArray(name)) { } else if (this.isStaticArray(name)) {
var length = this.staticArrayLength(name); // in int
var nestedName = this.nestedName(name);
var result = []; return (function () {
for (var i = 0; i < length; i++) { var length = self.staticArrayLength(name); // in int
result.push(this.encode(value[i], nestedName)); var nestedName = self.nestedName(name);
}
var result = [];
for (var i = 0; i < length; i++) {
result.push(self.encode(value[i], nestedName));
}
return result;
})();
return result;
} }
return this._inputFormatter(value, name).encode(); return this._inputFormatter(value, name).encode();
@ -181,39 +189,51 @@ SolidityType.prototype.encode = function (value, name) {
* @returns {Object} decoded value * @returns {Object} decoded value
*/ */
SolidityType.prototype.decode = function (bytes, offset, name) { SolidityType.prototype.decode = function (bytes, offset, name) {
var self = this;
if (this.isDynamicArray(name)) { if (this.isDynamicArray(name)) {
var arrayOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var length = parseInt('0x' + bytes.substr(arrayOffset * 2, 64)); // in int
var arrayStart = arrayOffset + 32; // array starts after length; // in bytes
var nestedName = this.nestedName(name); return (function () {
var nestedStaticPartLength = this.staticPartLength(nestedName); // in bytes var arrayOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var result = []; var length = parseInt('0x' + bytes.substr(arrayOffset * 2, 64)); // in int
var arrayStart = arrayOffset + 32; // array starts after length; // in bytes
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) { var nestedName = self.nestedName(name);
result.push(this.decode(bytes, arrayStart + i, nestedName)); var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
} var result = [];
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) {
result.push(self.decode(bytes, arrayStart + i, nestedName));
}
return result;
})();
return result;
} else if (this.isStaticArray(name)) { } else if (this.isStaticArray(name)) {
var length = this.staticArrayLength(name); // in int
var arrayStart = offset; // in bytes
var nestedName = this.nestedName(name); return (function () {
var nestedStaticPartLength = this.staticPartLength(nestedName); // in bytes var length = self.staticArrayLength(name); // in int
var result = []; var arrayStart = offset; // in bytes
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) { var nestedName = self.nestedName(name);
result.push(this.decode(bytes, arrayStart + i, nestedName)); var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
} var result = [];
return result; for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) {
result.push(self.decode(bytes, arrayStart + i, nestedName));
}
return result;
})();
} else if (this.isDynamicType(name)) { } else if (this.isDynamicType(name)) {
var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes
var roundedLength = Math.floor((length + 31) / 32); // in int
return this._outputFormatter(new SolidityParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0)); return (function () {
var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes
var roundedLength = Math.floor((length + 31) / 32); // in int
return self._outputFormatter(new SolidityParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0));
})();
} }
var length = this.staticPartLength(name); var length = this.staticPartLength(name);