@human-protocol/sdk 6.1.0 → 7.1.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/CHANGELOG.md +32 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +26 -14
- package/dist/encryption/encryption.d.ts +84 -0
- package/dist/encryption/encryption.d.ts.map +1 -0
- package/dist/{encryption.js → encryption/encryption.js} +3 -161
- package/dist/encryption/encryption_utils.d.ts +101 -0
- package/dist/encryption/encryption_utils.d.ts.map +1 -0
- package/dist/encryption/encryption_utils.js +191 -0
- package/dist/encryption/index.d.ts +4 -0
- package/dist/encryption/index.d.ts.map +1 -0
- package/dist/encryption/index.js +7 -0
- package/dist/encryption/types.d.ts +9 -0
- package/dist/encryption/types.d.ts.map +1 -0
- package/dist/encryption/types.js +9 -0
- package/dist/error.d.ts +6 -10
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +12 -14
- package/dist/{escrow.d.ts → escrow/escrow_client.d.ts} +4 -184
- package/dist/{escrow.d.ts.map → escrow/escrow_client.d.ts.map} +1 -1
- package/dist/{escrow.js → escrow/escrow_client.js} +35 -411
- package/dist/escrow/escrow_utils.d.ts +172 -0
- package/dist/escrow/escrow_utils.d.ts.map +1 -0
- package/dist/escrow/escrow_utils.js +394 -0
- package/dist/escrow/index.d.ts +3 -0
- package/dist/escrow/index.d.ts.map +1 -0
- package/dist/escrow/index.js +7 -0
- package/dist/graphql/queries/escrow.d.ts +1 -1
- package/dist/graphql/queries/escrow.d.ts.map +1 -1
- package/dist/graphql/queries/escrow.js +5 -1
- package/dist/graphql/queries/statistics.d.ts +1 -0
- package/dist/graphql/queries/statistics.d.ts.map +1 -1
- package/dist/graphql/queries/statistics.js +36 -2
- package/dist/graphql/queries/worker.d.ts.map +1 -1
- package/dist/graphql/queries/worker.js +0 -2
- package/dist/graphql/types.d.ts +5 -2
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +10 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -22
- package/dist/interfaces.d.ts +4 -7
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore/index.d.ts +3 -0
- package/dist/kvstore/index.d.ts.map +1 -0
- package/dist/kvstore/index.js +7 -0
- package/dist/{kvstore.d.ts → kvstore/kvstore_client.d.ts} +3 -108
- package/dist/kvstore/kvstore_client.d.ts.map +1 -0
- package/dist/{kvstore.js → kvstore/kvstore_client.js} +22 -184
- package/dist/kvstore/kvstore_utils.d.ts +105 -0
- package/dist/kvstore/kvstore_utils.d.ts.map +1 -0
- package/dist/kvstore/kvstore_utils.js +184 -0
- package/dist/operator/index.d.ts +2 -0
- package/dist/operator/index.d.ts.map +1 -0
- package/dist/operator/index.js +5 -0
- package/dist/{operator.d.ts → operator/operator_utils.d.ts} +3 -3
- package/dist/operator/operator_utils.d.ts.map +1 -0
- package/dist/{operator.js → operator/operator_utils.js} +6 -6
- package/dist/staking/index.d.ts +3 -0
- package/dist/staking/index.d.ts.map +1 -0
- package/dist/staking/index.js +7 -0
- package/dist/{staking.d.ts → staking/staking_client.d.ts} +4 -65
- package/dist/staking/staking_client.d.ts.map +1 -0
- package/dist/{staking.js → staking/staking_client.js} +6 -136
- package/dist/staking/staking_utils.d.ts +63 -0
- package/dist/staking/staking_utils.d.ts.map +1 -0
- package/dist/staking/staking_utils.js +137 -0
- package/dist/statistics/index.d.ts +2 -0
- package/dist/statistics/index.d.ts.map +1 -0
- package/dist/statistics/index.js +5 -0
- package/dist/{statistics.d.ts → statistics/statistics_utils.d.ts} +4 -13
- package/dist/statistics/statistics_utils.d.ts.map +1 -0
- package/dist/{statistics.js → statistics/statistics_utils.js} +8 -22
- package/dist/transaction/index.d.ts +2 -0
- package/dist/transaction/index.d.ts.map +1 -0
- package/dist/transaction/index.js +5 -0
- package/dist/{transaction.d.ts → transaction/transaction_utils.d.ts} +3 -3
- package/dist/transaction/transaction_utils.d.ts.map +1 -0
- package/dist/{transaction.js → transaction/transaction_utils.js} +5 -5
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +15 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +71 -4
- package/dist/worker/index.d.ts +2 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +5 -0
- package/dist/{worker.d.ts → worker/worker_utils.d.ts} +3 -4
- package/dist/worker/worker_utils.d.ts.map +1 -0
- package/dist/{worker.js → worker/worker_utils.js} +5 -7
- package/package.json +12 -8
- package/src/constants.ts +38 -14
- package/src/{encryption.ts → encryption/encryption.ts} +1 -193
- package/src/encryption/encryption_utils.ts +179 -0
- package/src/encryption/index.ts +3 -0
- package/src/encryption/types.ts +15 -0
- package/src/error.ts +11 -17
- package/src/{escrow.ts → escrow/escrow_client.ts} +36 -556
- package/src/escrow/escrow_utils.ts +518 -0
- package/src/escrow/index.ts +2 -0
- package/src/graphql/queries/escrow.ts +6 -1
- package/src/graphql/queries/statistics.ts +37 -1
- package/src/graphql/queries/worker.ts +0 -2
- package/src/graphql/types.ts +6 -2
- package/src/index.ts +11 -25
- package/src/interfaces.ts +4 -7
- package/src/kvstore/index.ts +2 -0
- package/src/{kvstore.ts → kvstore/kvstore_client.ts} +27 -241
- package/src/kvstore/kvstore_utils.ts +244 -0
- package/src/operator/index.ts +1 -0
- package/src/{operator.ts → operator/operator_utils.ts} +8 -8
- package/src/staking/index.ts +2 -0
- package/src/{staking.ts → staking/staking_client.ts} +8 -174
- package/src/staking/staking_utils.ts +170 -0
- package/src/statistics/index.ts +1 -0
- package/src/{statistics.ts → statistics/statistics_utils.ts} +14 -26
- package/src/transaction/index.ts +1 -0
- package/src/{transaction.ts → transaction/transaction_utils.ts} +7 -7
- package/src/types.ts +8 -0
- package/src/utils.ts +80 -3
- package/src/worker/index.ts +1 -0
- package/src/{worker.ts → worker/worker_utils.ts} +7 -9
- package/dist/encryption.d.ts +0 -189
- package/dist/encryption.d.ts.map +0 -1
- package/dist/kvstore.d.ts.map +0 -1
- package/dist/operator.d.ts.map +0 -1
- package/dist/staking.d.ts.map +0 -1
- package/dist/statistics.d.ts.map +0 -1
- package/dist/transaction.d.ts.map +0 -1
- package/dist/worker.d.ts.map +0 -1
|
@@ -9,15 +9,14 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.KVStoreClient = void 0;
|
|
13
13
|
const typechain_types_1 = require("@human-protocol/core/typechain-types");
|
|
14
14
|
const ethers_1 = require("ethers");
|
|
15
|
-
const base_1 = require("
|
|
16
|
-
const constants_1 = require("
|
|
17
|
-
const decorators_1 = require("
|
|
18
|
-
const error_1 = require("
|
|
19
|
-
const utils_1 = require("
|
|
20
|
-
const kvstore_1 = require("./graphql/queries/kvstore");
|
|
15
|
+
const base_1 = require("../base");
|
|
16
|
+
const constants_1 = require("../constants");
|
|
17
|
+
const decorators_1 = require("../decorators");
|
|
18
|
+
const error_1 = require("../error");
|
|
19
|
+
const utils_1 = require("../utils");
|
|
21
20
|
/**
|
|
22
21
|
* Client for interacting with the KVStore contract.
|
|
23
22
|
*
|
|
@@ -139,8 +138,10 @@ class KVStoreClient extends base_1.BaseEthersClient {
|
|
|
139
138
|
await this.sendTxAndWait((overrides) => this.contract.set(key, value, overrides), txOptions);
|
|
140
139
|
}
|
|
141
140
|
catch (e) {
|
|
142
|
-
if (e instanceof Error)
|
|
141
|
+
if (e instanceof Error) {
|
|
143
142
|
throw Error(`Failed to set value: ${e.message}`);
|
|
143
|
+
}
|
|
144
|
+
throw e;
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
147
|
/**
|
|
@@ -170,8 +171,10 @@ class KVStoreClient extends base_1.BaseEthersClient {
|
|
|
170
171
|
await this.sendTxAndWait((overrides) => this.contract.setBulk(keys, values, overrides), txOptions);
|
|
171
172
|
}
|
|
172
173
|
catch (e) {
|
|
173
|
-
if (e instanceof Error)
|
|
174
|
+
if (e instanceof Error) {
|
|
174
175
|
throw Error(`Failed to set bulk values: ${e.message}`);
|
|
176
|
+
}
|
|
177
|
+
throw e;
|
|
175
178
|
}
|
|
176
179
|
}
|
|
177
180
|
/**
|
|
@@ -201,8 +204,10 @@ class KVStoreClient extends base_1.BaseEthersClient {
|
|
|
201
204
|
await this.sendTxAndWait((overrides) => this.contract.setBulk([urlKey, hashKey], [url, contentHash], overrides), txOptions);
|
|
202
205
|
}
|
|
203
206
|
catch (e) {
|
|
204
|
-
if (e instanceof Error)
|
|
207
|
+
if (e instanceof Error) {
|
|
205
208
|
throw Error(`Failed to set URL and hash: ${e.message}`);
|
|
209
|
+
}
|
|
210
|
+
throw e;
|
|
206
211
|
}
|
|
207
212
|
}
|
|
208
213
|
/**
|
|
@@ -222,18 +227,21 @@ class KVStoreClient extends base_1.BaseEthersClient {
|
|
|
222
227
|
* ```
|
|
223
228
|
*/
|
|
224
229
|
async get(address, key) {
|
|
225
|
-
if (key === '')
|
|
230
|
+
if (key === '') {
|
|
226
231
|
throw error_1.ErrorKVStoreEmptyKey;
|
|
227
|
-
|
|
232
|
+
}
|
|
233
|
+
if (!ethers_1.ethers.isAddress(address)) {
|
|
228
234
|
throw error_1.ErrorInvalidAddress;
|
|
235
|
+
}
|
|
229
236
|
try {
|
|
230
237
|
const result = await this.contract?.get(address, key);
|
|
231
238
|
return result;
|
|
232
239
|
}
|
|
233
240
|
catch (e) {
|
|
234
|
-
if (e instanceof Error)
|
|
241
|
+
if (e instanceof Error) {
|
|
235
242
|
throw Error(`Failed to get value: ${e.message}`);
|
|
236
|
-
|
|
243
|
+
}
|
|
244
|
+
throw e;
|
|
237
245
|
}
|
|
238
246
|
}
|
|
239
247
|
}
|
|
@@ -256,173 +264,3 @@ __decorate([
|
|
|
256
264
|
__metadata("design:paramtypes", [String, Object, Object]),
|
|
257
265
|
__metadata("design:returntype", Promise)
|
|
258
266
|
], KVStoreClient.prototype, "setFileUrlAndHash", null);
|
|
259
|
-
/**
|
|
260
|
-
* Utility helpers for KVStore-related queries.
|
|
261
|
-
*
|
|
262
|
-
* @example
|
|
263
|
-
* ```ts
|
|
264
|
-
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
265
|
-
*
|
|
266
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
267
|
-
* ChainId.POLYGON_AMOY,
|
|
268
|
-
* "0x1234567890123456789012345678901234567890"
|
|
269
|
-
* );
|
|
270
|
-
* console.log('KVStore data:', kvStoreData);
|
|
271
|
-
* ```
|
|
272
|
-
*/
|
|
273
|
-
class KVStoreUtils {
|
|
274
|
-
/**
|
|
275
|
-
* This function returns the KVStore data for a given address.
|
|
276
|
-
*
|
|
277
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
278
|
-
* @param address - Address of the KVStore
|
|
279
|
-
* @param options - Optional configuration for subgraph requests.
|
|
280
|
-
* @returns KVStore data
|
|
281
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
282
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
283
|
-
*
|
|
284
|
-
* @example
|
|
285
|
-
* ```ts
|
|
286
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
287
|
-
* ChainId.POLYGON_AMOY,
|
|
288
|
-
* "0x1234567890123456789012345678901234567890"
|
|
289
|
-
* );
|
|
290
|
-
* console.log('KVStore data:', kvStoreData);
|
|
291
|
-
* ```
|
|
292
|
-
*/
|
|
293
|
-
static async getKVStoreData(chainId, address, options) {
|
|
294
|
-
const networkData = constants_1.NETWORKS[chainId];
|
|
295
|
-
if (!networkData) {
|
|
296
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
297
|
-
}
|
|
298
|
-
if (address && !ethers_1.ethers.isAddress(address)) {
|
|
299
|
-
throw error_1.ErrorInvalidAddress;
|
|
300
|
-
}
|
|
301
|
-
const { kvstores } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, kvstore_1.GET_KVSTORE_BY_ADDRESS_QUERY)(), { address: address.toLowerCase() }, options);
|
|
302
|
-
const kvStoreData = kvstores.map((item) => ({
|
|
303
|
-
key: item.key,
|
|
304
|
-
value: item.value,
|
|
305
|
-
}));
|
|
306
|
-
return kvStoreData || [];
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
310
|
-
*
|
|
311
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
312
|
-
* @param address - Address from which to get the key value.
|
|
313
|
-
* @param key - Key to obtain the value.
|
|
314
|
-
* @param options - Optional configuration for subgraph requests.
|
|
315
|
-
* @returns Value of the key.
|
|
316
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
317
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
318
|
-
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
319
|
-
* @throws InvalidKeyError If the key is not found
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
322
|
-
* ```ts
|
|
323
|
-
* const value = await KVStoreUtils.get(
|
|
324
|
-
* ChainId.POLYGON_AMOY,
|
|
325
|
-
* '0x1234567890123456789012345678901234567890',
|
|
326
|
-
* 'role'
|
|
327
|
-
* );
|
|
328
|
-
* console.log('Value:', value);
|
|
329
|
-
* ```
|
|
330
|
-
*/
|
|
331
|
-
static async get(chainId, address, key, options) {
|
|
332
|
-
if (key === '')
|
|
333
|
-
throw error_1.ErrorKVStoreEmptyKey;
|
|
334
|
-
if (!ethers_1.ethers.isAddress(address))
|
|
335
|
-
throw error_1.ErrorInvalidAddress;
|
|
336
|
-
const networkData = constants_1.NETWORKS[chainId];
|
|
337
|
-
if (!networkData) {
|
|
338
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
339
|
-
}
|
|
340
|
-
const { kvstores } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, kvstore_1.GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY)(), { address: address.toLowerCase(), key }, options);
|
|
341
|
-
if (!kvstores || kvstores.length === 0) {
|
|
342
|
-
throw new error_1.InvalidKeyError(key, address);
|
|
343
|
-
}
|
|
344
|
-
return kvstores[0].value;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Gets the URL value of the given entity, and verifies its hash.
|
|
348
|
-
*
|
|
349
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
350
|
-
* @param address - Address from which to get the URL value.
|
|
351
|
-
* @param urlKey - Configurable URL key. `url` by default.
|
|
352
|
-
* @param options - Optional configuration for subgraph requests.
|
|
353
|
-
* @returns URL value for the given address if it exists, and the content is valid
|
|
354
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
355
|
-
* @throws ErrorInvalidHash If the hash verification fails
|
|
356
|
-
* @throws Error If fetching URL or hash fails
|
|
357
|
-
*
|
|
358
|
-
* @example
|
|
359
|
-
* ```ts
|
|
360
|
-
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(
|
|
361
|
-
* ChainId.POLYGON_AMOY,
|
|
362
|
-
* '0x1234567890123456789012345678901234567890'
|
|
363
|
-
* );
|
|
364
|
-
* console.log('Verified URL:', url);
|
|
365
|
-
* ```
|
|
366
|
-
*/
|
|
367
|
-
static async getFileUrlAndVerifyHash(chainId, address, urlKey = 'url', options) {
|
|
368
|
-
if (!ethers_1.ethers.isAddress(address))
|
|
369
|
-
throw error_1.ErrorInvalidAddress;
|
|
370
|
-
const hashKey = urlKey + '_hash';
|
|
371
|
-
let url = '', hash = '';
|
|
372
|
-
try {
|
|
373
|
-
url = await this.get(chainId, address, urlKey, options);
|
|
374
|
-
}
|
|
375
|
-
catch (e) {
|
|
376
|
-
if (e instanceof Error)
|
|
377
|
-
throw Error(`Failed to get URL: ${e.message}`);
|
|
378
|
-
}
|
|
379
|
-
// Return empty string
|
|
380
|
-
if (!url?.length) {
|
|
381
|
-
return '';
|
|
382
|
-
}
|
|
383
|
-
try {
|
|
384
|
-
hash = await this.get(chainId, address, hashKey);
|
|
385
|
-
}
|
|
386
|
-
catch (e) {
|
|
387
|
-
if (e instanceof Error)
|
|
388
|
-
throw Error(`Failed to get Hash: ${e.message}`);
|
|
389
|
-
}
|
|
390
|
-
const content = await fetch(url).then((res) => res.text());
|
|
391
|
-
const contentHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(content));
|
|
392
|
-
const formattedHash = hash?.replace(/^0x/, '');
|
|
393
|
-
const formattedContentHash = contentHash?.replace(/^0x/, '');
|
|
394
|
-
if (formattedHash !== formattedContentHash) {
|
|
395
|
-
throw error_1.ErrorInvalidHash;
|
|
396
|
-
}
|
|
397
|
-
return url;
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Gets the public key of the given entity, and verifies its hash.
|
|
401
|
-
*
|
|
402
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
403
|
-
* @param address - Address from which to get the public key.
|
|
404
|
-
* @param options - Optional configuration for subgraph requests.
|
|
405
|
-
* @returns Public key for the given address if it exists, and the content is valid
|
|
406
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
407
|
-
* @throws ErrorInvalidHash If the hash verification fails
|
|
408
|
-
* @throws Error If fetching the public key fails
|
|
409
|
-
*
|
|
410
|
-
* @example
|
|
411
|
-
* ```ts
|
|
412
|
-
* const publicKey = await KVStoreUtils.getPublicKey(
|
|
413
|
-
* ChainId.POLYGON_AMOY,
|
|
414
|
-
* '0x1234567890123456789012345678901234567890'
|
|
415
|
-
* );
|
|
416
|
-
* console.log('Public key:', publicKey);
|
|
417
|
-
* ```
|
|
418
|
-
*/
|
|
419
|
-
static async getPublicKey(chainId, address, options) {
|
|
420
|
-
const publicKeyUrl = await this.getFileUrlAndVerifyHash(chainId, address, constants_1.KVStoreKeys.publicKey, options);
|
|
421
|
-
if (publicKeyUrl === '') {
|
|
422
|
-
return '';
|
|
423
|
-
}
|
|
424
|
-
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
425
|
-
return publicKey;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
exports.KVStoreUtils = KVStoreUtils;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { ChainId } from '../enums';
|
|
2
|
+
import { IKVStore, SubgraphOptions } from '../interfaces';
|
|
3
|
+
/**
|
|
4
|
+
* Utility helpers for KVStore-related queries.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
11
|
+
* ChainId.POLYGON_AMOY,
|
|
12
|
+
* "0x1234567890123456789012345678901234567890"
|
|
13
|
+
* );
|
|
14
|
+
* console.log('KVStore data:', kvStoreData);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class KVStoreUtils {
|
|
18
|
+
/**
|
|
19
|
+
* This function returns the KVStore data for a given address.
|
|
20
|
+
*
|
|
21
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
22
|
+
* @param address - Address of the KVStore
|
|
23
|
+
* @param options - Optional configuration for subgraph requests.
|
|
24
|
+
* @returns KVStore data
|
|
25
|
+
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
26
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
31
|
+
* ChainId.POLYGON_AMOY,
|
|
32
|
+
* "0x1234567890123456789012345678901234567890"
|
|
33
|
+
* );
|
|
34
|
+
* console.log('KVStore data:', kvStoreData);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
static getKVStoreData(chainId: ChainId, address: string, options?: SubgraphOptions): Promise<IKVStore[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
40
|
+
*
|
|
41
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
42
|
+
* @param address - Address from which to get the key value.
|
|
43
|
+
* @param key - Key to obtain the value.
|
|
44
|
+
* @param options - Optional configuration for subgraph requests.
|
|
45
|
+
* @returns Value of the key.
|
|
46
|
+
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
47
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
48
|
+
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const value = await KVStoreUtils.get(
|
|
53
|
+
* ChainId.POLYGON_AMOY,
|
|
54
|
+
* '0x1234567890123456789012345678901234567890',
|
|
55
|
+
* 'role'
|
|
56
|
+
* );
|
|
57
|
+
* console.log('Value:', value);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
static get(chainId: ChainId, address: string, key: string, options?: SubgraphOptions): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Gets the URL value of the given entity, and verifies its hash.
|
|
63
|
+
*
|
|
64
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
65
|
+
* @param address - Address from which to get the URL value.
|
|
66
|
+
* @param urlKey - Configurable URL key. `url` by default.
|
|
67
|
+
* @param options - Optional configuration for subgraph requests.
|
|
68
|
+
* @returns URL value for the given address if it exists, and the content is valid
|
|
69
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
70
|
+
* @throws ErrorInvalidHash If the hash verification fails
|
|
71
|
+
* @throws Error If fetching URL or hash fails
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(
|
|
76
|
+
* ChainId.POLYGON_AMOY,
|
|
77
|
+
* '0x1234567890123456789012345678901234567890'
|
|
78
|
+
* );
|
|
79
|
+
* console.log('Verified URL:', url);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static getFileUrlAndVerifyHash(chainId: ChainId, address: string, urlKey?: string, options?: SubgraphOptions): Promise<string>;
|
|
83
|
+
/**
|
|
84
|
+
* Gets the public key of the given entity, and verifies its hash.
|
|
85
|
+
*
|
|
86
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
87
|
+
* @param address - Address from which to get the public key.
|
|
88
|
+
* @param options - Optional configuration for subgraph requests.
|
|
89
|
+
* @returns Public key for the given address if it exists, and the content is valid
|
|
90
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
91
|
+
* @throws ErrorInvalidHash If the hash verification fails
|
|
92
|
+
* @throws Error If fetching the public key fails
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const publicKey = await KVStoreUtils.getPublicKey(
|
|
97
|
+
* ChainId.POLYGON_AMOY,
|
|
98
|
+
* '0x1234567890123456789012345678901234567890'
|
|
99
|
+
* );
|
|
100
|
+
* console.log('Public key:', publicKey);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
static getPublicKey(chainId: ChainId, address: string, options?: SubgraphOptions): Promise<string>;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=kvstore_utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kvstore_utils.d.ts","sourceRoot":"","sources":["../../src/kvstore/kvstore_utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAYnC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAE1D;;;;;;;;;;;;;GAaG;AACH,qBAAa,YAAY;IACvB;;;;;;;;;;;;;;;;;;OAkBG;WACiB,cAAc,CAChC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,QAAQ,EAAE,CAAC;IA0BtB;;;;;;;;;;;;;;;;;;;;;OAqBG;WACiB,GAAG,CACrB,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,MAAM,CAAC;IAwBlB;;;;;;;;;;;;;;;;;;;;OAoBG;WACiB,uBAAuB,CACzC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,MAAM,SAAQ,EACd,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,MAAM,CAAC;IA8ClB;;;;;;;;;;;;;;;;;;;OAmBG;WACiB,YAAY,CAC9B,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,MAAM,CAAC;CAgBnB"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KVStoreUtils = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
const error_1 = require("../error");
|
|
7
|
+
const kvstore_1 = require("../graphql/queries/kvstore");
|
|
8
|
+
const utils_1 = require("../utils");
|
|
9
|
+
/**
|
|
10
|
+
* Utility helpers for KVStore-related queries.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
15
|
+
*
|
|
16
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
17
|
+
* ChainId.POLYGON_AMOY,
|
|
18
|
+
* "0x1234567890123456789012345678901234567890"
|
|
19
|
+
* );
|
|
20
|
+
* console.log('KVStore data:', kvStoreData);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class KVStoreUtils {
|
|
24
|
+
/**
|
|
25
|
+
* This function returns the KVStore data for a given address.
|
|
26
|
+
*
|
|
27
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
28
|
+
* @param address - Address of the KVStore
|
|
29
|
+
* @param options - Optional configuration for subgraph requests.
|
|
30
|
+
* @returns KVStore data
|
|
31
|
+
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
32
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
37
|
+
* ChainId.POLYGON_AMOY,
|
|
38
|
+
* "0x1234567890123456789012345678901234567890"
|
|
39
|
+
* );
|
|
40
|
+
* console.log('KVStore data:', kvStoreData);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
static async getKVStoreData(chainId, address, options) {
|
|
44
|
+
const networkData = constants_1.NETWORKS[chainId];
|
|
45
|
+
if (!networkData) {
|
|
46
|
+
throw error_1.ErrorUnsupportedChainID;
|
|
47
|
+
}
|
|
48
|
+
if (address && !ethers_1.ethers.isAddress(address)) {
|
|
49
|
+
throw error_1.ErrorInvalidAddress;
|
|
50
|
+
}
|
|
51
|
+
const { kvstores } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, kvstore_1.GET_KVSTORE_BY_ADDRESS_QUERY)(), { address: address.toLowerCase() }, options);
|
|
52
|
+
const kvStoreData = kvstores.map((item) => ({
|
|
53
|
+
key: item.key,
|
|
54
|
+
value: item.value,
|
|
55
|
+
}));
|
|
56
|
+
return kvStoreData || [];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
60
|
+
*
|
|
61
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
62
|
+
* @param address - Address from which to get the key value.
|
|
63
|
+
* @param key - Key to obtain the value.
|
|
64
|
+
* @param options - Optional configuration for subgraph requests.
|
|
65
|
+
* @returns Value of the key.
|
|
66
|
+
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
67
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
68
|
+
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const value = await KVStoreUtils.get(
|
|
73
|
+
* ChainId.POLYGON_AMOY,
|
|
74
|
+
* '0x1234567890123456789012345678901234567890',
|
|
75
|
+
* 'role'
|
|
76
|
+
* );
|
|
77
|
+
* console.log('Value:', value);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
static async get(chainId, address, key, options) {
|
|
81
|
+
if (key === '')
|
|
82
|
+
throw error_1.ErrorKVStoreEmptyKey;
|
|
83
|
+
if (!ethers_1.ethers.isAddress(address))
|
|
84
|
+
throw error_1.ErrorInvalidAddress;
|
|
85
|
+
const networkData = constants_1.NETWORKS[chainId];
|
|
86
|
+
if (!networkData) {
|
|
87
|
+
throw error_1.ErrorUnsupportedChainID;
|
|
88
|
+
}
|
|
89
|
+
const { kvstores } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, kvstore_1.GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY)(), { address: address.toLowerCase(), key }, options);
|
|
90
|
+
if (!kvstores || kvstores.length === 0) {
|
|
91
|
+
return '';
|
|
92
|
+
}
|
|
93
|
+
return kvstores[0].value;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Gets the URL value of the given entity, and verifies its hash.
|
|
97
|
+
*
|
|
98
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
99
|
+
* @param address - Address from which to get the URL value.
|
|
100
|
+
* @param urlKey - Configurable URL key. `url` by default.
|
|
101
|
+
* @param options - Optional configuration for subgraph requests.
|
|
102
|
+
* @returns URL value for the given address if it exists, and the content is valid
|
|
103
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
104
|
+
* @throws ErrorInvalidHash If the hash verification fails
|
|
105
|
+
* @throws Error If fetching URL or hash fails
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(
|
|
110
|
+
* ChainId.POLYGON_AMOY,
|
|
111
|
+
* '0x1234567890123456789012345678901234567890'
|
|
112
|
+
* );
|
|
113
|
+
* console.log('Verified URL:', url);
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
static async getFileUrlAndVerifyHash(chainId, address, urlKey = 'url', options) {
|
|
117
|
+
if (!ethers_1.ethers.isAddress(address))
|
|
118
|
+
throw error_1.ErrorInvalidAddress;
|
|
119
|
+
const hashKey = urlKey + '_hash';
|
|
120
|
+
let url;
|
|
121
|
+
try {
|
|
122
|
+
url = await this.get(chainId, address, urlKey, options);
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
if (e instanceof Error) {
|
|
126
|
+
throw Error(`Failed to get URL: ${e.message}`);
|
|
127
|
+
}
|
|
128
|
+
throw e;
|
|
129
|
+
}
|
|
130
|
+
if (!url) {
|
|
131
|
+
throw new Error('No URL found for the given address and key');
|
|
132
|
+
}
|
|
133
|
+
let hash;
|
|
134
|
+
try {
|
|
135
|
+
hash = await this.get(chainId, address, hashKey);
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
if (e instanceof Error) {
|
|
139
|
+
throw Error(`Failed to get Hash: ${e.message}`);
|
|
140
|
+
}
|
|
141
|
+
throw e;
|
|
142
|
+
}
|
|
143
|
+
if (!hash) {
|
|
144
|
+
throw new Error('No hash found for the given address and url');
|
|
145
|
+
}
|
|
146
|
+
const content = await fetch(url).then((res) => res.text());
|
|
147
|
+
const contentHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(content));
|
|
148
|
+
const formattedHash = hash?.replace(/^0x/, '');
|
|
149
|
+
const formattedContentHash = contentHash?.replace(/^0x/, '');
|
|
150
|
+
if (formattedHash !== formattedContentHash) {
|
|
151
|
+
throw error_1.ErrorInvalidHash;
|
|
152
|
+
}
|
|
153
|
+
return url;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Gets the public key of the given entity, and verifies its hash.
|
|
157
|
+
*
|
|
158
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
159
|
+
* @param address - Address from which to get the public key.
|
|
160
|
+
* @param options - Optional configuration for subgraph requests.
|
|
161
|
+
* @returns Public key for the given address if it exists, and the content is valid
|
|
162
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
163
|
+
* @throws ErrorInvalidHash If the hash verification fails
|
|
164
|
+
* @throws Error If fetching the public key fails
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const publicKey = await KVStoreUtils.getPublicKey(
|
|
169
|
+
* ChainId.POLYGON_AMOY,
|
|
170
|
+
* '0x1234567890123456789012345678901234567890'
|
|
171
|
+
* );
|
|
172
|
+
* console.log('Public key:', publicKey);
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
static async getPublicKey(chainId, address, options) {
|
|
176
|
+
const publicKeyUrl = await this.getFileUrlAndVerifyHash(chainId, address, constants_1.KVStoreKeys.publicKey, options);
|
|
177
|
+
if (publicKeyUrl === '') {
|
|
178
|
+
return '';
|
|
179
|
+
}
|
|
180
|
+
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
181
|
+
return publicKey;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
exports.KVStoreUtils = KVStoreUtils;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/operator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OperatorUtils = void 0;
|
|
4
|
+
var operator_utils_1 = require("./operator_utils");
|
|
5
|
+
Object.defineProperty(exports, "OperatorUtils", { enumerable: true, get: function () { return operator_utils_1.OperatorUtils; } });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IOperator, IOperatorsFilter, IReward, SubgraphOptions } from '
|
|
2
|
-
import { ChainId } from '
|
|
1
|
+
import { IOperator, IOperatorsFilter, IReward, SubgraphOptions } from '../interfaces';
|
|
2
|
+
import { ChainId } from '../enums';
|
|
3
3
|
/**
|
|
4
4
|
* Utility helpers for operator-related queries.
|
|
5
5
|
*
|
|
@@ -102,4 +102,4 @@ export declare class OperatorUtils {
|
|
|
102
102
|
*/
|
|
103
103
|
static getRewards(chainId: ChainId, slasherAddress: string, options?: SubgraphOptions): Promise<IReward[]>;
|
|
104
104
|
}
|
|
105
|
-
//# sourceMappingURL=
|
|
105
|
+
//# sourceMappingURL=operator_utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operator_utils.d.ts","sourceRoot":"","sources":["../../src/operator/operator_utils.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,OAAO,EACP,eAAe,EAChB,MAAM,eAAe,CAAC;AAmBvB,OAAO,EAAE,OAAO,EAAkB,MAAM,UAAU,CAAC;AAGnD;;;;;;;;;;;;;GAaG;AACH,qBAAa,aAAa;IACxB;;;;;;;;;;;;;;;;;;;;OAoBG;WACiB,WAAW,CAC7B,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;IAwB5B;;;;;;;;;;;;;;;;;;OAkBG;WACiB,YAAY,CAC9B,MAAM,EAAE,gBAAgB,EACxB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,SAAS,EAAE,CAAC;IA+CvB;;;;;;;;;;;;;;;;;;;;OAoBG;WACiB,6BAA6B,CAC/C,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,SAAS,EAAE,CAAC;IAyBvB;;;;;;;;;;;;;;;;;;;;OAoBG;WACiB,UAAU,CAC5B,OAAO,EAAE,OAAO,EAChB,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,OAAO,EAAE,CAAC;CA8BtB"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OperatorUtils = void 0;
|
|
4
|
-
const reward_1 = require("
|
|
5
|
-
const operator_1 = require("
|
|
4
|
+
const reward_1 = require("../graphql/queries/reward");
|
|
5
|
+
const operator_1 = require("../graphql/queries/operator");
|
|
6
6
|
const ethers_1 = require("ethers");
|
|
7
|
-
const error_1 = require("
|
|
8
|
-
const utils_1 = require("
|
|
9
|
-
const enums_1 = require("
|
|
10
|
-
const constants_1 = require("
|
|
7
|
+
const error_1 = require("../error");
|
|
8
|
+
const utils_1 = require("../utils");
|
|
9
|
+
const enums_1 = require("../enums");
|
|
10
|
+
const constants_1 = require("../constants");
|
|
11
11
|
/**
|
|
12
12
|
* Utility helpers for operator-related queries.
|
|
13
13
|
*
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/staking/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StakingUtils = exports.StakingClient = void 0;
|
|
4
|
+
var staking_client_1 = require("./staking_client");
|
|
5
|
+
Object.defineProperty(exports, "StakingClient", { enumerable: true, get: function () { return staking_client_1.StakingClient; } });
|
|
6
|
+
var staking_utils_1 = require("./staking_utils");
|
|
7
|
+
Object.defineProperty(exports, "StakingUtils", { enumerable: true, get: function () { return staking_utils_1.StakingUtils; } });
|