JS: Restrict UInt160 parse_json to wire format.

This commit is contained in:
Arthur Britto
2013-01-03 00:54:20 -08:00
parent b0ea77809d
commit 01764aa090
2 changed files with 30 additions and 4 deletions

View File

@@ -254,6 +254,13 @@ UInt160.json_rewrite = function (j) {
return UInt160.from_json(j).to_json();
};
// Return a new UInt160 from j.
UInt160.from_generic = function (j) {
return 'string' === typeof j
? (new UInt160()).parse_generic(j)
: j.clone();
};
// Return a new UInt160 from j.
UInt160.from_json = function (j) {
return 'string' === typeof j
@@ -285,7 +292,7 @@ UInt160.prototype.is_valid = function () {
};
// value = NaN on error.
UInt160.prototype.parse_json = function (j) {
UInt160.prototype.parse_generic = function (j) {
// Canonicalize and validate
if (exports.config.accounts && j in exports.config.accounts)
j = exports.config.accounts[j].account;
@@ -329,6 +336,25 @@ UInt160.prototype.parse_json = function (j) {
return this;
};
// value = NaN on error.
UInt160.prototype.parse_json = function (j) {
// Canonicalize and validate
if (exports.config.accounts && j in exports.config.accounts)
j = exports.config.accounts[j].account;
if ('string' !== typeof j) {
this._value = NaN;
}
else if (j[0] === "r") {
this._value = decode_base_check(consts.VER_ACCOUNT_ID, j);
}
else {
this._value = NaN;
}
return this;
};
// Convert from internal form.
// XXX Json form should allow 0 and 1, C++ doesn't currently allow it.
UInt160.prototype.to_json = function () {