Skip to main content
Encrypt and decrypt private keys with password-based protection.
import { KeystoreService, KeystoreLive } from 'voltaire-effect/crypto'
import { Effect } from 'effect'

const result = await Effect.runPromise(
  Effect.gen(function* () {
    const keystore = yield* KeystoreService
    const encrypted = yield* keystore.encrypt(privateKey, 'password', { kdf: 'scrypt', n: 262144 })
    const decrypted = yield* keystore.decrypt(encrypted, 'password')
    return { encrypted, decrypted }
  }).pipe(Effect.provide(KeystoreLive))
)

Error Handling

program.pipe(
  Effect.catchTag('InvalidMacError', () => Effect.fail('Wrong password')),
  Effect.catchTag('UnsupportedVersionError', () => Effect.fail('Version not supported')),
  Effect.catchTag('UnsupportedKdfError', () => Effect.fail('KDF not supported'))
)

Testing

import { KeystoreTest } from 'voltaire-effect/crypto'
myProgram.pipe(Effect.provide(KeystoreTest))
// Mock keystore without crypto overhead

Interface

interface KeystoreServiceShape {
  readonly encrypt: (privateKey: PrivateKeyType, password: string, options?: EncryptOptions) => Effect.Effect<KeystoreV3, EncryptionError>
  readonly decrypt: (keystore: KeystoreV3, password: string) => Effect.Effect<PrivateKeyType, DecryptError>
}