Fixed contract interface coder for numbers.

This commit is contained in:
ricmoo 2016-08-01 17:57:00 -04:00
parent 024cc1806f
commit 0ceee56de3

@ -44,14 +44,28 @@ function zpad(buffer, length) {
return buffer; return buffer;
} }
// There seems to be a but in maskn, so we are doing this for now.
var bitmasks = [];
(function() {
var mask = '';
for (var i = 0; i < 33; i++) {
bitmasks.push(new utils.BN(mask, 16));
mask += 'ff';
}
})();
function coderNumber(size, signed) { function coderNumber(size, signed) {
return { return {
encode: function(value) { encode: function(value) {
value = numberOrBN(value) value = numberOrBN(value)
value = value.toTwos(size * 8).and(bitmasks[size]);
if (signed) { if (signed) {
value = value.toTwos(32 * 8); value = value.fromTwos(size * 8).toTwos(256);
} else if (value < 0) { //value = value.toTwos(size * 8).and(bitmasks[size]).fromTwos(size * 8).toTwos(256);
value = value.toTwos(size * 8); // } else { //if (value.isNeg()) {
// value = value.toTwos(size * 8).and(bitmasks[size]);
// } else {
// value = value.and(bitmasks[size]);
} }
return value.toArrayLike(Buffer, 'be', 32); return value.toArrayLike(Buffer, 'be', 32);
}, },
@ -60,7 +74,7 @@ function coderNumber(size, signed) {
if (signed) { if (signed) {
value = value.fromTwos(size * 8); value = value.fromTwos(size * 8);
} else { } else {
value = value.mod((new utils.BN(2)).pow(new utils.BN(size * 8))) value = value.fromTwos(size * 8); //.toTwos(256); //mod((new utils.BN(2)).pow(new utils.BN(size * 8)))
} }
return { return {
consumed: 32, consumed: 32,