diff --git a/src/core/serializedobject.js b/src/core/serializedobject.js index 193393d2e1..9875e6df3e 100644 --- a/src/core/serializedobject.js +++ b/src/core/serializedobject.js @@ -49,58 +49,10 @@ function SerializedObject(buf) { } this.pointer = 0; } - -SerializedObject.from_json = function(obj_) { - // Create a copy of the object so we don't modify it - const obj = extend(true, {}, obj_); - + +SerializedObject.from_json = function(obj) { const so = new SerializedObject(); - let typedef; - - if (typeof obj.TransactionType === 'number') { - obj.TransactionType = SerializedObject.lookup_type_tx(obj.TransactionType); - if (!obj.TransactionType) { - throw new Error('Transaction type ID is invalid.'); - } - } - - if (typeof obj.LedgerEntryType === 'number') { - obj.LedgerEntryType = SerializedObject.lookup_type_le(obj.LedgerEntryType); - - if (!obj.LedgerEntryType) { - throw new Error('LedgerEntryType ID is invalid.'); - } - } - - if (typeof obj.TransactionType === 'string') { - typedef = binformat.tx[obj.TransactionType]; - if (!Array.isArray(typedef)) { - throw new Error('Transaction type is invalid'); - } - - typedef = typedef.slice(); - obj.TransactionType = typedef.shift(); - } else if (typeof obj.LedgerEntryType === 'string') { - typedef = binformat.ledger[obj.LedgerEntryType]; - - if (!Array.isArray(typedef)) { - throw new Error('LedgerEntryType is invalid'); - } - - typedef = typedef.slice(); - obj.LedgerEntryType = typedef.shift(); - - } else if (typeof obj.AffectedNodes === 'object') { - typedef = binformat.metadata; - } else { - throw new Error('Object to be serialized must contain either' + - ' TransactionType, LedgerEntryType or AffectedNodes.'); - } - - // ND: This from_*json* seems a reasonable place to put validation of `json` - SerializedObject.check_fields(typedef, obj); - so.serialize(typedef, obj); - + so.parse_json(obj); return so; }; @@ -154,6 +106,57 @@ SerializedObject.check_fields = function(typedef, obj) { throw new Error(errorMessage); }; +SerializedObject.prototype.parse_json = function(obj_) { + const so = this; + // Create a copy of the object so we don't modify it + const obj = extend(true, {}, obj_); + let typedef; + + if (typeof obj.TransactionType === 'number') { + obj.TransactionType = SerializedObject.lookup_type_tx(obj.TransactionType); + if (!obj.TransactionType) { + throw new Error('Transaction type ID is invalid.'); + } + } + + if (typeof obj.LedgerEntryType === 'number') { + obj.LedgerEntryType = SerializedObject.lookup_type_le(obj.LedgerEntryType); + + if (!obj.LedgerEntryType) { + throw new Error('LedgerEntryType ID is invalid.'); + } + } + + if (typeof obj.TransactionType === 'string') { + typedef = binformat.tx[obj.TransactionType]; + if (!Array.isArray(typedef)) { + throw new Error('Transaction type is invalid'); + } + + typedef = typedef.slice(); + obj.TransactionType = typedef.shift(); + } else if (typeof obj.LedgerEntryType === 'string') { + typedef = binformat.ledger[obj.LedgerEntryType]; + + if (!Array.isArray(typedef)) { + throw new Error('LedgerEntryType is invalid'); + } + + typedef = typedef.slice(); + obj.LedgerEntryType = typedef.shift(); + + } else if (typeof obj.AffectedNodes === 'object') { + typedef = binformat.metadata; + } else { + throw new Error('Object to be serialized must contain either' + + ' TransactionType, LedgerEntryType or AffectedNodes.'); + } + + // ND: This from_*json* seems a reasonable place to put validation of `json` + SerializedObject.check_fields(typedef, obj); + so.serialize(typedef, obj); +}; + SerializedObject.prototype.append = function(bytes_) { const bytes = bytes_ instanceof SerializedObject ? bytes_.buffer : bytes_; @@ -176,14 +179,6 @@ SerializedObject.prototype.append = function(bytes_) { this.pointer += bytes.length; }; -SerializedObject.prototype.prepend = function(bytesArray) { - // Buffer is fixed length and does not have unshift - assert(Array.isArray(this.buffer)); - for (let i = bytesArray.length - 1; i >= 0; i--) { - this.buffer.unshift(bytesArray[i]); - } -}; - SerializedObject.prototype.resetPointer = function() { this.pointer = 0; }; diff --git a/src/core/transaction.js b/src/core/transaction.js index 2cbe981ab3..f9285cac33 100644 --- a/src/core/transaction.js +++ b/src/core/transaction.js @@ -436,8 +436,9 @@ Transaction.prototype.signingHash = function(testnet) { }; Transaction.prototype.signingData = function() { - const so = SerializedObject.from_json(this.tx_json); - so.prepend(hashprefixes.HASH_TX_SIGN_BYTES); + const so = new SerializedObject(); + so.append(hashprefixes.HASH_TX_SIGN_BYTES); + so.parse_json(this.tx_json); return so; };