Skip to content

Commit

Permalink
[WIP] Extract parse_json
Browse files Browse the repository at this point in the history
  • Loading branch information
sublimator committed Jun 23, 2015
1 parent 12fdbb2 commit 92d3e0e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 61 deletions.
113 changes: 54 additions & 59 deletions src/core/serializedobject.js
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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_;

Expand All @@ -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;
};
Expand Down
5 changes: 3 additions & 2 deletions src/core/transaction.js
Expand Up @@ -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;
};

Expand Down

0 comments on commit 92d3e0e

Please sign in to comment.