forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
61 lines (53 loc) · 2.09 KB
/
Copy pathcommon.js
File metadata and controls
61 lines (53 loc) · 2.09 KB
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
exports.Environments = {
prod: { uri: 'https://www.bitgo.com', network: 'bitcoin' },
staging: { uri: 'https://staging.bitgo.com', network: 'bitcoin' },
test: { uri: 'https://test.bitgo.com', network: 'testnet' },
dev: { uri: 'https://webdev.bitgo.com', network: 'testnet' },
local: { uri: 'http://localhost:3000', network: 'testnet' }
};
var bitcoinNetwork;
exports.setNetwork = function(network) {
if (network == 'bitcoin') {
bitcoinNetwork = 'bitcoin';
} else {
// test network
bitcoinNetwork = 'testnet';
}
};
exports.getNetwork = function() {
return bitcoinNetwork;
};
/**
* Helper function to validate the input parameters to an SDK method.
* Only validates for strings - if parameter is different, check that manually
*
* @param params {Object} dictionary of parameter key-value pairs
* @param expectedParams {string[]} list of expected string parameters
* @param optionalParams {string[]} list of optional string parameters
* @param optionalCallback {Function} if callback provided, must be a function
* @returns {boolean} true if validated, throws with reason otherwise
*/
exports.validateParams = function(params, expectedParams, optionalParams, optionalCallback) {
if (typeof(params) != 'object') {
throw new Error('Must pass in parameters dictionary');
}
expectedParams = expectedParams || [];
expectedParams.forEach(function(expectedParam) {
if (!params[expectedParam]) {
throw new Error('Missing parameter: ' + expectedParam);
}
if (typeof(params[expectedParam]) != 'string') {
throw new Error('Expecting parameter string: ' + expectedParam + ' but found ' + typeof(params[expectedParam]));
}
});
optionalParams = optionalParams || [];
optionalParams.forEach(function(expectedParam) {
if (params[expectedParam] && typeof(params[expectedParam]) != 'string') {
throw new Error('Expecting parameter string: ' + expectedParam + ' but found ' + typeof(params[expectedParam]));
}
});
if (optionalCallback && typeof(optionalCallback) != 'function') {
throw new Error('illegal callback argument');
}
return true;
};