diff --git a/src/js_realm.cpp b/src/js_realm.cpp index e8204eef..0bca0d23 100644 --- a/src/js_realm.cpp +++ b/src/js_realm.cpp @@ -239,10 +239,16 @@ static const JSStaticValue RealmStaticProperties[] = { JSValueRef RealmSchemaVersion(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { - RJSValidateArgumentCount(argumentCount, 1); + RJSValidateArgumentRange(argumentCount, 1, 2); Realm::Config config; config.path = RJSNormalizePath(RJSValidatedStringForValue(ctx, arguments[0])); + if (argumentCount == 2) { + JSValueRef encryptionKeyValue = arguments[1]; + std::string encryptionKey = RJSAccessor::to_binary(ctx, encryptionKeyValue); + config.encryption_key = std::vector(encryptionKey.begin(), encryptionKey.end()); + } + auto version = Realm::get_schema_version(config); if (version == ObjectStore::NotVersioned) { return JSValueMakeNumber(ctx, -1); diff --git a/tests/js/realm-tests.js b/tests/js/realm-tests.js index a94a3a70..465ec0c5 100644 --- a/tests/js/realm-tests.js +++ b/tests/js/realm-tests.js @@ -125,6 +125,17 @@ module.exports = BaseTest.extend({ realm = new Realm({schema: [], schemaVersion: 2, path: 'another.realm'}); TestCase.assertEqual(Realm.schemaVersion('another.realm'), 2); + + var encryptionKey = new Int8Array(64); + realm = new Realm({schema: [], schemaVersion: 3, path: 'encrypted.realm', encryptionKey: encryptionKey}); + TestCase.assertEqual(Realm.schemaVersion('encrypted.realm', encryptionKey), 3); + + TestCase.assertThrows(function() { + Realm.schemaVersion('encrypted.realm', encryptionKey, 'extra'); + }); + TestCase.assertThrows(function() { + Realm.schemaVersion('encrypted.realm', 'asdf'); + }); }, testRealmCreate: function() {