Skip to main content

Work in progress

Swift is not up to date with the core documentation. Some of the features you've seen described in other parts of the documentation might not be mentioned here. Those changes do not influence compatibility, however

Overview

Stores provide encrypted block storage, enabling simple file uploading and downloading. Before working with Stores, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.

Info

Before working with Stores, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.

The sample code on this page is based on the initial assumptions.

All policies in the following examples can be build with Policy Builders, according to Policies overview.

publicMeta and privateMeta fields in Stores support any kind of data formats encoded to byte arrays. Examples in this section use JSON format serialization, which is available directly in Swift.

Working with Stores

To access Store methods, get the field storeApi from active connection. Connection should be initialized with [.store] passed to PrivmxEndpoint.

privmxSession.storeApi?.... // instance of StoreApi

Assumptions

In further examples, the following structures are used:

struct StorePublicMeta:Codable{
let tags: [String]
}

Creating Stores

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

let contextId = "CONTEXT_ID"

let users = [privmx.endpoint.core.UserWithPubKey]() //should be prepared by developer
let managers = [privmx.endpoint.core.UserWithPubKey]() //should be prepared by developer
let publicMeta = Data()
let privateMeta = Data()

let storeId = try? endpointSession?.storeApi?
.createStore(in: contextId,
for: users,
managedBy: managers,
withPublicMeta: publicMeta,
withPrivateMeta: privateMeta,
withPolicies: nil)

Getting Stores

Fetching the most recent Stores in given Context:

var startIndex:Int64 = 0
var pageSize:Int64 = 100

let contextId = "CONTEXT_ID"


guard let pagingList = try? endpointSession?.storeApi?
.listStores(
from:contextId,
basedOn: .init(skip: startIndex, limit: pageSize, sortOrder: .asc)) else {return}

let stores =
pagingList.readItems.map { $0 }

Managing 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)
  • Store's current version
  • true if update action should be forced
 guard let store = try? endpointSession?.storeApi?
.getStore(storeId) else {return}

//users list to be extracted from store
let users = store.users
.map{
//Your application must provide a way,
//to get user's public key from their userId.
privmx.endpoint.core.UserWithPubKey(userId: $0, pubKey: "PUB")
}
//managers list to be extracted from store
let managers = store.managers.map{
//Your application must provide a way,
//to get user's public key from their userId.
privmx.endpoint.core.UserWithPubKey(userId: $0, pubKey: "PUB")
}

guard let newPrivateMeta = "New store name".data(using: .utf8) else {return}

_ = try? endpointSession?.storeApi?.updateStore(
storeId,
atVersion: store.version,
replacingUsers: users,
replacingManagers: managers,
replacingPublicMeta: store.publicMeta.getData() ?? Data(),
replacingPrivateMeta: newPrivateMeta,
force:false,
forceGenerateNewKey: false,
replacingPolicies:nil)