2014-04-14 14:23:00 -07:00
|
|
|
var imports = require('soop').imports();
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
var API = function(opts) {
|
2014-04-14 14:23:00 -07:00
|
|
|
this._init(opts);
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype._init = function(opts) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
2014-04-15 11:12:37 -07:00
|
|
|
opts = opts || {};
|
2014-04-15 10:50:14 -07:00
|
|
|
self.opts = opts;
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-16 17:02:53 -07:00
|
|
|
var WalletFactory = require('soop').load('./js/models/core/WalletFactory', {
|
2014-04-16 07:55:18 -07:00
|
|
|
Storage: opts.Storage || require('./test/mocks/FakeStorage'),
|
2014-06-03 14:38:56 -07:00
|
|
|
Network: opts.Network || require('./js/models/network/WebRTC'),
|
2014-04-15 15:17:48 -07:00
|
|
|
Blockchain: opts.Blockchain || require('./js/models/blockchain/Insight')
|
2014-04-15 10:50:14 -07:00
|
|
|
});
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-16 17:02:53 -07:00
|
|
|
this.walletFactory = new WalletFactory(opts);
|
2014-04-14 14:23:00 -07:00
|
|
|
};
|
|
|
|
|
2014-04-15 08:07:04 -07:00
|
|
|
API._coerceArgTypes = function(args, argTypes) {
|
|
|
|
for (var i in args) {
|
|
|
|
var arg = args[i];
|
|
|
|
var argType = argTypes[i][1];
|
|
|
|
if (typeof arg == 'string') {
|
|
|
|
switch (argType) {
|
|
|
|
case 'object':
|
|
|
|
args[i] = JSON.parse(arg);
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
args[i] = Number(arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype._command = function(command, args, callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
2014-04-15 06:50:54 -07:00
|
|
|
|
|
|
|
if (!command || command[0] == "_")
|
2014-04-14 14:23:00 -07:00
|
|
|
return callback(new Error('invalid command'));
|
|
|
|
|
2014-04-15 08:07:04 -07:00
|
|
|
if (!API._checkArgTypes(command, args)) {
|
|
|
|
var argTypes = API.prototype[command].argTypes;
|
|
|
|
API._coerceArgTypes(args, argTypes)
|
2014-04-15 08:21:24 -07:00
|
|
|
if (!API._checkArgTypes(command, args))
|
2014-04-15 12:53:23 -07:00
|
|
|
throw new Error('invalid arguments');
|
2014-04-15 08:07:04 -07:00
|
|
|
}
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
if (typeof self["_cmd_" + command] == 'function') {
|
2014-04-15 06:50:54 -07:00
|
|
|
var f = API.prototype[command];
|
2014-04-14 14:23:00 -07:00
|
|
|
if (f.argTypes[f.argTypes.length-1][1] == 'function')
|
2014-04-15 12:53:23 -07:00
|
|
|
return self["_cmd_" + command].apply(self, args.concat([callback]));
|
2014-04-14 14:23:00 -07:00
|
|
|
else
|
2014-04-15 12:53:23 -07:00
|
|
|
return callback(null, self["_cmd_" + command].apply(self, args));
|
2014-04-14 14:23:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return callback(new Error('invalid command'));
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API._checkArgTypes = function(command, args) {
|
|
|
|
var f = API.prototype[command];
|
2014-04-15 08:21:24 -07:00
|
|
|
|
|
|
|
if (f.argTypes.length != args.length) {
|
|
|
|
|
|
|
|
//if the function doesn't have a callback
|
|
|
|
if (!(f.argTypes.length == args.length + 1 && f.argTypes[f.argTypes.length-1][1] == 'function'))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:23:00 -07:00
|
|
|
for (var i in args) {
|
|
|
|
if (typeof args[i] != f.argTypes[i][1])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
function decorate(command, argTypes) {
|
|
|
|
var d = function() {
|
|
|
|
API.prototype._command.call(this, command, Array.prototype.slice.call(arguments, 0));
|
|
|
|
};
|
|
|
|
|
|
|
|
d.argTypes = argTypes;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
|
|
|
API.prototype._cmd_echo = function(str, callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, str);
|
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype.echo = decorate('echo', [
|
2014-04-14 14:23:00 -07:00
|
|
|
['str', 'string'],
|
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype._cmd_echoNumber = function(num, callback) {
|
2014-04-15 08:07:04 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, num);
|
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype.echoNumber = decorate('echoNumber', [
|
2014-04-15 08:07:04 -07:00
|
|
|
['num', 'number'],
|
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-15 08:07:04 -07:00
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype._cmd_echoObject = function(obj, callback) {
|
2014-04-15 08:07:04 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, obj);
|
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype.echoObject = decorate('echoObject', [
|
2014-04-15 08:07:04 -07:00
|
|
|
['obj', 'object'],
|
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-15 08:07:04 -07:00
|
|
|
|
2014-04-14 19:01:56 -07:00
|
|
|
/*
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.getBalance = function(callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, self.wallet.getBalance([]));
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.getBalance.argTypes =
|
2014-04-14 14:23:00 -07:00
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
2014-04-14 19:01:56 -07:00
|
|
|
*/
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype._cmd_getArgTypes = function(command, callback) {
|
2014-04-15 05:19:07 -07:00
|
|
|
var self = this;
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
if (command[0] == '_' || typeof API.prototype[command] != 'function')
|
2014-04-15 05:19:07 -07:00
|
|
|
return callback(new Error('Invalid command'));
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
var argTypes = API.prototype[command].argTypes;
|
2014-04-15 05:19:07 -07:00
|
|
|
|
|
|
|
return callback(null, argTypes);
|
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype.getArgTypes = decorate('getArgTypes', [
|
2014-04-15 05:19:07 -07:00
|
|
|
['command', 'string'],
|
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-15 05:19:07 -07:00
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype._cmd_getCommands = function(callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var fs = [];
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
for (var i in API.prototype) {
|
|
|
|
var f = API.prototype[i];
|
2014-04-14 14:23:00 -07:00
|
|
|
if (typeof f == 'function' && i[0] != "_")
|
|
|
|
fs.push(i);
|
|
|
|
};
|
|
|
|
|
|
|
|
return callback(null, fs);
|
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype.getCommands = decorate('getCommands', [
|
2014-04-14 14:23:00 -07:00
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-24 19:42:59 -07:00
|
|
|
API.prototype._cmd_getWallets = function(callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
2014-04-24 19:42:59 -07:00
|
|
|
return callback(null, self.walletFactory.getWallets());
|
2014-04-14 14:23:00 -07:00
|
|
|
};
|
|
|
|
|
2014-04-24 19:42:59 -07:00
|
|
|
API.prototype.getWallets = decorate('getWallets', [
|
2014-04-14 14:23:00 -07:00
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype._cmd_help = function(callback) {
|
|
|
|
this._cmd_getCommands.apply(this, arguments);
|
2014-04-14 19:01:56 -07:00
|
|
|
};
|
|
|
|
|
2014-04-15 12:53:23 -07:00
|
|
|
API.prototype.help = decorate('help', [
|
2014-04-14 19:01:56 -07:00
|
|
|
['callback', 'function']
|
2014-04-15 12:53:23 -07:00
|
|
|
]);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
module.exports = require('soop')(API);
|