Skip to content

Conversation

@NiKrause
Copy link

Problem

When opening an encrypted OrbitDB database without encryption options, OrbitDB doesn't throw errors or emit error events. This makes it difficult to detect if a database is encrypted and needs a password, especially when opening databases via URL.

Solution

Added isDatabaseEncrypted() function that detects encrypted databases by checking if entries exist but their values are undefined:

  • Encrypted DB without encryption: entries have hashes but values are undefined (encrypted data cannot be read)
  • Unencrypted DB: entries have hashes AND readable values
  • Empty DB: returns false (cannot determine)

This enables applications (like simple-todo) to detect encrypted databases and prompt for password when needed.

Changes

  • Added isDatabaseEncrypted() function to src/index.js
  • Added comprehensive tests verifying the detection logic
  • Test covers encrypted, unencrypted, and empty database scenarios

Usage

import SimpleEncryption, { isDatabaseEncrypted } from '@orbitdb/simple-encryption'

// Try opening database without encryption
const db = await orbitdb.open(address, {})

// Check if it's encrypted
const isEncrypted = await isDatabaseEncrypted(db)

if (isEncrypted) {
  // Show password modal
  const password = await promptForPassword()
  const encryption = await SimpleEncryption({ password })
  await db.close()
  const encryptedDb = await orbitdb.open(address, { encryption: { data: encryption } })
}

…tabases

Problem:
- When opening an encrypted OrbitDB database without encryption options,
  OrbitDB doesn't throw errors or emit error events
- This makes it difficult to detect if a database is encrypted and needs
  a password, especially when opening databases via URL

Solution:
- Added isDatabaseEncrypted() function that detects encrypted databases
  by checking if entries exist but their values are undefined
- When opening encrypted DB without encryption: entries have hashes but
  values are undefined (encrypted data cannot be read)
- When opening unencrypted DB: entries have hashes AND readable values
- This enables applications to detect encrypted databases and prompt for
  password when needed

Changes:
- Added isDatabaseEncrypted() function to src/index.js
- Added comprehensive tests verifying the detection logic
- Test covers encrypted, unencrypted, and empty database scenarios
@NiKrause
Copy link
Author

the current solution only works for data encryption not for replication encryption! I am going to think on another encryption detection for it! Please bare with me!

@NiKrause
Copy link
Author

NiKrause commented Dec 1, 2025

I added an encryption detection signal to the isDatabaseEncrypted utility function for replication encryption too!

Still looks more like a workaround then a correct solution. Anyhow, I am going to leave this here, if someone needs it or finds it useful.

Same time adding the missing issue: #2

@NiKrause
Copy link
Author

NiKrause commented Dec 6, 2025

closing because issue is more complex then thought initially

@NiKrause NiKrause closed this Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant