-
Notifications
You must be signed in to change notification settings - Fork 16
Add Room encryption docs for Kotlin SDK #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |||||
| The PowerSync SDK supports [SQLite3MultipleCiphers](https://utelle.github.io/SQLite3MultipleCiphers) | ||||||
| on all native platforms and the web, which can be used to encrypt databases. | ||||||
|
|
||||||
| <Note>Setting up encryption has changed in version 2.0 of the PowerSync SDK. When upgrading, follow these steps and remove dependencies on `powersync_sqlcipher`.</Note> | ||||||
|
|
||||||
| To enable encryption, pass an instance of `EncryptionOptions` to a `PowerSyncDatabase` constructor: | ||||||
|
|
||||||
|
|
@@ -87,7 +87,7 @@ | |||||
|
|
||||||
| 2. Add a dependency on `com.powersync:sqlite3multipleciphers`. | ||||||
|
|
||||||
| 3. Since `:core` includes a Ktor client implementation, you'll need to [add one manually](https://ktor.io/docs/client-engines.html) if you're not already using Ktor: | ||||||
|
Check warning on line 90 in client-sdks/advanced/data-encryption.mdx
|
||||||
| - Android/JVM: `io.ktor:ktor-client-okhttp` | ||||||
| - Apple targets (Kotlin/Native): `io.ktor:ktor-client-darwin` | ||||||
|
|
||||||
|
|
@@ -128,6 +128,36 @@ | |||||
| </Note> | ||||||
|
|
||||||
| For more details, see the [`sqlite3multipleciphers` README](https://github.com/powersync-ja/powersync-kotlin/tree/main/sqlite3multipleciphers) in the PowerSync Kotlin SDK repository. | ||||||
|
|
||||||
| **Using encryption with Room:** | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good to me. But I wonder if we should put this as a section in the guide on using Room, since:
So I think it might be better to move this to the page on Room, and add a link from here to that section perhaps.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. Will do that |
||||||
|
|
||||||
| When using PowerSync [with Room](/client-sdks/orms/kotlin/room), PowerSync wraps the Room database instead of opening its own connection. To use an encrypted database with Room, wrap an encrypted `PersistentConnectionFactory` in a `SQLiteDriver` and pass it to `RoomDatabase.Builder.setDriver`: | ||||||
|
|
||||||
| ```kotlin | ||||||
| class PowerSyncConnectionFactoryAsSqliteDriver( | ||||||
| private val powersyncFactory: PersistentConnectionFactory | ||||||
| ) : SQLiteDriver { | ||||||
| override fun open(fileName: String): SQLiteConnection { | ||||||
| if (fileName == ":memory:") { | ||||||
| return powersyncFactory.openInMemoryConnection() | ||||||
| } | ||||||
|
|
||||||
| return powersyncFactory.openConnection(fileName, null, false) | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Depending on your target platform, pass an `AndroidEncryptedDatabaseFactory`, `JavaEncryptedDatabaseFactory`, or `NativeEncryptedDatabaseFactory` to the wrapper. The factory automatically installs the SQLite3MultipleCiphers extension — no manual setup required. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sqlite3mc is a fork, not an extension. But we should mention that the PowerSync extension gets loaded automatically, since it's a manual step with the default Room setup.
Suggested change
|
||||||
|
|
||||||
| ```kotlin | ||||||
| val driver = PowerSyncConnectionFactoryAsSqliteDriver( | ||||||
| AndroidEncryptedDatabaseFactory(context, Key.Passphrase("your encryption key")) | ||||||
| ) | ||||||
|
|
||||||
| val roomDb = Room.databaseBuilder<MyDatabase>(context, "your_database") | ||||||
| .setDriver(driver) | ||||||
| .build() | ||||||
| ``` | ||||||
| </Accordion> | ||||||
|
|
||||||
| <Accordion title="Swift" icon="swift"> | ||||||
|
|
@@ -138,9 +168,9 @@ | |||||
| The PowerSync Swift SDK depends on [CSQLite](https://github.com/powersync-ja/CSQLite) to build and link SQLite. | ||||||
| That package can be configured to optionally link SQLite3 Multiple Ciphers by enabling the `Encryption` trait. Due to SwiftPM limitations, we can't directly expose that trait on the Swift SDK. | ||||||
|
|
||||||
| Instead, we recommend directly depending on CSQLite with the encryption trait, which will enable the same for the SDK (since each package can only appear in a build once). Since Xcode doesn't support specifying package traits when adding dependencies, you first need to add a local Swift package as a workaround. | ||||||
|
|
||||||
| 1. Create a local `Package.swift` in your project that depends on CSQLite with the `Encryption` trait: | ||||||
|
|
||||||
| ```swift | ||||||
| // swift-tools-version: 6.2 | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "powersync-docs", | ||
| "version": "1.0.0", | ||
| "scripts": { | ||
| "dev": "mintlify dev" | ||
| }, | ||
| "devDependencies": { | ||
| "mintlify": "^4.2.520" | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably enable corepack as well, to ensure pnpm is used consistently. |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love not having to install mintlify as a global tool 👍 To ensure this works, maybe we should also update the workflow in
.github/workflows/check.yaml(we can even cache pnpm dependencies automatically after upgradingactions/setup-node).