Skip to content

Commit

Permalink
Remove config singleton (global state)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed May 1, 2015
1 parent 3ba5a18 commit c655c2a
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 79 deletions.
10 changes: 0 additions & 10 deletions src/js/ripple/config.js

This file was deleted.

24 changes: 11 additions & 13 deletions src/js/ripple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,23 @@ exports.convertBase = require('./baseconverter');
exports.sjcl = require('./utils').sjcl;
exports.types = require('./serializedtypes');

exports.config = require('./config');

// camelCase to under_scored API conversion
function attachUnderscored(name) {
var o = exports[name];
var o = exports[name];

Object.keys(o.prototype).forEach(function(key) {
var UPPERCASE = /([A-Z]{1})[a-z]+/g;
Object.keys(o.prototype).forEach(function(key) {
var UPPERCASE = /([A-Z]{1})[a-z]+/g;

if (!UPPERCASE.test(key)) {
return;
}
if (!UPPERCASE.test(key)) {
return;
}

var underscored = key.replace(UPPERCASE, function(c) {
return '_' + c.toLowerCase();
});
var underscored = key.replace(UPPERCASE, function(c) {
return '_' + c.toLowerCase();
});

o.prototype[underscored] = o.prototype[key];
});
o.prototype[underscored] = o.prototype[key];
});
}

['Remote',
Expand Down
24 changes: 0 additions & 24 deletions src/js/ripple/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var SerializedObject = require('./serializedobject').SerializedObject;
var RippleError = require('./rippleerror').RippleError;
var utils = require('./utils');
var hashprefixes = require('./hashprefixes');
var config = require('./config');
var log = require('./log').internal.sub('remote');

/**
Expand Down Expand Up @@ -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
*
Expand Down
9 changes: 4 additions & 5 deletions src/js/ripple/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var Seed = require('./seed').Seed;
var SerializedObject = require('./serializedobject').SerializedObject;
var RippleError = require('./rippleerror').RippleError;
var hashprefixes = require('./hashprefixes');
var config = require('./config');
var log = require('./log').internal.sub('transaction');

/**
Expand Down Expand Up @@ -433,8 +432,8 @@ Transaction.prototype.serialize = function() {
return SerializedObject.from_json(this.tx_json);
};

Transaction.prototype.signingHash = function() {
return this.hash(config.testnet ? 'HASH_TX_SIGN_TESTNET' : 'HASH_TX_SIGN');
Transaction.prototype.signingHash = function(testnet) {
return this.hash(testnet ? 'HASH_TX_SIGN_TESTNET' : 'HASH_TX_SIGN');
};

Transaction.prototype.hash = function(prefix, asUINT256, serialized) {
Expand All @@ -452,13 +451,13 @@ Transaction.prototype.hash = function(prefix, asUINT256, serialized) {
return asUINT256 ? hash : hash.to_hex();
};

Transaction.prototype.sign = function() {
Transaction.prototype.sign = function(testnet) {
var seed = Seed.from_json(this._secret);

var prev_sig = 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 (prev_sig && hash === this.previousSigningHash) {
Expand Down
7 changes: 0 additions & 7 deletions src/js/ripple/uint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

var utils = require('./utils');
var sjcl = utils.sjcl;
var config = require('./config');

//
// Abstract UInt class
Expand Down Expand Up @@ -131,12 +130,6 @@ UInt.prototype._update = function() {

// value = NaN on error.
UInt.prototype.parse_generic = function(j) {
// Canonicalize and validate
if (config.accounts && (j in config.accounts)) {
j = config.accounts[j].account;
}


switch (j) {
case undefined:
case '0':
Expand Down
6 changes: 0 additions & 6 deletions src/js/ripple/uint160.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var utils = require('./utils');
var config = require('./config');
var extend = require('extend');

var UInt = require('./uint').UInt;
Expand Down Expand Up @@ -40,11 +39,6 @@ UInt160.prototype.get_version = function() {

// value = NaN on error.
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)) {
// Allow raw numbers - DEPRECATED
// This is used mostly by the test suite and is supported
Expand Down
14 changes: 0 additions & 14 deletions test/amount-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
var assert = require('assert');
var Amount = require('ripple-lib').Amount;
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('Negatives', function() {
Expand Down Expand Up @@ -309,9 +304,6 @@ describe('Amount', function() {
it('Parse rrrrrrrrrrrrrrrrrrrrBZbvji export', function () {
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 () {
assert(UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp'));
});
Expand Down Expand Up @@ -360,12 +352,6 @@ describe('Amount', 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');
});
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 () {
assert.strictEqual('0/XRP', Amount.from_json('0').to_text_full());
});
Expand Down

0 comments on commit c655c2a

Please sign in to comment.