Fix array and inner-object parsing

This commit is contained in:
jatchili
2013-08-20 17:36:12 -07:00
parent 083377651f
commit 7a5631a2ce
3 changed files with 10 additions and 7 deletions

View File

@@ -6,7 +6,7 @@ var binformat = require('./binformat'),
var UInt256 = require('./uint256').UInt256; var UInt256 = require('./uint256').UInt256;
var SerializedObject = function (buf) { var SerializedObject = function (buf) {
if (Array.isArray(buf)) { if (Array.isArray(buf) || (Buffer && Buffer.isBuffer(buf)) ) {
this.buffer = buf; this.buffer = buf;
} else if ("string" === typeof buf) { } else if ("string" === typeof buf) {
this.buffer = sjcl.codec.bytes.fromBits(sjcl.codec.hex.toBits(buf)); this.buffer = sjcl.codec.bytes.fromBits(sjcl.codec.hex.toBits(buf));
@@ -157,7 +157,7 @@ function jsonify_structure(thing,field_name) {
} else if (typeof_thing === "string") { } else if (typeof_thing === "string") {
output = "\"" + thing + "\""; output = "\"" + thing + "\"";
} else if ( "function" === typeof thing.to_json ) { } else if ( "function" === typeof thing.to_json ) {
output = "\"" + thing.to_json() + "\""; output = JSON.stringify(thing.to_json());
} else if (Array.isArray(thing)) { } else if (Array.isArray(thing)) {
//console.log("here2"); //console.log("here2");
//iterate over array [] //iterate over array []
@@ -183,6 +183,7 @@ function jsonify_structure(thing,field_name) {
output = output.slice(0,-1); output = output.slice(0,-1);
output += "}"; output += "}";
} }
return output; return output;
} }

View File

@@ -383,7 +383,7 @@ var STAccount = exports.Account = new SerializedType({
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 (false && !result.is_valid()) {
throw new Error("Invalid Account"); throw new Error("Invalid Account");
} }
return result; return result;
@@ -549,12 +549,10 @@ var parse_whatever = exports.parse_whatever = function(so) {
if (field_bits === 0) { if (field_bits === 0) {
field_name = FIELDS_MAP[type_bits][so.read(1)[0]]; field_name = FIELDS_MAP[type_bits][so.read(1)[0]];
} else { } else {
//console.log("!!!!!!!!!!IJOIOJIOJO", type_bits, field_bits);
field_name = FIELDS_MAP[type_bits][field_bits]; field_name = FIELDS_MAP[type_bits][field_bits];
} }
//console.log("PARSING WHATEVER!!!!", type_bits, type, field_name);
if ("undefined" === typeof field_name) { if ("undefined" === typeof field_name) {
throw Error("Unknown field"); throw Error("Unknown field "+tag_byte);
} else { } else {
return [field_name, type.parse(so)]; //key, value return [field_name, type.parse(so)]; //key, value
} }
@@ -596,6 +594,7 @@ var STObject = exports.Object = new SerializedType({
var output = {}; var output = {};
while (true) { while (true) {
if (so.peek(1)[0] === 0xe1) { //ending marker if (so.peek(1)[0] === 0xe1) { //ending marker
so.read(1);
break; break;
} else { } else {
//console.log("WTF M8"); //console.log("WTF M8");
@@ -626,6 +625,7 @@ var STArray = exports.Array = new SerializedType({
var output = []; var output = [];
while (true) { while (true) {
if (so.peek(1)[0] === 0xf1) { //ending marker if (so.peek(1)[0] === 0xf1) { //ending marker
so.read(1);
break; break;
} else { } else {
var key_and_value = parse_whatever(so); var key_and_value = parse_whatever(so);

View File

@@ -172,9 +172,10 @@ UInt.prototype.parse_bits = function (j) {
return this; return this;
}; };
UInt.prototype.parse_bytes = function (j) { UInt.prototype.parse_bytes = function (j) {
if (!Array.isArray(j) || j.length !== this.constructor.width) { if (!Array.isArray(j) || j.length !== this.constructor.width) {
this._value = NaN; this._value = NaN;
} else { } else {
this._value = new BigInteger(j, 256); this._value = new BigInteger(j, 256);
} }
@@ -182,6 +183,7 @@ UInt.prototype.parse_bytes = function (j) {
return this; return this;
}; };
UInt.prototype.parse_json = UInt.prototype.parse_hex; UInt.prototype.parse_json = UInt.prototype.parse_hex;
UInt.prototype.parse_bn = function (j) { UInt.prototype.parse_bn = function (j) {