JS: Restrict UInt160 parse_json to wire format.

This commit is contained in:
Arthur Britto
2013-01-03 00:54:20 -08:00
committed by Stefan Thomas
parent a574207b40
commit e5c6acde7d
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 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. // Return a new UInt160 from j.
UInt160.from_json = function (j) { UInt160.from_json = function (j) {
return 'string' === typeof j return 'string' === typeof j
@@ -285,7 +292,7 @@ UInt160.prototype.is_valid = function () {
}; };
// value = NaN on error. // value = NaN on error.
UInt160.prototype.parse_json = function (j) { UInt160.prototype.parse_generic = function (j) {
// Canonicalize and validate // Canonicalize and validate
if (exports.config.accounts && j in exports.config.accounts) if (exports.config.accounts && j in exports.config.accounts)
j = exports.config.accounts[j].account; j = exports.config.accounts[j].account;
@@ -329,6 +336,25 @@ UInt160.prototype.parse_json = function (j) {
return this; 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. // Convert from internal form.
// XXX Json form should allow 0 and 1, C++ doesn't currently allow it. // XXX Json form should allow 0 and 1, C++ doesn't currently allow it.
UInt160.prototype.to_json = function () { UInt160.prototype.to_json = function () {

View File

@@ -17,13 +17,13 @@ var config = require('./config.js');
buster.testCase("Amount", { buster.testCase("Amount", {
"UInt160" : { "UInt160" : {
"Parse 0" : function () { "Parse 0" : function () {
buster.assert.equals(nbi(), UInt160.from_json("0")._value); buster.assert.equals(nbi(), UInt160.from_generic("0")._value);
}, },
"Parse 0 export" : function () { "Parse 0 export" : function () {
buster.assert.equals(amount.consts.address_xns, UInt160.from_json("0").to_json()); buster.assert.equals(amount.consts.address_xns, UInt160.from_generic("0").to_json());
}, },
"Parse 1" : function () { "Parse 1" : function () {
buster.assert.equals(new BigInteger([1]), UInt160.from_json("1")._value); buster.assert.equals(new BigInteger([1]), UInt160.from_generic("1")._value);
}, },
"Parse rrrrrrrrrrrrrrrrrrrrrhoLvTp export" : function () { "Parse rrrrrrrrrrrrrrrrrrrrrhoLvTp export" : function () {
buster.assert.equals(amount.consts.address_xns, UInt160.from_json("rrrrrrrrrrrrrrrrrrrrrhoLvTp").to_json()); buster.assert.equals(amount.consts.address_xns, UInt160.from_json("rrrrrrrrrrrrrrrrrrrrrhoLvTp").to_json());