0

I have a Realm class in React Native which defines my database. I want to use encryption but the key is retrieved from the Keystore.

RealmService.js

function getSecret() {
    // retrieve key
    // return value
    return "example";
}

export default new Realm({path: 'db.realm', schema: [Auth, Wiretransfer], encryptionKey: getSecret()});

The problem is that I get 0 errors but when opening the Realm db in Realm Studio the file is not encrypted. So I assume it is because the encryptionKey value is null at creation.

How can I use the return value of a function in my export default?

Niel
  • 131
  • 1
  • 11

1 Answers1

0

Try using named exports instead:

const realmInstance = new Realm({path: 'db.realm', schema: [Auth, Wiretransfer], encryptionKey: getSecret()});
export { realmInstance };

Take a look at this answer explaining why it's bad to use export default new

Moad Ennagi
  • 1,058
  • 1
  • 11
  • 19
  • I'm using a named export now but still have the same issue. The key is being logged to console and Realm doesn't throw an error, yet it's not encrypted. – Niel Apr 04 '19 at 09:18
  • What does your `getSecret()` return ? What data type ? a string ? According to [Realm Doc] (https://realm.io/docs/javascript/2.16.0/api/Realm.html#~Configuration), `encryptionKey` has to be either an `ArrayBuffer` or `ArrayBufferView` – Moad Ennagi Apr 04 '19 at 09:22