Overview
Inboxes are a way for users to send encrypted data to your app.
The advantage of Inboxes is that they don't require the user to be registered in order to send encrypted data. This opens up various possibilities in environments where you need to gather sensitive information from unregistered users (e.g. on your website).
To learn more about Inboxes, their structure, how they are encrypted, and what are their use cases - read Inbox Docs.
Info
Before working with Inboxes, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.
Working with Inboxes
When working with Inboxes, you will use the following:
context.inboxes
- for methods used to manage Inboxes in given Context;context.inbox("INBOX_ID")
- for methods used to work with a specific Inbox. This includes getting/updating info, fetching entries and attached files.
Creating an Inbox
Creating a basic unnamed Inbox, which can be used if your app needs only one Inbox and you don't need a distinction between them:
const users = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }, { userId: 'MY_USER2', pubKey: 'PUB_KEY2' }];
const managers = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }];
const connection = await Endpoint.connection().inboxes.new({
contextId: 'CONTEXT_ID',
managers: managers,
users: users
});
Fetching Inboxes
Fetching the most recent Inboxes in given Context:
const inboxList = await Endpoint.connection().inboxes.list({ contextId: 'CONTEXT_ID' });
const inboxes = inboxList.readItems.map(inbox => {
return {
...inbox,
privateMeta: deserializeObject(inbox.privateMeta),
publicMeta: deserializeObject(inbox.publicMeta)
};
});
Remember that data is transmitted in binary format, so you'll need to parse it back into your desired format.
You can use the deserializeObject
function provided by the SDK for this purpose.
Modifying Inboxes
To update an Inbox you must always provide its current version, as well as:
- list of users,
- list of managers,
- new private and public meta (even if it didn't change).
const inboxInfo = await Endpoint.connection().inbox('INBOX_ID').info();
const newUsers = inboxInfo.users.map(user => ({
//Your application must provide a way,
//to get user's public key from their userId.
userId: user,
pubKey: 'USER_PUBLIC_KEY'
})
);
const newManagers = newUsers.filter(user =>
inboxInfo.managers.find(manager => manager == user.userId));
const newPrivateMeta = {
title: 'New Inbox name'
};
await Endpoint.connection().inbox('INBOX_ID').update({
publicMeta: inboxInfo.publicMeta,
version: inboxInfo.version,
users: newUsers,
managers: newManagers,
privateMeta: serializeObject(newPrivateMeta)
});