Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <hello@internxt.com>",
"version": "1.15.6",
"version": "1.15.7",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand All @@ -25,7 +25,8 @@
"build": "yarn clean && tsc",
"lint": "eslint ./src",
"format": "prettier --write **/*.{js,jsx,tsx,ts}",
"swagger": "openapi-typescript https://gateway.internxt.com/drive/api-json -o ./src/schema.ts && yarn format"
"swagger": "openapi-typescript https://gateway.internxt.com/drive/api-json -o ./src/schema.ts && yarn format",
"swagger:mail": "openapi-typescript http://localhost:3100/api-json -o ./src/mail/schema.ts && yarn format"
},
"devDependencies": {
"@internxt/eslint-config-internxt": "2.1.0",
Expand All @@ -51,4 +52,4 @@
"prettier --write"
]
}
}
}
148 changes: 148 additions & 0 deletions src/mail/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { ApiSecurity, ApiUrl, AppDetails } from '../shared';
import { headersWithToken } from '../shared/headers';
import { HttpClient } from '../shared/http/client';
import {
EncryptedKeystore,
KeystoreType,
HybridEncryptedEmail,
PwdProtectedEmail,
RecipientWithPublicKey,
base64ToUint8Array,
EmailPublicParameters,
} from 'internxt-crypto';
import {
MailboxResponse,
EmailListResponse,
EmailResponse,
EmailCreatedResponse,
SendEmailRequest,
DraftEmailRequest,
UpdateEmailRequest,
ListEmailsQuery,
} from './types';

export class MailApi {
private readonly client: HttpClient;
private readonly appDetails: AppDetails;
private readonly apiSecurity: ApiSecurity;
private readonly apiUrl: ApiUrl;

public static client(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) {
return new MailApi(apiUrl, appDetails, apiSecurity);
}

private constructor(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) {
this.client = HttpClient.create(apiUrl, apiSecurity.unauthorizedCallback);
this.appDetails = appDetails;
this.apiSecurity = apiSecurity;
this.apiUrl = apiUrl;
}

/**
* Uploads encrypted keystore to the server
*
* @param keystore - The encrypted keystore
* @returns Server response
*/
async uploadKeystore(keystore: EncryptedKeystore): Promise<void> {
return this.client.post(`${this.apiUrl}/keystore`, { encryptedKeystore: keystore }, this.headers());
}

/**
* Requests encrypted keystore from the server
*
* @param userEmail - The email of the user
* @param keystoreType - The type of the keystore
* @returns The encrypted keystore
*/
async downloadKeystore(userEmail: string, keystoreType: KeystoreType): Promise<EncryptedKeystore> {
return this.client.getWithParams(`${this.apiUrl}/user/keystore`, { userEmail, keystoreType }, this.headers());
}

/**
* Requests users with corresponding public keys from the server
*
* @param emails - The emails of the users
* @returns Users with corresponding public keys
*/
async getUsersWithPublicKeys(emails: string[]): Promise<RecipientWithPublicKey[]> {
const response = await this.client.post<{ publicKey: string; email: string }[]>(
`${this.apiUrl}/users/public-keys`,
{ emails },
this.headers(),
);

const result = await Promise.all(
response.map(async (item) => {
const publicHybridKey = base64ToUint8Array(item.publicKey);
return { email: item.email, publicHybridKey };
}),
);

return result;
}

/**
* Sends the encrypted emails to the server
*
* @param emails - The encrypted emails
* @param params - The public parameters of the email
* @returns Server response
*/
async sendE2EEmails(emails: HybridEncryptedEmail[], params: EmailPublicParameters): Promise<void> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabg2 It should be just sendEmail, no? Then we can add other functions that will send encrypted or password-protected emails

Copy link
Copy Markdown
Contributor Author

@xabg2 xabg2 Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the name to differentiate it from the other EPs that do not have the crypto layer )and they are not used rn in mail web). But we can change it in this PR if you want.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok, I've just thought that the next PR will add all crypto in the email, including new send methods. But at the end it will be the same

return this.client.post(`${this.apiUrl}/emails`, { emails, params }, this.headers());
}

/**
* Sends the password-protected email to the server
*
* @param email - The password-protected email
* @param params - The public parameters of the email
* @returns Server response
*/
async sendE2EPasswordProtectedEmail(email: PwdProtectedEmail, params: EmailPublicParameters): Promise<void> {
return this.client.post(`${this.apiUrl}/emails`, { email, params }, this.headers());
}

async getMailboxes(): Promise<MailboxResponse[]> {
return this.client.get(`${this.apiUrl}/email/mailboxes`, this.headers());
}

async listEmails(query?: ListEmailsQuery): Promise<EmailListResponse> {
return this.client.getWithParams(`${this.apiUrl}/email`, query ?? {}, this.headers());
}

async getEmail(id: string): Promise<EmailResponse> {
return this.client.get(`${this.apiUrl}/email/${id}`, this.headers());
}

async deleteEmail(id: string): Promise<void> {
return this.client.delete(`${this.apiUrl}/email/${id}`, this.headers());
}

async updateEmail(id: string, body: UpdateEmailRequest): Promise<void> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will this do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This EP will be used to mark an email as read/flagged.

return this.client.patch(`${this.apiUrl}/email/${id}`, body, this.headers());
}

async sendEmail(body: SendEmailRequest): Promise<EmailCreatedResponse> {
return this.client.post(`${this.apiUrl}/email/send`, body, this.headers());
}

async saveDraft(body: DraftEmailRequest): Promise<EmailCreatedResponse> {
return this.client.post(`${this.apiUrl}/email/drafts`, body, this.headers());
}

/**
* Returns the needed headers for the module requests
* @private
*/
private headers() {
return headersWithToken({
clientName: this.appDetails.clientName,
clientVersion: this.appDetails.clientVersion,
token: this.apiSecurity.token,
desktopToken: this.appDetails.desktopHeader,
customHeaders: this.appDetails.customHeaders,
});
}
}
File renamed without changes.
202 changes: 2 additions & 200 deletions src/mail/index.ts
Original file line number Diff line number Diff line change
@@ -1,200 +1,2 @@
import { ApiSecurity, ApiUrl, AppDetails } from '../shared';
import { headersWithToken } from '../shared/headers';
import { HttpClient } from '../shared/http/client';
import {
EncryptedKeystore,
KeystoreType,
HybridEncryptedEmail,
PwdProtectedEmail,
HybridKeyPair,
Email,
RecipientWithPublicKey,
base64ToUint8Array,
EmailPublicParameters,
} from 'internxt-crypto';

import { createKeystores, encryptEmail, passwordProtectAndSendEmail, openKeystore, recoverKeys } from './create';

export class Mail {
private readonly client: HttpClient;
private readonly appDetails: AppDetails;
private readonly apiSecurity: ApiSecurity;
private readonly apiUrl: ApiUrl;

public static client(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) {
return new Mail(apiUrl, appDetails, apiSecurity);
}

private constructor(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) {
this.client = HttpClient.create(apiUrl, apiSecurity.unauthorizedCallback);
this.appDetails = appDetails;
this.apiSecurity = apiSecurity;
this.apiUrl = apiUrl;
}

/**
* Uploads encrypted keystore to the server
*
* @param encryptedKeystore - The encrypted keystore
* @returns Server response
*/
async uploadKeystoreToServer(encryptedKeystore: EncryptedKeystore): Promise<void> {
return this.client.post(`${this.apiUrl}/keystore`, { encryptedKeystore }, this.headers());
}

/**
* Creates recovery and encryption keystores and uploads them to the server
*
* @param userEmail - The email of the user
* @param baseKey - The secret key of the user
* @returns The recovery codes and keys of the user
*/
async createAndUploadKeystores(
userEmail: string,
baseKey: Uint8Array,
): Promise<{ recoveryCodes: string; keys: HybridKeyPair }> {
const { encryptionKeystore, recoveryKeystore, recoveryCodes, keys } = await createKeystores(userEmail, baseKey);
await Promise.all([this.uploadKeystoreToServer(encryptionKeystore), this.uploadKeystoreToServer(recoveryKeystore)]);
return { recoveryCodes, keys };
}

/**
* Requests encrypted keystore from the server
*
* @param userEmail - The email of the user
* @param keystoreType - The type of the keystore
* @returns The encrypted keystore
*/
async downloadKeystoreFromServer(userEmail: string, keystoreType: KeystoreType): Promise<EncryptedKeystore> {
return this.client.getWithParams(`${this.apiUrl}/user/keystore`, { userEmail, keystoreType }, this.headers());
}

/**
* Requests encrypted keystore from the server and opens it
*
* @param userEmail - The email of the user
* @param baseKey - The secret key of the user
* @returns The hybrid keys of the user
*/
async getUserEmailKeys(userEmail: string, baseKey: Uint8Array): Promise<HybridKeyPair> {
const keystore = await this.downloadKeystoreFromServer(userEmail, KeystoreType.ENCRYPTION);
return openKeystore(keystore, baseKey);
}

/**
* Requests recovery keystore from the server and opens it
*
* @param userEmail - The email of the user
* @param recoveryCodes - The recovery codes of the user
* @returns The hybrid keys of the user
*/
async recoverUserEmailKeys(userEmail: string, recoveryCodes: string): Promise<HybridKeyPair> {
const keystore = await this.downloadKeystoreFromServer(userEmail, KeystoreType.RECOVERY);
return recoverKeys(keystore, recoveryCodes);
}

/**
* Request user with corresponding public keys from the server
*
* @param userEmail - The email of the user
* @returns User with corresponding public keys
*/
async getUserWithPublicKeys(userEmail: string): Promise<RecipientWithPublicKey> {
const response = await this.client.post<{ publicKey: string; email: string }[]>(
`${this.apiUrl}/users/public-keys`,
{ emails: [userEmail] },
this.headers(),
);
if (!response[0]) throw new Error(`No public keys found for ${userEmail}`);
const singleResponse = response[0];
const publicHybridKey = base64ToUint8Array(singleResponse.publicKey);
const result = { email: singleResponse.email, publicHybridKey };
return result;
}

/**
* Request users with corresponding public keys from the server
*
* @param emails - The emails of the users
* @returns Users with corresponding public keys
*/
async getSeveralUsersWithPublicKeys(emails: string[]): Promise<RecipientWithPublicKey[]> {
const response = await this.client.post<{ publicKey: string; email: string }[]>(
`${this.apiUrl}/users/public-keys`,
{ emails },
this.headers(),
);

const result = await Promise.all(
response.map(async (item) => {
const publicHybridKey = base64ToUint8Array(item.publicKey);
return { email: item.email, publicHybridKey };
}),
);

return result;
}

/**
* Sends the encrypted emails to the server
*
* @param emails - The encrypted emails
* @param params - The public parameters of the email (sender, recipients, CCs, BCCs, etc.)
* @returns Server response
*/
async sendEncryptedEmail(emails: HybridEncryptedEmail[], params: EmailPublicParameters): Promise<void> {
return this.client.post(`${this.apiUrl}/emails`, { emails, params }, this.headers());
}

/**
* Encrypts and sends email(s) to the server
*
* @param email - The message to encrypt
* @param aux - The optional auxilary data to encrypt together with the email (e.g. email sender)
* @returns Server response
*/
async encryptAndSendEmail(email: Email, aux?: string): Promise<void> {
const recipientEmails = email.params.recipients.map((user) => user.email);
const recipients = await this.getSeveralUsersWithPublicKeys(recipientEmails);

const encEmails = await encryptEmail(email, recipients, aux);
return this.sendEncryptedEmail(encEmails, email.params);
}

/**
* Sends the password-protected email to the server
*
* @param email - The password-protected email
* @returns Server response
*/
async sendPasswordProtectedEmail(email: PwdProtectedEmail, params: EmailPublicParameters): Promise<void> {
return this.client.post(`${this.apiUrl}/emails`, { email, params }, this.headers());
}

/**
* Creates the password-protected email and sends it to the server
*
* @param email - The email
* @param pwd - The password
* @param aux - The optional auxilary data to encrypt together with the email (e.g. email sender)
* @returns Server response
*/
async passwordProtectAndSendEmail(email: Email, pwd: string, aux?: string): Promise<void> {
const encEmail = await passwordProtectAndSendEmail(email, pwd, aux);
return this.sendPasswordProtectedEmail(encEmail, email.params);
}

/**
* Returns the needed headers for the module requests
* @private
*/
private headers() {
return headersWithToken({
clientName: this.appDetails.clientName,
clientVersion: this.appDetails.clientVersion,
token: this.apiSecurity.token,
desktopToken: this.appDetails.desktopHeader,
customHeaders: this.appDetails.customHeaders,
});
}
}
export * from './types';
export * from './mail';
Loading
Loading