Skip to content

Instantly share code, notes, and snippets.

@rigwild
Last active May 9, 2024 20:38
Show Gist options
  • Save rigwild/056bf82fab6d5eb697842a22a1505a7e to your computer and use it in GitHub Desktop.
Save rigwild/056bf82fab6d5eb697842a22a1505a7e to your computer and use it in GitHub Desktop.
Register an email, grant access to someone, then send me an email - iExec Web3Mail
import { IExecDataProtectorCore, getWeb3Provider } from '@iexec/dataprotector'
import { IExecWeb3mail } from '@iexec/web3mail'
const WALLET_ADMIN_ADDRESS = '0xAA0b58975c43b1Fd0b49E6d50F018b8e8451805e'
const WALLET_ADMIN_PRIVATE_KEY = '5e1ffa8ae0d1c6727c86290b6b69391c813f17f1f7642eb9a5e28fcb9e348309'
const WALLET_CUSTOMER_ADDRESS = '0xFF1CBA1Dba9879d7Fa32a776d8e9166Dc9A2F173'
const WALLET_CUSTOMER_PRIVATE_KEY = 'c3fce58c12d1845a8104183b872af124a1bd7f563645fe3630e127644fcfaa6e'
const dataProtector = new IExecDataProtectorCore(getWeb3Provider(WALLET_CUSTOMER_PRIVATE_KEY))
//
// Register email of customer
// See https://documentation-tools.vercel.app/tools/dataProtector/dataProtectorCore/protectData.html
//
console.log('[CUSTOMER] Protecting data...')
const protectedData = await dataProtector.protectData({
name: 'Nemeos Web3Mail',
data: {
email: 'hello.test.email@yopmail.com', // https://yopmail.com/?hello.test.email
},
})
// const protectedData = {
// name: 'Nemeos Web3Mail',
// address: '0x3e38c8bF1EE00123cb704A7B449a0081F6c4416c',
// owner: '0xFF1CBA1Dba9879d7Fa32a776d8e9166Dc9A2F173',
// schema: {
// email: 'string',
// },
// creationTimestamp: 1715027075,
// transactionHash: '0xb363bb6a7423e33cb30eee750e81b921762d907d8e5595ed12098683bcdd9cea',
// encryptionKey: 'J96JJZ7Mv6Z6MGKSrAEJ92W63AnMnrsL81BrgI7F+Jo=',
// }
console.log({ protectedData }, '[CUSTOMER] Protected data')
//
// Authorize admin to send email
// See https://documentation-tools.vercel.app/tools/dataProtector/dataProtectorCore/grantAccess.html
//
console.log('[CUSTOMER] Granting access to admin to send email...')
const WEB3MAIL_WHITELIST_CONTRACT = '0x781482C39CcE25546583EaC4957Fb7Bf04C277D2'
const grantedAccess = await dataProtector.grantAccess({
protectedData: protectedData.address,
authorizedApp: WEB3MAIL_WHITELIST_CONTRACT,
authorizedUser: WALLET_ADMIN_ADDRESS,
})
// const grantedAccess = {
// dataset: '0x3e38c8bf1ee00123cb704a7b449a0081f6c4416c',
// datasetprice: '0',
// volume: '1',
// tag: '0x0000000000000000000000000000000000000000000000000000000000000003',
// apprestrict: '0x781482c39cce25546583eac4957fb7bf04c277d2',
// workerpoolrestrict: '0x0000000000000000000000000000000000000000',
// requesterrestrict: '0xff2e74eb3b0b5ddbc16ab165520e6c609528eadd',
// salt: '0x7cec8c5bf438a03b8fe1288b76613e43d4beb76029abea1170a7d7b8ba09f8da',
// sign: '0x8fa0063f3dfcec6377d303b3eb8fc151f353d8637c1a8a00a92cc7e6386ff4850990de79f3c09b3d11312ff040168d5d12d80041afd47eaed428efe6352a293a1c',
// }
console.log({ grantedAccess }, '[CUSTOMER] Granted access to admin to send email')
// ------------------------
const web3Mail = new IExecWeb3mail(getWeb3Provider(WALLET_ADMIN_PRIVATE_KEY))
//
// Fetch my contacts to check if the customer is already in the list
// See https://documentation-tools.vercel.app/tools/web3mail/methods/fetchMyContacts.html
//
console.log('[ADMIN] Fetching contacts...')
const contactsList = await web3Mail.fetchMyContacts()
// const contactsList = [
// {
// address: '0x84763edd6130b1c49853397bc1dd059fb77bbee0',
// // \/ \/ \/ Customer address is in contacts list
// owner: '0xff1cba1dba9879d7fa32a776d8e9166dc9a2f173',
// accessGrantTimestamp: '2024-05-06T20:43:22.573Z',
// },
// {
// address: '0x9f08c5b71ae37aecbf15346ebc98d2cdd4f1c1cf',
// owner: '0x316a389d7f0ac46b19fcbe7076f125566f09cebc',
// accessGrantTimestamp: '2024-04-29T14:26:04.086Z',
// },
// // ...
// ]
console.log({ contactsList }, '[ADMIN] Fetched contacts')
if (!contactsList.find(contact => contact.owner.toLowerCase() === WALLET_CUSTOMER_ADDRESS.toLowerCase())) {
throw new Error('Customer was not found in the list of contacts of the Admin')
}
//
// Send email to customer
// See https://documentation-tools.vercel.app/tools/web3mail/methods/sendEmail.html
//
console.log('[ADMIN] Sending email...')
const sendEmailResponse = await web3Mail.sendEmail({
emailSubject: 'Hello!',
emailContent: 'This is a test email sent through Web3Mail!',
protectedData: protectedData.address,
contentType: 'text/html',
senderName: 'Nemeos',
})
// const sendEmailResponse = {
// taskId: '0x4ccf29c705b9cc83475a7ae17a1a348acd528844f532e188d665cc282d3c06d2',
// }
console.log({ sendEmailResponse }, '[ADMIN] Sent email')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment