@human-protocol/sdk 3.0.1 → 3.0.3
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/constants.d.ts +1 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +15 -14
- package/dist/escrow.d.ts +0 -6
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +0 -6
- package/dist/graphql/queries/escrow.d.ts.map +1 -1
- package/dist/graphql/queries/escrow.js +0 -3
- package/dist/graphql/queries/kvstore.d.ts +1 -0
- package/dist/graphql/queries/kvstore.d.ts.map +1 -1
- package/dist/graphql/queries/kvstore.js +12 -1
- package/dist/graphql/queries/operator.js +1 -1
- package/dist/graphql/queries/statistics.d.ts +2 -2
- package/dist/graphql/queries/statistics.d.ts.map +1 -1
- package/dist/graphql/queries/statistics.js +11 -7
- package/dist/graphql/types.d.ts +0 -5
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/interfaces.d.ts +3 -5
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +69 -106
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +123 -159
- package/dist/statistics.d.ts +72 -66
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +121 -89
- package/package.json +1 -1
- package/src/constants.ts +15 -14
- package/src/escrow.ts +0 -6
- package/src/graphql/queries/escrow.ts +0 -3
- package/src/graphql/queries/kvstore.ts +11 -0
- package/src/graphql/queries/operator.ts +1 -1
- package/src/graphql/queries/statistics.ts +13 -9
- package/src/graphql/types.ts +0 -5
- package/src/index.ts +2 -1
- package/src/interfaces.ts +3 -5
- package/src/kvstore.ts +145 -158
- package/src/statistics.ts +138 -100
package/dist/kvstore.d.ts
CHANGED
|
@@ -176,80 +176,6 @@ export declare class KVStoreClient extends BaseEthersClient {
|
|
|
176
176
|
* ```
|
|
177
177
|
*/
|
|
178
178
|
setFileUrlAndHash(url: string, urlKey?: string, txOptions?: Overrides): Promise<void>;
|
|
179
|
-
/**
|
|
180
|
-
* Gets the value of a key-value pair in the contract.
|
|
181
|
-
*
|
|
182
|
-
* @param {string} address Address from which to get the key value.
|
|
183
|
-
* @param {string} key Key to obtain the value.
|
|
184
|
-
* @returns {string} Value of the key.
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* **Code example**
|
|
188
|
-
*
|
|
189
|
-
* > Need to have available stake.
|
|
190
|
-
*
|
|
191
|
-
* ```ts
|
|
192
|
-
* import { providers } from 'ethers';
|
|
193
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
194
|
-
*
|
|
195
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
196
|
-
*
|
|
197
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
198
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
199
|
-
*
|
|
200
|
-
* const value = await kvstoreClient.get('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'Role');
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
get(address: string, key: string): Promise<string>;
|
|
204
|
-
/**
|
|
205
|
-
* Gets the URL value of the given entity, and verify its hash.
|
|
206
|
-
*
|
|
207
|
-
* @param {string} address Address from which to get the URL value.
|
|
208
|
-
* @param {string} urlKey Configurable URL key. `url` by default.
|
|
209
|
-
* @returns {string} URL value for the given address if exists, and the content is valid
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
* **Code example**
|
|
213
|
-
*
|
|
214
|
-
* ```ts
|
|
215
|
-
* import { providers } from 'ethers';
|
|
216
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
217
|
-
*
|
|
218
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
219
|
-
*
|
|
220
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
221
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
222
|
-
*
|
|
223
|
-
* const url = await kvstoreClient.getFileUrlAndVerifyHash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266');
|
|
224
|
-
* const linkedinUrl = await kvstoreClient.getFileUrlAndVerifyHash(
|
|
225
|
-
* '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
|
|
226
|
-
* 'linkedin_url'
|
|
227
|
-
* );
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
getFileUrlAndVerifyHash(address: string, urlKey?: string): Promise<string>;
|
|
231
|
-
/**
|
|
232
|
-
* Gets the public key of the given entity, and verify its hash.
|
|
233
|
-
*
|
|
234
|
-
* @param {string} address Address from which to get the public key.
|
|
235
|
-
* @returns {string} Public key for the given address if exists, and the content is valid
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* **Code example**
|
|
239
|
-
*
|
|
240
|
-
* ```ts
|
|
241
|
-
* import { providers } from 'ethers';
|
|
242
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
243
|
-
*
|
|
244
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
245
|
-
*
|
|
246
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
247
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
248
|
-
*
|
|
249
|
-
* const publicKey = await kvstoreClient.getPublicKey('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266');
|
|
250
|
-
* ```
|
|
251
|
-
*/
|
|
252
|
-
getPublicKey(address: string): Promise<string>;
|
|
253
179
|
}
|
|
254
180
|
/**
|
|
255
181
|
* ## Introduction
|
|
@@ -277,60 +203,97 @@ export declare class KVStoreClient extends BaseEthersClient {
|
|
|
277
203
|
* ```ts
|
|
278
204
|
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
279
205
|
*
|
|
280
|
-
* const KVStoreAddresses = new KVStoreUtils.
|
|
281
|
-
*
|
|
282
|
-
*
|
|
206
|
+
* const KVStoreAddresses = new KVStoreUtils.getKVStoreData({
|
|
207
|
+
* ChainId.POLYGON_AMOY,
|
|
208
|
+
* "0x1234567890123456789012345678901234567890",
|
|
209
|
+
* );
|
|
283
210
|
* ```
|
|
284
211
|
*/
|
|
285
212
|
export declare class KVStoreUtils {
|
|
286
213
|
/**
|
|
287
214
|
* This function returns the KVStore data for a given address.
|
|
288
215
|
*
|
|
289
|
-
*
|
|
216
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
217
|
+
* @param {string} address Address of the KVStore
|
|
218
|
+
* @returns {Promise<IKVStore[]>} KVStore data
|
|
219
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
220
|
+
* @throws {ErrorInvalidAddress} - Thrown if the Address sent is invalid
|
|
290
221
|
*
|
|
291
|
-
* **
|
|
222
|
+
* **Code example**
|
|
292
223
|
*
|
|
293
224
|
* ```ts
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
* GOERLI = 5,
|
|
299
|
-
* BSC_MAINNET = 56,
|
|
300
|
-
* BSC_TESTNET = 97,
|
|
301
|
-
* POLYGON = 137,
|
|
302
|
-
* POLYGON_MUMBAI = 80001,
|
|
303
|
-
* POLYGON_AMOY = 80002,
|
|
304
|
-
* MOONBEAM = 1284,
|
|
305
|
-
* MOONBASE_ALPHA = 1287,
|
|
306
|
-
* AVALANCHE = 43114,
|
|
307
|
-
* AVALANCHE_TESTNET = 43113,
|
|
308
|
-
* CELO = 42220,
|
|
309
|
-
* CELO_ALFAJORES = 44787,
|
|
310
|
-
* LOCALHOST = 1338,
|
|
311
|
-
* }
|
|
225
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
226
|
+
*
|
|
227
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(ChainId.POLYGON_AMOY, "0x1234567890123456789012345678901234567890");
|
|
228
|
+
* console.log(kvStoreData);
|
|
312
229
|
* ```
|
|
230
|
+
*/
|
|
231
|
+
static getKVStoreData(chainId: ChainId, address: string): Promise<IKVStore[]>;
|
|
232
|
+
/**
|
|
233
|
+
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
234
|
+
*
|
|
235
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
236
|
+
* @param {string} address Address from which to get the key value.
|
|
237
|
+
* @param {string} key Key to obtain the value.
|
|
238
|
+
* @returns {Promise<string>} Value of the key.
|
|
239
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
240
|
+
* @throws {ErrorInvalidAddress} - Thrown if the Address sent is invalid
|
|
241
|
+
* @throws {ErrorKVStoreEmptyKey} - Thrown if the key is empty
|
|
242
|
+
*
|
|
243
|
+
* **Code example**
|
|
313
244
|
*
|
|
314
245
|
* ```ts
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
246
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
247
|
+
*
|
|
248
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
249
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
250
|
+
* const key = 'role';
|
|
251
|
+
*
|
|
252
|
+
* const value = await KVStoreUtils.get(chainId, address, key);
|
|
253
|
+
* console.log(value);
|
|
319
254
|
* ```
|
|
255
|
+
*/
|
|
256
|
+
static get(chainId: ChainId, address: string, key: string): Promise<string>;
|
|
257
|
+
/**
|
|
258
|
+
* Gets the URL value of the given entity, and verifies its hash.
|
|
320
259
|
*
|
|
321
260
|
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
322
|
-
* @param {string} address Address
|
|
323
|
-
* @
|
|
261
|
+
* @param {string} address Address from which to get the URL value.
|
|
262
|
+
* @param {string} urlKey Configurable URL key. `url` by default.
|
|
263
|
+
* @returns {Promise<string>} URL value for the given address if it exists, and the content is valid
|
|
324
264
|
*
|
|
325
265
|
* **Code example**
|
|
326
266
|
*
|
|
327
267
|
* ```ts
|
|
328
268
|
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
329
269
|
*
|
|
330
|
-
* const
|
|
331
|
-
*
|
|
270
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
271
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
272
|
+
*
|
|
273
|
+
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(chainId, address);
|
|
274
|
+
* console.log(url);
|
|
332
275
|
* ```
|
|
333
276
|
*/
|
|
334
|
-
static
|
|
277
|
+
static getFileUrlAndVerifyHash(chainId: ChainId, address: string, urlKey?: string): Promise<string>;
|
|
278
|
+
/**
|
|
279
|
+
* Gets the public key of the given entity, and verifies its hash.
|
|
280
|
+
*
|
|
281
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
282
|
+
* @param {string} address Address from which to get the public key.
|
|
283
|
+
* @returns {Promise<string>} Public key for the given address if it exists, and the content is valid
|
|
284
|
+
*
|
|
285
|
+
* **Code example**
|
|
286
|
+
*
|
|
287
|
+
* ```ts
|
|
288
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
289
|
+
*
|
|
290
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
291
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
292
|
+
*
|
|
293
|
+
* const publicKey = await KVStoreUtils.getPublicKey(chainId, address);
|
|
294
|
+
* console.log(publicKey);
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
static getPublicKey(chainId: ChainId, address: string): Promise<string>;
|
|
335
298
|
}
|
|
336
299
|
//# sourceMappingURL=kvstore.d.ts.map
|
package/dist/kvstore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kvstore.d.ts","sourceRoot":"","sources":["../src/kvstore.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAU,MAAM,QAAQ,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAG1C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAWlC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"kvstore.d.ts","sourceRoot":"","sources":["../src/kvstore.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAU,MAAM,QAAQ,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAG1C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAWlC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAOtC,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AAEH,qBAAa,aAAc,SAAQ,gBAAgB;IACjD,OAAO,CAAC,QAAQ,CAAU;IAE1B;;;;;OAKG;gBACS,MAAM,EAAE,cAAc,EAAE,WAAW,EAAE,WAAW;IAS5D;;;;;;;;OAQG;WACiB,KAAK,CAAC,MAAM,EAAE,cAAc;IAiBhD;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IAEU,GAAG,CACd,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,SAAS,GAAE,SAAc,GACxB,OAAO,CAAC,IAAI,CAAC;IAShB;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IAEU,OAAO,CAClB,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,EAAE,MAAM,EAAE,EAChB,SAAS,GAAE,SAAc,GACxB,OAAO,CAAC,IAAI,CAAC;IAYhB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IAEU,iBAAiB,CAC5B,GAAG,EAAE,MAAM,EACX,MAAM,SAAQ,EACd,SAAS,GAAE,SAAc,GACxB,OAAO,CAAC,IAAI,CAAC;CAuBjB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,YAAY;IACvB;;;;;;;;;;;;;;;;;OAiBG;WACiB,cAAc,CAChC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,QAAQ,EAAE,CAAC;IAyBtB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;WACiB,GAAG,CACrB,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,MAAM,CAAC;IAyBlB;;;;;;;;;;;;;;;;;;;OAmBG;WACiB,uBAAuB,CACzC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,MAAM,SAAQ,GACb,OAAO,CAAC,MAAM,CAAC;IAkClB;;;;;;;;;;;;;;;;;;OAkBG;WACiB,YAAY,CAC9B,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,MAAM,CAAC;CAenB"}
|
package/dist/kvstore.js
CHANGED
|
@@ -244,130 +244,6 @@ class KVStoreClient extends base_1.BaseEthersClient {
|
|
|
244
244
|
throw Error(`Failed to set URL and hash: ${e.message}`);
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
|
-
/**
|
|
248
|
-
* Gets the value of a key-value pair in the contract.
|
|
249
|
-
*
|
|
250
|
-
* @param {string} address Address from which to get the key value.
|
|
251
|
-
* @param {string} key Key to obtain the value.
|
|
252
|
-
* @returns {string} Value of the key.
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
* **Code example**
|
|
256
|
-
*
|
|
257
|
-
* > Need to have available stake.
|
|
258
|
-
*
|
|
259
|
-
* ```ts
|
|
260
|
-
* import { providers } from 'ethers';
|
|
261
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
262
|
-
*
|
|
263
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
264
|
-
*
|
|
265
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
266
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
267
|
-
*
|
|
268
|
-
* const value = await kvstoreClient.get('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'Role');
|
|
269
|
-
* ```
|
|
270
|
-
*/
|
|
271
|
-
async get(address, key) {
|
|
272
|
-
if (key === '')
|
|
273
|
-
throw error_1.ErrorKVStoreEmptyKey;
|
|
274
|
-
if (!ethers_1.ethers.isAddress(address))
|
|
275
|
-
throw error_1.ErrorInvalidAddress;
|
|
276
|
-
try {
|
|
277
|
-
const result = await this.contract?.get(address, key);
|
|
278
|
-
return result;
|
|
279
|
-
}
|
|
280
|
-
catch (e) {
|
|
281
|
-
if (e instanceof Error)
|
|
282
|
-
throw Error(`Failed to get value: ${e.message}`);
|
|
283
|
-
return e;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Gets the URL value of the given entity, and verify its hash.
|
|
288
|
-
*
|
|
289
|
-
* @param {string} address Address from which to get the URL value.
|
|
290
|
-
* @param {string} urlKey Configurable URL key. `url` by default.
|
|
291
|
-
* @returns {string} URL value for the given address if exists, and the content is valid
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
* **Code example**
|
|
295
|
-
*
|
|
296
|
-
* ```ts
|
|
297
|
-
* import { providers } from 'ethers';
|
|
298
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
299
|
-
*
|
|
300
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
301
|
-
*
|
|
302
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
303
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
304
|
-
*
|
|
305
|
-
* const url = await kvstoreClient.getFileUrlAndVerifyHash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266');
|
|
306
|
-
* const linkedinUrl = await kvstoreClient.getFileUrlAndVerifyHash(
|
|
307
|
-
* '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
|
|
308
|
-
* 'linkedin_url'
|
|
309
|
-
* );
|
|
310
|
-
* ```
|
|
311
|
-
*/
|
|
312
|
-
async getFileUrlAndVerifyHash(address, urlKey = 'url') {
|
|
313
|
-
if (!ethers_1.ethers.isAddress(address))
|
|
314
|
-
throw error_1.ErrorInvalidAddress;
|
|
315
|
-
const hashKey = urlKey + '_hash';
|
|
316
|
-
let url = '', hash = '';
|
|
317
|
-
try {
|
|
318
|
-
url = await this.contract?.get(address, urlKey);
|
|
319
|
-
}
|
|
320
|
-
catch (e) {
|
|
321
|
-
if (e instanceof Error)
|
|
322
|
-
throw Error(`Failed to get URL: ${e.message}`);
|
|
323
|
-
}
|
|
324
|
-
// Return empty string
|
|
325
|
-
if (!url?.length) {
|
|
326
|
-
return '';
|
|
327
|
-
}
|
|
328
|
-
try {
|
|
329
|
-
hash = await this.contract?.get(address, hashKey);
|
|
330
|
-
}
|
|
331
|
-
catch (e) {
|
|
332
|
-
if (e instanceof Error)
|
|
333
|
-
throw Error(`Failed to get Hash: ${e.message}`);
|
|
334
|
-
}
|
|
335
|
-
const content = await fetch(url).then((res) => res.text());
|
|
336
|
-
const contentHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(content));
|
|
337
|
-
if (hash !== contentHash) {
|
|
338
|
-
throw error_1.ErrorInvalidHash;
|
|
339
|
-
}
|
|
340
|
-
return url;
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Gets the public key of the given entity, and verify its hash.
|
|
344
|
-
*
|
|
345
|
-
* @param {string} address Address from which to get the public key.
|
|
346
|
-
* @returns {string} Public key for the given address if exists, and the content is valid
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
* **Code example**
|
|
350
|
-
*
|
|
351
|
-
* ```ts
|
|
352
|
-
* import { providers } from 'ethers';
|
|
353
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
354
|
-
*
|
|
355
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
356
|
-
*
|
|
357
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
358
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
359
|
-
*
|
|
360
|
-
* const publicKey = await kvstoreClient.getPublicKey('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266');
|
|
361
|
-
* ```
|
|
362
|
-
*/
|
|
363
|
-
async getPublicKey(address) {
|
|
364
|
-
const publicKeyUrl = await this.getFileUrlAndVerifyHash(address, constants_1.KVStoreKeys.publicKey);
|
|
365
|
-
if (publicKeyUrl === '') {
|
|
366
|
-
return '';
|
|
367
|
-
}
|
|
368
|
-
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
369
|
-
return publicKey;
|
|
370
|
-
}
|
|
371
247
|
}
|
|
372
248
|
__decorate([
|
|
373
249
|
decorators_1.requiresSigner,
|
|
@@ -414,50 +290,21 @@ exports.KVStoreClient = KVStoreClient;
|
|
|
414
290
|
* ```ts
|
|
415
291
|
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
416
292
|
*
|
|
417
|
-
* const KVStoreAddresses = new KVStoreUtils.
|
|
418
|
-
*
|
|
419
|
-
*
|
|
293
|
+
* const KVStoreAddresses = new KVStoreUtils.getKVStoreData({
|
|
294
|
+
* ChainId.POLYGON_AMOY,
|
|
295
|
+
* "0x1234567890123456789012345678901234567890",
|
|
296
|
+
* );
|
|
420
297
|
* ```
|
|
421
298
|
*/
|
|
422
299
|
class KVStoreUtils {
|
|
423
300
|
/**
|
|
424
301
|
* This function returns the KVStore data for a given address.
|
|
425
302
|
*
|
|
426
|
-
* > This uses Subgraph
|
|
427
|
-
*
|
|
428
|
-
* **Input parameters**
|
|
429
|
-
*
|
|
430
|
-
* ```ts
|
|
431
|
-
* enum ChainId {
|
|
432
|
-
* ALL = -1,
|
|
433
|
-
* MAINNET = 1,
|
|
434
|
-
* RINKEBY = 4,
|
|
435
|
-
* GOERLI = 5,
|
|
436
|
-
* BSC_MAINNET = 56,
|
|
437
|
-
* BSC_TESTNET = 97,
|
|
438
|
-
* POLYGON = 137,
|
|
439
|
-
* POLYGON_MUMBAI = 80001,
|
|
440
|
-
* POLYGON_AMOY = 80002,
|
|
441
|
-
* MOONBEAM = 1284,
|
|
442
|
-
* MOONBASE_ALPHA = 1287,
|
|
443
|
-
* AVALANCHE = 43114,
|
|
444
|
-
* AVALANCHE_TESTNET = 43113,
|
|
445
|
-
* CELO = 42220,
|
|
446
|
-
* CELO_ALFAJORES = 44787,
|
|
447
|
-
* LOCALHOST = 1338,
|
|
448
|
-
* }
|
|
449
|
-
* ```
|
|
450
|
-
*
|
|
451
|
-
* ```ts
|
|
452
|
-
* interface IKVStore {
|
|
453
|
-
* key: string;
|
|
454
|
-
* value: string;
|
|
455
|
-
* }
|
|
456
|
-
* ```
|
|
457
|
-
*
|
|
458
303
|
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
459
304
|
* @param {string} address Address of the KVStore
|
|
460
305
|
* @returns {Promise<IKVStore[]>} KVStore data
|
|
306
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
307
|
+
* @throws {ErrorInvalidAddress} - Thrown if the Address sent is invalid
|
|
461
308
|
*
|
|
462
309
|
* **Code example**
|
|
463
310
|
*
|
|
@@ -483,5 +330,122 @@ class KVStoreUtils {
|
|
|
483
330
|
}));
|
|
484
331
|
return kvStoreData || [];
|
|
485
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
335
|
+
*
|
|
336
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
337
|
+
* @param {string} address Address from which to get the key value.
|
|
338
|
+
* @param {string} key Key to obtain the value.
|
|
339
|
+
* @returns {Promise<string>} Value of the key.
|
|
340
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
341
|
+
* @throws {ErrorInvalidAddress} - Thrown if the Address sent is invalid
|
|
342
|
+
* @throws {ErrorKVStoreEmptyKey} - Thrown if the key is empty
|
|
343
|
+
*
|
|
344
|
+
* **Code example**
|
|
345
|
+
*
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
348
|
+
*
|
|
349
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
350
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
351
|
+
* const key = 'role';
|
|
352
|
+
*
|
|
353
|
+
* const value = await KVStoreUtils.get(chainId, address, key);
|
|
354
|
+
* console.log(value);
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
static async get(chainId, address, key) {
|
|
358
|
+
if (key === '')
|
|
359
|
+
throw error_1.ErrorKVStoreEmptyKey;
|
|
360
|
+
if (!ethers_1.ethers.isAddress(address))
|
|
361
|
+
throw error_1.ErrorInvalidAddress;
|
|
362
|
+
const networkData = constants_1.NETWORKS[chainId];
|
|
363
|
+
if (!networkData) {
|
|
364
|
+
throw error_1.ErrorUnsupportedChainID;
|
|
365
|
+
}
|
|
366
|
+
const { kvstores } = await (0, graphql_request_1.default)((0, utils_1.getSubgraphUrl)(networkData), (0, kvstore_1.GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY)(), { address: address.toLowerCase(), key });
|
|
367
|
+
if (!kvstores || kvstores.length === 0) {
|
|
368
|
+
throw new Error(`Key "${key}" not found for address ${address}`);
|
|
369
|
+
}
|
|
370
|
+
const value = kvstores[0].value;
|
|
371
|
+
return value;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Gets the URL value of the given entity, and verifies its hash.
|
|
375
|
+
*
|
|
376
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
377
|
+
* @param {string} address Address from which to get the URL value.
|
|
378
|
+
* @param {string} urlKey Configurable URL key. `url` by default.
|
|
379
|
+
* @returns {Promise<string>} URL value for the given address if it exists, and the content is valid
|
|
380
|
+
*
|
|
381
|
+
* **Code example**
|
|
382
|
+
*
|
|
383
|
+
* ```ts
|
|
384
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
385
|
+
*
|
|
386
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
387
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
388
|
+
*
|
|
389
|
+
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(chainId, address);
|
|
390
|
+
* console.log(url);
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
static async getFileUrlAndVerifyHash(chainId, address, urlKey = 'url') {
|
|
394
|
+
if (!ethers_1.ethers.isAddress(address))
|
|
395
|
+
throw error_1.ErrorInvalidAddress;
|
|
396
|
+
const hashKey = urlKey + '_hash';
|
|
397
|
+
let url = '', hash = '';
|
|
398
|
+
try {
|
|
399
|
+
url = await this.get(chainId, address, urlKey);
|
|
400
|
+
}
|
|
401
|
+
catch (e) {
|
|
402
|
+
if (e instanceof Error)
|
|
403
|
+
throw Error(`Failed to get URL: ${e.message}`);
|
|
404
|
+
}
|
|
405
|
+
// Return empty string
|
|
406
|
+
if (!url?.length) {
|
|
407
|
+
return '';
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
hash = await this.get(chainId, address, hashKey);
|
|
411
|
+
}
|
|
412
|
+
catch (e) {
|
|
413
|
+
if (e instanceof Error)
|
|
414
|
+
throw Error(`Failed to get Hash: ${e.message}`);
|
|
415
|
+
}
|
|
416
|
+
const content = await fetch(url).then((res) => res.text());
|
|
417
|
+
const contentHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(content));
|
|
418
|
+
if (hash !== contentHash) {
|
|
419
|
+
throw error_1.ErrorInvalidHash;
|
|
420
|
+
}
|
|
421
|
+
return url;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Gets the public key of the given entity, and verifies its hash.
|
|
425
|
+
*
|
|
426
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
427
|
+
* @param {string} address Address from which to get the public key.
|
|
428
|
+
* @returns {Promise<string>} Public key for the given address if it exists, and the content is valid
|
|
429
|
+
*
|
|
430
|
+
* **Code example**
|
|
431
|
+
*
|
|
432
|
+
* ```ts
|
|
433
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
434
|
+
*
|
|
435
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
436
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
437
|
+
*
|
|
438
|
+
* const publicKey = await KVStoreUtils.getPublicKey(chainId, address);
|
|
439
|
+
* console.log(publicKey);
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
static async getPublicKey(chainId, address) {
|
|
443
|
+
const publicKeyUrl = await this.getFileUrlAndVerifyHash(chainId, address, constants_1.KVStoreKeys.publicKey);
|
|
444
|
+
if (publicKeyUrl === '') {
|
|
445
|
+
return '';
|
|
446
|
+
}
|
|
447
|
+
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
448
|
+
return publicKey;
|
|
449
|
+
}
|
|
486
450
|
}
|
|
487
451
|
exports.KVStoreUtils = KVStoreUtils;
|