Skip to main content
js

Overview

Stores provide encrypted block storage, enabling simple file uploading and downloading.

Info

Before working with Stores, follow our Getting Started Guide. It shows how to set up a project to work with PrivMX Bridge.

Working with Stores

When working with Stores, you will use the following:

  • connection().stores - provides methods used to manage Stores in given Context;
  • connection().store("STORE_ID") - provides methods used to work with a specific Store. This includes getting/updating info, uploading and downloading files.

Creating Stores

Creating a basic unnamed Store, which can act as an encrypted block container:

const users = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }, { userId: 'MY_USER2', pubKey: 'PUB_KEY2' }];
const managers = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }];

const storeId = await Endpoint.connection().stores.new({
contextId: 'CONTEXT_ID',
users,
managers
});

Fetching Stores

Fetching the most recent Stores in given Context:

const context = Endpoint.connection();
const storesList = await Endpoint.connection().stores.list({
contextId: 'CONTEXT_ID'
});

const parsedStores = storesList.readItems.map(store => {
return {
...store,
privateMeta: deserializeObject(store.privateMeta),
publicMeta: deserializeObject(store.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 Stores

To update a Store 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 store = Endpoint.connection().store('STORE_ID');
const storeInfo = await store.info();

const newUsers = storeInfo.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 =>
storeInfo.managers.find(manager => manager == user.userId));

const newPrivateMeta = {
title: 'New Store name'
};

await store.update({
publicMeta: storeInfo.publicMeta,
version: storeInfo.version,
users: newUsers,
managers: newManagers,
privateMeta: serializeObject(newPrivateMeta)
});