@human-protocol/sdk 5.2.0 → 6.0.0
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/dist/base.d.ts +4 -5
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +4 -5
- package/dist/constants.js +6 -6
- package/dist/encryption.d.ts +68 -203
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +66 -202
- package/dist/error.d.ts +0 -24
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +2 -26
- package/dist/escrow.d.ts +427 -780
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +314 -684
- package/dist/graphql/queries/operator.d.ts.map +1 -1
- package/dist/graphql/queries/operator.js +3 -1
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -4
- package/dist/kvstore.d.ts +119 -181
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +119 -182
- package/dist/operator.d.ts +59 -30
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +59 -30
- package/dist/staking.d.ts +135 -134
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +135 -134
- package/dist/statistics.d.ts +104 -134
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +119 -144
- package/dist/transaction.d.ts +36 -15
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +36 -15
- package/dist/types.d.ts +0 -54
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +31 -17
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +31 -17
- package/dist/worker.d.ts +35 -14
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +35 -14
- package/package.json +7 -24
- package/src/base.ts +4 -5
- package/src/constants.ts +6 -6
- package/src/encryption.ts +69 -203
- package/src/error.ts +0 -36
- package/src/escrow.ts +425 -780
- package/src/graphql/queries/operator.ts +3 -1
- package/src/graphql/types.ts +4 -2
- package/src/index.ts +4 -5
- package/src/kvstore.ts +120 -183
- package/src/operator.ts +59 -30
- package/src/staking.ts +135 -134
- package/src/statistics.ts +125 -146
- package/src/transaction.ts +36 -15
- package/src/types.ts +0 -57
- package/src/utils.ts +31 -17
- package/src/worker.ts +35 -14
- package/dist/storage.d.ts +0 -186
- package/dist/storage.d.ts.map +0 -1
- package/dist/storage.js +0 -319
- package/src/storage.ts +0 -313
package/src/storage.ts
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
import crypto from 'crypto';
|
|
4
|
-
import * as Minio from 'minio';
|
|
5
|
-
import {
|
|
6
|
-
ErrorInvalidUrl,
|
|
7
|
-
ErrorStorageBucketNotFound,
|
|
8
|
-
ErrorStorageClientNotInitialized,
|
|
9
|
-
ErrorStorageFileNotFound,
|
|
10
|
-
ErrorStorageFileNotUploaded,
|
|
11
|
-
} from './error';
|
|
12
|
-
import { UploadFile, StorageCredentials, StorageParams } from './types';
|
|
13
|
-
import { isValidUrl } from './utils';
|
|
14
|
-
import { HttpStatus } from './constants';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* @deprecated StorageClient is deprecated. Use Minio.Client directly.
|
|
19
|
-
*
|
|
20
|
-
* ## Introduction
|
|
21
|
-
*
|
|
22
|
-
* This client enables interacting with S3 cloud storage services like Amazon S3 Bucket, Google Cloud Storage, and others.
|
|
23
|
-
*
|
|
24
|
-
* The instance creation of `StorageClient` should be made using its constructor:
|
|
25
|
-
*
|
|
26
|
-
* ```ts
|
|
27
|
-
* constructor(params: StorageParams, credentials?: StorageCredentials)
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* > If credentials are not provided, it uses anonymous access to the bucket for downloading files.
|
|
31
|
-
*
|
|
32
|
-
* ## Installation
|
|
33
|
-
*
|
|
34
|
-
* ### npm
|
|
35
|
-
* ```bash
|
|
36
|
-
* npm install @human-protocol/sdk
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* ### yarn
|
|
40
|
-
* ```bash
|
|
41
|
-
* yarn install @human-protocol/sdk
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* ## Code example
|
|
45
|
-
*
|
|
46
|
-
* ```ts
|
|
47
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
48
|
-
*
|
|
49
|
-
* const credentials: StorageCredentials = {
|
|
50
|
-
* accessKey: 'ACCESS_KEY',
|
|
51
|
-
* secretKey: 'SECRET_KEY',
|
|
52
|
-
* };
|
|
53
|
-
* const params: StorageParams = {
|
|
54
|
-
* endPoint: 'http://localhost',
|
|
55
|
-
* port: 9000,
|
|
56
|
-
* useSSL: false,
|
|
57
|
-
* region: 'us-east-1'
|
|
58
|
-
* };
|
|
59
|
-
*
|
|
60
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export class StorageClient {
|
|
64
|
-
private client: Minio.Client;
|
|
65
|
-
private clientParams: StorageParams;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* **Storage client constructor**
|
|
69
|
-
*
|
|
70
|
-
* @param {StorageParams} params - Cloud storage params
|
|
71
|
-
* @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials are not provided - use anonymous access to the bucket
|
|
72
|
-
*/
|
|
73
|
-
constructor(params: StorageParams, credentials?: StorageCredentials) {
|
|
74
|
-
try {
|
|
75
|
-
this.clientParams = params;
|
|
76
|
-
|
|
77
|
-
this.client = new Minio.Client({
|
|
78
|
-
...params,
|
|
79
|
-
accessKey: credentials?.accessKey ?? '',
|
|
80
|
-
secretKey: credentials?.secretKey ?? '',
|
|
81
|
-
});
|
|
82
|
-
} catch {
|
|
83
|
-
throw ErrorStorageClientNotInitialized;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* This function downloads files from a bucket.
|
|
89
|
-
*
|
|
90
|
-
* @param {string[]} keys Array of filenames to download.
|
|
91
|
-
* @param {string} bucket Bucket name.
|
|
92
|
-
* @returns {Promise<any[]>} Returns an array of JSON files downloaded and parsed into objects.
|
|
93
|
-
*
|
|
94
|
-
* **Code example**
|
|
95
|
-
*
|
|
96
|
-
* ```ts
|
|
97
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
98
|
-
*
|
|
99
|
-
* const params: StorageParams = {
|
|
100
|
-
* endPoint: 'http://localhost',
|
|
101
|
-
* port: 9000,
|
|
102
|
-
* useSSL: false,
|
|
103
|
-
* region: 'us-east-1'
|
|
104
|
-
* };
|
|
105
|
-
*
|
|
106
|
-
* const storageClient = new StorageClient(params);
|
|
107
|
-
*
|
|
108
|
-
* const keys = ['file1.json', 'file2.json'];
|
|
109
|
-
* const files = await storageClient.downloadFiles(keys, 'bucket-name');
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
public async downloadFiles(keys: string[], bucket: string): Promise<any[]> {
|
|
113
|
-
const isBucketExists = await this.client.bucketExists(bucket);
|
|
114
|
-
if (!isBucketExists) {
|
|
115
|
-
throw ErrorStorageBucketNotFound;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return Promise.all(
|
|
119
|
-
keys.map(async (key) => {
|
|
120
|
-
try {
|
|
121
|
-
const response = await this.client.getObject(bucket, key);
|
|
122
|
-
const content = response?.read();
|
|
123
|
-
|
|
124
|
-
return { key, content: JSON.parse(content?.toString('utf-8') || '') };
|
|
125
|
-
} catch {
|
|
126
|
-
throw ErrorStorageFileNotFound;
|
|
127
|
-
}
|
|
128
|
-
})
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* This function downloads files from a URL.
|
|
134
|
-
*
|
|
135
|
-
* @param {string} url URL of the file to download.
|
|
136
|
-
* @returns {Promise<any>} Returns the JSON file downloaded and parsed into an object.
|
|
137
|
-
*
|
|
138
|
-
* **Code example**
|
|
139
|
-
*
|
|
140
|
-
* ```ts
|
|
141
|
-
* import { StorageClient } from '@human-protocol/sdk';
|
|
142
|
-
*
|
|
143
|
-
* const file = await StorageClient.downloadFileFromUrl('http://localhost/file.json');
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
public static async downloadFileFromUrl(url: string): Promise<any> {
|
|
147
|
-
if (!isValidUrl(url)) {
|
|
148
|
-
throw ErrorInvalidUrl;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
const { data, status } = await axios.get(url, {
|
|
153
|
-
headers: {
|
|
154
|
-
'Content-Type': 'application/json',
|
|
155
|
-
},
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
if (status !== HttpStatus.OK) {
|
|
159
|
-
throw ErrorStorageFileNotFound;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return data;
|
|
163
|
-
} catch {
|
|
164
|
-
throw ErrorStorageFileNotFound;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* This function uploads files to a bucket.
|
|
170
|
-
*
|
|
171
|
-
* @param {any[]} files Array of objects to upload serialized into JSON.
|
|
172
|
-
* @param {string} bucket Bucket name.
|
|
173
|
-
* @returns {Promise<UploadFile[]>} Returns an array of uploaded file metadata.
|
|
174
|
-
*
|
|
175
|
-
* **Code example**
|
|
176
|
-
*
|
|
177
|
-
* ```ts
|
|
178
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
179
|
-
*
|
|
180
|
-
* const credentials: StorageCredentials = {
|
|
181
|
-
* accessKey: 'ACCESS_KEY',
|
|
182
|
-
* secretKey: 'SECRET_KEY',
|
|
183
|
-
* };
|
|
184
|
-
* const params: StorageParams = {
|
|
185
|
-
* endPoint: 'http://localhost',
|
|
186
|
-
* port: 9000,
|
|
187
|
-
* useSSL: false,
|
|
188
|
-
* region: 'us-east-1'
|
|
189
|
-
* };
|
|
190
|
-
*
|
|
191
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
192
|
-
* const file1 = { name: 'file1', description: 'description of file1' };
|
|
193
|
-
* const file2 = { name: 'file2', description: 'description of file2' };
|
|
194
|
-
* const files = [file1, file2];
|
|
195
|
-
* const uploadedFiles = await storageClient.uploadFiles(files, 'bucket-name');
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
public async uploadFiles(
|
|
199
|
-
files: any[],
|
|
200
|
-
bucket: string
|
|
201
|
-
): Promise<UploadFile[]> {
|
|
202
|
-
const isBucketExists = await this.client.bucketExists(bucket);
|
|
203
|
-
if (!isBucketExists) {
|
|
204
|
-
throw ErrorStorageBucketNotFound;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return Promise.all(
|
|
208
|
-
files.map(async (file) => {
|
|
209
|
-
const content = JSON.stringify(file);
|
|
210
|
-
|
|
211
|
-
const hash = crypto.createHash('sha1').update(content).digest('hex');
|
|
212
|
-
const key = `s3${hash}.json`;
|
|
213
|
-
|
|
214
|
-
try {
|
|
215
|
-
await this.client.putObject(bucket, key, content, {
|
|
216
|
-
'Content-Type': 'application/json',
|
|
217
|
-
'Cache-Control': 'no-store',
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
return {
|
|
221
|
-
key,
|
|
222
|
-
url: `${this.clientParams.useSSL ? 'https' : 'http'}://${
|
|
223
|
-
this.clientParams.endPoint
|
|
224
|
-
}${
|
|
225
|
-
this.clientParams.port ? `:${this.clientParams.port}` : ''
|
|
226
|
-
}/${bucket}/${key}`,
|
|
227
|
-
hash,
|
|
228
|
-
};
|
|
229
|
-
} catch {
|
|
230
|
-
throw ErrorStorageFileNotUploaded;
|
|
231
|
-
}
|
|
232
|
-
})
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* This function checks if a bucket exists.
|
|
238
|
-
*
|
|
239
|
-
* @param {string} bucket Bucket name.
|
|
240
|
-
* @returns {Promise<boolean>} Returns `true` if exists, `false` if it doesn't.
|
|
241
|
-
*
|
|
242
|
-
* **Code example**
|
|
243
|
-
*
|
|
244
|
-
* ```ts
|
|
245
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
246
|
-
*
|
|
247
|
-
* const credentials: StorageCredentials = {
|
|
248
|
-
* accessKey: 'ACCESS_KEY',
|
|
249
|
-
* secretKey: 'SECRET_KEY',
|
|
250
|
-
* };
|
|
251
|
-
* const params: StorageParams = {
|
|
252
|
-
* endPoint: 'http://localhost',
|
|
253
|
-
* port: 9000,
|
|
254
|
-
* useSSL: false,
|
|
255
|
-
* region: 'us-east-1'
|
|
256
|
-
* };
|
|
257
|
-
*
|
|
258
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
259
|
-
* const exists = await storageClient.bucketExists('bucket-name');
|
|
260
|
-
* ```
|
|
261
|
-
*/
|
|
262
|
-
public async bucketExists(bucket: string): Promise<boolean> {
|
|
263
|
-
return this.client.bucketExists(bucket);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* This function lists all file names contained in the bucket.
|
|
268
|
-
*
|
|
269
|
-
* @param {string} bucket Bucket name.
|
|
270
|
-
* @returns {Promise<string[]>} Returns the list of file names contained in the bucket.
|
|
271
|
-
*
|
|
272
|
-
* **Code example**
|
|
273
|
-
*
|
|
274
|
-
* ```ts
|
|
275
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
276
|
-
*
|
|
277
|
-
* const credentials: StorageCredentials = {
|
|
278
|
-
* accessKey: 'ACCESS_KEY',
|
|
279
|
-
* secretKey: 'SECRET_KEY',
|
|
280
|
-
* };
|
|
281
|
-
* const params: StorageParams = {
|
|
282
|
-
* endPoint: 'http://localhost',
|
|
283
|
-
* port: 9000,
|
|
284
|
-
* useSSL: false,
|
|
285
|
-
* region: 'us-east-1'
|
|
286
|
-
* };
|
|
287
|
-
*
|
|
288
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
289
|
-
* const fileNames = await storageClient.listObjects('bucket-name');
|
|
290
|
-
* ```
|
|
291
|
-
*/
|
|
292
|
-
public async listObjects(bucket: string): Promise<string[]> {
|
|
293
|
-
const isBucketExists = await this.client.bucketExists(bucket);
|
|
294
|
-
if (!isBucketExists) {
|
|
295
|
-
throw ErrorStorageBucketNotFound;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
try {
|
|
299
|
-
return new Promise((resolve, reject) => {
|
|
300
|
-
const keys: string[] = [];
|
|
301
|
-
const stream = this.client.listObjectsV2(bucket, '', true, '');
|
|
302
|
-
|
|
303
|
-
stream.on('data', (obj: { name: string }) => keys.push(obj.name));
|
|
304
|
-
stream.on('error', reject);
|
|
305
|
-
stream.on('end', () => {
|
|
306
|
-
resolve(keys);
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
} catch (e) {
|
|
310
|
-
throw new Error(String(e));
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|