dkg.js 6.0.0 → 6.0.2

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.
@@ -1,8 +1,211 @@
1
- const { MAX_FILE_SIZE, BLOCKCHAINS, OPERATIONS, GET_OUTPUT_FORMATS } = require('../constants.js');
1
+ const { ASSET_STATES, CONTENT_TYPES, GRAPH_LOCATIONS, GRAPH_STATES, MAX_FILE_SIZE, OPERATIONS, GET_OUTPUT_FORMATS, QUERY_TYPES } = require('../constants.js');
2
2
  const { nodeSupported } = require('./utilities.js');
3
3
 
4
4
  class ValidationService {
5
- validatePublishRequest(content, options) {
5
+ validateNodeInfo(endpoint, port, authToken) {
6
+ this.validateEndpoint(endpoint);
7
+ this.validatePort(port);
8
+ this.validateAuthToken(authToken);
9
+ }
10
+
11
+ validateGraphQuery(
12
+ queryString,
13
+ queryType,
14
+ graphLocation,
15
+ graphState,
16
+ endpoint,
17
+ port,
18
+ maxNumberOfRetries,
19
+ frequency,
20
+ authToken,
21
+ ) {
22
+ this.validateQueryString(queryString);
23
+ this.validateQueryType(queryType);
24
+ this.validateGraphLocation(graphLocation);
25
+ this.validateGraphState(graphState);
26
+ this.validateEndpoint(endpoint);
27
+ this.validatePort(port);
28
+ this.validateMaxNumberOfRetries(maxNumberOfRetries);
29
+ this.validateFrequency(frequency);
30
+ this.validateAuthToken(authToken);
31
+ }
32
+
33
+ validateAssetCreate(
34
+ content,
35
+ blockchain,
36
+ endpoint,
37
+ port,
38
+ maxNumberOfRetries,
39
+ frequency,
40
+ epochsNum,
41
+ hashFunctionId,
42
+ scoreFunctionId,
43
+ immutable,
44
+ tokenAmount,
45
+ authToken,
46
+ ) {
47
+ this.validateContent(content)
48
+ this.validateBlockchain(blockchain, OPERATIONS.PUBLISH);
49
+ this.validateEndpoint(endpoint);
50
+ this.validatePort(port);
51
+ this.validateMaxNumberOfRetries(maxNumberOfRetries);
52
+ this.validateFrequency(frequency);
53
+ this.validateEpochsNum(epochsNum);
54
+ this.validateHashFunctionId(hashFunctionId);
55
+ this.validateScoreFunctionId(scoreFunctionId);
56
+ this.validateImmutable(immutable);
57
+ this.validateTokenAmount(tokenAmount);
58
+ this.validateAuthToken(authToken);
59
+ }
60
+
61
+ validateAssetGet(
62
+ UAL,
63
+ blockchain,
64
+ endpoint,
65
+ port,
66
+ maxNumberOfRetries,
67
+ frequency,
68
+ state,
69
+ contentType,
70
+ hashFunctionId,
71
+ validate,
72
+ outputFormat,
73
+ authToken,
74
+ ) {
75
+ this.validateUAL(UAL);
76
+ this.validateBlockchain(blockchain, OPERATIONS.GET);
77
+ this.validateEndpoint(endpoint);
78
+ this.validatePort(port);
79
+ this.validateMaxNumberOfRetries(maxNumberOfRetries);
80
+ this.validateFrequency(frequency);
81
+ this.validateState(state);
82
+ this.validateContentType(contentType);
83
+ this.validateHashFunctionId(hashFunctionId);
84
+ this.validateValidate(validate);
85
+ this.validateOutputFormat(outputFormat);
86
+ this.validateAuthToken(authToken);
87
+ }
88
+
89
+ validateAssetUpdate(
90
+ content,
91
+ blockchain,
92
+ endpoint,
93
+ port,
94
+ maxNumberOfRetries,
95
+ frequency,
96
+ hashFunctionId,
97
+ scoreFunctionId,
98
+ tokenAmount,
99
+ authToken,
100
+ ) {
101
+ this.validateContent(content)
102
+ this.validateBlockchain(blockchain, OPERATIONS.UPDATE);
103
+ this.validateEndpoint(endpoint);
104
+ this.validatePort(port);
105
+ this.validateMaxNumberOfRetries(maxNumberOfRetries);
106
+ this.validateFrequency(frequency);
107
+ this.validateHashFunctionId(hashFunctionId);
108
+ this.validateScoreFunctionId(scoreFunctionId);
109
+ this.validateTokenAmount(tokenAmount);
110
+ this.validateAuthToken(authToken);
111
+ }
112
+
113
+ validateWaitAssetUpdateFinalization(UAL, blockchain) {
114
+ this.validateUAL(UAL);
115
+ this.validateBlockchain(blockchain);
116
+ }
117
+
118
+ validateAssetUpdateCancel(UAL, blockchain) {
119
+ this.validateUAL(UAL);
120
+ this.validateBlockchain(blockchain);
121
+ }
122
+
123
+ validateAssetTransfer(UAL, newOwner, blockchain) {
124
+ this.validateUAL(UAL);
125
+ this.validateNewOwner(newOwner);
126
+ this.validateBlockchain(blockchain);
127
+ }
128
+
129
+ validateAssetGetOwner(UAL, blockchain) {
130
+ this.validateUAL(UAL);
131
+ this.validateBlockchain(blockchain);
132
+ }
133
+
134
+ validateAssetBurn(UAL, blockchain) {
135
+ this.validateUAL(UAL);
136
+ this.validateBlockchain(blockchain);
137
+ }
138
+
139
+ validateExtendAssetStoringPeriod(UAL, epochsNum, tokenAmount, blockchain) {
140
+ this.validateUAL(UAL);
141
+ this.validateEpochsNum(epochsNum);
142
+ this.validateTokenAmount(tokenAmount);
143
+ this.validateBlockchain(blockchain);
144
+ }
145
+
146
+ validateAddTokens(UAL, tokenAmount, blockchain) {
147
+ this.validateUAL(UAL);
148
+ this.validateTokenAmount(tokenAmount);
149
+ this.validateBlockchain(blockchain);
150
+ }
151
+
152
+ validateRequiredParam(paramName, param) {
153
+ if (param == null) throw Error(`${paramName} is missing.`);
154
+ }
155
+
156
+ validateParamType(paramName, param, type) {
157
+ let parameter = param;
158
+ if (type === 'number') {
159
+ parameter = parseInt(param, 10);
160
+ }
161
+ // eslint-disable-next-line valid-typeof
162
+ if (typeof parameter !== type) throw Error(`${paramName} must be of type ${type}.`);
163
+ }
164
+
165
+ validateQueryString(queryString) {
166
+ this.validateRequiredParam('queryString', queryString);
167
+ this.validateParamType('queryString', queryString, 'string');
168
+ }
169
+
170
+ validateQueryType(queryType) {
171
+ this.validateRequiredParam('queryType', queryType);
172
+ const validQueryTypes = Object.values(QUERY_TYPES);
173
+ if (!validQueryTypes.includes(queryType))
174
+ throw Error(`Invalid query Type: available query types: ${validQueryTypes}`);
175
+ }
176
+
177
+ validateGraphLocation(graphLocation) {
178
+ this.validateRequiredParam('graphLocation', graphLocation);
179
+ const validGraphLocations = Object.keys(GRAPH_LOCATIONS);
180
+ if (!validGraphLocations.includes(graphLocation))
181
+ throw Error(`Invalid graph location: available locations: ${validGraphLocations}`);
182
+ }
183
+
184
+ validateGraphState(graphState) {
185
+ this.validateRequiredParam('graphState', graphState);
186
+ const validGraphStates = Object.keys(GRAPH_STATES);
187
+ if (!validGraphStates.includes(graphState))
188
+ throw Error(`Invalid graph state: available states: ${validGraphStates}`);
189
+ }
190
+
191
+ validateUAL(ual) {
192
+ this.validateRequiredParam('UAL', ual);
193
+ this.validateParamType('UAL', ual, 'string');
194
+
195
+ const segments = ual.split(':');
196
+ const argsString = segments.length === 3 ? segments[2] : segments[2] + segments[3];
197
+ const args = argsString.split('/');
198
+
199
+ if (!(args?.length === 3)) throw Error('Invalid UAL.');
200
+ }
201
+
202
+ validateObjectType(obj) {
203
+ if (!(!!obj && typeof obj === 'object')) throw Error('Content must be an object');
204
+ }
205
+
206
+ validateContent(content) {
207
+ this.validateRequiredParam('content', content);
208
+
6
209
  const keys = Object.keys(content);
7
210
 
8
211
  if (
@@ -11,79 +214,113 @@ class ValidationService {
11
214
  )
12
215
  throw Error('content keys can only be "public", "private" or both.');
13
216
 
14
- if(!content.public && !content.private) {
217
+ if (!content.public && !content.private) {
15
218
  throw Error('Public or private content must be defined');
16
219
  }
17
220
 
18
- if (content.public) {
19
- this.validateSize(content.public);
20
- }
21
- if (content.private) {
22
- this.validateSize(content.private);
23
- }
24
- this.validateBlockchain(options.blockchain);
221
+ if (Buffer.byteLength(JSON.stringify(content), 'utf-8') > MAX_FILE_SIZE)
222
+ throw Error(`File size limit is ${MAX_FILE_SIZE / (1024 * 1024)}MB.`);
25
223
  }
26
224
 
27
- validateGetRequest(UAL, options) {
28
- if (!UAL) throw Error('UAL is missing.');
29
- this.validateValidate(options?.validate);
30
- this.validateOutputFormat(options?.outputFormat);
225
+ validateEndpoint(endpoint) {
226
+ this.validateRequiredParam('endpoint', endpoint);
227
+ this.validateParamType('endpoint', endpoint, 'string');
228
+ if (!endpoint.startsWith('http') && !endpoint.startsWith('ws'))
229
+ throw Error('Endpoint should start with either "http" or "ws"');
31
230
  }
32
231
 
33
- validateAssetTransferRequest(UAL, newOwner, options) {
34
- if (!UAL || !newOwner) throw Error('Wrong parameters for the transfer.');
35
- this.validateBlockchain(options.blockchain);
232
+ validatePort(port) {
233
+ this.validateRequiredParam('port', port);
234
+ this.validateParamType('port', port, 'number');
36
235
  }
37
236
 
38
- validateContentType(obj) {
39
- if (!(!!obj && typeof obj === 'object')) throw Error('Content must be an object');
237
+ validateMaxNumberOfRetries(maxNumberOfRetries) {
238
+ this.validateRequiredParam('maxNumberOfRetries', maxNumberOfRetries);
239
+ this.validateParamType('maxNumberOfRetries', maxNumberOfRetries, 'number');
40
240
  }
41
241
 
42
- validateSize(content) {
43
- if (!content) throw Error('No content provided');
44
- if (Buffer.byteLength(JSON.stringify(content), 'utf-8') > MAX_FILE_SIZE)
45
- throw Error(`File size limit is ${MAX_FILE_SIZE / (1024 * 1024)}MB.`);
242
+ validateFrequency(frequency) {
243
+ this.validateRequiredParam('frequency', frequency);
244
+ this.validateParamType('frequency', frequency, 'number');
245
+ }
246
+
247
+ validateState(state) {
248
+ this.validateRequiredParam('state', state);
249
+ this.validateParamType('state', state, 'string');
250
+ const validStates = Object.values(ASSET_STATES);
251
+ if (!validStates.includes(state.toUpperCase()))
252
+ throw Error(`Invalid state, available states: ${validStates}`);
253
+ }
254
+
255
+ validateContentType(contentType) {
256
+ this.validateRequiredParam('contentType', contentType);
257
+
258
+ const validContentTypes = Object.values(CONTENT_TYPES);
259
+ if (!validContentTypes.includes(contentType))
260
+ throw Error(`Invalid content visibility! Available parameters: ${validContentTypes}`)
261
+ }
262
+
263
+ validateEpochsNum(epochsNum) {
264
+ this.validateRequiredParam('epochsNum', epochsNum);
265
+ this.validateParamType('epochsNum', epochsNum, 'number');
266
+ }
267
+
268
+ validateHashFunctionId(hashFunctionId) {
269
+ this.validateRequiredParam('hashFunctionId', hashFunctionId);
270
+ this.validateParamType('hashFunctionId', hashFunctionId, 'number');
271
+ }
272
+
273
+ validateScoreFunctionId(scoreFunctionId) {
274
+ this.validateRequiredParam('scoreFunctionId', scoreFunctionId);
275
+ this.validateParamType('scoreFunctionId', scoreFunctionId, 'number');
276
+ }
277
+
278
+ validateImmutable(immutable) {
279
+ this.validateRequiredParam('immutable', immutable);
280
+ this.validateParamType('immutable', immutable, 'boolean');
281
+ }
282
+
283
+ validateTokenAmount(tokenAmount) {
284
+ if (tokenAmount == null) return;
285
+
286
+ this.validateParamType('tokenAmount', tokenAmount, 'number');
287
+ }
288
+
289
+ validateAuthToken(authToken) {
290
+ if (authToken == null) return;
291
+
292
+ this.validateParamType('authToken', authToken, 'string');
46
293
  }
47
294
 
48
295
  validateValidate(validate) {
49
- if (validate && typeof validate !== 'boolean') throw Error('Valid option must be boolean');
296
+ this.validateRequiredParam('validate', validate);
297
+ this.validateParamType('validate', validate, 'boolean');
50
298
  }
51
299
 
52
300
  validateOutputFormat(outputFormat) {
53
- if (
54
- outputFormat &&
55
- GET_OUTPUT_FORMATS.JSON_LD !== outputFormat &&
56
- GET_OUTPUT_FORMATS.N_QUADS !== outputFormat
57
- )
58
- throw Error('Unknown output format');
301
+ this.validateRequiredParam('outputFormat', outputFormat);
302
+ const validOutputFormats = Object.values(GET_OUTPUT_FORMATS);
303
+ if (!validOutputFormats.includes(outputFormat))
304
+ throw Error(`Invalid query Type: available query types: ${validOutputFormats}`);
59
305
  }
60
306
 
61
307
  validateBlockchain(blockchain, operation) {
62
- if (!blockchain) throw Error('Blockchain configuration missing');
63
- if (!blockchain.name) throw Error('Blockchain name missing');
64
- if (nodeSupported() && !blockchain.rpc && !BLOCKCHAINS[blockchain.name].rpc)
65
- throw Error('Blockchain rpc missing');
66
- if (!blockchain.hubContract && !BLOCKCHAINS[blockchain.name].hubContract)
67
- throw Error('Blockchain hub contract missing');
68
- if (operation !== OPERATIONS.GET) {
69
- if (!blockchain.publicKey && nodeSupported())
70
- throw Error('Blockchain public key missing');
71
- if (!blockchain.privateKey && nodeSupported())
72
- throw Error('Blockchain private key missing');
73
- }
74
- }
308
+ this.validateRequiredParam('blockchain', blockchain);
309
+ this.validateRequiredParam('blockchain name', blockchain.name);
310
+ this.validateRequiredParam('blockchain hub contract', blockchain.hubContract);
311
+ if (nodeSupported()) {
312
+ this.validateRequiredParam('blockchain rpc', blockchain.rpc);
75
313
 
76
- validateKeywords(keywords) {
77
- if (!keywords) throw Error('No keywords provided');
78
- if (!Array.isArray(keywords)) throw Error('Keywords must be an array');
79
- if (keywords.length <= 0) throw Error('Keywords array must be non empty');
80
- if (keywords.length > 10) throw Error('Too many keywords provided, limit is 10.');
81
- if (!keywords.every((i) => typeof i === 'string' && i !== ''))
82
- throw Error('all keywords must be non empty strings');
314
+ if (operation !== OPERATIONS.GET) {
315
+ this.validateRequiredParam('blockchain public key', blockchain.publicKey);
316
+ this.validateRequiredParam('blockchain private key', blockchain.privateKey);
317
+ }
318
+ }
83
319
  }
84
320
 
85
- validateGetOwnerRequest(UAL) {
86
- if (!UAL) throw Error('UAL is missing.');
321
+ validateNewOwner(newOwner) {
322
+ this.validateRequiredParam('newOwner', newOwner);
323
+ this.validateParamType('newOwner', newOwner, 'string');
87
324
  }
88
325
  }
89
326
  module.exports = ValidationService;
package/webpack.config.js CHANGED
@@ -1,9 +1,9 @@
1
1
  const path = require('path');
2
- const TerserPlugin = require('terser-webpack-plugin');
3
2
  const webpack = require('webpack');
3
+ const TerserPlugin = require('terser-webpack-plugin');
4
4
 
5
5
  module.exports = {
6
- mode: 'development',
6
+ mode: 'production',
7
7
  entry: './index.js',
8
8
  output: {
9
9
  path: path.resolve(__dirname, 'dist'),
@@ -23,6 +23,12 @@ module.exports = {
23
23
  resolve: {
24
24
  fallback: {
25
25
  fs: false,
26
+ zlib: false,
27
+ url: false,
28
+ os: false,
29
+ path: false,
30
+ net: false,
31
+ tls: false,
26
32
  stream: require.resolve('stream-browserify'),
27
33
  http: require.resolve('stream-http/'),
28
34
  https: require.resolve('https-browserify/'),