@human-protocol/sdk 6.0.0 → 7.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/CHANGELOG.md +34 -0
- package/dist/base.d.ts +4 -2
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +14 -0
- package/dist/constants.d.ts +0 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +27 -16
- 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} +16 -196
- package/dist/escrow/escrow_client.d.ts.map +1 -0
- package/dist/{escrow.js → escrow/escrow_client.js} +50 -430
- 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 +388 -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/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/types.d.ts +3 -1
- 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 +2 -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} +7 -112
- package/dist/kvstore/kvstore_client.d.ts.map +1 -0
- package/dist/{kvstore.js → kvstore/kvstore_client.js} +25 -187
- 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} +10 -71
- package/dist/staking/staking_client.d.ts.map +1 -0
- package/dist/{staking.js → staking/staking_client.js} +11 -141
- 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} +5 -5
- package/dist/transaction/transaction_utils.d.ts.map +1 -0
- package/dist/{transaction.js → transaction/transaction_utils.js} +9 -9
- package/dist/types.d.ts +22 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +16 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +72 -6
- 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 -3
- package/dist/worker/worker_utils.d.ts.map +1 -0
- package/dist/{worker.js → worker/worker_utils.js} +5 -5
- package/package.json +5 -5
- package/src/base.ts +38 -2
- package/src/constants.ts +38 -16
- 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} +151 -659
- package/src/escrow/escrow_utils.ts +510 -0
- package/src/escrow/index.ts +2 -0
- package/src/graphql/queries/statistics.ts +37 -1
- package/src/graphql/types.ts +4 -1
- package/src/index.ts +11 -25
- package/src/interfaces.ts +2 -7
- package/src/kvstore/index.ts +2 -0
- package/src/kvstore/kvstore_client.ts +291 -0
- 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} +49 -199
- 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} +11 -11
- package/src/types.ts +24 -1
- package/src/utils.ts +82 -9
- package/src/worker/index.ts +1 -0
- package/src/{worker.ts → worker/worker_utils.ts} +7 -7
- package/dist/encryption.d.ts +0 -189
- package/dist/encryption.d.ts.map +0 -1
- package/dist/escrow.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/kvstore.ts +0 -497
package/src/kvstore.ts
DELETED
|
@@ -1,497 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
KVStore,
|
|
3
|
-
KVStore__factory,
|
|
4
|
-
} from '@human-protocol/core/typechain-types';
|
|
5
|
-
import { ContractRunner, Overrides, ethers } from 'ethers';
|
|
6
|
-
import { BaseEthersClient } from './base';
|
|
7
|
-
import { KVStoreKeys, NETWORKS } from './constants';
|
|
8
|
-
import { requiresSigner } from './decorators';
|
|
9
|
-
import { ChainId } from './enums';
|
|
10
|
-
import {
|
|
11
|
-
ErrorInvalidAddress,
|
|
12
|
-
ErrorInvalidHash,
|
|
13
|
-
ErrorInvalidUrl,
|
|
14
|
-
ErrorKVStoreArrayLength,
|
|
15
|
-
ErrorKVStoreEmptyKey,
|
|
16
|
-
ErrorProviderDoesNotExist,
|
|
17
|
-
ErrorUnsupportedChainID,
|
|
18
|
-
InvalidKeyError,
|
|
19
|
-
} from './error';
|
|
20
|
-
import { NetworkData } from './types';
|
|
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';
|
|
28
|
-
/**
|
|
29
|
-
* Client for interacting with the KVStore contract.
|
|
30
|
-
*
|
|
31
|
-
* Internally, the SDK will use one network or another according to the network ID of the `runner`.
|
|
32
|
-
* To use this client, it is recommended to initialize it using the static [`build`](/ts/classes/KVStoreClient/#build) method.
|
|
33
|
-
*
|
|
34
|
-
* ```ts
|
|
35
|
-
* static async build(runner: ContractRunner): Promise<KVStoreClient>;
|
|
36
|
-
* ```
|
|
37
|
-
*
|
|
38
|
-
* A `Signer` or a `Provider` should be passed depending on the use case of this module:
|
|
39
|
-
*
|
|
40
|
-
* - **Signer**: when the user wants to use this model to send transactions calling the contract functions.
|
|
41
|
-
* - **Provider**: when the user wants to use this model to get information from the contracts or subgraph.
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
*
|
|
45
|
-
* ###Using Signer
|
|
46
|
-
*
|
|
47
|
-
* ####Using private key (backend)
|
|
48
|
-
*
|
|
49
|
-
* ```ts
|
|
50
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
51
|
-
* import { Wallet, JsonRpcProvider } from 'ethers';
|
|
52
|
-
*
|
|
53
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
54
|
-
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
55
|
-
*
|
|
56
|
-
* const provider = new JsonRpcProvider(rpcUrl);
|
|
57
|
-
* const signer = new Wallet(privateKey, provider);
|
|
58
|
-
* const kvstoreClient = await KVStoreClient.build(signer);
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* ####Using Wagmi (frontend)
|
|
62
|
-
*
|
|
63
|
-
* ```ts
|
|
64
|
-
* import { useSigner, useChainId } from 'wagmi';
|
|
65
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
66
|
-
*
|
|
67
|
-
* const { data: signer } = useSigner();
|
|
68
|
-
* const kvstoreClient = await KVStoreClient.build(signer);
|
|
69
|
-
* ```
|
|
70
|
-
*
|
|
71
|
-
* ###Using Provider
|
|
72
|
-
*
|
|
73
|
-
* ```ts
|
|
74
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
75
|
-
* import { JsonRpcProvider } from 'ethers';
|
|
76
|
-
*
|
|
77
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
78
|
-
*
|
|
79
|
-
* const provider = new JsonRpcProvider(rpcUrl);
|
|
80
|
-
* const kvstoreClient = await KVStoreClient.build(provider);
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
|
|
84
|
-
export class KVStoreClient extends BaseEthersClient {
|
|
85
|
-
public contract: KVStore;
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* **KVStoreClient constructor**
|
|
89
|
-
*
|
|
90
|
-
* @param runner - The Runner object to interact with the Ethereum network
|
|
91
|
-
* @param networkData - The network information required to connect to the KVStore contract
|
|
92
|
-
*/
|
|
93
|
-
constructor(runner: ContractRunner, networkData: NetworkData) {
|
|
94
|
-
super(runner, networkData);
|
|
95
|
-
|
|
96
|
-
this.contract = KVStore__factory.connect(
|
|
97
|
-
networkData.kvstoreAddress,
|
|
98
|
-
runner
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Creates an instance of KVStoreClient from a runner.
|
|
104
|
-
*
|
|
105
|
-
* @param runner - The Runner object to interact with the Ethereum network
|
|
106
|
-
* @returns An instance of KVStoreClient
|
|
107
|
-
* @throws ErrorProviderDoesNotExist If the provider does not exist for the provided Signer
|
|
108
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```ts
|
|
112
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
113
|
-
* import { Wallet, JsonRpcProvider } from 'ethers';
|
|
114
|
-
*
|
|
115
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
116
|
-
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
117
|
-
*
|
|
118
|
-
* const provider = new JsonRpcProvider(rpcUrl);
|
|
119
|
-
* const signer = new Wallet(privateKey, provider);
|
|
120
|
-
* const kvstoreClient = await KVStoreClient.build(signer);
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
public static async build(runner: ContractRunner): Promise<KVStoreClient> {
|
|
124
|
-
if (!runner.provider) {
|
|
125
|
-
throw ErrorProviderDoesNotExist;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const network = await runner.provider?.getNetwork();
|
|
129
|
-
|
|
130
|
-
const chainId: ChainId = Number(network?.chainId);
|
|
131
|
-
const networkData = NETWORKS[chainId];
|
|
132
|
-
|
|
133
|
-
if (!networkData) {
|
|
134
|
-
throw ErrorUnsupportedChainID;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return new KVStoreClient(runner, networkData);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* This function sets a key-value pair associated with the address that submits the transaction.
|
|
142
|
-
*
|
|
143
|
-
* @param key - Key of the key-value pair
|
|
144
|
-
* @param value - Value of the key-value pair
|
|
145
|
-
* @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
|
|
146
|
-
* @returns -
|
|
147
|
-
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
148
|
-
* @throws Error If the transaction fails
|
|
149
|
-
*
|
|
150
|
-
* @example
|
|
151
|
-
* ```ts
|
|
152
|
-
* await kvstoreClient.set('Role', 'RecordingOracle');
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
@requiresSigner
|
|
156
|
-
public async set(
|
|
157
|
-
key: string,
|
|
158
|
-
value: string,
|
|
159
|
-
txOptions: Overrides = {}
|
|
160
|
-
): Promise<void> {
|
|
161
|
-
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
162
|
-
try {
|
|
163
|
-
await (await this.contract.set(key, value, txOptions)).wait();
|
|
164
|
-
} catch (e) {
|
|
165
|
-
if (e instanceof Error) throw Error(`Failed to set value: ${e.message}`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* This function sets key-value pairs in bulk associated with the address that submits the transaction.
|
|
171
|
-
*
|
|
172
|
-
* @param keys - Array of keys (keys and value must have the same order)
|
|
173
|
-
* @param values - Array of values
|
|
174
|
-
* @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
|
|
175
|
-
* @returns -
|
|
176
|
-
* @throws ErrorKVStoreArrayLength If keys and values arrays have different lengths
|
|
177
|
-
* @throws ErrorKVStoreEmptyKey If any key is empty
|
|
178
|
-
* @throws Error If the transaction fails
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```ts
|
|
182
|
-
* const keys = ['role', 'webhook_url'];
|
|
183
|
-
* const values = ['RecordingOracle', 'http://localhost'];
|
|
184
|
-
* await kvstoreClient.setBulk(keys, values);
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
@requiresSigner
|
|
188
|
-
public async setBulk(
|
|
189
|
-
keys: string[],
|
|
190
|
-
values: string[],
|
|
191
|
-
txOptions: Overrides = {}
|
|
192
|
-
): Promise<void> {
|
|
193
|
-
if (keys.length !== values.length) throw ErrorKVStoreArrayLength;
|
|
194
|
-
if (keys.includes('')) throw ErrorKVStoreEmptyKey;
|
|
195
|
-
|
|
196
|
-
try {
|
|
197
|
-
await (await this.contract.setBulk(keys, values, txOptions)).wait();
|
|
198
|
-
} catch (e) {
|
|
199
|
-
if (e instanceof Error)
|
|
200
|
-
throw Error(`Failed to set bulk values: ${e.message}`);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Sets a URL value for the address that submits the transaction, and its hash.
|
|
206
|
-
*
|
|
207
|
-
* @param url - URL to set
|
|
208
|
-
* @param urlKey - Configurable URL key. `url` by default.
|
|
209
|
-
* @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
|
|
210
|
-
* @returns -
|
|
211
|
-
* @throws ErrorInvalidUrl If the URL is invalid
|
|
212
|
-
* @throws Error If the transaction fails
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```ts
|
|
216
|
-
* await kvstoreClient.setFileUrlAndHash('example.com');
|
|
217
|
-
* await kvstoreClient.setFileUrlAndHash('linkedin.com/example', 'linkedin_url');
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
|
-
@requiresSigner
|
|
221
|
-
public async setFileUrlAndHash(
|
|
222
|
-
url: string,
|
|
223
|
-
urlKey = 'url',
|
|
224
|
-
txOptions: Overrides = {}
|
|
225
|
-
): Promise<void> {
|
|
226
|
-
if (!isValidUrl(url)) {
|
|
227
|
-
throw ErrorInvalidUrl;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const content = await fetch(url).then((res) => res.text());
|
|
231
|
-
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(content));
|
|
232
|
-
|
|
233
|
-
const hashKey = urlKey + '_hash';
|
|
234
|
-
|
|
235
|
-
try {
|
|
236
|
-
await (
|
|
237
|
-
await this.contract.setBulk(
|
|
238
|
-
[urlKey, hashKey],
|
|
239
|
-
[url, contentHash],
|
|
240
|
-
txOptions
|
|
241
|
-
)
|
|
242
|
-
).wait();
|
|
243
|
-
} catch (e) {
|
|
244
|
-
if (e instanceof Error)
|
|
245
|
-
throw Error(`Failed to set URL and hash: ${e.message}`);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Gets the value of a key-value pair in the contract.
|
|
250
|
-
*
|
|
251
|
-
* @param address - Address from which to get the key value.
|
|
252
|
-
* @param key - Key to obtain the value.
|
|
253
|
-
* @returns Value of the key.
|
|
254
|
-
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
255
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
256
|
-
* @throws Error If the contract call fails
|
|
257
|
-
*
|
|
258
|
-
* @example
|
|
259
|
-
* ```ts
|
|
260
|
-
* const value = await kvstoreClient.get('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'Role');
|
|
261
|
-
* console.log('Value:', value);
|
|
262
|
-
* ```
|
|
263
|
-
*/
|
|
264
|
-
public async get(address: string, key: string): Promise<string> {
|
|
265
|
-
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
266
|
-
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
267
|
-
|
|
268
|
-
try {
|
|
269
|
-
const result = await this.contract?.get(address, key);
|
|
270
|
-
return result;
|
|
271
|
-
} catch (e) {
|
|
272
|
-
if (e instanceof Error) throw Error(`Failed to get value: ${e.message}`);
|
|
273
|
-
return e;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Utility helpers for KVStore-related queries.
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```ts
|
|
283
|
-
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
284
|
-
*
|
|
285
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
286
|
-
* ChainId.POLYGON_AMOY,
|
|
287
|
-
* "0x1234567890123456789012345678901234567890"
|
|
288
|
-
* );
|
|
289
|
-
* console.log('KVStore data:', kvStoreData);
|
|
290
|
-
* ```
|
|
291
|
-
*/
|
|
292
|
-
export class KVStoreUtils {
|
|
293
|
-
/**
|
|
294
|
-
* This function returns the KVStore data for a given address.
|
|
295
|
-
*
|
|
296
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
297
|
-
* @param address - Address of the KVStore
|
|
298
|
-
* @param options - Optional configuration for subgraph requests.
|
|
299
|
-
* @returns KVStore data
|
|
300
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
301
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
302
|
-
*
|
|
303
|
-
* @example
|
|
304
|
-
* ```ts
|
|
305
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(
|
|
306
|
-
* ChainId.POLYGON_AMOY,
|
|
307
|
-
* "0x1234567890123456789012345678901234567890"
|
|
308
|
-
* );
|
|
309
|
-
* console.log('KVStore data:', kvStoreData);
|
|
310
|
-
* ```
|
|
311
|
-
*/
|
|
312
|
-
public static async getKVStoreData(
|
|
313
|
-
chainId: ChainId,
|
|
314
|
-
address: string,
|
|
315
|
-
options?: SubgraphOptions
|
|
316
|
-
): Promise<IKVStore[]> {
|
|
317
|
-
const networkData = NETWORKS[chainId];
|
|
318
|
-
|
|
319
|
-
if (!networkData) {
|
|
320
|
-
throw ErrorUnsupportedChainID;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (address && !ethers.isAddress(address)) {
|
|
324
|
-
throw ErrorInvalidAddress;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
const { kvstores } = await customGqlFetch<{ kvstores: KVStoreData[] }>(
|
|
328
|
-
getSubgraphUrl(networkData),
|
|
329
|
-
GET_KVSTORE_BY_ADDRESS_QUERY(),
|
|
330
|
-
{ address: address.toLowerCase() },
|
|
331
|
-
options
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
const kvStoreData = kvstores.map((item) => ({
|
|
335
|
-
key: item.key,
|
|
336
|
-
value: item.value,
|
|
337
|
-
}));
|
|
338
|
-
|
|
339
|
-
return kvStoreData || [];
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
344
|
-
*
|
|
345
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
346
|
-
* @param address - Address from which to get the key value.
|
|
347
|
-
* @param key - Key to obtain the value.
|
|
348
|
-
* @param options - Optional configuration for subgraph requests.
|
|
349
|
-
* @returns Value of the key.
|
|
350
|
-
* @throws ErrorUnsupportedChainID If the network's chainId is not supported
|
|
351
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
352
|
-
* @throws ErrorKVStoreEmptyKey If the key is empty
|
|
353
|
-
* @throws InvalidKeyError If the key is not found
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```ts
|
|
357
|
-
* const value = await KVStoreUtils.get(
|
|
358
|
-
* ChainId.POLYGON_AMOY,
|
|
359
|
-
* '0x1234567890123456789012345678901234567890',
|
|
360
|
-
* 'role'
|
|
361
|
-
* );
|
|
362
|
-
* console.log('Value:', value);
|
|
363
|
-
* ```
|
|
364
|
-
*/
|
|
365
|
-
public static async get(
|
|
366
|
-
chainId: ChainId,
|
|
367
|
-
address: string,
|
|
368
|
-
key: string,
|
|
369
|
-
options?: SubgraphOptions
|
|
370
|
-
): Promise<string> {
|
|
371
|
-
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
372
|
-
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
373
|
-
|
|
374
|
-
const networkData = NETWORKS[chainId];
|
|
375
|
-
|
|
376
|
-
if (!networkData) {
|
|
377
|
-
throw ErrorUnsupportedChainID;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
const { kvstores } = await customGqlFetch<{ kvstores: KVStoreData[] }>(
|
|
381
|
-
getSubgraphUrl(networkData),
|
|
382
|
-
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY(),
|
|
383
|
-
{ address: address.toLowerCase(), key },
|
|
384
|
-
options
|
|
385
|
-
);
|
|
386
|
-
|
|
387
|
-
if (!kvstores || kvstores.length === 0) {
|
|
388
|
-
throw new InvalidKeyError(key, address);
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
return kvstores[0].value;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Gets the URL value of the given entity, and verifies its hash.
|
|
396
|
-
*
|
|
397
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
398
|
-
* @param address - Address from which to get the URL value.
|
|
399
|
-
* @param urlKey - Configurable URL key. `url` by default.
|
|
400
|
-
* @param options - Optional configuration for subgraph requests.
|
|
401
|
-
* @returns URL value for the given address if it exists, and the content is valid
|
|
402
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
403
|
-
* @throws ErrorInvalidHash If the hash verification fails
|
|
404
|
-
* @throws Error If fetching URL or hash fails
|
|
405
|
-
*
|
|
406
|
-
* @example
|
|
407
|
-
* ```ts
|
|
408
|
-
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(
|
|
409
|
-
* ChainId.POLYGON_AMOY,
|
|
410
|
-
* '0x1234567890123456789012345678901234567890'
|
|
411
|
-
* );
|
|
412
|
-
* console.log('Verified URL:', url);
|
|
413
|
-
* ```
|
|
414
|
-
*/
|
|
415
|
-
public static async getFileUrlAndVerifyHash(
|
|
416
|
-
chainId: ChainId,
|
|
417
|
-
address: string,
|
|
418
|
-
urlKey = 'url',
|
|
419
|
-
options?: SubgraphOptions
|
|
420
|
-
): Promise<string> {
|
|
421
|
-
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
422
|
-
const hashKey = urlKey + '_hash';
|
|
423
|
-
|
|
424
|
-
let url = '',
|
|
425
|
-
hash = '';
|
|
426
|
-
|
|
427
|
-
try {
|
|
428
|
-
url = await this.get(chainId, address, urlKey, options);
|
|
429
|
-
} catch (e) {
|
|
430
|
-
if (e instanceof Error) throw Error(`Failed to get URL: ${e.message}`);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Return empty string
|
|
434
|
-
if (!url?.length) {
|
|
435
|
-
return '';
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
try {
|
|
439
|
-
hash = await this.get(chainId, address, hashKey);
|
|
440
|
-
} catch (e) {
|
|
441
|
-
if (e instanceof Error) throw Error(`Failed to get Hash: ${e.message}`);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
const content = await fetch(url).then((res) => res.text());
|
|
445
|
-
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(content));
|
|
446
|
-
|
|
447
|
-
const formattedHash = hash?.replace(/^0x/, '');
|
|
448
|
-
const formattedContentHash = contentHash?.replace(/^0x/, '');
|
|
449
|
-
|
|
450
|
-
if (formattedHash !== formattedContentHash) {
|
|
451
|
-
throw ErrorInvalidHash;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
return url;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* Gets the public key of the given entity, and verifies its hash.
|
|
459
|
-
*
|
|
460
|
-
* @param chainId - Network in which the KVStore is deployed
|
|
461
|
-
* @param address - Address from which to get the public key.
|
|
462
|
-
* @param options - Optional configuration for subgraph requests.
|
|
463
|
-
* @returns Public key for the given address if it exists, and the content is valid
|
|
464
|
-
* @throws ErrorInvalidAddress If the address is invalid
|
|
465
|
-
* @throws ErrorInvalidHash If the hash verification fails
|
|
466
|
-
* @throws Error If fetching the public key fails
|
|
467
|
-
*
|
|
468
|
-
* @example
|
|
469
|
-
* ```ts
|
|
470
|
-
* const publicKey = await KVStoreUtils.getPublicKey(
|
|
471
|
-
* ChainId.POLYGON_AMOY,
|
|
472
|
-
* '0x1234567890123456789012345678901234567890'
|
|
473
|
-
* );
|
|
474
|
-
* console.log('Public key:', publicKey);
|
|
475
|
-
* ```
|
|
476
|
-
*/
|
|
477
|
-
public static async getPublicKey(
|
|
478
|
-
chainId: ChainId,
|
|
479
|
-
address: string,
|
|
480
|
-
options?: SubgraphOptions
|
|
481
|
-
): Promise<string> {
|
|
482
|
-
const publicKeyUrl = await this.getFileUrlAndVerifyHash(
|
|
483
|
-
chainId,
|
|
484
|
-
address,
|
|
485
|
-
KVStoreKeys.publicKey,
|
|
486
|
-
options
|
|
487
|
-
);
|
|
488
|
-
|
|
489
|
-
if (publicKeyUrl === '') {
|
|
490
|
-
return '';
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
const publicKey = await fetch(publicKeyUrl).then((res) => res.text());
|
|
494
|
-
|
|
495
|
-
return publicKey;
|
|
496
|
-
}
|
|
497
|
-
}
|