Remove config singleton (global state)

This commit is contained in:
Chris Clark
2015-04-30 18:30:53 -07:00
parent 3ba5a18b91
commit c655c2a20e
7 changed files with 15 additions and 79 deletions

View File

@@ -1,10 +0,0 @@
// This object serves as a singleton to store config options
var extend = require('extend');
var config = module.exports = {
load: function (newOpts) {
extend(config, newOpts);
return config;
}
};

View File

@@ -33,8 +33,6 @@ exports.convertBase = require('./baseconverter');
exports.sjcl = require('./utils').sjcl; exports.sjcl = require('./utils').sjcl;
exports.types = require('./serializedtypes'); exports.types = require('./serializedtypes');
exports.config = require('./config');
// camelCase to under_scored API conversion // camelCase to under_scored API conversion
function attachUnderscored(name) { function attachUnderscored(name) {
var o = exports[name]; var o = exports[name];

View File

@@ -34,7 +34,6 @@ var SerializedObject = require('./serializedobject').SerializedObject;
var RippleError = require('./rippleerror').RippleError; var RippleError = require('./rippleerror').RippleError;
var utils = require('./utils'); var utils = require('./utils');
var hashprefixes = require('./hashprefixes'); var hashprefixes = require('./hashprefixes');
var config = require('./config');
var log = require('./log').internal.sub('remote'); var log = require('./log').internal.sub('remote');
/** /**
@@ -238,29 +237,6 @@ Remote.flags = {
} }
}; };
Remote.from_config = function(obj, trace) {
var serverConfig = (typeof obj === 'string') ? config.servers[obj] : obj;
var remote = new Remote(serverConfig, trace);
function initializeAccount(account) {
var accountInfo = config.accounts[account];
if (typeof accountInfo === 'object') {
if (accountInfo.secret) {
// Index by nickname
remote.setSecret(account, accountInfo.secret);
// Index by account ID
remote.setSecret(accountInfo.account, accountInfo.secret);
}
}
}
if (config.accounts) {
Object.keys(config.accounts).forEach(initializeAccount);
}
return remote;
};
/** /**
* Check that server message is valid * Check that server message is valid
* *

View File

@@ -11,7 +11,6 @@ var Seed = require('./seed').Seed;
var SerializedObject = require('./serializedobject').SerializedObject; var SerializedObject = require('./serializedobject').SerializedObject;
var RippleError = require('./rippleerror').RippleError; var RippleError = require('./rippleerror').RippleError;
var hashprefixes = require('./hashprefixes'); var hashprefixes = require('./hashprefixes');
var config = require('./config');
var log = require('./log').internal.sub('transaction'); var log = require('./log').internal.sub('transaction');
/** /**
@@ -433,8 +432,8 @@ Transaction.prototype.serialize = function() {
return SerializedObject.from_json(this.tx_json); return SerializedObject.from_json(this.tx_json);
}; };
Transaction.prototype.signingHash = function() { Transaction.prototype.signingHash = function(testnet) {
return this.hash(config.testnet ? 'HASH_TX_SIGN_TESTNET' : 'HASH_TX_SIGN'); return this.hash(testnet ? 'HASH_TX_SIGN_TESTNET' : 'HASH_TX_SIGN');
}; };
Transaction.prototype.hash = function(prefix, asUINT256, serialized) { Transaction.prototype.hash = function(prefix, asUINT256, serialized) {
@@ -452,13 +451,13 @@ Transaction.prototype.hash = function(prefix, asUINT256, serialized) {
return asUINT256 ? hash : hash.to_hex(); return asUINT256 ? hash : hash.to_hex();
}; };
Transaction.prototype.sign = function() { Transaction.prototype.sign = function(testnet) {
var seed = Seed.from_json(this._secret); var seed = Seed.from_json(this._secret);
var prev_sig = this.tx_json.TxnSignature; var prev_sig = this.tx_json.TxnSignature;
delete this.tx_json.TxnSignature; delete this.tx_json.TxnSignature;
var hash = this.signingHash(); var hash = this.signingHash(testnet);
// If the hash is the same, we can re-use the previous signature // If the hash is the same, we can re-use the previous signature
if (prev_sig && hash === this.previousSigningHash) { if (prev_sig && hash === this.previousSigningHash) {

View File

@@ -4,7 +4,6 @@
var utils = require('./utils'); var utils = require('./utils');
var sjcl = utils.sjcl; var sjcl = utils.sjcl;
var config = require('./config');
// //
// Abstract UInt class // Abstract UInt class
@@ -131,12 +130,6 @@ UInt.prototype._update = function() {
// value = NaN on error. // value = NaN on error.
UInt.prototype.parse_generic = function(j) { UInt.prototype.parse_generic = function(j) {
// Canonicalize and validate
if (config.accounts && (j in config.accounts)) {
j = config.accounts[j].account;
}
switch (j) { switch (j) {
case undefined: case undefined:
case '0': case '0':

View File

@@ -1,7 +1,6 @@
'use strict'; 'use strict';
var utils = require('./utils'); var utils = require('./utils');
var config = require('./config');
var extend = require('extend'); var extend = require('extend');
var UInt = require('./uint').UInt; var UInt = require('./uint').UInt;
@@ -40,11 +39,6 @@ UInt160.prototype.get_version = function() {
// value = NaN on error. // value = NaN on error.
UInt160.prototype.parse_json = function(j) { UInt160.prototype.parse_json = function(j) {
// Canonicalize and validate
if (config.accounts && (j in config.accounts)) {
j = config.accounts[j].account;
}
if (typeof j === 'number' && !isNaN(j)) { if (typeof j === 'number' && !isNaN(j)) {
// Allow raw numbers - DEPRECATED // Allow raw numbers - DEPRECATED
// This is used mostly by the test suite and is supported // This is used mostly by the test suite and is supported

View File

@@ -3,11 +3,6 @@
var assert = require('assert'); var assert = require('assert');
var Amount = require('ripple-lib').Amount; var Amount = require('ripple-lib').Amount;
var UInt160 = require('ripple-lib').UInt160; var UInt160 = require('ripple-lib').UInt160;
var load_config = require('ripple-lib').config.load;
var config = require('./config-example');
load_config(config);
describe('Amount', function() { describe('Amount', function() {
describe('Negatives', function() { describe('Negatives', function() {
@@ -309,9 +304,6 @@ describe('Amount', function() {
it('Parse rrrrrrrrrrrrrrrrrrrrBZbvji export', function () { it('Parse rrrrrrrrrrrrrrrrrrrrBZbvji export', function () {
assert.strictEqual(UInt160.ACCOUNT_ONE, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrBZbvji').to_json()); assert.strictEqual(UInt160.ACCOUNT_ONE, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrBZbvji').to_json());
}); });
it('Parse mtgox export', function () {
assert.strictEqual(config.accounts.mtgox.account, UInt160.from_json('mtgox').to_json());
});
it('is_valid rrrrrrrrrrrrrrrrrrrrrhoLvTp', function () { it('is_valid rrrrrrrrrrrrrrrrrrrrrhoLvTp', function () {
assert(UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp')); assert(UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp'));
}); });
@@ -360,12 +352,6 @@ describe('Amount', function() {
it('parse dem human', function() { it('parse dem human', function() {
assert.strictEqual(Amount.from_json('10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'); assert.strictEqual(Amount.from_json('10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
}); });
it('Parse 800/USD/mtgox', function () {
assert.strictEqual('800/USD/' + config.accounts.mtgox.account, Amount.from_json('800/USD/mtgox').to_text_full());
});
it('Parse 800/USD/mtgox human', function () {
assert.strictEqual('800/USD/' + config.accounts.mtgox.account, Amount.from_json('800/USD/mtgox').to_human_full());
});
it('Parse native 0', function () { it('Parse native 0', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').to_text_full()); assert.strictEqual('0/XRP', Amount.from_json('0').to_text_full());
}); });