Overview
Stores provide encrypted block storage, enabling simple file uploading and downloading.
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.
Info
Store publicMeta
and privateMeta
fields support any kind of data formats encoded to byte arrays. Examples in this section use kotlinx-serialization-json-jvm
dependency for Kotlin and com.google.code.gson:gson
for Java to serialize object to JSON format.
Working with Stores
To access Store methods, get the field storeApi
from active connection. Connection should be initialized with Modules.STORE
passed to PrivmxEndpoint()
.
val storeApi = endpointSession.storeApi
Creating Stores
Creating a basic, unnamed Store, which can act as an encrypted data container:
val users : List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey),
UserWithPubKey(user2Id, user2PublicKey)
)
val managers : List<UserWithPubKey> = listOf(
UserWithPubKey(user1Id, user1PublicKey)
)
val publicMeta = ByteArray(0)
val privateMeta = ByteArray(0)
val storeId = storeApi.createStore(
contextId,
users,
managers,
publicMeta,
privateMeta
)
Getting Stores
Define Store item class with decoded publicMeta
and privateMeta
:
data class StoreItem(
val store: Store,
val decodedPrivateMeta: String,
val decodedPublicMeta: StorePublicMeta
)
Fetching the most recent Stores in given Context:
val startIndex = 0L
val pageSize = 100L
val storesPagingList = storeApi.listStores(
contextId,
startIndex,
pageSize,
SortOrder.DESC
)
val stores = storesPagingList.readItems.map {
StoreItem(
it,
it.privateMeta.decodeToString(),
Json.decodeFromString(it.publicMeta.decodeToString())
)
}
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
val storeID = "STORE_ID"
val store: Store = storeApi.getStore(storeID)
val users = store
.users
.map { userId ->
//Your application must provide a way,
//to get user's public key from their userId.
UserWithPubKey(
userId,
"USER_PUBLIC_KEY"
)
}
val managers = store
.managers
.map { userId ->
//Your application must provide a way,
//to get user's public key from their userId.
UserWithPubKey(
userId,
"USER_PUBLIC_KEY"
)
}
val newStoreNameAsPrivateMeta = "New store name"
storeApi.updateStore(
storeID,
users,
managers,
store.publicMeta,
newStoreNameAsPrivateMeta.encodeToByteArray(),
store.version,
false
)