-
Notifications
You must be signed in to change notification settings - Fork 10
feat(iapp)!: add bulk processing support #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PierreJeanjacquot
merged 10 commits into
main
from
feature/iapp-support-bulk-processing
Nov 17, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f5e8809
feat(iapp): add bulk processing support
abbesBenayache 44feeb4
refactor(dapp): rename dataset to protected-data in sendEmail results
abbesBenayache 09aeba6
test(dapp): change test assertion from 'dataset' to 'protectedData' p…
abbesBenayache 1162c29
refactor: improve bulk processing structure and error handling
abbesBenayache 4b0515b
fix: sync package-lock
PierreJeanjacquot 1fc7455
fix!: ensure iapp exit 0 and update outputs
PierreJeanjacquot f785628
feat: use 2 bits callbacks for verification occurred (0b10) and verif…
PierreJeanjacquot f9faa51
test: fix test
PierreJeanjacquot f4465bf
feat: add isEmailValid in result
PierreJeanjacquot a8f6f35
fix: avoid rate limiting by process data sequentially with 1sec throt…
PierreJeanjacquot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .env | ||
| node_modules | ||
| coverage | ||
| tests/_test_outputs_ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const start = require('./sendEmail'); | ||
| const start = require('./executeTask'); | ||
|
|
||
| start().catch((error) => { | ||
| console.error(`Error: ${error.message}`); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| const { promises: fs } = require('fs'); | ||
| const { | ||
| IExecDataProtectorDeserializer, | ||
| } = require('@iexec/dataprotector-deserializer'); | ||
| const sendEmail = require('./emailService'); | ||
| const { | ||
| validateWorkerEnv, | ||
| validateAppSecret, | ||
| validateRequesterSecret, | ||
| validateProtectedData, | ||
| } = require('./validation'); | ||
| const { | ||
| downloadEncryptedContent, | ||
| decryptContent, | ||
| } = require('./decryptEmailContent'); | ||
| const { validateEmailAddress } = require('./validateEmailAddress'); | ||
| const { | ||
| checkEmailPreviousValidation, | ||
| } = require('./checkEmailPreviousValidation'); | ||
|
|
||
| async function processProtectedData({ | ||
| index, | ||
| IEXEC_IN, | ||
| appDeveloperSecret, | ||
| requesterSecret, | ||
| }) { | ||
| const datasetFilename = | ||
| index > 0 | ||
| ? process.env[`IEXEC_DATASET_${index}_FILENAME`] | ||
| : process.env.IEXEC_DATASET_FILENAME; | ||
| const result = { | ||
| index, | ||
| protectedData: datasetFilename, | ||
| isEmailValid: undefined, | ||
| }; | ||
| try { | ||
| let protectedData; | ||
| try { | ||
| const deserializerConfig = datasetFilename | ||
| ? { protectedDataPath: `${IEXEC_IN}/${datasetFilename}` } | ||
| : {}; | ||
| const deserializer = new IExecDataProtectorDeserializer( | ||
| deserializerConfig | ||
| ); | ||
| protectedData = { | ||
| email: await deserializer.getValue('email', 'string'), | ||
| }; | ||
| } catch (e) { | ||
| throw Error(`Failed to parse ProtectedData ${index}: ${e.message}`); | ||
| } | ||
|
|
||
| // Validate the protected data | ||
| validateProtectedData(protectedData); | ||
|
|
||
| // Step 1: Check if email was already validated | ||
| result.isEmailValid = await checkEmailPreviousValidation({ | ||
| datasetAddress: protectedData.email, | ||
| dappAddresses: appDeveloperSecret.WEB3MAIL_WHITELISTED_APPS, | ||
| pocoSubgraphUrl: appDeveloperSecret.POCO_SUBGRAPH_URL, | ||
| }); | ||
|
|
||
| // Step 2: If not, try Mailgun | ||
| if (result.isEmailValid === undefined) { | ||
| console.log('No prior verification found. Trying Mailgun...'); | ||
| result.isEmailValid = await validateEmailAddress({ | ||
| emailAddress: protectedData.email, | ||
| mailgunApiKey: appDeveloperSecret.MAILGUN_APIKEY, | ||
| }); | ||
| } else { | ||
| console.log('Email already verified, skipping Mailgun check.'); | ||
| } | ||
|
|
||
| if (result.isEmailValid === false) { | ||
| throw Error('The protected email address seems to be invalid.'); | ||
| } | ||
|
|
||
| // Step 3: Decrypt email content | ||
| const encryptedEmailContent = await downloadEncryptedContent( | ||
| requesterSecret.emailContentMultiAddr | ||
| ); | ||
| const requesterEmailContent = decryptContent( | ||
| encryptedEmailContent, | ||
| requesterSecret.emailContentEncryptionKey | ||
| ); | ||
|
|
||
| // Step 4: Send email | ||
| await sendEmail({ | ||
| email: protectedData.email, | ||
| mailJetApiKeyPublic: appDeveloperSecret.MJ_APIKEY_PUBLIC, | ||
| mailJetApiKeyPrivate: appDeveloperSecret.MJ_APIKEY_PRIVATE, | ||
| mailJetSender: appDeveloperSecret.MJ_SENDER, | ||
| mailgunApiKey: appDeveloperSecret.MAILGUN_APIKEY, | ||
| emailContent: requesterEmailContent, | ||
| emailSubject: requesterSecret.emailSubject, | ||
| contentType: requesterSecret.contentType, | ||
| senderName: requesterSecret.senderName, | ||
| }); | ||
| result.success = true; | ||
| } catch (e) { | ||
| result.success = false; | ||
| result.error = e.message; | ||
| } | ||
| console.log(`Protected data ${index} processed:`, result); | ||
| return result; | ||
| } | ||
|
|
||
| async function start() { | ||
| const { | ||
| IEXEC_IN, | ||
| IEXEC_OUT, | ||
| IEXEC_APP_DEVELOPER_SECRET, | ||
| IEXEC_REQUESTER_SECRET_1, | ||
| IEXEC_BULK_SLICE_SIZE, | ||
| } = process.env; | ||
|
|
||
| // Check worker env | ||
| const workerEnv = validateWorkerEnv({ IEXEC_OUT }); | ||
|
|
||
| let result; // { success: boolean, error?: string, protectedData?: string, results?: { index: number, protectedData: string, success: boolean, error?: string }[] } | ||
| let callbackData; | ||
| try { | ||
| // Parse the app developer secret environment variable | ||
| let appDeveloperSecret; | ||
| try { | ||
| appDeveloperSecret = JSON.parse(IEXEC_APP_DEVELOPER_SECRET); | ||
| appDeveloperSecret.WEB3MAIL_WHITELISTED_APPS = | ||
| appDeveloperSecret.WEB3MAIL_WHITELISTED_APPS | ||
| ? JSON.parse(appDeveloperSecret.WEB3MAIL_WHITELISTED_APPS) | ||
| : undefined; | ||
| } catch { | ||
| throw Error('Failed to parse the developer secret'); | ||
| } | ||
| appDeveloperSecret = validateAppSecret(appDeveloperSecret); | ||
|
|
||
| let requesterSecret; | ||
| try { | ||
| requesterSecret = JSON.parse(IEXEC_REQUESTER_SECRET_1); | ||
| } catch { | ||
| throw Error('Failed to parse requester secret'); | ||
| } | ||
| requesterSecret = validateRequesterSecret(requesterSecret); | ||
|
|
||
| const bulkSize = parseInt(IEXEC_BULK_SLICE_SIZE, 10) || 0; | ||
|
|
||
| // Process multiple protected data | ||
| if (bulkSize > 0) { | ||
| console.log(`Processing ${bulkSize} protected data...`); | ||
| const results = []; | ||
| // Process each protected data one by one to avoid rate limiting issues | ||
| for (let index = 1; index <= bulkSize; index += 1) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| const protectedDataResult = await processProtectedData({ | ||
| index, | ||
| IEXEC_IN, | ||
| appDeveloperSecret, | ||
| requesterSecret, | ||
| }); | ||
| results.push(protectedDataResult); | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await new Promise((res) => { | ||
| setTimeout(res, 1000); | ||
| }); // Add a delay to avoid rate limiting | ||
| } | ||
| const successCount = results.filter((r) => r.success === true).length; | ||
| const errorCount = results.filter((r) => r.success !== true).length; | ||
| result = { | ||
| success: errorCount === 0, | ||
| error: errorCount > 0 ? 'Partial failure' : undefined, | ||
| totalCount: results.length, | ||
| successCount, | ||
| errorCount, | ||
| results: results.map((r) => ({ | ||
| index: r.index, | ||
| protectedData: r.protectedData, | ||
| success: r.success, | ||
| isEmailValid: r.isEmailValid, | ||
| error: r.error, | ||
| })), | ||
| }; | ||
| } else { | ||
| console.log('Processing single protected data...'); | ||
| const { protectedData, success, error, isEmailValid } = | ||
| await processProtectedData({ | ||
| index: 0, | ||
| IEXEC_IN, | ||
| appDeveloperSecret, | ||
| requesterSecret, | ||
| }); | ||
| // set result json | ||
| result = { protectedData, success, isEmailValid, error }; | ||
| // Add callback data for single processing if useCallback is enabled | ||
| if (requesterSecret.useCallback) { | ||
| const bool32Bytes = Buffer.alloc(32); | ||
| // Encode 2 bits: | ||
| // - Bit 1: Email validation was performed (1 = yes, 0 = no) | ||
| // - Bit 0: Email is valid (1 = yes, 0 = no) | ||
| if (isEmailValid === true) { | ||
| // eslint-disable-next-line no-bitwise | ||
| bool32Bytes[31] |= 0b11; | ||
| } else if (isEmailValid === false) { | ||
| // eslint-disable-next-line no-bitwise | ||
| bool32Bytes[31] |= 0b10; | ||
| } | ||
| callbackData = `0x${bool32Bytes.toString('hex')}`; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| console.error('Something went wrong:', e.message); | ||
| result = { success: false, error: e.message }; | ||
| } | ||
|
|
||
| console.log('Writing results:', JSON.stringify(result)); | ||
| await fs.writeFile( | ||
| `${workerEnv.IEXEC_OUT}/result.json`, | ||
| JSON.stringify(result, null, 2) | ||
| ); | ||
|
|
||
| const computedData = { | ||
| 'deterministic-output-path': `${workerEnv.IEXEC_OUT}/result.json`, | ||
| 'callback-data': callbackData, | ||
| }; | ||
|
|
||
| await fs.writeFile( | ||
| `${workerEnv.IEXEC_OUT}/computed.json`, | ||
| JSON.stringify(computedData, null, 2) | ||
| ); | ||
| } | ||
|
|
||
| module.exports = start; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.