@iexec/web3telegram
Version:
Enables secure, blockchain-based messaging by encrypting Telegram user IDs for privacy. It lets users message Ethereum account holders without knowing their Telegram details.
169 lines • 6.61 kB
JavaScript
import { IExec } from 'iexec';
import { IExecDataProtectorCore } from '@iexec/dataprotector';
import { GraphQLClient } from 'graphql-request';
import { fetchUserContacts } from './fetchUserContacts.js';
import { fetchMyContacts } from './fetchMyContacts.js';
import { sendTelegram } from './sendTelegram.js';
import { sendTelegramCampaign } from './sendTelegramCampaign.js';
import { getChainDefaultConfig } from '../config/config.js';
import { isValidProvider } from '../utils/validators.js';
import { getChainIdFromProvider } from '../utils/getChainId.js';
import { resolveDappAddressFromCompass } from '../utils/resolveDappAddressFromCompass.js';
import { prepareTelegramCampaign } from './prepareTelegramCampaign.js';
export class IExecWeb3telegram {
dappAddressOrENS;
dappWhitelistAddress;
graphQLClient;
ipfsNode;
ipfsGateway;
defaultWorkerpool;
iexec;
dataProtector;
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.dappAddressOrENS = config.dappAddressOrENS;
this.dappWhitelistAddress = config.dappWhitelistAddress;
this.graphQLClient = config.graphQLClient;
this.ipfsNode = config.ipfsNode;
this.ipfsGateway = config.ipfsGateway;
this.defaultWorkerpool = config.defaultWorkerpool;
this.iexec = config.iexec;
this.dataProtector = config.dataProtector;
});
}
return this.initPromise;
}
async fetchMyContacts(args) {
await this.init();
await isValidProvider(this.iexec);
return fetchMyContacts({
...args,
iexec: this.iexec,
graphQLClient: this.graphQLClient,
dappAddressOrENS: this.dappAddressOrENS,
dappWhitelistAddress: this.dappWhitelistAddress,
});
}
async fetchUserContacts(args) {
await this.init();
return fetchUserContacts({
...args,
iexec: this.iexec,
graphQLClient: this.graphQLClient,
dappAddressOrENS: this.dappAddressOrENS,
dappWhitelistAddress: this.dappWhitelistAddress,
});
}
async sendTelegram(args) {
await this.init();
await isValidProvider(this.iexec);
return sendTelegram({
...args,
workerpoolAddressOrEns: args.workerpoolAddressOrEns || this.defaultWorkerpool,
iexec: this.iexec,
ipfsNode: this.ipfsNode,
ipfsGateway: this.ipfsGateway,
dappAddressOrENS: this.dappAddressOrENS,
dappWhitelistAddress: this.dappWhitelistAddress,
graphQLClient: this.graphQLClient,
});
}
async sendTelegramCampaign(args) {
await this.init();
await isValidProvider(this.iexec);
return sendTelegramCampaign({
...args,
workerpoolAddressOrEns: args.workerpoolAddressOrEns || this.defaultWorkerpool,
dataProtector: this.dataProtector,
});
}
async prepareTelegramCampaign(args) {
await this.init();
return prepareTelegramCampaign({
...args,
iexec: this.iexec,
dataProtector: this.dataProtector,
ipfsNode: this.ipfsNode,
ipfsGateway: this.ipfsGateway,
dappAddressOrENS: this.dappAddressOrENS,
});
}
async resolveConfig() {
const chainId = await getChainIdFromProvider(this.ethProvider);
const chainDefaultConfig = getChainDefaultConfig(chainId, {
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
});
const ipfsGateway = this.options?.ipfsGateway || chainDefaultConfig?.ipfsGateway;
let iexec, graphQLClient;
try {
iexec = new IExec({ ethProvider: this.ethProvider }, {
ipfsGatewayURL: ipfsGateway,
...this.options?.iexecOptions,
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
});
}
catch (e) {
throw new Error(`Unsupported ethProvider: ${e.message}`);
}
const subgraphUrl = this.options?.dataProtectorSubgraph ||
chainDefaultConfig?.dataProtectorSubgraph;
const dappAddressOrENS = this.options?.dappAddressOrENS ||
chainDefaultConfig?.dappAddress ||
(await resolveDappAddressFromCompass(await iexec.config.resolveCompassURL(), chainId));
const dappWhitelistAddress = this.options?.dappWhitelistAddress ||
chainDefaultConfig?.whitelistSmartContract;
const defaultWorkerpool = chainDefaultConfig?.prodWorkerpoolAddress;
const ipfsNode = this.options?.ipfsNode || chainDefaultConfig?.ipfsUploadUrl;
const missing = [];
if (!subgraphUrl)
missing.push('dataProtectorSubgraph');
if (!dappAddressOrENS)
missing.push('dappAddress');
if (!dappWhitelistAddress)
missing.push('whitelistSmartContract');
if (!ipfsGateway)
missing.push('ipfsGateway');
if (!defaultWorkerpool)
missing.push('prodWorkerpoolAddress');
if (!ipfsNode)
missing.push('ipfsUploadUrl');
if (missing.length > 0) {
throw new Error(`Missing required configuration for chainId ${chainId}: ${missing.join(', ')}`);
}
try {
graphQLClient = new GraphQLClient(subgraphUrl);
}
catch (error) {
throw new Error(`Failed to create GraphQLClient: ${error.message}`);
}
const dataProtector = new IExecDataProtectorCore(this.ethProvider, {
iexecOptions: {
ipfsGatewayURL: ipfsGateway,
...this.options?.iexecOptions,
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
},
ipfsGateway,
ipfsNode,
subgraphUrl,
});
return {
dappAddressOrENS,
dappWhitelistAddress: dappWhitelistAddress.toLowerCase(),
defaultWorkerpool,
graphQLClient,
ipfsNode,
ipfsGateway,
iexec,
dataProtector,
};
}
}
//# sourceMappingURL=IExecWeb3telegram.js.map