@ellipticltd/aml-utils 0.0.3 → 0.0.9
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/.eslintrc +3 -0
- package/.nvmrc +1 -0
- package/README.md +8 -0
- package/index.js +8 -12
- package/lib/errors.js +53 -51
- package/lib/formatting.js +15 -25
- package/lib/middleware.js +20 -28
- package/lib/ormHelpers.js +17 -26
- package/lib/types.js +202 -170
- package/lib/validations.js +189 -174
- package/package.json +22 -7
- package/.idea/aml-utils.iml +0 -12
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.idea/workspace.xml +0 -283
- package/dist/errors/errors.d.ts +0 -9
- package/dist/errors/errors.js +0 -42
- package/dist/errors/errors.spec.d.ts +0 -1
- package/dist/errors/errors.spec.js +0 -23
- package/dist/file-parser/__tests/file-parser.spec.d.ts +0 -1
- package/dist/file-parser/__tests/file-parser.spec.js +0 -118
- package/dist/file-parser/__tests/parse-row.spec.d.ts +0 -1
- package/dist/file-parser/__tests/parse-row.spec.js +0 -29
- package/dist/file-parser/__tests/sanitize-rows.spec.d.ts +0 -1
- package/dist/file-parser/__tests/sanitize-rows.spec.js +0 -78
- package/dist/file-parser/errors.d.ts +0 -3
- package/dist/file-parser/errors.js +0 -11
- package/dist/file-parser/file-parser.d.ts +0 -8
- package/dist/file-parser/file-parser.js +0 -68
- package/dist/file-parser/parse-row.d.ts +0 -2
- package/dist/file-parser/parse-row.js +0 -39
- package/dist/file-parser/sanitzeRows.d.ts +0 -3
- package/dist/file-parser/sanitzeRows.js +0 -18
- package/dist/formatting/formatting.d.ts +0 -2
- package/dist/formatting/formatting.js +0 -17
- package/dist/formatting/formatting.spec.d.ts +0 -1
- package/dist/formatting/formatting.spec.js +0 -37
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -20
- package/dist/middleware/middleware.d.ts +0 -4
- package/dist/middleware/middleware.js +0 -22
- package/dist/orm-helpers/ormHelpers.d.ts +0 -1
- package/dist/orm-helpers/ormHelpers.js +0 -17
- package/dist/orm-helpers/ormHelpers.spec.d.ts +0 -1
- package/dist/orm-helpers/ormHelpers.spec.js +0 -38
- package/dist/types/types.d.ts +0 -17
- package/dist/types/types.js +0 -203
- package/dist/validations/old.validations.d.ts +0 -5
- package/dist/validations/old.validations.js +0 -303
- package/dist/validations/validations.d.ts +0 -118
- package/dist/validations/validations.js +0 -300
- package/dist/validations/validations.spec.d.ts +0 -1
- package/dist/validations/validations.spec.js +0 -287
package/lib/validations.js
CHANGED
|
@@ -1,199 +1,214 @@
|
|
|
1
|
-
|
|
2
|
-
(
|
|
3
|
-
|
|
1
|
+
const E = require('./errors');
|
|
2
|
+
const V = require('validator');
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const {
|
|
5
|
+
crypto: { Signature },
|
|
6
|
+
HDPrivateKey,
|
|
7
|
+
HDPublicKey,
|
|
8
|
+
PublicKey,
|
|
9
|
+
Script,
|
|
10
|
+
Transaction,
|
|
11
|
+
} = require('bitcore-lib');
|
|
12
|
+
const addressValidator = require('wallet-address-validator');
|
|
13
|
+
const web3 = require('web3-utils');
|
|
4
14
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
15
|
+
const validations = {
|
|
16
|
+
_validate(Err, pred, x, msg) {
|
|
17
|
+
if (pred(x)) {
|
|
18
|
+
return x;
|
|
19
|
+
}
|
|
20
|
+
throw new Err(msg);
|
|
21
|
+
},
|
|
22
|
+
_validateArg(pred, x, msg) {
|
|
23
|
+
return this._validate(E.InvalidArguments, pred, x, msg);
|
|
24
|
+
},
|
|
25
|
+
_tryCheck(fn, arg) {
|
|
26
|
+
try {
|
|
27
|
+
fn(arg);
|
|
28
|
+
return true;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
10
33
|
|
|
11
|
-
|
|
34
|
+
exists(x) {
|
|
35
|
+
return x != null;
|
|
36
|
+
},
|
|
37
|
+
nonEmpty(x) {
|
|
38
|
+
return (x != null ? x.length : undefined) > 0;
|
|
39
|
+
},
|
|
40
|
+
isNonEmptyString(x) {
|
|
41
|
+
return _.isString(x) && (x != null ? x.length : undefined) > 0;
|
|
42
|
+
},
|
|
43
|
+
ensureShortEnough(limit, x) {
|
|
44
|
+
const l = x != null ? x.length : undefined;
|
|
45
|
+
if (l <= limit) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
throw new E.BadRequest(`Max query size exceeded: ${l} / ${limit}`);
|
|
49
|
+
},
|
|
50
|
+
ensure(crit, msg) {
|
|
51
|
+
if (crit) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
throw new E.BadRequest(msg);
|
|
55
|
+
},
|
|
56
|
+
argExists(x, msg) {
|
|
57
|
+
return this._validateArg(this.exists, x, msg);
|
|
58
|
+
},
|
|
12
59
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
60
|
+
check: {
|
|
61
|
+
matches(required, given, msg) {
|
|
62
|
+
given = _.uniq(given);
|
|
63
|
+
const crit = _.intersection(given, required).length === given.length;
|
|
64
|
+
if (crit) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
throw new E.BadRequest(msg);
|
|
68
|
+
},
|
|
69
|
+
wasFound(type) {
|
|
70
|
+
return (item) => {
|
|
71
|
+
if (item == null) {
|
|
72
|
+
throw new E.NotFound(`Unknown ${type}`);
|
|
73
|
+
} else {
|
|
74
|
+
return item;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
wasCreated(msg) {
|
|
79
|
+
return (arr) => {
|
|
80
|
+
if (!arr[1]) {
|
|
81
|
+
throw new E.BadRequest(msg);
|
|
82
|
+
} else {
|
|
83
|
+
return arr;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
isTrue(crit, msg) {
|
|
88
|
+
if (crit) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
throw new E.BadRequest(msg);
|
|
92
|
+
},
|
|
93
|
+
isFalse(crit, msg) {
|
|
94
|
+
if (!crit) {
|
|
95
|
+
return true;
|
|
19
96
|
}
|
|
97
|
+
throw new E.BadRequest(msg);
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
isCustomerReference(x) {
|
|
102
|
+
return (
|
|
103
|
+
_.isString(x) &&
|
|
104
|
+
(x != null ? x.length : undefined) > 0 &&
|
|
105
|
+
(x != null ? x.length : undefined) <= 100
|
|
106
|
+
);
|
|
107
|
+
},
|
|
108
|
+
isCustomerLabelName(x) {
|
|
109
|
+
return (
|
|
110
|
+
_.isString(x) &&
|
|
111
|
+
(x != null ? x.length : undefined) > 0 &&
|
|
112
|
+
(x != null ? x.length : undefined) <= 50
|
|
113
|
+
);
|
|
114
|
+
},
|
|
115
|
+
ethereum: {
|
|
116
|
+
isAddress(str) {
|
|
117
|
+
return web3.isAddress(str);
|
|
118
|
+
},
|
|
119
|
+
isBlockHash(str) {
|
|
120
|
+
return str.length === 66 && web3.isHexStrict(str);
|
|
20
121
|
},
|
|
21
|
-
|
|
22
|
-
return
|
|
122
|
+
isTxHash(str) {
|
|
123
|
+
return str.length === 66 && web3.isHexStrict(str);
|
|
23
124
|
},
|
|
24
|
-
|
|
25
|
-
|
|
125
|
+
isAddressCode(str) {
|
|
126
|
+
return web3.isHexStrict(str);
|
|
127
|
+
},
|
|
128
|
+
isValidWeiAmount(str) {
|
|
26
129
|
try {
|
|
27
|
-
|
|
130
|
+
web3.fromWei(str);
|
|
28
131
|
return true;
|
|
29
|
-
} catch (
|
|
132
|
+
} catch (e) {
|
|
30
133
|
return false;
|
|
31
134
|
}
|
|
32
135
|
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
136
|
+
},
|
|
137
|
+
bitcoin: {
|
|
138
|
+
isAddress(str) {
|
|
139
|
+
const network =
|
|
140
|
+
process.env.BITCOIN_NETWORK === 'testnet' ? 'testnet' : 'livenet';
|
|
141
|
+
return addressValidator(str, network);
|
|
38
142
|
},
|
|
39
|
-
|
|
40
|
-
|
|
143
|
+
isHDPublicKey(str) {
|
|
144
|
+
try {
|
|
145
|
+
HDPublicKey(str);
|
|
146
|
+
return true;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
41
150
|
},
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (l <= limit) {
|
|
151
|
+
isPublicKey(str) {
|
|
152
|
+
try {
|
|
153
|
+
PublicKey(str);
|
|
46
154
|
return true;
|
|
47
|
-
}
|
|
48
|
-
|
|
155
|
+
} catch (error) {
|
|
156
|
+
return false;
|
|
49
157
|
}
|
|
50
158
|
},
|
|
51
|
-
|
|
52
|
-
if (
|
|
159
|
+
isTxHash(str) {
|
|
160
|
+
if (!V.isHexadecimal(`${str}`)) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
return str.length === 64;
|
|
164
|
+
},
|
|
165
|
+
isTxHex(str) {
|
|
166
|
+
if (!V.isHexadecimal(`${str}`)) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
Transaction(str);
|
|
53
171
|
return true;
|
|
54
|
-
}
|
|
55
|
-
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return false;
|
|
56
174
|
}
|
|
57
175
|
},
|
|
58
|
-
|
|
59
|
-
return
|
|
176
|
+
isHDPath(str) {
|
|
177
|
+
return HDPrivateKey.isValidPath(str);
|
|
60
178
|
},
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
wasFound: function(type) {
|
|
73
|
-
return function(item) {
|
|
74
|
-
if (item == null) {
|
|
75
|
-
throw new E.NotFound("Unknown " + type);
|
|
76
|
-
} else {
|
|
77
|
-
return item;
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
},
|
|
81
|
-
wasCreated: function(msg) {
|
|
82
|
-
return function(arr) {
|
|
83
|
-
if (!arr[1]) {
|
|
84
|
-
throw new E.BadRequest(msg);
|
|
85
|
-
} else {
|
|
86
|
-
return arr;
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
},
|
|
90
|
-
isTrue: function(crit, msg) {
|
|
91
|
-
if (crit) {
|
|
92
|
-
return true;
|
|
93
|
-
} else {
|
|
94
|
-
throw new E.BadRequest(msg);
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
isFalse: function(crit, msg) {
|
|
98
|
-
if (!crit) {
|
|
99
|
-
return true;
|
|
100
|
-
} else {
|
|
101
|
-
throw new E.BadRequest(msg);
|
|
102
|
-
}
|
|
179
|
+
isScriptHex(str) {
|
|
180
|
+
if (!V.isHexadecimal(`${str}`)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
Script(str);
|
|
185
|
+
return true;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
return false;
|
|
103
188
|
}
|
|
104
189
|
},
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
isCustomerLabelName: function(x) {
|
|
109
|
-
return _.isString(x) && (x != null ? x.length : void 0) > 0 && (x != null ? x.length : void 0) <= 50;
|
|
110
|
-
},
|
|
111
|
-
bitcoin: {
|
|
112
|
-
isAddress: function(str) {
|
|
113
|
-
var error, network;
|
|
114
|
-
network = process.env.BITCOIN_NETWORK === 'testnet' ? 'testnet' : 'livenet';
|
|
115
|
-
try {
|
|
116
|
-
Address(str, network);
|
|
117
|
-
return true;
|
|
118
|
-
} catch (error) {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
isHDPublicKey: function(str) {
|
|
123
|
-
var error;
|
|
124
|
-
try {
|
|
125
|
-
HDPublicKey(str);
|
|
126
|
-
return true;
|
|
127
|
-
} catch (error) {
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
isPublicKey: function(str) {
|
|
132
|
-
var error;
|
|
133
|
-
try {
|
|
134
|
-
PublicKey(str);
|
|
135
|
-
return true;
|
|
136
|
-
} catch (error) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
isTxHash: function(str) {
|
|
141
|
-
if (!V.isHexadecimal(str + '')) {
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
return str.length === 64;
|
|
145
|
-
},
|
|
146
|
-
isTxHex: function(str) {
|
|
147
|
-
var error;
|
|
148
|
-
if (!V.isHexadecimal(str + '')) {
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
try {
|
|
152
|
-
Transaction(str);
|
|
153
|
-
return true;
|
|
154
|
-
} catch (error) {
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
isHDPath: function(str) {
|
|
159
|
-
return HDPrivateKey.isValidPath(str);
|
|
160
|
-
},
|
|
161
|
-
isScriptHex: function(str) {
|
|
162
|
-
var error;
|
|
163
|
-
if (!V.isHexadecimal(str + '')) {
|
|
164
|
-
return false;
|
|
165
|
-
}
|
|
166
|
-
try {
|
|
167
|
-
Script(str);
|
|
168
|
-
return true;
|
|
169
|
-
} catch (error) {
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
isTxSignature: function(str) {
|
|
174
|
-
var b, error;
|
|
175
|
-
if (!V.isHexadecimal(str + '')) {
|
|
176
|
-
return false;
|
|
177
|
-
}
|
|
178
|
-
try {
|
|
179
|
-
b = new Buffer(str, 'hex');
|
|
180
|
-
Signature.fromTxFormat(b);
|
|
181
|
-
return true;
|
|
182
|
-
} catch (error) {
|
|
183
|
-
return false;
|
|
184
|
-
}
|
|
185
|
-
},
|
|
186
|
-
isBlockHeight: function(obj) {
|
|
187
|
-
return _.isInteger(+obj) && parseInt(obj) >= 0;
|
|
190
|
+
isTxSignature(str) {
|
|
191
|
+
if (!V.isHexadecimal(`${str}`)) {
|
|
192
|
+
return false;
|
|
188
193
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
194
|
+
try {
|
|
195
|
+
const b = Buffer.from(str, 'hex');
|
|
196
|
+
Signature.fromTxFormat(b);
|
|
197
|
+
return true;
|
|
198
|
+
} catch (error) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
isBlockHeight(obj) {
|
|
203
|
+
return _.isInteger(+obj) && parseInt(obj, 10) >= 0;
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
};
|
|
196
207
|
|
|
197
|
-
|
|
208
|
+
Object.keys(V).forEach((k) => {
|
|
209
|
+
const v = V[k];
|
|
210
|
+
// don't trigger validation with null values (prevents swagger 500 error)
|
|
211
|
+
validations[k] = x => (x === null ? false : v(x));
|
|
212
|
+
});
|
|
198
213
|
|
|
199
|
-
|
|
214
|
+
module.exports = validations;
|
package/package.json
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ellipticltd/aml-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "Utilities, helpers, validations, type-checking, etc",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": "10.1.0",
|
|
7
|
+
"npm": "6.1.0"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+ssh://git@bitbucket.org/elliptic/aml-utils.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://bitbucket.org/elliptic/aml-utils#readme",
|
|
5
14
|
"main": "index.js",
|
|
6
15
|
"scripts": {
|
|
7
16
|
"test": "./node_modules/.bin/mocha --reporter spec --compilers coffee:coffee-script/register --require 'test/index.coffee'",
|
|
@@ -11,15 +20,21 @@
|
|
|
11
20
|
"author": "Adam Joyce <adam@ellipitc.co>",
|
|
12
21
|
"license": "MIT",
|
|
13
22
|
"dependencies": {
|
|
14
|
-
"bitcore-lib": "0.
|
|
23
|
+
"bitcore-lib": "5.0.0-beta.1",
|
|
15
24
|
"create-error": "0.3.1",
|
|
16
|
-
"lodash": "4.
|
|
25
|
+
"lodash": "4.17.4",
|
|
17
26
|
"type-check": "0.3.2",
|
|
18
|
-
"validator": "
|
|
27
|
+
"validator": "10.10.0",
|
|
28
|
+
"wallet-address-validator": "0.2.4",
|
|
29
|
+
"web3-utils": "1.0.0-beta.28"
|
|
19
30
|
},
|
|
20
31
|
"devDependencies": {
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
32
|
+
"@ellipticltd/eslint-config": "0.0.3",
|
|
33
|
+
"chai": "4.1.1",
|
|
34
|
+
"coffee-script": "1.12.7",
|
|
35
|
+
"eslint": "5.9.0",
|
|
36
|
+
"eslint-config-airbnb-base": "13.1.0",
|
|
37
|
+
"eslint-plugin-import": "2.14.0",
|
|
38
|
+
"mocha": "3.5.0"
|
|
24
39
|
}
|
|
25
40
|
}
|
package/.idea/aml-utils.iml
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="WEB_MODULE" version="4">
|
|
3
|
-
<component name="NewModuleRootManager">
|
|
4
|
-
<content url="file://$MODULE_DIR$">
|
|
5
|
-
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
6
|
-
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
7
|
-
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
-
</content>
|
|
9
|
-
<orderEntry type="inheritedJdk" />
|
|
10
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
|
11
|
-
</component>
|
|
12
|
-
</module>
|
package/.idea/misc.xml
DELETED
package/.idea/modules.xml
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<project version="4">
|
|
3
|
-
<component name="ProjectModuleManager">
|
|
4
|
-
<modules>
|
|
5
|
-
<module fileurl="file://$PROJECT_DIR$/.idea/aml-utils.iml" filepath="$PROJECT_DIR$/.idea/aml-utils.iml" />
|
|
6
|
-
</modules>
|
|
7
|
-
</component>
|
|
8
|
-
</project>
|