2014-04-14 14:23:00 -07:00
|
|
|
var imports = require('soop').imports();
|
|
|
|
var PublicKeyRing = imports.PublicKeyRing || require('../js/models/core/PublicKeyRing');
|
2014-04-14 19:01:56 -07:00
|
|
|
//var Wallet = imports.Wallet || require('../js/models/core/Wallet');
|
2014-04-14 14:23:00 -07:00
|
|
|
|
|
|
|
var Copay = function(opts) {
|
|
|
|
this._init(opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype._init = function(opts) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
opts = opts ? opts : {};
|
|
|
|
|
|
|
|
self.optsList = [
|
|
|
|
'publicKeyRing',
|
|
|
|
'wallet'
|
|
|
|
];
|
|
|
|
|
|
|
|
self.opts = {};
|
|
|
|
for (var a in self.optsList) {
|
|
|
|
if (opts.hasOwnProperty(a))
|
|
|
|
self.opts[a] = opts[a];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.publicKeyRing = self.opts.publicKeyRing ? self.opts.publicKeyRing : new PublicKeyRing();
|
2014-04-14 19:01:56 -07:00
|
|
|
//self.wallet = self.opts.wallet ? self.opts.wallet : new Wallet();
|
2014-04-14 14:23:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype._command = function(command, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (command[0] == "_")
|
|
|
|
return callback(new Error('invalid command'));
|
|
|
|
|
|
|
|
if (typeof self[command] == 'function') {
|
|
|
|
var f = Copay.prototype[command];
|
|
|
|
if (f.argTypes[f.argTypes.length-1][1] == 'function')
|
|
|
|
return self[command].apply(self, args.concat([callback]));
|
|
|
|
else
|
|
|
|
return callback(null, self[command].apply(self, args));
|
|
|
|
};
|
|
|
|
|
|
|
|
return callback(new Error('invalid command'));
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay._checkArgTypes = function(command, args) {
|
|
|
|
var f = Copay.prototype[command];
|
|
|
|
for (var i in args) {
|
|
|
|
if (typeof args[i] != f.argTypes[i][1])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkArgs(name, args) {
|
|
|
|
if (!Copay._checkArgTypes(name, args))
|
|
|
|
throw new Error('Invalid arguments');
|
|
|
|
}
|
|
|
|
|
2014-04-14 19:01:56 -07:00
|
|
|
Copay.prototype.echo = function echo(str, callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
2014-04-14 19:01:56 -07:00
|
|
|
checkArgs(arguments.callee.name, arguments);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
|
|
|
return callback(null, str);
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype.echo.argTypes =
|
|
|
|
[
|
|
|
|
['str', 'string'],
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-14 19:01:56 -07:00
|
|
|
/*
|
2014-04-14 14:23:00 -07:00
|
|
|
Copay.prototype.getBalance = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
checkArgs('getBalance', arguments);
|
|
|
|
|
|
|
|
return callback(null, self.wallet.getBalance([]));
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype.getBalance.argTypes =
|
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
2014-04-14 19:01:56 -07:00
|
|
|
*/
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 05:19:07 -07:00
|
|
|
Copay.prototype.getArgTypes = function getArgTypes(command, callback) {
|
|
|
|
var self = this;
|
|
|
|
checkArgs(arguments.callee.name, arguments);
|
|
|
|
|
|
|
|
if (command[0] == '_' || typeof Copay.prototype[command] != 'function')
|
|
|
|
return callback(new Error('Invalid command'));
|
|
|
|
|
|
|
|
var argTypes = Copay.prototype[command].argTypes;
|
|
|
|
|
|
|
|
return callback(null, argTypes);
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype.getArgTypes.argTypes =
|
|
|
|
[
|
|
|
|
['command', 'string'],
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-14 19:01:56 -07:00
|
|
|
Copay.prototype.getCommands = function getCommands(callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
2014-04-14 19:01:56 -07:00
|
|
|
checkArgs(arguments.callee.name, arguments);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
|
|
|
var fs = [];
|
|
|
|
|
|
|
|
for (var i in Copay.prototype) {
|
|
|
|
var f = Copay.prototype[i];
|
|
|
|
if (typeof f == 'function' && i[0] != "_")
|
|
|
|
fs.push(i);
|
|
|
|
};
|
|
|
|
|
|
|
|
return callback(null, fs);
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype.getCommands.argTypes =
|
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-14 19:01:56 -07:00
|
|
|
Copay.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
2014-04-14 19:01:56 -07:00
|
|
|
checkArgs(arguments.callee.name, arguments);
|
2014-04-14 14:23:00 -07:00
|
|
|
|
|
|
|
return callback(null, self.publicKeyRing.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype.getPublicKeyRingId.argTypes =
|
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-14 19:01:56 -07:00
|
|
|
Copay.prototype.help = function help(callback) {
|
|
|
|
this.getCommands.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
Copay.prototype.help.argTypes =
|
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
2014-04-14 14:23:00 -07:00
|
|
|
|
|
|
|
module.exports = require('soop')(Copay);
|