Skip to content

Commit a70b351

Browse files
committed
Add support for remote encryption
This adds a new `remoteEncryptionKey` option to database options, which is used on keep data on server side encrypted.
1 parent 55d4bbd commit a70b351

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ class Database {
8585
const syncPeriod = opts?.syncPeriod ?? 0.0;
8686
const readYourWrites = opts?.readYourWrites ?? true;
8787
const offline = opts?.offline ?? false;
88-
this.db = databaseOpenWithSync(path, opts.syncUrl, authToken, encryptionCipher, encryptionKey, syncPeriod, readYourWrites, offline);
88+
const remoteEncryptionKey = opts?.remoteEncryptionKey ?? "";
89+
this.db = databaseOpenWithSync(path, opts.syncUrl, authToken, encryptionCipher, encryptionKey, syncPeriod, readYourWrites, offline, remoteEncryptionKey);
8990
} else {
9091
const authToken = opts?.authToken ?? "";
9192
const encryptionKey = opts?.encryptionKey ?? "";
9293
const timeout = opts?.timeout ?? 0.0;
93-
this.db = databaseOpen(path, authToken, encryptionCipher, encryptionKey, timeout);
94+
const remoteEncryptionKey = opts?.remoteEncryptionKey ?? "";
95+
this.db = databaseOpen(path, authToken, encryptionCipher, encryptionKey, timeout, remoteEncryptionKey);
9496
}
9597
// TODO: Use a libSQL API for this?
9698
this.memory = path === ":memory:";

promise.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ class Database {
8989
const encryptionKey = opts?.encryptionKey ?? "";
9090
const syncPeriod = opts?.syncPeriod ?? 0.0;
9191
const offline = opts?.offline ?? false;
92-
this.db = databaseOpenWithSync(path, opts.syncUrl, authToken, encryptionCipher, encryptionKey, syncPeriod, offline);
92+
const remoteEncryptionKey = opts?.remoteEncryptionKey ?? "";
93+
this.db = databaseOpenWithSync(path, opts.syncUrl, authToken, encryptionCipher, encryptionKey, syncPeriod, offline, remoteEncryptionKey);
9394
} else {
9495
const authToken = opts?.authToken ?? "";
9596
const encryptionKey = opts?.encryptionKey ?? "";
9697
const timeout = opts?.timeout ?? 0.0;
97-
this.db = databaseOpen(path, authToken, encryptionCipher, encryptionKey, timeout);
98+
const remoteEncryptionKey = opts?.remoteEncryptionKey ?? "";
99+
this.db = databaseOpen(path, authToken, encryptionCipher, encryptionKey, timeout, remoteEncryptionKey);
98100
}
99101
// TODO: Use a libSQL API for this?
100102
this.memory = path === ":memory:";

src/database.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ impl Database {
3636
let encryption_cipher = cx.argument::<JsString>(2)?.value(&mut cx);
3737
let encryption_key = cx.argument::<JsString>(3)?.value(&mut cx);
3838
let busy_timeout = cx.argument::<JsNumber>(4)?.value(&mut cx);
39+
let remote_encryption_key = cx.argument::<JsString>(5)?.value(&mut cx);
3940
let db = if is_remote_path(&db_path) {
40-
let version = version("remote");
4141
trace!("Opening remote database: {}", db_path);
42-
libsql::Database::open_remote_internal(db_path.clone(), auth_token, version)
42+
let mut builder = libsql::Builder::new_remote(db_path.clone(), auth_token);
43+
if !remote_encryption_key.is_empty() {
44+
let encryption_context = libsql::EncryptionContext {
45+
key: libsql::EncryptionKey::Base64Encoded(remote_encryption_key),
46+
};
47+
builder = builder.remote_encryption(Some(encryption_context));
48+
}
49+
rt.block_on(builder.build())
4350
} else {
4451
let cipher = libsql::Cipher::from_str(&encryption_cipher).or_else(|err| {
4552
throw_libsql_error(
@@ -76,7 +83,7 @@ impl Database {
7683
let sync_period = cx.argument::<JsNumber>(5)?.value(&mut cx);
7784
let read_your_writes = cx.argument::<JsBoolean>(6)?.value(&mut cx);
7885
let offline = cx.argument::<JsBoolean>(7)?.value(&mut cx);
79-
86+
let remote_encryption_key = cx.argument::<JsString>(8)?.value(&mut cx);
8087
let cipher = libsql::Cipher::from_str(&encryption_cipher).or_else(|err| {
8188
throw_libsql_error(
8289
&mut cx,
@@ -103,7 +110,17 @@ impl Database {
103110
);
104111
let rt = runtime(&mut cx)?;
105112
let result = if offline {
106-
rt.block_on(libsql::Builder::new_synced_database(db_path, sync_url, sync_auth).build())
113+
rt.block_on(async {
114+
let mut builder =
115+
libsql::Builder::new_synced_database(db_path, sync_url, sync_auth);
116+
if !remote_encryption_key.is_empty() {
117+
let encryption_context = libsql::EncryptionContext {
118+
key: libsql::EncryptionKey::Base64Encoded(remote_encryption_key),
119+
};
120+
builder = builder.remote_encryption(Some(encryption_context));
121+
}
122+
builder.build().await
123+
})
107124
} else {
108125
rt.block_on(async {
109126
let mut builder = libsql::Builder::new_remote_replica(db_path, sync_url, sync_auth);
@@ -113,6 +130,12 @@ impl Database {
113130
if let Some(sync_period) = sync_period {
114131
builder = builder.sync_interval(sync_period);
115132
}
133+
if !remote_encryption_key.is_empty() {
134+
let encryption_context = libsql::EncryptionContext {
135+
key: libsql::EncryptionKey::Base64Encoded(remote_encryption_key),
136+
};
137+
builder = builder.remote_encryption(Some(encryption_context));
138+
}
116139
builder.build().await
117140
})
118141
};

0 commit comments

Comments
 (0)