mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 04:35:49 +00:00
Fix code indentation.
This commit is contained in:
@@ -19,7 +19,7 @@ var amount = require('./amount'),
|
|||||||
// Shortcuts
|
// Shortcuts
|
||||||
var hex = sjcl.codec.hex,
|
var hex = sjcl.codec.hex,
|
||||||
bytes = sjcl.codec.bytes;
|
bytes = sjcl.codec.bytes;
|
||||||
|
|
||||||
var jsbn = require('./jsbn');
|
var jsbn = require('./jsbn');
|
||||||
var BigInteger = jsbn.BigInteger;
|
var BigInteger = jsbn.BigInteger;
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ SerializedType.serialize_hex = function (so, hexData, noLength) {
|
|||||||
* parses bytes as hex
|
* parses bytes as hex
|
||||||
*/
|
*/
|
||||||
function convert_bytes_to_hex (byte_array) {
|
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) {
|
SerializedType.serialize_varint = function (so, val) {
|
||||||
@@ -68,17 +68,17 @@ SerializedType.serialize_varint = function (so, val) {
|
|||||||
SerializedType.parse_varint = function (so) {
|
SerializedType.parse_varint = function (so) {
|
||||||
var b1 = so.read(1)[0], b2, b3;
|
var b1 = so.read(1)[0], b2, b3;
|
||||||
if (b1 <= 192) {
|
if (b1 <= 192) {
|
||||||
return b1;
|
return b1;
|
||||||
} else if (b1 <= 240) {
|
} else if (b1 <= 240) {
|
||||||
b2 = so.read(1)[0];
|
b2 = so.read(1)[0];
|
||||||
return 193 + (b1-193)*256 + b2;
|
return 193 + (b1-193)*256 + b2;
|
||||||
} else if (b1 <= 254) {
|
} else if (b1 <= 254) {
|
||||||
b2 = so.read(1)[0];
|
b2 = so.read(1)[0];
|
||||||
b3 = so.read(1)[0];
|
b3 = so.read(1)[0];
|
||||||
return 12481 + (b1-241)*65536 + b2*256 + b3
|
return 12481 + (b1-241)*65536 + b2*256 + b3
|
||||||
}
|
}
|
||||||
else {
|
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")
|
// Convert an integer value into an array of bytes and append it to the serialized object ("so")
|
||||||
function append_byte_array(so, val, bytes) {
|
function append_byte_array(so, val, bytes) {
|
||||||
if (val < 0 || val >= (Math.pow(256, bytes))) {
|
if (val < 0 || val >= (Math.pow(256, bytes))) {
|
||||||
throw new Error("Integer out of bounds");
|
throw new Error("Integer out of bounds");
|
||||||
}
|
}
|
||||||
var newBytes = [];
|
var newBytes = [];
|
||||||
for (var i=0; i<bytes; i++) {
|
for (var i=0; i<bytes; i++) {
|
||||||
newBytes.unshift(val >>> (i*8) & 0xff);
|
newBytes.unshift(val >>> (i*8) & 0xff);
|
||||||
}
|
}
|
||||||
so.append(newBytes);
|
so.append(newBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert a certain number of bytes from the serialized object ("so") into an integer.
|
// Convert a certain number of bytes from the serialized object ("so") into an integer.
|
||||||
function readAndSum(so, bytes) {
|
function readAndSum(so, bytes) {
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
for (var i = 0; i<bytes; i++) {
|
for (var i = 0; i<bytes; i++) {
|
||||||
sum += (so.read(1)[0] << (8*(bytes-1-i)) );
|
sum += (so.read(1)[0] << (8*(bytes-1-i)) );
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -123,9 +123,9 @@ var STInt16 = exports.Int16 = new SerializedType({
|
|||||||
serialize: function (so, val) {
|
serialize: function (so, val) {
|
||||||
append_byte_array(so, val, 2);
|
append_byte_array(so, val, 2);
|
||||||
/*so.append([
|
/*so.append([
|
||||||
val >>> 8 & 0xff,
|
val >>> 8 & 0xff,
|
||||||
val & 0xff
|
val & 0xff
|
||||||
]);*/
|
]);*/
|
||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
return readAndSum(so, 2);
|
return readAndSum(so, 2);
|
||||||
@@ -136,11 +136,11 @@ var STInt32 = exports.Int32 = new SerializedType({
|
|||||||
serialize: function (so, val) {
|
serialize: function (so, val) {
|
||||||
append_byte_array(so, val, 4)
|
append_byte_array(so, val, 4)
|
||||||
/*so.append([
|
/*so.append([
|
||||||
val >>> 24 & 0xff,
|
val >>> 24 & 0xff,
|
||||||
val >>> 16 & 0xff,
|
val >>> 16 & 0xff,
|
||||||
val >>> 8 & 0xff,
|
val >>> 8 & 0xff,
|
||||||
val & 0xff
|
val & 0xff
|
||||||
]);*/
|
]);*/
|
||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
return readAndSum(so, 4);
|
return readAndSum(so, 4);
|
||||||
@@ -152,32 +152,32 @@ var STInt64 = exports.Int64 = new SerializedType({
|
|||||||
serialize: function (so, val) {
|
serialize: function (so, val) {
|
||||||
var bigNumObject;
|
var bigNumObject;
|
||||||
if ("number" === typeof val) {
|
if ("number" === typeof val) {
|
||||||
bigNumObject = new BigInteger(val);
|
bigNumObject = new BigInteger(val);
|
||||||
} else if ("string" === typeof val) {
|
} else if ("string" === typeof val) {
|
||||||
bigNumObject = new BigInteger(val, 16);
|
bigNumObject = new BigInteger(val, 16);
|
||||||
} else if (val instanceof BigInteger) {
|
} else if (val instanceof BigInteger) {
|
||||||
bigNumObject = val;
|
bigNumObject = val;
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Invalid type for Int64");
|
throw new Error("Invalid type for Int64");
|
||||||
}
|
}
|
||||||
|
|
||||||
var hex = bigNumObject.toString(16);
|
var hex = bigNumObject.toString(16);
|
||||||
if (hex.length > 16) {
|
if (hex.length > 16) {
|
||||||
throw new Error("Int64 is too large");
|
throw new Error("Int64 is too large");
|
||||||
}
|
}
|
||||||
while (hex.length < 16) {
|
while (hex.length < 16) {
|
||||||
hex = "0" + hex;
|
hex = "0" + hex;
|
||||||
}
|
}
|
||||||
return this.serialize_hex(so, hash.to_hex(), true); //noLength = true
|
return this.serialize_hex(so, hash.to_hex(), true); //noLength = true
|
||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
var hi = readAndSum(so, 4);
|
var hi = readAndSum(so, 4);
|
||||||
var lo = readAndSum(so, 4);
|
var lo = readAndSum(so, 4);
|
||||||
|
|
||||||
var result = new BigInteger(hi);
|
var result = new BigInteger(hi);
|
||||||
result.shiftLeft(32);
|
result.shiftLeft(32);
|
||||||
result.add(lo);
|
result.add(lo);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ var STHash128 = exports.Hash128 = new SerializedType({
|
|||||||
serialize: function (so, val) {
|
serialize: function (so, val) {
|
||||||
var hash = UInt128.from_json(val);
|
var hash = UInt128.from_json(val);
|
||||||
if (!hash.is_valid()) {
|
if (!hash.is_valid()) {
|
||||||
throw new Error("Invalid Hash128");
|
throw new Error("Invalid Hash128");
|
||||||
}
|
}
|
||||||
this.serialize_hex(so, hash.to_hex(), true); //noLength = true
|
this.serialize_hex(so, hash.to_hex(), true); //noLength = true
|
||||||
},
|
},
|
||||||
@@ -198,7 +198,7 @@ var STHash256 = exports.Hash256 = new SerializedType({
|
|||||||
serialize: function (so, val) {
|
serialize: function (so, val) {
|
||||||
var hash = UInt256.from_json(val);
|
var hash = UInt256.from_json(val);
|
||||||
if (!hash.is_valid()) {
|
if (!hash.is_valid()) {
|
||||||
throw new Error("Invalid Hash256");
|
throw new Error("Invalid Hash256");
|
||||||
}
|
}
|
||||||
this.serialize_hex(so, hash.to_hex(), true); //noLength = true
|
this.serialize_hex(so, hash.to_hex(), true); //noLength = true
|
||||||
},
|
},
|
||||||
@@ -241,11 +241,11 @@ var STCurrency = new SerializedType({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
var currency = Currency.from_bytes(so.read(20));
|
var currency = Currency.from_bytes(so.read(20));
|
||||||
if (!currency.is_valid()) {
|
if (!currency.is_valid()) {
|
||||||
throw new Error("Invalid currency");
|
throw new Error("Invalid currency");
|
||||||
}
|
}
|
||||||
return currency;
|
return currency;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -309,42 +309,42 @@ var STAmount = exports.Amount = new SerializedType({
|
|||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
var amount = new Amount();
|
var amount = new Amount();
|
||||||
var value_bytes = so.read(8);
|
var value_bytes = so.read(8);
|
||||||
var is_zero = !(value_bytes[0] & 0x7f);
|
var is_zero = !(value_bytes[0] & 0x7f);
|
||||||
for (var i=1; i<8; i++) {
|
for (var i=1; i<8; i++) {
|
||||||
is_zero = is_zero && !value_bytes[i];
|
is_zero = is_zero && !value_bytes[i];
|
||||||
}
|
}
|
||||||
if (value_bytes[0] & 0x80) {
|
if (value_bytes[0] & 0x80) {
|
||||||
//non-native
|
//non-native
|
||||||
var currency_bytes = so.read(20);
|
var currency_bytes = so.read(20);
|
||||||
var issuer_bytes = so.read(20);
|
var issuer_bytes = so.read(20);
|
||||||
var currency = STCurrency.parse(currency_bytes);
|
var currency = STCurrency.parse(currency_bytes);
|
||||||
var issuer = UInt160.from_bytes(issuer_bytes);
|
var issuer = UInt160.from_bytes(issuer_bytes);
|
||||||
|
|
||||||
var offset = ((value_bytes[0] & 0x3f) << 2) + (value_bytes[1] >>> 6);
|
var offset = ((value_bytes[0] & 0x3f) << 2) + (value_bytes[1] >>> 6);
|
||||||
var mantissa_bytes = value_bytes.slice(1);
|
var mantissa_bytes = value_bytes.slice(1);
|
||||||
mantissa_bytes[0] &= 0x3f;
|
mantissa_bytes[0] &= 0x3f;
|
||||||
var value = new BigInteger(mantissa_bytes, 256);
|
var value = new BigInteger(mantissa_bytes, 256);
|
||||||
|
|
||||||
if (value.equals(BigInteger.ZERO) && !is_zero ) {
|
if (value.equals(BigInteger.ZERO) && !is_zero ) {
|
||||||
throw new Error("Invalid zero representation");
|
throw new Error("Invalid zero representation");
|
||||||
}
|
}
|
||||||
|
|
||||||
amount._value = value;
|
amount._value = value;
|
||||||
amount._offset = offset;
|
amount._offset = offset;
|
||||||
amount._currency = currency;
|
amount._currency = currency;
|
||||||
amount._issuer = issuer;
|
amount._issuer = issuer;
|
||||||
amount._is_native = false;
|
amount._is_native = false;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//native
|
//native
|
||||||
var integer_bytes = value_bytes.slice();
|
var integer_bytes = value_bytes.slice();
|
||||||
integer_bytes[0] &= 0x3f;
|
integer_bytes[0] &= 0x3f;
|
||||||
amount._value = new BigInteger(integer_bytes, 256);
|
amount._value = new BigInteger(integer_bytes, 256);
|
||||||
amount._is_native = true;
|
amount._is_native = true;
|
||||||
}
|
}
|
||||||
amount._is_negative = !is_zero && !(value_bytes[0] & 0x40);
|
amount._is_negative = !is_zero && !(value_bytes[0] & 0x40);
|
||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -355,7 +355,7 @@ var STVL = exports.VariableLength = new SerializedType({
|
|||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
var len = this.parse_varint(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) {
|
parse: function (so) {
|
||||||
var len = this.parse_varint(so);
|
var len = this.parse_varint(so);
|
||||||
if (len !== 20) {
|
if (len !== 20) {
|
||||||
throw new Error("Non-standard-length account ID");
|
throw new Error("Non-standard-length account ID");
|
||||||
}
|
}
|
||||||
var result = UInt160.from_bytes(so.read(len));
|
var result = UInt160.from_bytes(so.read(len));
|
||||||
if (!result.is_valid()) {
|
if (!result.is_valid()) {
|
||||||
throw new Error("Invalid Account");
|
throw new Error("Invalid Account");
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,30 +11,28 @@ try {
|
|||||||
|
|
||||||
var config = require('../src/js/ripple/config').load(conf);
|
var config = require('../src/js/ripple/config').load(conf);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
buster.testCase("Serialized types", {
|
buster.testCase("Serialized types", {
|
||||||
"Int8" : {
|
"Int8" : {
|
||||||
"Serialize 0" : function () {
|
"Serialize 0" : function () {
|
||||||
var so = new SerializedObject();
|
var so = new SerializedObject();
|
||||||
types.Int8.serialize(so, 0);
|
types.Int8.serialize(so, 0);
|
||||||
assert.equals(so.to_hex(), "00");
|
assert.equals(so.to_hex(), "00");
|
||||||
},
|
},
|
||||||
"Serialize 123" : function () {
|
"Serialize 123" : function () {
|
||||||
var so = new SerializedObject();
|
var so = new SerializedObject();
|
||||||
types.Int8.serialize(so, 123);
|
types.Int8.serialize(so, 123);
|
||||||
assert.equals(so.to_hex(), "7B");
|
assert.equals(so.to_hex(), "7B");
|
||||||
},
|
},
|
||||||
"Serialize 255" : function () {
|
"Serialize 255" : function () {
|
||||||
var so = new SerializedObject();
|
var so = new SerializedObject();
|
||||||
types.Int8.serialize(so, 255);
|
types.Int8.serialize(so, 255);
|
||||||
assert.equals(so.to_hex(), "FF");
|
assert.equals(so.to_hex(), "FF");
|
||||||
},
|
},
|
||||||
"Fail to serialize 256" : function () {
|
"Fail to serialize 256" : function () {
|
||||||
var so = new SerializedObject();
|
var so = new SerializedObject();
|
||||||
assert.exception(function () {
|
assert.exception(function () {
|
||||||
types.Int8.serialize(so, 256);
|
types.Int8.serialize(so, 256);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user