@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
package/src/interfaces.ts
CHANGED
|
@@ -87,9 +87,6 @@ export interface IEscrowConfig {
|
|
|
87
87
|
recordingOracle: string;
|
|
88
88
|
reputationOracle: string;
|
|
89
89
|
exchangeOracle: string;
|
|
90
|
-
recordingOracleFee: bigint;
|
|
91
|
-
reputationOracleFee: bigint;
|
|
92
|
-
exchangeOracleFee: bigint;
|
|
93
90
|
manifest: string;
|
|
94
91
|
manifestHash: string;
|
|
95
92
|
}
|
|
@@ -179,12 +176,12 @@ export interface IStatusEventFilter extends IPagination {
|
|
|
179
176
|
from?: Date;
|
|
180
177
|
to?: Date;
|
|
181
178
|
launcher?: string;
|
|
179
|
+
escrowAddress?: string;
|
|
182
180
|
}
|
|
183
181
|
|
|
184
182
|
export interface IWorker {
|
|
185
183
|
id: string;
|
|
186
184
|
address: string;
|
|
187
|
-
totalHMTAmountReceived: bigint;
|
|
188
185
|
payoutCount: number;
|
|
189
186
|
}
|
|
190
187
|
|
|
@@ -254,9 +251,7 @@ export interface IWorkerStatistics {
|
|
|
254
251
|
|
|
255
252
|
export interface IDailyPayment {
|
|
256
253
|
timestamp: number;
|
|
257
|
-
totalAmountPaid: bigint;
|
|
258
254
|
totalCount: number;
|
|
259
|
-
averageAmountPerWorker: bigint;
|
|
260
255
|
}
|
|
261
256
|
|
|
262
257
|
export interface IPaymentStatistics {
|
|
@@ -285,8 +280,10 @@ export interface IDailyHMT {
|
|
|
285
280
|
export interface IStatusEvent {
|
|
286
281
|
timestamp: number;
|
|
287
282
|
escrowAddress: string;
|
|
288
|
-
status: EscrowStatus;
|
|
283
|
+
status: keyof typeof EscrowStatus;
|
|
289
284
|
chainId: ChainId;
|
|
285
|
+
block: bigint;
|
|
286
|
+
txHash: string;
|
|
290
287
|
}
|
|
291
288
|
|
|
292
289
|
export interface ICancellationRefund {
|
|
@@ -3,28 +3,20 @@ import {
|
|
|
3
3
|
KVStore__factory,
|
|
4
4
|
} from '@human-protocol/core/typechain-types';
|
|
5
5
|
import { ContractRunner, ethers } from 'ethers';
|
|
6
|
-
import { BaseEthersClient } from '
|
|
7
|
-
import {
|
|
8
|
-
import { requiresSigner } from '
|
|
9
|
-
import { ChainId } from '
|
|
6
|
+
import { BaseEthersClient } from '../base';
|
|
7
|
+
import { NETWORKS } from '../constants';
|
|
8
|
+
import { requiresSigner } from '../decorators';
|
|
9
|
+
import { ChainId } from '../enums';
|
|
10
10
|
import {
|
|
11
11
|
ErrorInvalidAddress,
|
|
12
|
-
ErrorInvalidHash,
|
|
13
12
|
ErrorInvalidUrl,
|
|
14
13
|
ErrorKVStoreArrayLength,
|
|
15
14
|
ErrorKVStoreEmptyKey,
|
|
16
15
|
ErrorProviderDoesNotExist,
|
|
17
16
|
ErrorUnsupportedChainID,
|
|
18
|
-
|
|
19
|
-
} from '
|
|
20
|
-
import {
|
|
21
|
-
import { getSubgraphUrl, customGqlFetch, isValidUrl } from './utils';
|
|
22
|
-
import {
|
|
23
|
-
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY,
|
|
24
|
-
GET_KVSTORE_BY_ADDRESS_QUERY,
|
|
25
|
-
} from './graphql/queries/kvstore';
|
|
26
|
-
import { KVStoreData } from './graphql';
|
|
27
|
-
import { IKVStore, SubgraphOptions } from './interfaces';
|
|
17
|
+
} from '../error';
|
|
18
|
+
import { NetworkData, TransactionOverrides } from '../types';
|
|
19
|
+
import { isValidUrl } from '../utils';
|
|
28
20
|
/**
|
|
29
21
|
* Client for interacting with the KVStore contract.
|
|
30
22
|
*
|
|
@@ -159,13 +151,17 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
159
151
|
txOptions: TransactionOverrides = {}
|
|
160
152
|
): Promise<void> {
|
|
161
153
|
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
154
|
+
|
|
162
155
|
try {
|
|
163
156
|
await this.sendTxAndWait(
|
|
164
157
|
(overrides) => this.contract.set(key, value, overrides),
|
|
165
158
|
txOptions
|
|
166
159
|
);
|
|
167
160
|
} catch (e) {
|
|
168
|
-
if (e instanceof Error)
|
|
161
|
+
if (e instanceof Error) {
|
|
162
|
+
throw Error(`Failed to set value: ${e.message}`);
|
|
163
|
+
}
|
|
164
|
+
throw e;
|
|
169
165
|
}
|
|
170
166
|
}
|
|
171
167
|
|
|
@@ -202,8 +198,10 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
202
198
|
txOptions
|
|
203
199
|
);
|
|
204
200
|
} catch (e) {
|
|
205
|
-
if (e instanceof Error)
|
|
201
|
+
if (e instanceof Error) {
|
|
206
202
|
throw Error(`Failed to set bulk values: ${e.message}`);
|
|
203
|
+
}
|
|
204
|
+
throw e;
|
|
207
205
|
}
|
|
208
206
|
}
|
|
209
207
|
|
|
@@ -249,8 +247,10 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
249
247
|
txOptions
|
|
250
248
|
);
|
|
251
249
|
} catch (e) {
|
|
252
|
-
if (e instanceof Error)
|
|
250
|
+
if (e instanceof Error) {
|
|
253
251
|
throw Error(`Failed to set URL and hash: ${e.message}`);
|
|
252
|
+
}
|
|
253
|
+
throw e;
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
256
|
/**
|
|
@@ -270,236 +270,22 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
270
270
|
* ```
|
|
271
271
|
*/
|
|
272
272
|
public async get(address: string, key: string): Promise<string> {
|
|
273
|
-
if (key === '')
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
try {
|
|
277
|
-
const result = await this.contract?.get(address, key);
|
|
278
|
-
return result;
|
|
279
|
-
} catch (e) {
|
|
280
|
-
if (e instanceof Error) throw Error(`Failed to get value: ${e.message}`);
|
|
281
|
-
return e;
|
|
273
|
+
if (key === '') {
|
|
274
|
+
throw ErrorKVStoreEmptyKey;
|
|
282
275
|
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Utility helpers for KVStore-related queries.
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* ```ts
|
|
291
|
-
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
292
|
-
*
|
|
293
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
294
|
-
* ChainId.POLYGON_AMOY,
|
|
295
|
-
* "0x1234567890123456789012345678901234567890"
|
|
296
|
-
* );
|
|
297
|
-
* console.log('KVStore data:', kvStoreData);
|
|
298
|
-
* ```
|
|
299
|
-
*/
|
|
300
|
-
export class KVStoreUtils {
|
|
301
|
-
/**
|
|
302
|
-
* This function returns the KVStore data for a given address.
|
|
303
|
-
*
|
|
304
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
305
|
-
* @param address - Address of the KVStore
|
|
306
|
-
* @param options - Optional configuration for subgraph requests.
|
|
307
|
-
* @returns KVStore data
|
|
308
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
309
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* ```ts
|
|
313
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
314
|
-
* ChainId.POLYGON_AMOY,
|
|
315
|
-
* "0x1234567890123456789012345678901234567890"
|
|
316
|
-
* );
|
|
317
|
-
* console.log('KVStore data:', kvStoreData);
|
|
318
|
-
* ```
|
|
319
|
-
*/
|
|
320
|
-
public static async getKVStoreData(
|
|
321
|
-
chainId: ChainId,
|
|
322
|
-
address: string,
|
|
323
|
-
options?: SubgraphOptions
|
|
324
|
-
): Promise<IKVStore[]> {
|
|
325
|
-
const networkData = NETWORKS[chainId];
|
|
326
276
|
|
|
327
|
-
if (!
|
|
328
|
-
throw ErrorUnsupportedChainID;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
if (address && !ethers.isAddress(address)) {
|
|
277
|
+
if (!ethers.isAddress(address)) {
|
|
332
278
|
throw ErrorInvalidAddress;
|
|
333
279
|
}
|
|
334
280
|
|
|
335
|
-
const { kvstores } = await customGqlFetch<{ kvstores: KVStoreData[] }>(
|
|
336
|
-
getSubgraphUrl(networkData),
|
|
337
|
-
GET_KVSTORE_BY_ADDRESS_QUERY(),
|
|
338
|
-
{ address: address.toLowerCase() },
|
|
339
|
-
options
|
|
340
|
-
);
|
|
341
|
-
|
|
342
|
-
const kvStoreData = kvstores.map((item) => ({
|
|
343
|
-
key: item.key,
|
|
344
|
-
value: item.value,
|
|
345
|
-
}));
|
|
346
|
-
|
|
347
|
-
return kvStoreData || [];
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
352
|
-
*
|
|
353
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
354
|
-
* @param address - Address from which to get the key value.
|
|
355
|
-
* @param key - Key to obtain the value.
|
|
356
|
-
* @param options - Optional configuration for subgraph requests.
|
|
357
|
-
* @returns Value of the key.
|
|
358
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
359
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
360
|
-
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
361
|
-
* @throws InvalidKeyError If the key is not found
|
|
362
|
-
*
|
|
363
|
-
* @example
|
|
364
|
-
* ```ts
|
|
365
|
-
* const value = await KVStoreUtils.get(
|
|
366
|
-
* ChainId.POLYGON_AMOY,
|
|
367
|
-
* '0x1234567890123456789012345678901234567890',
|
|
368
|
-
* 'role'
|
|
369
|
-
* );
|
|
370
|
-
* console.log('Value:', value);
|
|
371
|
-
* ```
|
|
372
|
-
*/
|
|
373
|
-
public static async get(
|
|
374
|
-
chainId: ChainId,
|
|
375
|
-
address: string,
|
|
376
|
-
key: string,
|
|
377
|
-
options?: SubgraphOptions
|
|
378
|
-
): Promise<string> {
|
|
379
|
-
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
380
|
-
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
381
|
-
|
|
382
|
-
const networkData = NETWORKS[chainId];
|
|
383
|
-
|
|
384
|
-
if (!networkData) {
|
|
385
|
-
throw ErrorUnsupportedChainID;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
const { kvstores } = await customGqlFetch<{ kvstores: KVStoreData[] }>(
|
|
389
|
-
getSubgraphUrl(networkData),
|
|
390
|
-
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY(),
|
|
391
|
-
{ address: address.toLowerCase(), key },
|
|
392
|
-
options
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
if (!kvstores || kvstores.length === 0) {
|
|
396
|
-
throw new InvalidKeyError(key, address);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
return kvstores[0].value;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Gets the URL value of the given entity, and verifies its hash.
|
|
404
|
-
*
|
|
405
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
406
|
-
* @param address - Address from which to get the URL value.
|
|
407
|
-
* @param urlKey - Configurable URL key. `url` by default.
|
|
408
|
-
* @param options - Optional configuration for subgraph requests.
|
|
409
|
-
* @returns URL value for the given address if it exists, and the content is valid
|
|
410
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
411
|
-
* @throws ErrorInvalidHash If the hash verification fails
|
|
412
|
-
* @throws Error If fetching URL or hash fails
|
|
413
|
-
*
|
|
414
|
-
* @example
|
|
415
|
-
* ```ts
|
|
416
|
-
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(
|
|
417
|
-
* ChainId.POLYGON_AMOY,
|
|
418
|
-
* '0x1234567890123456789012345678901234567890'
|
|
419
|
-
* );
|
|
420
|
-
* console.log('Verified URL:', url);
|
|
421
|
-
* ```
|
|
422
|
-
*/
|
|
423
|
-
public static async getFileUrlAndVerifyHash(
|
|
424
|
-
chainId: ChainId,
|
|
425
|
-
address: string,
|
|
426
|
-
urlKey = 'url',
|
|
427
|
-
options?: SubgraphOptions
|
|
428
|
-
): Promise<string> {
|
|
429
|
-
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
430
|
-
const hashKey = urlKey + '_hash';
|
|
431
|
-
|
|
432
|
-
let url = '',
|
|
433
|
-
hash = '';
|
|
434
|
-
|
|
435
|
-
try {
|
|
436
|
-
url = await this.get(chainId, address, urlKey, options);
|
|
437
|
-
} catch (e) {
|
|
438
|
-
if (e instanceof Error) throw Error(`Failed to get URL: ${e.message}`);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
// Return empty string
|
|
442
|
-
if (!url?.length) {
|
|
443
|
-
return '';
|
|
444
|
-
}
|
|
445
|
-
|
|
446
281
|
try {
|
|
447
|
-
|
|
282
|
+
const result = await this.contract?.get(address, key);
|
|
283
|
+
return result;
|
|
448
284
|
} catch (e) {
|
|
449
|
-
if (e instanceof Error)
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(content));
|
|
454
|
-
|
|
455
|
-
const formattedHash = hash?.replace(/^0x/, '');
|
|
456
|
-
const formattedContentHash = contentHash?.replace(/^0x/, '');
|
|
457
|
-
|
|
458
|
-
if (formattedHash !== formattedContentHash) {
|
|
459
|
-
throw ErrorInvalidHash;
|
|
285
|
+
if (e instanceof Error) {
|
|
286
|
+
throw Error(`Failed to get value: ${e.message}`);
|
|
287
|
+
}
|
|
288
|
+
throw e;
|
|
460
289
|
}
|
|
461
|
-
|
|
462
|
-
return url;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Gets the public key of the given entity, and verifies its hash.
|
|
467
|
-
*
|
|
468
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
469
|
-
* @param address - Address from which to get the public key.
|
|
470
|
-
* @param options - Optional configuration for subgraph requests.
|
|
471
|
-
* @returns Public key for the given address if it exists, and the content is valid
|
|
472
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
473
|
-
* @throws ErrorInvalidHash If the hash verification fails
|
|
474
|
-
* @throws Error If fetching the public key fails
|
|
475
|
-
*
|
|
476
|
-
* @example
|
|
477
|
-
* ```ts
|
|
478
|
-
* const publicKey = await KVStoreUtils.getPublicKey(
|
|
479
|
-
* ChainId.POLYGON_AMOY,
|
|
480
|
-
* '0x1234567890123456789012345678901234567890'
|
|
481
|
-
* );
|
|
482
|
-
* console.log('Public key:', publicKey);
|
|
483
|
-
* ```
|
|
484
|
-
*/
|
|
485
|
-
public static async getPublicKey(
|
|
486
|
-
chainId: ChainId,
|
|
487
|
-
address: string,
|
|
488
|
-
options?: SubgraphOptions
|
|
489
|
-
): Promise<string> {
|
|
490
|
-
const publicKeyUrl = await this.getFileUrlAndVerifyHash(
|
|
491
|
-
chainId,
|
|
492
|
-
address,
|
|
493
|
-
KVStoreKeys.publicKey,
|
|
494
|
-
options
|
|
495
|
-
);
|
|
496
|
-
|
|
497
|
-
if (publicKeyUrl === '') {
|
|
498
|
-
return '';
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
502
|
-
|
|
503
|
-
return publicKey;
|
|
504
290
|
}
|
|
505
291
|
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { KVStoreKeys, NETWORKS } from '../constants';
|
|
3
|
+
import { ChainId } from '../enums';
|
|
4
|
+
import {
|
|
5
|
+
ErrorInvalidAddress,
|
|
6
|
+
ErrorInvalidHash,
|
|
7
|
+
ErrorKVStoreEmptyKey,
|
|
8
|
+
ErrorUnsupportedChainID,
|
|
9
|
+
} from '../error';
|
|
10
|
+
import { KVStoreData } from '../graphql';
|
|
11
|
+
import {
|
|
12
|
+
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY,
|
|
13
|
+
GET_KVSTORE_BY_ADDRESS_QUERY,
|
|
14
|
+
} from '../graphql/queries/kvstore';
|
|
15
|
+
import { IKVStore, SubgraphOptions } from '../interfaces';
|
|
16
|
+
import { customGqlFetch, getSubgraphUrl } from '../utils';
|
|
17
|
+
/**
|
|
18
|
+
* Utility helpers for KVStore-related queries.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
23
|
+
*
|
|
24
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
25
|
+
* ChainId.POLYGON_AMOY,
|
|
26
|
+
* "0x1234567890123456789012345678901234567890"
|
|
27
|
+
* );
|
|
28
|
+
* console.log('KVStore data:', kvStoreData);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class KVStoreUtils {
|
|
32
|
+
/**
|
|
33
|
+
* This function returns the KVStore data for a given address.
|
|
34
|
+
*
|
|
35
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
36
|
+
* @param address - Address of the KVStore
|
|
37
|
+
* @param options - Optional configuration for subgraph requests.
|
|
38
|
+
* @returns KVStore data
|
|
39
|
+
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
40
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
45
|
+
* ChainId.POLYGON_AMOY,
|
|
46
|
+
* "0x1234567890123456789012345678901234567890"
|
|
47
|
+
* );
|
|
48
|
+
* console.log('KVStore data:', kvStoreData);
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
public static async getKVStoreData(
|
|
52
|
+
chainId: ChainId,
|
|
53
|
+
address: string,
|
|
54
|
+
options?: SubgraphOptions
|
|
55
|
+
): Promise<IKVStore[]> {
|
|
56
|
+
const networkData = NETWORKS[chainId];
|
|
57
|
+
|
|
58
|
+
if (!networkData) {
|
|
59
|
+
throw ErrorUnsupportedChainID;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (address && !ethers.isAddress(address)) {
|
|
63
|
+
throw ErrorInvalidAddress;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const { kvstores } = await customGqlFetch<{ kvstores: KVStoreData[] }>(
|
|
67
|
+
getSubgraphUrl(networkData),
|
|
68
|
+
GET_KVSTORE_BY_ADDRESS_QUERY(),
|
|
69
|
+
{ address: address.toLowerCase() },
|
|
70
|
+
options
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const kvStoreData = kvstores.map((item) => ({
|
|
74
|
+
key: item.key,
|
|
75
|
+
value: item.value,
|
|
76
|
+
}));
|
|
77
|
+
|
|
78
|
+
return kvStoreData || [];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
83
|
+
*
|
|
84
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
85
|
+
* @param address - Address from which to get the key value.
|
|
86
|
+
* @param key - Key to obtain the value.
|
|
87
|
+
* @param options - Optional configuration for subgraph requests.
|
|
88
|
+
* @returns Value of the key.
|
|
89
|
+
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
90
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
91
|
+
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* const value = await KVStoreUtils.get(
|
|
96
|
+
* ChainId.POLYGON_AMOY,
|
|
97
|
+
* '0x1234567890123456789012345678901234567890',
|
|
98
|
+
* 'role'
|
|
99
|
+
* );
|
|
100
|
+
* console.log('Value:', value);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
public static async get(
|
|
104
|
+
chainId: ChainId,
|
|
105
|
+
address: string,
|
|
106
|
+
key: string,
|
|
107
|
+
options?: SubgraphOptions
|
|
108
|
+
): Promise<string> {
|
|
109
|
+
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
110
|
+
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
111
|
+
|
|
112
|
+
const networkData = NETWORKS[chainId];
|
|
113
|
+
|
|
114
|
+
if (!networkData) {
|
|
115
|
+
throw ErrorUnsupportedChainID;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const { kvstores } = await customGqlFetch<{ kvstores: KVStoreData[] }>(
|
|
119
|
+
getSubgraphUrl(networkData),
|
|
120
|
+
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY(),
|
|
121
|
+
{ address: address.toLowerCase(), key },
|
|
122
|
+
options
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if (!kvstores || kvstores.length === 0) {
|
|
126
|
+
return '';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return kvstores[0].value;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Gets the URL value of the given entity, and verifies its hash.
|
|
134
|
+
*
|
|
135
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
136
|
+
* @param address - Address from which to get the URL value.
|
|
137
|
+
* @param urlKey - Configurable URL key. `url` by default.
|
|
138
|
+
* @param options - Optional configuration for subgraph requests.
|
|
139
|
+
* @returns URL value for the given address if it exists, and the content is valid
|
|
140
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
141
|
+
* @throws ErrorInvalidHash If the hash verification fails
|
|
142
|
+
* @throws Error If fetching URL or hash fails
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(
|
|
147
|
+
* ChainId.POLYGON_AMOY,
|
|
148
|
+
* '0x1234567890123456789012345678901234567890'
|
|
149
|
+
* );
|
|
150
|
+
* console.log('Verified URL:', url);
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
public static async getFileUrlAndVerifyHash(
|
|
154
|
+
chainId: ChainId,
|
|
155
|
+
address: string,
|
|
156
|
+
urlKey = 'url',
|
|
157
|
+
options?: SubgraphOptions
|
|
158
|
+
): Promise<string> {
|
|
159
|
+
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
160
|
+
const hashKey = urlKey + '_hash';
|
|
161
|
+
|
|
162
|
+
let url: string;
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
url = await this.get(chainId, address, urlKey, options);
|
|
166
|
+
} catch (e) {
|
|
167
|
+
if (e instanceof Error) {
|
|
168
|
+
throw Error(`Failed to get URL: ${e.message}`);
|
|
169
|
+
}
|
|
170
|
+
throw e;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!url) {
|
|
174
|
+
throw new Error('No URL found for the given address and key');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let hash: string;
|
|
178
|
+
try {
|
|
179
|
+
hash = await this.get(chainId, address, hashKey);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
if (e instanceof Error) {
|
|
182
|
+
throw Error(`Failed to get Hash: ${e.message}`);
|
|
183
|
+
}
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!hash) {
|
|
188
|
+
throw new Error('No hash found for the given address and url');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const content = await fetch(url).then((res) => res.text());
|
|
192
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(content));
|
|
193
|
+
|
|
194
|
+
const formattedHash = hash?.replace(/^0x/, '');
|
|
195
|
+
const formattedContentHash = contentHash?.replace(/^0x/, '');
|
|
196
|
+
|
|
197
|
+
if (formattedHash !== formattedContentHash) {
|
|
198
|
+
throw ErrorInvalidHash;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return url;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Gets the public key of the given entity, and verifies its hash.
|
|
206
|
+
*
|
|
207
|
+
* @param chainId - Network in which the KVStore is deployed
|
|
208
|
+
* @param address - Address from which to get the public key.
|
|
209
|
+
* @param options - Optional configuration for subgraph requests.
|
|
210
|
+
* @returns Public key for the given address if it exists, and the content is valid
|
|
211
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
212
|
+
* @throws ErrorInvalidHash If the hash verification fails
|
|
213
|
+
* @throws Error If fetching the public key fails
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* const publicKey = await KVStoreUtils.getPublicKey(
|
|
218
|
+
* ChainId.POLYGON_AMOY,
|
|
219
|
+
* '0x1234567890123456789012345678901234567890'
|
|
220
|
+
* );
|
|
221
|
+
* console.log('Public key:', publicKey);
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
public static async getPublicKey(
|
|
225
|
+
chainId: ChainId,
|
|
226
|
+
address: string,
|
|
227
|
+
options?: SubgraphOptions
|
|
228
|
+
): Promise<string> {
|
|
229
|
+
const publicKeyUrl = await this.getFileUrlAndVerifyHash(
|
|
230
|
+
chainId,
|
|
231
|
+
address,
|
|
232
|
+
KVStoreKeys.publicKey,
|
|
233
|
+
options
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
if (publicKeyUrl === '') {
|
|
237
|
+
return '';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
241
|
+
|
|
242
|
+
return publicKey;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { OperatorUtils } from './operator_utils';
|
|
@@ -4,27 +4,27 @@ import {
|
|
|
4
4
|
IOperatorsFilter,
|
|
5
5
|
IReward,
|
|
6
6
|
SubgraphOptions,
|
|
7
|
-
} from '
|
|
8
|
-
import { GET_REWARD_ADDED_EVENTS_QUERY } from '
|
|
7
|
+
} from '../interfaces';
|
|
8
|
+
import { GET_REWARD_ADDED_EVENTS_QUERY } from '../graphql/queries/reward';
|
|
9
9
|
import {
|
|
10
10
|
IOperatorSubgraph,
|
|
11
11
|
IReputationNetworkSubgraph,
|
|
12
12
|
RewardAddedEventData,
|
|
13
|
-
} from '
|
|
13
|
+
} from '../graphql';
|
|
14
14
|
import {
|
|
15
15
|
GET_LEADER_QUERY,
|
|
16
16
|
GET_LEADERS_QUERY,
|
|
17
17
|
GET_REPUTATION_NETWORK_QUERY,
|
|
18
|
-
} from '
|
|
18
|
+
} from '../graphql/queries/operator';
|
|
19
19
|
import { ethers } from 'ethers';
|
|
20
20
|
import {
|
|
21
21
|
ErrorInvalidSlasherAddressProvided,
|
|
22
22
|
ErrorInvalidStakerAddressProvided,
|
|
23
23
|
ErrorUnsupportedChainID,
|
|
24
|
-
} from '
|
|
25
|
-
import { getSubgraphUrl, customGqlFetch } from '
|
|
26
|
-
import { ChainId, OrderDirection } from '
|
|
27
|
-
import { NETWORKS } from '
|
|
24
|
+
} from '../error';
|
|
25
|
+
import { getSubgraphUrl, customGqlFetch } from '../utils';
|
|
26
|
+
import { ChainId, OrderDirection } from '../enums';
|
|
27
|
+
import { NETWORKS } from '../constants';
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Utility helpers for operator-related queries.
|