UNPKG

@iexec/dataprotector

Version:

This product enables users to confidentially store data–such as mail address, documents, personal information ...

97 lines 3.9 kB
import { GraphQLClient } from 'graphql-request'; import { IExec } from 'iexec'; import { getChainConfig, DEFAULT_ARWEAVE_UPLOAD_API, } from '../config/config.js'; import { getChainIdFromProvider } from '../utils/getChainId.js'; class IExecDataProtectorModule { dataprotectorContractAddress; graphQLClient; pocoSubgraphClient; ipfsNode; ipfsGateway; arweaveUploadApi = DEFAULT_ARWEAVE_UPLOAD_API; defaultWorkerpool; iexec; initPromise = null; ethProvider; options; constructor(ethProvider, options) { this.ethProvider = ethProvider || 'bellecour'; this.options = options || {}; } async init() { if (!this.initPromise) { this.initPromise = this.resolveConfig().then((config) => { this.dataprotectorContractAddress = config.dataprotectorContractAddress; this.graphQLClient = config.graphQLClient; this.pocoSubgraphClient = config.pocoSubgraphClient; this.ipfsNode = config.ipfsNode; this.ipfsGateway = config.ipfsGateway; this.defaultWorkerpool = config.defaultWorkerpool; this.iexec = config.iexec; }); } return this.initPromise; } async resolveConfig() { const chainId = await getChainIdFromProvider(this.ethProvider); const chainDefaultConfig = getChainConfig(chainId, { allowExperimentalNetworks: this.options.allowExperimentalNetworks, }); const subgraphUrl = this.options?.subgraphUrl || chainDefaultConfig?.subgraphUrl; const dataprotectorContractAddress = this.options?.dataprotectorContractAddress || chainDefaultConfig?.dataprotectorContractAddress; const ipfsGateway = this.options?.ipfsGateway || chainDefaultConfig?.ipfsGateway; const defaultWorkerpool = chainDefaultConfig?.workerpoolAddress; const ipfsNode = this.options?.ipfsNode || chainDefaultConfig?.ipfsNode; const missing = []; if (!subgraphUrl) missing.push('subgraphUrl'); if (!dataprotectorContractAddress) missing.push('dataprotectorContractAddress'); if (!ipfsGateway) missing.push('ipfsGateway'); if (!defaultWorkerpool) missing.push('defaultWorkerpool'); if (!ipfsNode) missing.push('ipfsNode'); if (missing.length > 0) { throw new Error(`Missing required configuration for chainId ${chainId}: ${missing.join(', ')}`); } let iexec, graphQLClient, pocoSubgraphClient; try { iexec = new IExec({ ethProvider: this.ethProvider }, { ipfsGatewayURL: ipfsGateway, ipfsNodeURL: ipfsNode, ...this.options?.iexecOptions, allowExperimentalNetworks: this.options.allowExperimentalNetworks, }); } catch (e) { throw new Error(`Unsupported ethProvider: ${e.message}`); } try { graphQLClient = new GraphQLClient(subgraphUrl); } catch (error) { throw new Error(`Failed to create GraphQLClient: ${error.message}`); } try { const pocoSubgraphURL = await iexec.config.resolvePocoSubgraphURL(); pocoSubgraphClient = new GraphQLClient(pocoSubgraphURL); } catch (error) { throw new Error(`Failed to create PoCo GraphQLClient: ${error.message}`); } return { dataprotectorContractAddress: dataprotectorContractAddress.toLowerCase(), defaultWorkerpool, graphQLClient, ipfsNode, ipfsGateway, iexec, pocoSubgraphClient, }; } } export { IExecDataProtectorModule }; //# sourceMappingURL=IExecDataProtectorModule.js.map