Refactored method encodeWithOffset (#970)

* Example Code for Message Signing Added

Added an example java script which demonstrates the following
- How to sign a message using Ethereum account
- How to find the address using which the message was signed

* Refactored method encodeWithOffset

Performed refactoring for encodeWithOffset method. Removed code redundancy.
This commit is contained in:
Susmit 2017-08-10 17:44:10 +05:30 committed by Fabian Vogelsteller
parent 8879a71796
commit f646ab45f9
1 changed files with 31 additions and 50 deletions

View File

@ -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
*