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
Store publicMeta
and privateMeta
fields 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
.
let privMXEndpoint = try? await endpointContainer.newEndpoint(enabling: [.store], connectingAs: privateKey, to: solutionId, on: platformURL)
guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}
privMXEndpoint.storeApi?.... // instance of StoreApi
Creating Stores
Creating a basic, unnamed Store, which can act as an encrypted data container:
let contextId = "CONTEXT_ID"
guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}
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? privMXEndpoint.storeApi?
.createStore(in: contextId,
for: users,
managedBy: managers,
withPublicMeta: publicMeta,
withPrivateMeta: privateMeta)
Getting Stores
Fetching the most recent Stores in given Context:
var startIndex:Int64 = 0
var pageSize:Int64 = 100
let contextId = "CONTEXT_ID"
guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}
guard let pagingList = try? privMXEndpoint.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
var storeId = "STORE_ID"
guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}
guard let store = try? privMXEndpoint.storeApi?
.getStore(storeId) else {return}
//users list to be extracted from store
let users = store.users
.filter{
$0 != "username-to-remove"
}
.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? privMXEndpoint.storeApi?.updateStore(
storeId,
atVersion: store.version,
replacingUsers: users,
replacingManagers: managers,
replacingPublicMeta: store.publicMeta.getData() ?? Data(),
replacingPrivateMeta: newPrivateMeta,
force:false,
forceGenerateNewKey: false)