From 8333c3fbfcbff3fc75bbf467a2b05b1c93bb639e Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Thu, 15 Aug 2013 11:53:38 -0700 Subject: [PATCH] Fix for typeof NaN === number --- src/js/ripple/uint160.js | 51 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/js/ripple/uint160.js b/src/js/ripple/uint160.js index 4aaf9da0..1a893f60 100644 --- a/src/js/ripple/uint160.js +++ b/src/js/ripple/uint160.js @@ -1,4 +1,3 @@ - var sjcl = require('../../../build/sjcl'); var utils = require('./utils'); var config = require('./config'); @@ -8,8 +7,8 @@ var extend = require('extend'); var BigInteger = jsbn.BigInteger; var nbi = jsbn.nbi; -var UInt = require('./uint').UInt, - Base = require('./base').Base; +var UInt = require('./uint').UInt; +var Base = require('./base').Base; // // UInt160 support @@ -24,30 +23,28 @@ UInt160.width = 20; UInt160.prototype = extend({}, UInt.prototype); UInt160.prototype.constructor = UInt160; -var ACCOUNT_ZERO = UInt160.ACCOUNT_ZERO = "rrrrrrrrrrrrrrrrrrrrrhoLvTp"; -var ACCOUNT_ONE = UInt160.ACCOUNT_ONE = "rrrrrrrrrrrrrrrrrrrrBZbvji"; -var HEX_ZERO = UInt160.HEX_ZERO = "0000000000000000000000000000000000000000"; -var HEX_ONE = UInt160.HEX_ONE = "0000000000000000000000000000000000000001"; -var STR_ZERO = UInt160.STR_ZERO = utils.hexToString(HEX_ZERO); -var STR_ONE = UInt160.STR_ONE = utils.hexToString(HEX_ONE); +var ACCOUNT_ZERO = UInt160.ACCOUNT_ZERO = 'rrrrrrrrrrrrrrrrrrrrrhoLvTp'; +var ACCOUNT_ONE = UInt160.ACCOUNT_ONE = 'rrrrrrrrrrrrrrrrrrrrBZbvji'; +var HEX_ZERO = UInt160.HEX_ZERO = '0000000000000000000000000000000000000000'; +var HEX_ONE = UInt160.HEX_ONE = '0000000000000000000000000000000000000001'; +var STR_ZERO = UInt160.STR_ZERO = utils.hexToString(HEX_ZERO); +var STR_ONE = UInt160.STR_ONE = utils.hexToString(HEX_ONE); // value = NaN on error. UInt160.prototype.parse_json = function (j) { // Canonicalize and validate - if (config.accounts && j in config.accounts) + if (config.accounts && j in config.accounts) { j = config.accounts[j].account; + } - if ('number' === typeof j) { + if (typeof j === 'number' && !isNaN(j)) { this._value = new BigInteger(String(j)); - } - else if ('string' !== typeof j) { - this._value = NaN; - } - else if (j[0] === "r") { + } else if (typeof j !== 'string') { + this._value = NaN; + } else if (j[0] === 'r') { this._value = Base.decode_check(Base.VER_ACCOUNT_ID, j); - } - else { - this._value = NaN; + } else { + this._value = NaN; } return this; @@ -55,15 +52,15 @@ UInt160.prototype.parse_json = function (j) { // XXX Json form should allow 0 and 1, C++ doesn't currently allow it. UInt160.prototype.to_json = function (opts) { - opts = opts || {}; + var opts = opts || {}; + var output = NaN; - if (!(this._value instanceof BigInteger)) - return NaN; - - var output = Base.encode_check(Base.VER_ACCOUNT_ID, this.to_bytes()); - - if (opts.gateways && output in opts.gateways) - output = opts.gateways[output]; + if (this._value instanceof BigInteger) { + output = Base.encode_check(Base.VER_ACCOUNT_ID, this.to_bytes()); + if (opts.gateways && output in opts.gateways) { + output = opts.gateways[output]; + } + } return output; };