Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
JoelKatz
2012-11-08 04:36:57 -08:00
12 changed files with 67 additions and 316 deletions

View File

@@ -6,13 +6,10 @@ var bn = require('./sjcl/core.js').bn;
var utils = require('./utils.js');
var jsbn = require('./jsbn.js');
// Don't include in browser context.
var config = require('../../test/config.js');
var BigInteger = jsbn.BigInteger;
var nbi = jsbn.nbi;
var alphabets = {
var alphabets = {
'ripple' : "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
'bitcoin' : "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
};
@@ -158,8 +155,8 @@ UInt160.prototype.equals = function(d) {
// 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 (exports.config.accounts && j in exports.config.accounts)
j = exports.config.accounts[j].account;
switch (j) {
case undefined:
@@ -603,6 +600,8 @@ exports.Amount = Amount;
exports.Currency = Currency;
exports.UInt160 = UInt160;
exports.config = {};
exports.consts = {
'address_xns' : "rrrrrrrrrrrrrrrrrrrrrhoLvTp",
'address_one' : "rrrrrrrrrrrrrrrrrrrrBZbvji",

View File

@@ -1,291 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var isArray = Array.isArray;
var domain;
function EventEmitter() {
if (exports.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
}
}
exports.EventEmitter = EventEmitter;
// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. This is a useful default which
// helps finding memory leaks.
//
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
EventEmitter.prototype.setMaxListeners = function(n) {
if (!this._events) this._events = {};
this._maxListeners = n;
};
// non-global reference, for speed.
var PROCESS;
EventEmitter.prototype.emit = function() {
var type = arguments[0];
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events || !this._events.error ||
(isArray(this._events.error) && !this._events.error.length))
{
if (this.domain) {
var er = arguments[1];
er.domain_emitter = this;
er.domain = this.domain;
er.domain_thrown = false;
this.domain.emit('error', er);
return false;
}
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
throw new Error("Uncaught, unspecified 'error' event.");
}
return false;
}
}
if (!this._events) return false;
var handler = this._events[type];
if (!handler) return false;
if (typeof handler == 'function') {
if (this.domain) {
PROCESS = PROCESS || process;
if (this !== PROCESS) {
this.domain.enter();
}
}
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
var l = arguments.length;
var args = new Array(l - 1);
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
handler.apply(this, args);
}
if (this.domain && this !== PROCESS) {
this.domain.exit();
}
return true;
} else if (isArray(handler)) {
if (this.domain) {
PROCESS = PROCESS || process;
if (this !== PROCESS) {
this.domain.enter();
}
}
var l = arguments.length;
var args = new Array(l - 1);
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
if (this.domain && this !== PROCESS) {
this.domain.exit();
}
return true;
} else {
return false;
}
};
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
if (this._events.newListener) {
this.emit('newListener', type, typeof listener.listener === 'function' ?
listener.listener : listener);
}
if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
} else if (isArray(this._events[type])) {
// If we've already got an array, just append.
this._events[type].push(listener);
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}
// Check for listener leak
if (isArray(this._events[type]) && !this._events[type].warned) {
var m;
if (this._maxListeners !== undefined) {
m = this._maxListeners;
} else {
m = defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
console.trace();
}
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('.once only takes instances of Function');
}
var self = this;
function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
};
g.listener = listener;
self.on(type, g);
return this;
};
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
}
// does not use listeners(), so no side effect of creating _events[type]
if (!this._events || !this._events[type]) return this;
var list = this._events[type];
if (isArray(list)) {
var position = -1;
for (var i = 0, length = list.length; i < length; i++) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener))
{
position = i;
break;
}
}
if (position < 0) return this;
list.splice(position, 1);
if (list.length == 0)
delete this._events[type];
if (this._events.removeListener) {
this.emit('removeListener', type, listener);
}
} else if (list === listener ||
(list.listener && list.listener === listener))
{
delete this._events[type];
if (this._events.removeListener) {
this.emit('removeListener', type, listener);
}
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
if (!this._events) return this;
// fast path
if (!this._events.removeListener) {
if (arguments.length === 0) {
this._events = {};
} else if (type && this._events && this._events[type]) {
this._events[type] = null;
}
return this;
}
// slow(ish) path, emit 'removeListener' events for all removals
if (arguments.length === 0) {
for (var key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
var listeners = this._events[type];
if (isArray(listeners)) {
while (listeners.length) {
// LIFO order
this.removeListener(type, listeners[listeners.length - 1]);
}
} else if (listeners) {
this.removeListener(type, listeners);
}
this._events[type] = null;
return this;
};
EventEmitter.prototype.listeners = function(type) {
if (!this._events || !this._events[type]) return [];
if (!isArray(this._events[type])) {
return [this._events[type]];
}
return this._events[type].slice(0);
};

View File

@@ -17,13 +17,10 @@
// npm
var WebSocket = require('ws');
var EventEmitter = require('./events').EventEmitter;
var EventEmitter = require('events').EventEmitter;
var Amount = require('./amount.js').Amount;
var UInt160 = require('./amount.js').UInt160;
// Don't include in browser context.
var config = require('../../test/config.js');
// Request events emmitted:
// 'success' : Request successful.
// 'error' : Request failed.
@@ -159,7 +156,6 @@ var Remote = function (trusted, websocket_ip, websocket_port, trace) {
this.state = 'offline'; // 'online', 'offline'
this.retry_timer = undefined;
this.retry = undefined;
this.config = config || { 'accounts' : {}};
// Cache information for accounts.
this.accounts = {
@@ -169,7 +165,14 @@ var Remote = function (trusted, websocket_ip, websocket_port, trace) {
// account : { seq : __ }
};
// List of secrets that we know about.
this.secrets = {
// Secrets can be set by calling set_secret(account, secret).
// account : secret
};
// Cache for various ledgers.
// XXX Clear when ledger advances.
this.ledgers = {
@@ -182,9 +185,23 @@ var Remote = function (trusted, websocket_ip, websocket_port, trace) {
Remote.prototype = new EventEmitter;
Remote.from_config = function (name, trace) {
var serverConfig = config.servers[name];
var serverConfig = exports.config.servers[name];
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port, trace);
var remote = new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port, trace);
for (var account in exports.config.accounts) {
var accountInfo = exports.config.accounts[account];
if ("object" === typeof accountInfo) {
if (accountInfo.secret) {
// Index by nickname ...
remote.set_secret(account, accountInfo.secret);
// ... and by account ID
remote.set_secret(accountInfo.account, accountInfo.secret);
}
}
}
return remote;
};
var isTemMalformed = function (engine_result_code) {
@@ -778,6 +795,12 @@ Remote.prototype.dirty_account_root = function (account) {
delete this.ledgers.current.account_root[account];
};
// Store a secret - allows the Remote to automatically fill out auth information.
Remote.prototype.set_secret = function (account, secret) {
this.secrets[account] = secret;
};
// Return a request to get a ripple balance.
//
// --> account: String
@@ -1140,8 +1163,8 @@ Transaction.prototype.set_flags = function (flags) {
//
Transaction.prototype._account_secret = function (account) {
// Fill in secret from config, if needed.
return this.remote.config.accounts[account] ? this.remote.config.accounts[account].secret : undefined;
// Fill in secret from remote, if available.
return this.remote.secrets[account];
};
// Options:
@@ -1268,6 +1291,7 @@ Transaction.prototype.wallet_add = function (src, amount, authorized_key, public
return this;
};
exports.config = {};
exports.Remote = Remote;
// vim:sw=2:sts=2:ts=8