UNPKG

2.51 kBPlain TextView Raw
1/**
2 * @prettier
3 */
4import * as _ from 'lodash';
5import { NodeCallback, V1Network, V1RmgNetwork } from './v2/types';
6
7// re-export from here for backwards compatibility reasons
8export { Environments } from './v2/environments';
9
10let bitcoinNetwork: V1Network;
11let rmgNetwork: V1RmgNetwork;
12
13/**
14 * Set the global Bitcoin network. Used for v1 only.
15 *
16 * @deprecated
17 */
18export function setNetwork(network: V1Network): void {
19 if (network === 'bitcoin') {
20 bitcoinNetwork = 'bitcoin';
21 } else {
22 // test network
23 bitcoinNetwork = 'testnet';
24 }
25}
26
27/**
28 * Get the global Bitcoin network. Used for v1 only.
29 *
30 * @deprecated
31 */
32export function getNetwork(): V1Network {
33 return bitcoinNetwork;
34}
35
36/**
37 * Get the global RMG network. Used for v1 only.
38 *
39 * @deprecated
40 */
41export function getRmgNetwork(): V1RmgNetwork {
42 return rmgNetwork;
43}
44
45/**
46 * Set the global RMG network. Used for v1 only.
47 *
48 * @deprecated
49 */
50export function setRmgNetwork(network: V1RmgNetwork): void {
51 rmgNetwork = network;
52}
53
54/**
55 * Helper function to validate the input parameters to an SDK method.
56 * Only validates for strings - if parameter is different, check that manually
57 *
58 * @deprecated
59 * @param params dictionary of parameter key-value pairs
60 * @param expectedParams list of expected string parameters
61 * @param optionalParams list of optional string parameters
62 * @param optionalCallback if callback provided, must be a function
63 * @returns true if validated, throws with reason otherwise
64 */
65export function validateParams(
66 params: object,
67 expectedParams: string[],
68 optionalParams: string[] = [],
69 optionalCallback?: NodeCallback<any>
70): boolean {
71 if (!_.isObject(params)) {
72 throw new Error('Must pass in parameters dictionary');
73 }
74
75 expectedParams = expectedParams || [];
76
77 expectedParams.forEach(function (expectedParam) {
78 if (!params[expectedParam]) {
79 throw new Error('Missing parameter: ' + expectedParam);
80 }
81 if (!_.isString(params[expectedParam])) {
82 throw new Error('Expecting parameter string: ' + expectedParam + ' but found ' + typeof params[expectedParam]);
83 }
84 });
85
86 optionalParams.forEach(function (optionalParam) {
87 if (params[optionalParam] && !_.isString(params[optionalParam])) {
88 throw new Error('Expecting parameter string: ' + optionalParam + ' but found ' + typeof params[optionalParam]);
89 }
90 });
91
92 if (optionalCallback && !_.isFunction(optionalCallback)) {
93 throw new Error('illegal callback argument');
94 }
95
96 return true;
97}