forked from bitpay/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.js
190 lines (143 loc) · 4.23 KB
/
API.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var imports = require('soop').imports();
var API = function(opts) {
this._init(opts);
};
API.prototype._init = function(opts) {
var self = this;
opts = opts || {};
self.opts = opts;
var WalletFactory = require('soop').load('./js/models/core/WalletFactory', {
Storage: opts.Storage || require('./test/mocks/FakeStorage'),
Network: opts.Network || require('./js/models/network/WebRTC'),
Blockchain: opts.Blockchain || require('./js/models/blockchain/Insight')
});
this.walletFactory = new WalletFactory(opts);
};
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;
};
API.prototype._command = function(command, args, callback) {
var self = this;
if (!command || command[0] == "_")
return callback(new Error('invalid command'));
if (!API._checkArgTypes(command, args)) {
var argTypes = API.prototype[command].argTypes;
API._coerceArgTypes(args, argTypes)
if (!API._checkArgTypes(command, args))
throw new Error('invalid arguments');
}
if (typeof self["_cmd_" + command] == 'function') {
var f = API.prototype[command];
if (f.argTypes[f.argTypes.length-1][1] == 'function')
return self["_cmd_" + command].apply(self, args.concat([callback]));
else
return callback(null, self["_cmd_" + command].apply(self, args));
};
return callback(new Error('invalid command'));
};
API._checkArgTypes = function(command, args) {
var f = API.prototype[command];
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;
}
for (var i in args) {
if (typeof args[i] != f.argTypes[i][1])
return false;
}
return true;
};
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) {
var self = this;
return callback(null, str);
};
API.prototype.echo = decorate('echo', [
['str', 'string'],
['callback', 'function']
]);
API.prototype._cmd_echoNumber = function(num, callback) {
var self = this;
return callback(null, num);
};
API.prototype.echoNumber = decorate('echoNumber', [
['num', 'number'],
['callback', 'function']
]);
API.prototype._cmd_echoObject = function(obj, callback) {
var self = this;
return callback(null, obj);
};
API.prototype.echoObject = decorate('echoObject', [
['obj', 'object'],
['callback', 'function']
]);
/*
API.prototype.getBalance = function(callback) {
var self = this;
return callback(null, self.wallet.getBalance([]));
};
API.prototype.getBalance.argTypes =
[
['callback', 'function']
];
*/
API.prototype._cmd_getArgTypes = function(command, callback) {
var self = this;
if (command[0] == '_' || typeof API.prototype[command] != 'function')
return callback(new Error('Invalid command'));
var argTypes = API.prototype[command].argTypes;
return callback(null, argTypes);
};
API.prototype.getArgTypes = decorate('getArgTypes', [
['command', 'string'],
['callback', 'function']
]);
API.prototype._cmd_getCommands = function(callback) {
var self = this;
var fs = [];
for (var i in API.prototype) {
var f = API.prototype[i];
if (typeof f == 'function' && i[0] != "_")
fs.push(i);
};
return callback(null, fs);
};
API.prototype.getCommands = decorate('getCommands', [
['callback', 'function']
]);
API.prototype._cmd_getWallets = function(callback) {
var self = this;
return callback(null, self.walletFactory.getWallets());
};
API.prototype.getWallets = decorate('getWallets', [
['callback', 'function']
]);
API.prototype._cmd_help = function(callback) {
this._cmd_getCommands.apply(this, arguments);
};
API.prototype.help = decorate('help', [
['callback', 'function']
]);
module.exports = require('soop')(API);