From 43f7b2654df93b9df968de25c1be61d0aadc2b39 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 18 Oct 2012 18:50:35 -0700 Subject: [PATCH] JS: Make Amount more flexible. --- js/amount.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/js/amount.js b/js/amount.js index c3dfa3905..6e4d99f86 100644 --- a/js/amount.js +++ b/js/amount.js @@ -6,6 +6,12 @@ var jsbn = require('./jsbn.js'); var BigInteger = jsbn.BigInteger; +var accounts = {}; + +var setAccounts = function (accounts_new) { + accounts = accounts_new; +}; + var UInt160 = function () { // Internal form: // 0, 1, 'iXXXXX', 20 byte string, or NaN. @@ -13,8 +19,11 @@ var UInt160 = function () { this.value = NaN; }; +// Return a new UInt160 from j. UInt160.from_json = function (j) { - return (new UInt160()).parse_json(j in accounts ? accounts[j].account : j); + return 'string' === typeof j + ? (new UInt160()).parse_json(j in accounts ? accounts[j].account : j) + : j.clone(); }; UInt160.prototype.clone = function() { @@ -140,12 +149,6 @@ Currency.prototype.to_human = function() { return this.value ? this.value : "XNS"; }; -var accounts = {}; - -var setAccounts = function (accounts_new) { - accounts = accounts_new; -}; - var Amount = function () { // Json format: // integer : XNS @@ -371,6 +374,9 @@ Amount.prototype.parse_value = function(j) { this.canonicalize(); } + else if (j.constructor == BigInteger) { + this.value = j.clone(); + } else { this.value = NaN; } @@ -396,10 +402,10 @@ Amount.prototype.parse_json = function(j) { } } else if ('object' === typeof j && j.currency) { - // Never XNS. + // Parse the passed value to sanitize and copy it. this.parse_value(j.value); - this.currency.parse_json(j.currency); + this.currency.parse_json(j.currency); // Never XNS. this.issuer.parse_json(j.issuer); } else {