@ellipticltd/aml-utils 0.1.1-ts.4 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +4 -0
- package/index.d.ts +1 -0
- package/index.js +8 -0
- package/{dist → lib}/errors.js +34 -24
- package/lib/formatting.js +18 -0
- package/lib/middleware.js +25 -0
- package/lib/ormHelpers.js +19 -0
- package/lib/types.js +211 -0
- package/lib/validations.js +236 -0
- package/package.json +5 -15
- package/.idea/aml-utils.iml +0 -12
- package/.idea/codeStyles/Project.xml +0 -52
- package/.idea/dbnavigator.xml +0 -456
- package/.idea/encodings.xml +0 -4
- package/.idea/markdown-navigator/profiles_settings.xml +0 -3
- package/.idea/markdown-navigator.xml +0 -86
- package/.idea/misc.xml +0 -46
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.idea/workspace.xml +0 -540
- package/dist/errors.d.ts +0 -12
- package/dist/formatting.d.ts +0 -10
- package/dist/formatting.js +0 -25
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -14
- package/dist/middleware.d.ts +0 -8
- package/dist/middleware.js +0 -23
- package/dist/ormHelpers.d.ts +0 -4
- package/dist/ormHelpers.js +0 -25
- package/dist/test.d.ts +0 -1
- package/dist/test.js +0 -2
- package/dist/types.d.ts +0 -14
- package/dist/types.js +0 -207
- package/dist/validations.d.ts +0 -49
- package/dist/validations.js +0 -242
- package/tsconfig.json +0 -80
- package/tslint.json +0 -18
package/dist/formatting.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
-
if (mod && mod.__esModule) return mod;
|
|
4
|
-
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
-
result["default"] = mod;
|
|
7
|
-
return result;
|
|
8
|
-
};
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
const _ = __importStar(require("lodash"));
|
|
11
|
-
const rethrowError = (fnName, e) => {
|
|
12
|
-
let formattedError;
|
|
13
|
-
if (_.isPlainObject(e.error) || _.isArray(e.error)) {
|
|
14
|
-
formattedError = `\n${JSON.stringify(e.error, null, 2)}`;
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
formattedError = e.error;
|
|
18
|
-
}
|
|
19
|
-
return `${fnName} - ${e.name}: ${e.statusCode} - ${formattedError}`;
|
|
20
|
-
};
|
|
21
|
-
const sqlEscapeWildcard = (str) => str.replace(/_|%|\\/g, x => `\\${x}`);
|
|
22
|
-
exports.default = {
|
|
23
|
-
rethrowError,
|
|
24
|
-
sqlEscapeWildcard,
|
|
25
|
-
};
|
package/dist/index.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { default as types } from "./types";
|
|
2
|
-
export { default as validations } from "./validations";
|
|
3
|
-
export { default as errors } from "./errors";
|
|
4
|
-
export { default as formatting } from "./formatting";
|
|
5
|
-
export { default as ormHelpers } from "./ormHelpers";
|
|
6
|
-
export { default as middleware } from "./middleware";
|
package/dist/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var types_1 = require("./types");
|
|
4
|
-
exports.types = types_1.default;
|
|
5
|
-
var validations_1 = require("./validations");
|
|
6
|
-
exports.validations = validations_1.default;
|
|
7
|
-
var errors_1 = require("./errors");
|
|
8
|
-
exports.errors = errors_1.default;
|
|
9
|
-
var formatting_1 = require("./formatting");
|
|
10
|
-
exports.formatting = formatting_1.default;
|
|
11
|
-
var ormHelpers_1 = require("./ormHelpers");
|
|
12
|
-
exports.ormHelpers = ormHelpers_1.default;
|
|
13
|
-
var middleware_1 = require("./middleware");
|
|
14
|
-
exports.middleware = middleware_1.default;
|
package/dist/middleware.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from 'express';
|
|
2
|
-
declare const _default: {
|
|
3
|
-
ensureType: (type: string) => (req: Request, res: Response, next: NextFunction, id: any, name: string) => void;
|
|
4
|
-
ensureUUID: (req: Request, res: Response, next: NextFunction, id: any, name: string) => void;
|
|
5
|
-
ensureHex32: (req: Request, res: Response, next: NextFunction, id: any, name: string) => void;
|
|
6
|
-
ensureInteger: (req: Request, res: Response, next: NextFunction, id: any, name: string) => void;
|
|
7
|
-
};
|
|
8
|
-
export default _default;
|
package/dist/middleware.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const T = require('./types');
|
|
4
|
-
const ensureType = (type) => (req, res, next, id, name) => {
|
|
5
|
-
let error;
|
|
6
|
-
try {
|
|
7
|
-
T.ensureType(type, id, `Invalid ${name}`);
|
|
8
|
-
return next();
|
|
9
|
-
}
|
|
10
|
-
catch (error1) {
|
|
11
|
-
error = error1;
|
|
12
|
-
return next(error);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
const ensureUUID = ensureType('UUID');
|
|
16
|
-
const ensureHex32 = ensureType('Hex32');
|
|
17
|
-
const ensureInteger = ensureType('IntString');
|
|
18
|
-
exports.default = {
|
|
19
|
-
ensureType,
|
|
20
|
-
ensureUUID,
|
|
21
|
-
ensureHex32,
|
|
22
|
-
ensureInteger,
|
|
23
|
-
};
|
package/dist/ormHelpers.d.ts
DELETED
package/dist/ormHelpers.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
-
if (mod && mod.__esModule) return mod;
|
|
4
|
-
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
-
result["default"] = mod;
|
|
7
|
-
return result;
|
|
8
|
-
};
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
const _ = __importStar(require("lodash"));
|
|
11
|
-
const includeNested = (orm, models) => _.map(_.toPairs(models), (arg) => {
|
|
12
|
-
const [modelName, inclusion] = arg;
|
|
13
|
-
const include = includeNested(orm, inclusion);
|
|
14
|
-
const model = orm[modelName];
|
|
15
|
-
if (_.isEmpty(include)) {
|
|
16
|
-
return model;
|
|
17
|
-
}
|
|
18
|
-
return {
|
|
19
|
-
model,
|
|
20
|
-
include,
|
|
21
|
-
};
|
|
22
|
-
});
|
|
23
|
-
exports.default = {
|
|
24
|
-
includeNested,
|
|
25
|
-
};
|
package/dist/test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/test.js
DELETED
package/dist/types.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
declare const _default: {
|
|
2
|
-
parseType(str: any): any;
|
|
3
|
-
parsedTypeCheck(type: any, obj: any): any;
|
|
4
|
-
typeCheck(type: any, obj: any): any;
|
|
5
|
-
opts: {
|
|
6
|
-
customTypes: {};
|
|
7
|
-
};
|
|
8
|
-
ensureType(type: any, obj: any, msg: string): any;
|
|
9
|
-
checkArg(obj: any, prop: any, type: any, msg: string): any;
|
|
10
|
-
checkArgs(input: {
|
|
11
|
-
[K: string]: any;
|
|
12
|
-
}, types: any[]): void;
|
|
13
|
-
};
|
|
14
|
-
export default _default;
|
package/dist/types.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
-
if (mod && mod.__esModule) return mod;
|
|
4
|
-
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
-
result["default"] = mod;
|
|
7
|
-
return result;
|
|
8
|
-
};
|
|
9
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
const TC = __importStar(require("type-check"));
|
|
15
|
-
const _ = __importStar(require("lodash"));
|
|
16
|
-
const validations_1 = __importDefault(require("./validations"));
|
|
17
|
-
const errors_1 = __importDefault(require("./errors"));
|
|
18
|
-
const customTypes = {
|
|
19
|
-
Integer: {
|
|
20
|
-
typeOf: 'Number',
|
|
21
|
-
validate(x) {
|
|
22
|
-
return x % 1 === 0;
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
IntString: {
|
|
26
|
-
typeOf: 'String',
|
|
27
|
-
validate: validations_1.default.isInt,
|
|
28
|
-
},
|
|
29
|
-
BoolString: {
|
|
30
|
-
typeOf: 'String',
|
|
31
|
-
validate: validations_1.default.isBoolean,
|
|
32
|
-
},
|
|
33
|
-
Hex32: {
|
|
34
|
-
typeOf: 'String',
|
|
35
|
-
validate(x) {
|
|
36
|
-
return validations_1.default.isHexadecimal(x) && x.length === 32;
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
Email: {
|
|
40
|
-
typeOf: 'String',
|
|
41
|
-
validate: validations_1.default.isEmail,
|
|
42
|
-
},
|
|
43
|
-
UUID: {
|
|
44
|
-
typeOf: 'String',
|
|
45
|
-
validate: validations_1.default.isUUID,
|
|
46
|
-
},
|
|
47
|
-
UUIDv4: {
|
|
48
|
-
typeOf: 'String',
|
|
49
|
-
validate(x) {
|
|
50
|
-
return validations_1.default.isUUID(x, 4);
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
JSON: {
|
|
54
|
-
typeOf: 'String',
|
|
55
|
-
validate: validations_1.default.isJSON,
|
|
56
|
-
},
|
|
57
|
-
Url: {
|
|
58
|
-
typeOf: 'String',
|
|
59
|
-
validate: validations_1.default.isURL,
|
|
60
|
-
},
|
|
61
|
-
NonEmptyArray: {
|
|
62
|
-
typeOf: 'Array',
|
|
63
|
-
validate: validations_1.default.nonEmpty,
|
|
64
|
-
},
|
|
65
|
-
NonNullString: {
|
|
66
|
-
typeOf: 'String',
|
|
67
|
-
validate: validations_1.default.nonEmpty,
|
|
68
|
-
},
|
|
69
|
-
CustomerReference: {
|
|
70
|
-
typeOf: 'String',
|
|
71
|
-
validate: validations_1.default.isCustomerReference,
|
|
72
|
-
},
|
|
73
|
-
CustomerLabelName: {
|
|
74
|
-
typeOf: 'String',
|
|
75
|
-
validate: validations_1.default.isCustomerLabelName,
|
|
76
|
-
},
|
|
77
|
-
BitcoinAddress: {
|
|
78
|
-
typeOf: 'String',
|
|
79
|
-
validate: validations_1.default.bitcoin.isAddress,
|
|
80
|
-
},
|
|
81
|
-
EthereumAddress: {
|
|
82
|
-
typeOf: 'String',
|
|
83
|
-
validate: validations_1.default.ethereum.isAddress,
|
|
84
|
-
},
|
|
85
|
-
EthereumTx: {
|
|
86
|
-
typeOf: 'String',
|
|
87
|
-
validate: validations_1.default.ethereum.isTxHash,
|
|
88
|
-
},
|
|
89
|
-
EthereumBlockHash: {
|
|
90
|
-
typeOf: 'String',
|
|
91
|
-
validate: validations_1.default.ethereum.isBlockHash,
|
|
92
|
-
},
|
|
93
|
-
EthereumWeiAmount: {
|
|
94
|
-
typeOf: 'String',
|
|
95
|
-
validate: validations_1.default.ethereum.isValidWeiAmount,
|
|
96
|
-
},
|
|
97
|
-
EthereumAddressCode: {
|
|
98
|
-
typeOf: 'String',
|
|
99
|
-
validate: validations_1.default.ethereum.isAddressCode,
|
|
100
|
-
},
|
|
101
|
-
MineId: {
|
|
102
|
-
typeOf: 'String',
|
|
103
|
-
validate(x) {
|
|
104
|
-
return x === 'mine';
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
BitcoinTxHash: {
|
|
108
|
-
typeOf: 'String',
|
|
109
|
-
validate: validations_1.default.bitcoin.isTxHash,
|
|
110
|
-
},
|
|
111
|
-
BitcoinAddressArray: {
|
|
112
|
-
typeOf: 'Array',
|
|
113
|
-
validate(as) {
|
|
114
|
-
return _.every(as, validations_1.default.bitcoin.isAddress);
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
BitcoinTxHashArray: {
|
|
118
|
-
typeOf: 'Array',
|
|
119
|
-
validate(as) {
|
|
120
|
-
return _.every(as, validations_1.default.bitcoin.isTxHash);
|
|
121
|
-
},
|
|
122
|
-
},
|
|
123
|
-
BitcoinTxHex: {
|
|
124
|
-
typeOf: 'String',
|
|
125
|
-
validate: validations_1.default.bitcoin.isTxHex,
|
|
126
|
-
},
|
|
127
|
-
BitcoinScriptHex: {
|
|
128
|
-
typeOf: 'String',
|
|
129
|
-
validate: validations_1.default.bitcoin.isScriptHex,
|
|
130
|
-
},
|
|
131
|
-
BitcoinHDPath: {
|
|
132
|
-
typeOf: 'String',
|
|
133
|
-
validate: validations_1.default.bitcoin.isHDPath,
|
|
134
|
-
},
|
|
135
|
-
BitcoinPublicKey: {
|
|
136
|
-
typeOf: 'String',
|
|
137
|
-
validate: validations_1.default.bitcoin.isPublicKey,
|
|
138
|
-
},
|
|
139
|
-
BitcoinXpub: {
|
|
140
|
-
typeOf: 'String',
|
|
141
|
-
validate: validations_1.default.bitcoin.isHDPublicKey,
|
|
142
|
-
},
|
|
143
|
-
BitcoinTxSignature: {
|
|
144
|
-
typeOf: 'String',
|
|
145
|
-
validate: validations_1.default.bitcoin.isTxSignature,
|
|
146
|
-
},
|
|
147
|
-
BitcoinBlockHeight: {
|
|
148
|
-
typeOf: 'Number',
|
|
149
|
-
validate: validations_1.default.bitcoin.isBlockHeight,
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
// add support for nullable properties
|
|
153
|
-
const withNullable = Object.keys(customTypes).reduce((acc, _type) => {
|
|
154
|
-
acc[_type] = customTypes[_type];
|
|
155
|
-
const { typeOf, validate } = customTypes[_type];
|
|
156
|
-
// for every type, add Nullable+type. A nullable string can also be empty
|
|
157
|
-
acc[`Nullable${_type}`] = Object.assign({}, customTypes[_type], {
|
|
158
|
-
validate: (x) => x == null || (typeOf === 'String' && x === '') || validate(x),
|
|
159
|
-
});
|
|
160
|
-
return acc;
|
|
161
|
-
}, {});
|
|
162
|
-
exports.default = {
|
|
163
|
-
parseType(str) {
|
|
164
|
-
return TC.parseType(str);
|
|
165
|
-
},
|
|
166
|
-
parsedTypeCheck(type, obj) {
|
|
167
|
-
return TC.parsedTypeCheck(type, obj, this.opts);
|
|
168
|
-
},
|
|
169
|
-
typeCheck(type, obj) {
|
|
170
|
-
return TC.typeCheck(type, obj, this.opts);
|
|
171
|
-
},
|
|
172
|
-
opts: {
|
|
173
|
-
customTypes: withNullable,
|
|
174
|
-
},
|
|
175
|
-
ensureType(type, obj, msg) {
|
|
176
|
-
return validations_1.default.ensure(this.typeCheck(type, obj), msg);
|
|
177
|
-
},
|
|
178
|
-
checkArg(obj, prop, type, msg) {
|
|
179
|
-
if (msg == null) {
|
|
180
|
-
msg = `Invalid ${prop}`;
|
|
181
|
-
}
|
|
182
|
-
this.ensureType(type, obj[prop], msg);
|
|
183
|
-
return obj[prop];
|
|
184
|
-
},
|
|
185
|
-
checkArgs(input, types) {
|
|
186
|
-
const inputKeys = {};
|
|
187
|
-
const [typ] = types;
|
|
188
|
-
const fields = typ.of;
|
|
189
|
-
let numInputKeys = 0;
|
|
190
|
-
let numKeys;
|
|
191
|
-
Object.keys(input).forEach((k) => {
|
|
192
|
-
inputKeys[k] = true;
|
|
193
|
-
numInputKeys += 1;
|
|
194
|
-
});
|
|
195
|
-
numKeys = 0;
|
|
196
|
-
Object.keys(fields).forEach((key) => {
|
|
197
|
-
types = fields[key];
|
|
198
|
-
validations_1.default.ensure(this.parsedTypeCheck(types, input[key]), `Invalid ${key}`);
|
|
199
|
-
if (inputKeys[key]) {
|
|
200
|
-
numKeys += 1;
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
if (!(typ.subset || numInputKeys === numKeys)) {
|
|
204
|
-
throw new errors_1.default.BadRequest('invalid extra arguments are present');
|
|
205
|
-
}
|
|
206
|
-
},
|
|
207
|
-
};
|
package/dist/validations.d.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
declare type ValidatorFunction = (...args: any) => any;
|
|
2
|
-
declare type Validations = {
|
|
3
|
-
_validate: (Err: new (msg: string) => Error, pred: (x: any) => boolean, x: any, msg: string) => any;
|
|
4
|
-
_validateArg: (pred: (x: any) => boolean, x: any, msg: string) => any;
|
|
5
|
-
_tryCheck: (fn: (arg: any) => any, arg: any) => boolean;
|
|
6
|
-
exists: (x: any) => boolean;
|
|
7
|
-
argExists: (x: any, msg: string) => boolean;
|
|
8
|
-
nonEmpty: ValidatorFunction;
|
|
9
|
-
isNonEmptyString: ValidatorFunction;
|
|
10
|
-
ensureShortEnough: ValidatorFunction;
|
|
11
|
-
ensure: ValidatorFunction;
|
|
12
|
-
check: {
|
|
13
|
-
matches: ValidatorFunction;
|
|
14
|
-
wasFound: ValidatorFunction;
|
|
15
|
-
wasCreated: ValidatorFunction;
|
|
16
|
-
isTrue: ValidatorFunction;
|
|
17
|
-
isFalse: ValidatorFunction;
|
|
18
|
-
};
|
|
19
|
-
isCustomerReference: ValidatorFunction;
|
|
20
|
-
isCustomerLabelName: ValidatorFunction;
|
|
21
|
-
ethereum: {
|
|
22
|
-
isAddress: ValidatorFunction;
|
|
23
|
-
isBlockHash: ValidatorFunction;
|
|
24
|
-
isTxHash: ValidatorFunction;
|
|
25
|
-
isAddressCode: ValidatorFunction;
|
|
26
|
-
isValidWeiAmount: ValidatorFunction;
|
|
27
|
-
};
|
|
28
|
-
bitcoin: {
|
|
29
|
-
isAddress: ValidatorFunction;
|
|
30
|
-
isHDPublicKey: ValidatorFunction;
|
|
31
|
-
isPublicKey: ValidatorFunction;
|
|
32
|
-
isTxHash: ValidatorFunction;
|
|
33
|
-
isTxHex: ValidatorFunction;
|
|
34
|
-
isHDPath: ValidatorFunction;
|
|
35
|
-
isScriptHex: ValidatorFunction;
|
|
36
|
-
isTxSignature: ValidatorFunction;
|
|
37
|
-
isBlockHeight: ValidatorFunction;
|
|
38
|
-
};
|
|
39
|
-
bitcoinCash: {
|
|
40
|
-
isAddress: ValidatorFunction;
|
|
41
|
-
isTxHash: ValidatorFunction;
|
|
42
|
-
};
|
|
43
|
-
litecoin: {
|
|
44
|
-
isAddress: ValidatorFunction;
|
|
45
|
-
isTxHash: ValidatorFunction;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
declare const _default: Validations & IValidatorStatic;
|
|
49
|
-
export default _default;
|
package/dist/validations.js
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
-
if (mod && mod.__esModule) return mod;
|
|
4
|
-
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
-
result["default"] = mod;
|
|
7
|
-
return result;
|
|
8
|
-
};
|
|
9
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
/// <reference path="../node_modules/@types/validator/index.d.ts" />
|
|
14
|
-
const _ = __importStar(require("lodash"));
|
|
15
|
-
const V = __importStar(require("validator"));
|
|
16
|
-
const errors_1 = __importDefault(require("./errors"));
|
|
17
|
-
const { crypto: { Signature }, HDPrivateKey, HDPublicKey, PublicKey, Script, Transaction, } = require('bitcore-lib');
|
|
18
|
-
const addressValidator = require('wallet-address-validator');
|
|
19
|
-
const web3 = require('web3-utils');
|
|
20
|
-
const helpers = {};
|
|
21
|
-
const validations = {
|
|
22
|
-
_validate(Err, pred, x, msg) {
|
|
23
|
-
if (pred(x)) {
|
|
24
|
-
return x;
|
|
25
|
-
}
|
|
26
|
-
throw new Err(msg);
|
|
27
|
-
},
|
|
28
|
-
_validateArg(pred, x, msg) {
|
|
29
|
-
return this._validate(errors_1.default.InvalidArguments, pred, x, msg);
|
|
30
|
-
},
|
|
31
|
-
_tryCheck(fn, arg) {
|
|
32
|
-
try {
|
|
33
|
-
fn(arg);
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
exists(x) {
|
|
41
|
-
return x != null;
|
|
42
|
-
},
|
|
43
|
-
argExists(x, msg) {
|
|
44
|
-
return this._validateArg(this.exists, x, msg);
|
|
45
|
-
},
|
|
46
|
-
nonEmpty(x) {
|
|
47
|
-
return (x != null ? x.length : undefined) > 0;
|
|
48
|
-
},
|
|
49
|
-
isNonEmptyString(x) {
|
|
50
|
-
return _.isString(x) && (x != null ? x.length : 0) > 0;
|
|
51
|
-
},
|
|
52
|
-
ensureShortEnough(limit, x) {
|
|
53
|
-
const l = x != null ? x.length : undefined;
|
|
54
|
-
if (l <= limit) {
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
throw new errors_1.default.BadRequest(`Max query size exceeded: ${l} / ${limit}`);
|
|
58
|
-
},
|
|
59
|
-
ensure(crit, msg) {
|
|
60
|
-
if (crit) {
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
throw new errors_1.default.BadRequest(msg);
|
|
64
|
-
},
|
|
65
|
-
check: {
|
|
66
|
-
matches(required, given, msg) {
|
|
67
|
-
given = _.uniq(given);
|
|
68
|
-
const crit = _.intersection(given, required).length === given.length;
|
|
69
|
-
if (crit) {
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
throw new errors_1.default.BadRequest(msg);
|
|
73
|
-
},
|
|
74
|
-
wasFound(type) {
|
|
75
|
-
return (item) => {
|
|
76
|
-
if (item == null) {
|
|
77
|
-
throw new errors_1.default.NotFound(`Unknown ${type}`);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
return item;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
wasCreated(msg) {
|
|
85
|
-
return (arr) => {
|
|
86
|
-
if (!arr[1]) {
|
|
87
|
-
throw new errors_1.default.BadRequest(msg);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
return arr;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
},
|
|
94
|
-
isTrue(crit, msg) {
|
|
95
|
-
if (crit) {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
throw new errors_1.default.BadRequest(msg);
|
|
99
|
-
},
|
|
100
|
-
isFalse(crit, msg) {
|
|
101
|
-
if (!crit) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
throw new errors_1.default.BadRequest(msg);
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
isCustomerReference(x) {
|
|
108
|
-
return (_.isString(x) &&
|
|
109
|
-
(x != null ? x.length > 0 : false) &&
|
|
110
|
-
(x != null ? x.length <= 100 : false));
|
|
111
|
-
},
|
|
112
|
-
isCustomerLabelName(x) {
|
|
113
|
-
return (_.isString(x) &&
|
|
114
|
-
(x != null ? x.length > 0 : false) &&
|
|
115
|
-
(x != null ? x.length <= 50 : false));
|
|
116
|
-
},
|
|
117
|
-
ethereum: {
|
|
118
|
-
isAddress(str) {
|
|
119
|
-
return web3.isAddress(str);
|
|
120
|
-
},
|
|
121
|
-
isBlockHash(str) {
|
|
122
|
-
return str.length === 66 && web3.isHexStrict(str);
|
|
123
|
-
},
|
|
124
|
-
isTxHash(str) {
|
|
125
|
-
return str.length === 66 && web3.isHexStrict(str);
|
|
126
|
-
},
|
|
127
|
-
isAddressCode(str) {
|
|
128
|
-
return web3.isHexStrict(str);
|
|
129
|
-
},
|
|
130
|
-
isValidWeiAmount(str) {
|
|
131
|
-
try {
|
|
132
|
-
web3.fromWei(str);
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
catch (e) {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
bitcoin: {
|
|
141
|
-
isAddress(str) {
|
|
142
|
-
const network = process.env.BITCOIN_NETWORK === 'testnet' ? 'testnet' : 'livenet';
|
|
143
|
-
return typeof str === 'string' && addressValidator.validate(str.trim(), 'BTC', network);
|
|
144
|
-
},
|
|
145
|
-
isHDPublicKey(str) {
|
|
146
|
-
try {
|
|
147
|
-
HDPublicKey(str);
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
},
|
|
154
|
-
isPublicKey(str) {
|
|
155
|
-
try {
|
|
156
|
-
PublicKey(str);
|
|
157
|
-
return true;
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
isTxHash(str) {
|
|
164
|
-
if (!V.isHexadecimal(`${str}`)) {
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
return str.length === 64;
|
|
168
|
-
},
|
|
169
|
-
isTxHex(str) {
|
|
170
|
-
if (!V.isHexadecimal(`${str}`)) {
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
try {
|
|
174
|
-
Transaction(str);
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
catch (error) {
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
},
|
|
181
|
-
isHDPath(str) {
|
|
182
|
-
return HDPrivateKey.isValidPath(str);
|
|
183
|
-
},
|
|
184
|
-
isScriptHex(str) {
|
|
185
|
-
if (!V.isHexadecimal(`${str}`)) {
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
try {
|
|
189
|
-
Script(str);
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
isTxSignature(str) {
|
|
197
|
-
if (!V.isHexadecimal(`${str}`)) {
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
try {
|
|
201
|
-
const b = Buffer.from(str, 'hex');
|
|
202
|
-
Signature.fromTxFormat(b);
|
|
203
|
-
return true;
|
|
204
|
-
}
|
|
205
|
-
catch (error) {
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
},
|
|
209
|
-
isBlockHeight(obj) {
|
|
210
|
-
return _.isInteger(+obj) && parseInt(obj, 10) >= 0;
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
bitcoinCash: {
|
|
214
|
-
isAddress(str) {
|
|
215
|
-
return /^[13][a-km-zA-HJ-NP-Z1-9]{25,34}|^(bitcoincash:)?[q|p][a-z0-9]{41}|^(BITCOINCASH:)?[Q|P][A-Z0-9]{41}/.test(str);
|
|
216
|
-
},
|
|
217
|
-
isTxHash(str) {
|
|
218
|
-
if (!V.isHexadecimal(`${str}`)) {
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
return str.length === 64;
|
|
222
|
-
},
|
|
223
|
-
},
|
|
224
|
-
litecoin: {
|
|
225
|
-
isAddress(str) {
|
|
226
|
-
return /^[3LM][a-km-zA-HJ-NP-Z1-9]{24,33}|^ltc1[ac-hj-np-z02-9]{6,86}|^LTC1[AC-HJ-NP-Z02-9]{6,86}/.test(str);
|
|
227
|
-
},
|
|
228
|
-
isTxHash(str) {
|
|
229
|
-
if (!V.isHexadecimal(`${str}`)) {
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
return str.length === 64;
|
|
233
|
-
},
|
|
234
|
-
},
|
|
235
|
-
};
|
|
236
|
-
Object.assign(validations, helpers);
|
|
237
|
-
Object.keys(V).forEach((k) => {
|
|
238
|
-
const v = V[k];
|
|
239
|
-
// don't trigger validation with null values (prevents swagger 500 error)
|
|
240
|
-
validations[k] = (x) => (x === null ? false : v(x));
|
|
241
|
-
});
|
|
242
|
-
exports.default = validations;
|