diff --git a/src/js/ripple/serializedobject.js b/src/js/ripple/serializedobject.js index e61f1a38..e7924b07 100644 --- a/src/js/ripple/serializedobject.js +++ b/src/js/ripple/serializedobject.js @@ -5,7 +5,7 @@ var stypes = require('./serializedtypes'); var UInt256 = require('./uint256').UInt256; -var SerializedObject = function (buf) { +function SerializedObject(buf) { if (Array.isArray(buf) || (Buffer && Buffer.isBuffer(buf)) ) { this.buffer = buf; } else if (typeof buf === 'string') { @@ -13,35 +13,41 @@ var SerializedObject = function (buf) { } else if (!buf) { this.buffer = []; } else { - throw new Error("Invalid buffer passed."); + throw new Error('Invalid buffer passed.'); } this.pointer = 0; }; SerializedObject.from_json = function (obj) { - var so = new SerializedObject(); - var typedef; - // Create a copy of the object so we don't modify it - obj = extend({}, obj); + var obj = extend({}, obj); + var so = new SerializedObject; + var typedef; switch (typeof obj.TransactionType) { case 'number': obj.TransactionType = SerializedObject.lookup_type_tx(obj.TransactionType); + if (!obj.TransactionType) { - throw new Error("Transaction type ID is invalid."); + throw new Error('Transaction type ID is invalid.'); } break; case 'string': - typedef = binformat.tx[obj.TransactionType].slice(); + typedef = binformat.tx[obj.TransactionType]; + + if (!Array.isArray(typedef)) { + throw new Error('Transaction type is invalid'); + } + + typedef = typedef.slice(); obj.TransactionType = typedef.shift(); break; default: if (typeof obj.LedgerEntryType !== 'undefined') { // XXX: TODO - throw new Error("Ledger entry binary format not yet implemented."); + throw new Error('Ledger entry binary format not yet implemented.'); } else { - throw new Error("Object to be serialized must contain either " + "TransactionType or LedgerEntryType."); + throw new Error('Object to be serialized must contain either ' + 'TransactionType or LedgerEntryType.'); } } @@ -60,18 +66,21 @@ SerializedObject.prototype.resetPointer = function () { }; function readOrPeek(advance) { - return function(numberOfBytes) { + return function(bytes) { var start = this.pointer; - var end = start + numberOfBytes; + var end = start + bytes; + if (end > this.buffer.length) { - throw new Error("There aren't that many bytes left."); - } else { - var result = this.buffer.slice(start, end); - if (advance) { - this.pointer = end; - } - return result; + throw new Error('Buffer length exceeded'); } + + var result = this.buffer.slice(start, end); + + if (advance) { + this.pointer = end; + } + + return result; } }; @@ -87,51 +96,51 @@ SerializedObject.prototype.to_hex = function () { }; var TRANSACTION_TYPES = { - 0: "Payment", - 3: "AccountSet", - 5: "SetRegularKey", - 7: "OfferCreate", - 8: "OfferCancel", - 9: "Contract", - 10: "RemoveContract", - 20: "TrustSet", - 100: "EnableFeature", - 101: "SetFee" + 0: 'Payment', + 3: 'AccountSet', + 5: 'SetRegularKey', + 7: 'OfferCreate', + 8: 'OfferCancel', + 9: 'Contract', + 10: 'RemoveContract', + 20: 'TrustSet', + 100: 'EnableFeature', + 101: 'SetFee' }; var LEDGER_ENTRY_TYPES = { - 97: "AccountRoot", - 99: "Contract", - 100: "DirectoryNode", - 102: "Features", - 103: "GeneratorMap", - 104: "LedgerHashes", - 110: "Nickname", - 111: "Offer", - 114: "RippleState", - 115: "FeeSettings" + 97: 'AccountRoot', + 99: 'Contract', + 100: 'DirectoryNode', + 102: 'Features', + 103: 'GeneratorMap', + 104: 'LedgerHashes', + 110: 'Nickname', + 111: 'Offer', + 114: 'RippleState', + 115: 'FeeSettings' }; var TRANSACTION_RESULTS = { - 0 : "tesSUCCESS", - 100: "tecCLAIM", - 101: "tecPATH_PARTIAL", - 102: "tecUNFUNDED_ADD", - 103: "tecUNFUNDED_OFFER", - 104: "tecUNFUNDED_PAYMENT", - 105: "tecFAILED_PROCESSING", - 121: "tecDIR_FULL", - 122: "tecINSUF_RESERVE_LINE", - 123: "tecINSUF_RESERVE_OFFER", - 124: "tecNO_DST", - 125: "tecNO_DST_INSUF_XRP", - 126: "tecNO_LINE_INSUF_RESERVE", - 127: "tecNO_LINE_REDUNDANT", - 128: "tecPATH_DRY", - 129: "tecUNFUNDED", // Deprecated, old ambiguous unfunded. - 130: "tecMASTER_DISABLED", - 131: "tecNO_REGULAR_KEY", - 132: "tecOWNERS" + 0 : 'tesSUCCESS', + 100: 'tecCLAIM', + 101: 'tecPATH_PARTIAL', + 102: 'tecUNFUNDED_ADD', + 103: 'tecUNFUNDED_OFFER', + 104: 'tecUNFUNDED_PAYMENT', + 105: 'tecFAILED_PROCESSING', + 121: 'tecDIR_FULL', + 122: 'tecINSUF_RESERVE_LINE', + 123: 'tecINSUF_RESERVE_OFFER', + 124: 'tecNO_DST', + 125: 'tecNO_DST_INSUF_XRP', + 126: 'tecNO_LINE_INSUF_RESERVE', + 127: 'tecNO_LINE_REDUNDANT', + 128: 'tecPATH_DRY', + 129: 'tecUNFUNDED', // Deprecated, old ambiguous unfunded. + 130: 'tecMASTER_DISABLED', + 131: 'tecNO_REGULAR_KEY', + 132: 'tecOWNERS' }; SerializedObject.prototype.to_json = function() { @@ -143,7 +152,7 @@ SerializedObject.prototype.to_json = function() { var key_and_value = stypes.parse_whatever(this); var key = key_and_value[0]; var value = key_and_value[1]; - output[key] = jsonify_structure(value,key); + output[key] = jsonify_structure(value, key); } this.pointer = old_pointer; @@ -151,41 +160,42 @@ SerializedObject.prototype.to_json = function() { return output; } -function jsonify_structure(thing, field_name) { +function jsonify_structure(structure, field_name) { var output; - switch (typeof thing) { + + switch (typeof structure) { case 'number': switch (field_name) { case 'LedgerEntryType': - output = LEDGER_ENTRY_TYPES[thing] || thing; + output = LEDGER_ENTRY_TYPES[structure] || thing; break; case 'TransactionResult': - output = TRANSACTION_RESULTS[thing] || thing; + output = TRANSACTION_RESULTS[structure] || thing; break; case 'TransactionType': - output = TRANSACTION_TYPES[thing] || thing; + output = TRANSACTION_TYPES[structure] || thing; break; default: - output = thing; + output = structure; } break; case 'object': - if (typeof thing.to_json === 'function') { - output = thing.to_json(); - } else if (thing === null) { - break; + if (!structure) break; //null + if (typeof structure.to_json === 'function') { + output = structure.to_json(); } else { - output = new thing.constructor; - var keys = Object.keys(thing); - for (var i=0; i 0xf) { - buffer.push(type_id & 0xff); + if (type_id > 0xF) { + buffer.push(type_id & 0xFF); } else { - buffer[0] += (type_id & 0xf) << 4; + buffer[0] += (type_id & 0xF) << 4; } - if (field_id > 0xf) { - buffer.push(field_id & 0xff); + if (field_id > 0xF) { + buffer.push(field_id & 0xFF); } else { - buffer[0] += field_id & 0xf; + buffer[0] += field_id & 0xF; } return buffer; @@ -269,7 +275,7 @@ SerializedObject.lookup_type_tx = function (id) { var keys = Object.keys(binformat.tx); var result = null; - for (var i=0; i