JS: Make Amount more flexible.

This commit is contained in:
Arthur Britto
2012-10-18 18:50:35 -07:00
committed by Stefan Thomas
parent 234272a0bf
commit 19c887e64f

View File

@@ -6,6 +6,12 @@ var jsbn = require('./jsbn.js');
var BigInteger = jsbn.BigInteger; var BigInteger = jsbn.BigInteger;
var accounts = {};
var setAccounts = function (accounts_new) {
accounts = accounts_new;
};
var UInt160 = function () { var UInt160 = function () {
// Internal form: // Internal form:
// 0, 1, 'iXXXXX', 20 byte string, or NaN. // 0, 1, 'iXXXXX', 20 byte string, or NaN.
@@ -13,8 +19,11 @@ var UInt160 = function () {
this.value = NaN; this.value = NaN;
}; };
// Return a new UInt160 from j.
UInt160.from_json = function (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() { UInt160.prototype.clone = function() {
@@ -140,12 +149,6 @@ Currency.prototype.to_human = function() {
return this.value ? this.value : "XNS"; return this.value ? this.value : "XNS";
}; };
var accounts = {};
var setAccounts = function (accounts_new) {
accounts = accounts_new;
};
var Amount = function () { var Amount = function () {
// Json format: // Json format:
// integer : XNS // integer : XNS
@@ -371,6 +374,9 @@ Amount.prototype.parse_value = function(j) {
this.canonicalize(); this.canonicalize();
} }
else if (j.constructor == BigInteger) {
this.value = j.clone();
}
else { else {
this.value = NaN; this.value = NaN;
} }
@@ -396,10 +402,10 @@ Amount.prototype.parse_json = function(j) {
} }
} }
else if ('object' === typeof j && j.currency) { else if ('object' === typeof j && j.currency) {
// Never XNS. // Parse the passed value to sanitize and copy it.
this.parse_value(j.value); 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); this.issuer.parse_json(j.issuer);
} }
else { else {