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;
|
|
|
|
|
|
|
|
opts = opts ? opts : {};
|
2014-04-15 10:50:14 -07:00
|
|
|
self.opts = opts;
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 10:50:14 -07:00
|
|
|
var Wallet = require('soop').load('./js/models/core/Wallet', {
|
|
|
|
Storage: opts.Storage ? opts.Storage : require('./test/FakeStorage'),
|
|
|
|
Network: opts.Network ? opts.Network : require('./js/models/Network/WebRTC'),
|
|
|
|
Blockchain: opts.Blockchain ? opts.Blockchain : require('./js/models/Blockchain/Insight')
|
|
|
|
});
|
|
|
|
|
|
|
|
var config = {
|
|
|
|
wallet: {
|
|
|
|
requiredCopayers: opts.requiredCopayers ? opts.requiredCopayers : 3,
|
|
|
|
totalCopayers: opts.totalCopayers ? opts.totalCopayers : 5,
|
|
|
|
}
|
|
|
|
};
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 10:50:14 -07:00
|
|
|
var walletConfig = opts.walletConfig ? opts.walletConfig : config;
|
|
|
|
var walletOpts = opts.walletOpts ? opts.walletOpts : {};
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 10:50:14 -07:00
|
|
|
self.wallet = self.opts.wallet ? self.opts.wallet : Wallet.factory.create(walletConfig, walletOpts);
|
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 08:07:04 -07:00
|
|
|
throw new Error('Invalid arguments');
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:23:00 -07:00
|
|
|
if (typeof self[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')
|
|
|
|
return self[command].apply(self, args.concat([callback]));
|
|
|
|
else
|
|
|
|
return callback(null, self[command].apply(self, args));
|
|
|
|
};
|
|
|
|
|
|
|
|
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 06:50:54 -07:00
|
|
|
API.prototype.echo = function echo(str, callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, str);
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.echo.argTypes =
|
2014-04-14 14:23:00 -07:00
|
|
|
[
|
|
|
|
['str', 'string'],
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-15 08:07:04 -07:00
|
|
|
API.prototype.echoNumber = function echoNumber(num, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, num);
|
|
|
|
};
|
|
|
|
|
|
|
|
API.prototype.echoNumber.argTypes =
|
|
|
|
[
|
|
|
|
['num', 'number'],
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
|
|
|
API.prototype.echoObject = function echoNumber(obj, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return callback(null, obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
API.prototype.echoObject.argTypes =
|
|
|
|
[
|
|
|
|
['obj', 'object'],
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
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 06:50:54 -07:00
|
|
|
API.prototype.getArgTypes = function getArgTypes(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 06:50:54 -07:00
|
|
|
API.prototype.getArgTypes.argTypes =
|
2014-04-15 05:19:07 -07:00
|
|
|
[
|
|
|
|
['command', 'string'],
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.getCommands = function getCommands(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 06:50:54 -07:00
|
|
|
API.prototype.getCommands.argTypes =
|
2014-04-14 14:23:00 -07:00
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
|
2014-04-14 14:23:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
2014-04-15 10:50:14 -07:00
|
|
|
return callback(null, self.wallet.publicKeyRing.walletId);
|
2014-04-14 14:23:00 -07:00
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.getPublicKeyRingId.argTypes =
|
2014-04-14 14:23:00 -07:00
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.help = function help(callback) {
|
2014-04-14 19:01:56 -07:00
|
|
|
this.getCommands.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
API.prototype.help.argTypes =
|
2014-04-14 19:01:56 -07:00
|
|
|
[
|
|
|
|
['callback', 'function']
|
|
|
|
];
|
2014-04-14 14:23:00 -07:00
|
|
|
|
2014-04-15 06:50:54 -07:00
|
|
|
module.exports = require('soop')(API);
|