Skip to main content
java

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 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().

StoreApi storeApi = connection.storeApi;

Creating Stores

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

val contextId = "CONTEXT_ID"
val users : List<UserWithPubKey> = listOf() // users to add to the Store
val managers : List<UserWithPubKey> = listOf() // managers to add to the Store
val storeApi = connection.storeApi

val storeId = storeApi.createStore(
contextId,
users,
managers,
ByteArray(0),
ByteArray(0)
)

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 contextId = "CONTEXT_ID"
val startIndex = 0L
val pageSize = 100L
val storeApi = connection.storeApi

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 = connection.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"
val storeApi = connection.storeApi
storeApi.updateStore(
storeID,
users,
managers,
store.publicMeta,
newStoreNameAsPrivateMeta.encodeToByteArray(),
store.version,
false
)