Fix code indentation.

This commit is contained in:
Stefan Thomas
2013-07-30 21:32:02 -07:00
parent 5bc1603f43
commit 815e2af3cf
2 changed files with 113 additions and 115 deletions

View File

@@ -42,7 +42,7 @@ SerializedType.serialize_hex = function (so, hexData, noLength) {
* parses bytes as hex
*/
function convert_bytes_to_hex (byte_array) {
return sjcl.codec.hex.fromBits(sjcl.codec.bytes.toBits(byte_array));
return sjcl.codec.hex.fromBits(sjcl.codec.bytes.toBits(byte_array));
}
SerializedType.serialize_varint = function (so, val) {
@@ -68,17 +68,17 @@ SerializedType.serialize_varint = function (so, val) {
SerializedType.parse_varint = function (so) {
var b1 = so.read(1)[0], b2, b3;
if (b1 <= 192) {
return b1;
return b1;
} else if (b1 <= 240) {
b2 = so.read(1)[0];
return 193 + (b1-193)*256 + b2;
b2 = so.read(1)[0];
return 193 + (b1-193)*256 + b2;
} else if (b1 <= 254) {
b2 = so.read(1)[0];
b3 = so.read(1)[0];
return 12481 + (b1-241)*65536 + b2*256 + b3
b2 = so.read(1)[0];
b3 = so.read(1)[0];
return 12481 + (b1-241)*65536 + b2*256 + b3
}
else {
throw new Error("Invalid varint length indicator");
throw new Error("Invalid varint length indicator");
}
};
@@ -88,23 +88,23 @@ SerializedType.parse_varint = function (so) {
// Convert an integer value into an array of bytes and append it to the serialized object ("so")
function append_byte_array(so, val, bytes) {
if (val < 0 || val >= (Math.pow(256, bytes))) {
throw new Error("Integer out of bounds");
}
var newBytes = [];
for (var i=0; i<bytes; i++) {
newBytes.unshift(val >>> (i*8) & 0xff);
}
so.append(newBytes);
if (val < 0 || val >= (Math.pow(256, bytes))) {
throw new Error("Integer out of bounds");
}
var newBytes = [];
for (var i=0; i<bytes; i++) {
newBytes.unshift(val >>> (i*8) & 0xff);
}
so.append(newBytes);
}
// Convert a certain number of bytes from the serialized object ("so") into an integer.
function readAndSum(so, bytes) {
var sum = 0;
for (var i = 0; i<bytes; i++) {
sum += (so.read(1)[0] << (8*(bytes-1-i)) );
}
return sum;
var sum = 0;
for (var i = 0; i<bytes; i++) {
sum += (so.read(1)[0] << (8*(bytes-1-i)) );
}
return sum;
}
@@ -123,9 +123,9 @@ var STInt16 = exports.Int16 = new SerializedType({
serialize: function (so, val) {
append_byte_array(so, val, 2);
/*so.append([
val >>> 8 & 0xff,
val & 0xff
]);*/
val >>> 8 & 0xff,
val & 0xff
]);*/
},
parse: function (so) {
return readAndSum(so, 2);
@@ -136,11 +136,11 @@ var STInt32 = exports.Int32 = new SerializedType({
serialize: function (so, val) {
append_byte_array(so, val, 4)
/*so.append([
val >>> 24 & 0xff,
val >>> 16 & 0xff,
val >>> 8 & 0xff,
val & 0xff
]);*/
val >>> 24 & 0xff,
val >>> 16 & 0xff,
val >>> 8 & 0xff,
val & 0xff
]);*/
},
parse: function (so) {
return readAndSum(so, 4);
@@ -152,32 +152,32 @@ var STInt64 = exports.Int64 = new SerializedType({
serialize: function (so, val) {
var bigNumObject;
if ("number" === typeof val) {
bigNumObject = new BigInteger(val);
bigNumObject = new BigInteger(val);
} else if ("string" === typeof val) {
bigNumObject = new BigInteger(val, 16);
bigNumObject = new BigInteger(val, 16);
} else if (val instanceof BigInteger) {
bigNumObject = val;
bigNumObject = val;
} else {
throw new Error("Invalid type for Int64");
throw new Error("Invalid type for Int64");
}
var hex = bigNumObject.toString(16);
if (hex.length > 16) {
throw new Error("Int64 is too large");
}
while (hex.length < 16) {
hex = "0" + hex;
}
return this.serialize_hex(so, hash.to_hex(), true); //noLength = true
var hex = bigNumObject.toString(16);
if (hex.length > 16) {
throw new Error("Int64 is too large");
}
while (hex.length < 16) {
hex = "0" + hex;
}
return this.serialize_hex(so, hash.to_hex(), true); //noLength = true
},
parse: function (so) {
var hi = readAndSum(so, 4);
var lo = readAndSum(so, 4);
var hi = readAndSum(so, 4);
var lo = readAndSum(so, 4);
var result = new BigInteger(hi);
result.shiftLeft(32);
result.add(lo);
return result;
var result = new BigInteger(hi);
result.shiftLeft(32);
result.add(lo);
return result;
}
});
@@ -185,7 +185,7 @@ var STHash128 = exports.Hash128 = new SerializedType({
serialize: function (so, val) {
var hash = UInt128.from_json(val);
if (!hash.is_valid()) {
throw new Error("Invalid Hash128");
throw new Error("Invalid Hash128");
}
this.serialize_hex(so, hash.to_hex(), true); //noLength = true
},
@@ -198,7 +198,7 @@ var STHash256 = exports.Hash256 = new SerializedType({
serialize: function (so, val) {
var hash = UInt256.from_json(val);
if (!hash.is_valid()) {
throw new Error("Invalid Hash256");
throw new Error("Invalid Hash256");
}
this.serialize_hex(so, hash.to_hex(), true); //noLength = true
},
@@ -241,11 +241,11 @@ var STCurrency = new SerializedType({
}
},
parse: function (so) {
var currency = Currency.from_bytes(so.read(20));
if (!currency.is_valid()) {
throw new Error("Invalid currency");
}
return currency;
var currency = Currency.from_bytes(so.read(20));
if (!currency.is_valid()) {
throw new Error("Invalid currency");
}
return currency;
}
});
@@ -309,42 +309,42 @@ var STAmount = exports.Amount = new SerializedType({
},
parse: function (so) {
var amount = new Amount();
var value_bytes = so.read(8);
var is_zero = !(value_bytes[0] & 0x7f);
for (var i=1; i<8; i++) {
is_zero = is_zero && !value_bytes[i];
}
if (value_bytes[0] & 0x80) {
//non-native
var currency_bytes = so.read(20);
var issuer_bytes = so.read(20);
var currency = STCurrency.parse(currency_bytes);
var issuer = UInt160.from_bytes(issuer_bytes);
var value_bytes = so.read(8);
var is_zero = !(value_bytes[0] & 0x7f);
for (var i=1; i<8; i++) {
is_zero = is_zero && !value_bytes[i];
}
if (value_bytes[0] & 0x80) {
//non-native
var currency_bytes = so.read(20);
var issuer_bytes = so.read(20);
var currency = STCurrency.parse(currency_bytes);
var issuer = UInt160.from_bytes(issuer_bytes);
var offset = ((value_bytes[0] & 0x3f) << 2) + (value_bytes[1] >>> 6);
var mantissa_bytes = value_bytes.slice(1);
mantissa_bytes[0] &= 0x3f;
var value = new BigInteger(mantissa_bytes, 256);
var offset = ((value_bytes[0] & 0x3f) << 2) + (value_bytes[1] >>> 6);
var mantissa_bytes = value_bytes.slice(1);
mantissa_bytes[0] &= 0x3f;
var value = new BigInteger(mantissa_bytes, 256);
if (value.equals(BigInteger.ZERO) && !is_zero ) {
throw new Error("Invalid zero representation");
}
if (value.equals(BigInteger.ZERO) && !is_zero ) {
throw new Error("Invalid zero representation");
}
amount._value = value;
amount._offset = offset;
amount._currency = currency;
amount._issuer = issuer;
amount._is_native = false;
amount._value = value;
amount._offset = offset;
amount._currency = currency;
amount._issuer = issuer;
amount._is_native = false;
} else {
//native
var integer_bytes = value_bytes.slice();
integer_bytes[0] &= 0x3f;
amount._value = new BigInteger(integer_bytes, 256);
amount._is_native = true;
}
amount._is_negative = !is_zero && !(value_bytes[0] & 0x40);
return amount;
} else {
//native
var integer_bytes = value_bytes.slice();
integer_bytes[0] &= 0x3f;
amount._value = new BigInteger(integer_bytes, 256);
amount._is_native = true;
}
amount._is_negative = !is_zero && !(value_bytes[0] & 0x40);
return amount;
}
});
@@ -355,7 +355,7 @@ var STVL = exports.VariableLength = new SerializedType({
},
parse: function (so) {
var len = this.parse_varint(so);
return convert_bytes_to_hex(so.read(len));
return convert_bytes_to_hex(so.read(len));
}
});
@@ -366,13 +366,13 @@ var STAccount = exports.Account = new SerializedType({
},
parse: function (so) {
var len = this.parse_varint(so);
if (len !== 20) {
throw new Error("Non-standard-length account ID");
}
var result = UInt160.from_bytes(so.read(len));
if (!result.is_valid()) {
throw new Error("Invalid Account");
}
if (len !== 20) {
throw new Error("Non-standard-length account ID");
}
var result = UInt160.from_bytes(so.read(len));
if (!result.is_valid()) {
throw new Error("Invalid Account");
}
return result;
}
});

View File

@@ -11,30 +11,28 @@ try {
var config = require('../src/js/ripple/config').load(conf);
buster.testCase("Serialized types", {
"Int8" : {
"Serialize 0" : function () {
var so = new SerializedObject();
types.Int8.serialize(so, 0);
assert.equals(so.to_hex(), "00");
var so = new SerializedObject();
types.Int8.serialize(so, 0);
assert.equals(so.to_hex(), "00");
},
"Serialize 123" : function () {
var so = new SerializedObject();
types.Int8.serialize(so, 123);
assert.equals(so.to_hex(), "7B");
var so = new SerializedObject();
types.Int8.serialize(so, 123);
assert.equals(so.to_hex(), "7B");
},
"Serialize 255" : function () {
var so = new SerializedObject();
types.Int8.serialize(so, 255);
assert.equals(so.to_hex(), "FF");
var so = new SerializedObject();
types.Int8.serialize(so, 255);
assert.equals(so.to_hex(), "FF");
},
"Fail to serialize 256" : function () {
var so = new SerializedObject();
assert.exception(function () {
types.Int8.serialize(so, 256);
});
var so = new SerializedObject();
assert.exception(function () {
types.Int8.serialize(so, 256);
});
},
}
});