PrivMX DOCS
Kotlin

Overview

KVDBs provide encrypted key-value databases.

Before working with KVDBs, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge. Sample code on this page is based on the initial assumptions.

Working with KVDBs

To access KVDBs methods, get the field kvdbApi from active connection. Connection should be initialized with Modules.KVDB passed to PrivmxEndpoint().

val kvdbApi: KvdbApi = endpointSession.kvdbApi

Creating KVDBs

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 kvdbId = kvdbApi.createKvdb(
    contextId,
    users,
    managers,
    publicMeta,
    privateMeta
)

Getting KVDBs

Define KVDB item class with decoded publicMeta and privateMeta:

data class KvdbItem(
    val kvdb: Kvdb,
    val decodedPrivateMeta: String,
    val decodedPublicMeta: KvdbPublicMeta
)

Fetching the most recent KVDBs in given Context:

val startIndex = 0L
val pageSize = 100L

val kvdbsPagingList = kvdbApi.listKvdbs(
    contextId,
    startIndex,
    pageSize,
    SortOrder.DESC
)
val kvdbs = kvdbsPagingList.readItems.map {
    KvdbItem(
        it,
        it.privateMeta.decodeToString(),
        Json.decodeFromString(it.publicMeta.decodeToString())
    )
}

Managing KVDBs

To update a KVDB 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)
  • KVDB's current version
  • true if update action should be forced
val kvdbId = "KVDB_ID"
val kvdb: Kvdb = kvdbApi.getKvdb(kvdbId)
val users = kvdb
    .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 = kvdb
    .managers
    .map { userId ->
        //Your application must provide a way,
        //to get user's public key from their userId.
        UserWithPubKey(
            userId,
            "USER_PUBLIC_KEY"
        )
    }
val newKvdbNameAsPrivateMeta = "New KVDB name"

kvdbApi.updateKvdb(
    kvdb.kvdbId,
    users,
    managers,
    kvdb.publicMeta,
    newKvdbNameAsPrivateMeta.encodeToByteArray(),
    kvdb.version!! + 1,
    false
)

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

On this page