Skip to main content

buildfire.services.keyStore

The Key Store service provides a secure flow for creating, recovering, and changing user PIN codes, as well as generating and managing encrypted key pairs. The private key is never stored on Buildfire servers; instead, it is generated on-device and encrypted using a PIN-selected key before being uploaded.

Use this service to implement:

  • End-to-end encrypted user data
  • PIN-protected user flows
  • Secure key pair generation
  • Recovery of existing encrypted keys when switching devices
  • PIN reset flows that generate a fresh key pair
danger

The Key Store service is not available on HTTP domains.
Use HTTPS, the Buildfire Previewer, or production app builds.


How the Key Store Works (Architectural Overview)

Before using the methods, it's important to understand the underlying model:

Private Key

  • Generated on the user device.
  • Never stored on the server.
  • Must be stored locally by the developer.

Encrypted Private Key

  • The private key is encrypted with a key derived from the user's PIN.
  • Stored on the Buildfire server.
  • Only recoverable with the correct PIN.
  • Wrong PIN = cannot decrypt.

Public Key

  • Stored on the server and retrievable anytime.
  • Not sensitive.

PIN and Recovery Behavior

  • PIN ≠ password — it's a key used to decrypt the encrypted private key.
  • Buildfire does not know the PIN and cannot recover it.
  • If the user resets the PIN → a brand new key pair is generated.
  • Old encrypted content becomes permanently unreadable.

Requirements

Widget

Include keyStore.js right after buildfire.min.js

<head>
<!-- ... -->
<script src="../../../scripts/buildfire.min.js"></script>
<script src="../../../scripts/buildfire/services/keyStore/keyStore.js"></script>
</head>

Methods

initiateSetup(options, callback)

Begins the PIN creation and key pair setup flow.
After completion, developers must store the returned privateKey locally.

tip

To check whether the user already has a Key Store entry, call
buildfire.services.keyStore.get() with the default key.

buildfire.services.keyStore.initiateSetup(
{
allowCancel: true,
translations: {
screens: {
introView: { title: 'Enable Encryption' }
},
errorMessages: {
mismatchMessage: 'PIN codes don’t match.'
}
}
},
(err, result) => {
if (err) return console.error(err);
console.log('Setup complete', result);
}
);

Options

NameTypeRequiredDescriptionDefault
allowCancelbooleannoShows a cancel button.false
translationsobjectnoOverrides UI text.

Translations

The translations object is structured as follows:

translations: {
screens: {
introView: { /* ... */ },
entryPinView: { /* ... */ },
entryConfirmView: { /* ... */ }
},
errorMessages: { /* ... */ }
}

screens

introView

NameTypeDefault
titlestring"Enable Encryption"
descriptionstring"End-to-end encryption keeps your data private..."
infoNotestring"If you lose or reset your PIN, you'll lose access..."
primaryButtonTextstring"Create PIN"

entryPinView

NameTypeDefault
titlestring"Enter 6-digit PIN"
descriptionstring"Create a 6-digit PIN to protect your encrypted data..."
hintTextstring"Avoid simple combinations like 123456"
primaryButtonTextstring"Create PIN"

entryConfirmView

NameTypeDefault
titlestring"Verify entered PIN"
descriptionstring"Confirm your new 6-digit PIN..."
hintTextstring"Avoid simple combinations like 123456"
primaryButtonTextstring"Confirm PIN"
secondaryButtonTextstring"Back"

Callback

callback(err, data)

NameTypeDescription
errstringnull when successful
dataobjectKey pair data

Returned Data

NameTypeDescription
privateKeystringStore this locally (user device only)
publicKeystringPublic key
actionstring"initiate"
generatedAtdateTimestamp
Important

You must store the privateKey locally (e.g., secure device storage).
Without it, the user will be forced to enter their PIN again to decrypt it.


get(options, callback)

Retrieves metadata for an existing key pair (encrypted private key + public key).
Does not return the decrypted private key.

buildfire.services.keyStore.get({}, (err, keyPair) => {
if (err) return console.error(err);
console.log('Stored key pair info', keyPair);
});

Options

NameTypeRequiredDescriptionDefault
idstringnoKey pair identifier"default"

Returned Data

NameTypeDescription
encryptedPrivateKeystringStored encrypted private key
publicKeystringPublic key
saltstringsalt
keyPairIdstringIdentifier
generatedAtdateTimestamp

initiateRecovery(options, callback)

Used when a user:

  • switches devices
  • uses a PWA / different platform
  • clears the locally stored privateKey

The user enters their PIN to decrypt the encrypted private key stored on the server.

If the PIN is incorrect → decryption fails.
If the user chooses Reset PIN → A new key pair is generated and old encrypted content becomes unreadable.

tip

This method can be called when users change devices or use different platforms like PWAs.

caution

Resetting the PIN creates a new key pair.
All data encrypted with the old private key becomes permanently inaccessible.

buildfire.services.keyStore.initiateRecovery(
{ allowCancel: false },
(err, result) => {
if (err) return console.error(err);
console.log('Recovery completed', result);
}
);

Options

NameTypeDefault
allowCancelbooleanfalse
translationsobject

screens

recoverView
NameTypeRequiredDefault
titlestringno"Unlock Encrypted Content"
descriptionstringno"Enter your PIN to view encrypted content"
primaryButtonTextstringno"Unlock"
secondaryLinkTextstringno"Reset PIN"
resetConfirmView
NameTypeRequiredDefault
titlestringno"Reset PIN?"
descriptionstringno"Resetting your PIN will permanently remove access to all previously encrypted data. This action cannot be undone."
primaryButtonTextstringno"Reset Pin"
secondaryButtonTextstringno"Cancel"
resetPinEntryView
NameTypeRequiredDefault
titlestringno"Enter 6-digit PIN"
descriptionstringno"Enter a new 6-digit PIN to protect your new encryption key. You’ll use this new PIN to unlock encrypted content on all devices."
infoNotestringno"Resetting your PIN will permanently remove access to all previously encrypted content."
hintTextstringno"Avoid simple combinations like 123456"
primaryButtonTextstringno"Continue"
secondaryButtonTextstringno"Cancel"
resetPinEntryConfirmView
NameTypeRequiredDefault
titlestringno"Verify entered PIN"
descriptionstringno"Confirm your new 6-digit PIN. Resetting your PIN will permanently erase access to all previously encrypted data."
primaryButtonTextstringno"Reset PIN"
secondaryButtonTextstringno"Cancel"

Returned Data

note

The callback returns different data depending on whether the user successfully recovered their existing private key or reset their PIN and generated a new key pair.

When Recovery Succeeds

NameDescription
privateKeyRecovered private key
action"recovered"

When PIN Is Reset

NameDescription
privateKeyNew private key
publicKeyNew public key
action"reset"
generatedAtTimestamp

initiatePinChange(options, callback)

Used to change the user’s PIN without generating a new key pair.

User must:

  1. Enter current PIN
  2. Enter new PIN
  3. Confirm new PIN

All encrypted data remains accessible.

buildfire.services.keyStore.initiatePinChange(
{ allowCancel: true },
(err, result) => {
if (err) return console.error(err);
console.log('PIN change complete', result);
}
);

Options

NameTypeDefault
allowCancelbooleanfalse
translationsobject

screens

changeCurrentPinView
NameTypeRequiredDefault
titlestringno"Enter your current PIN"
descriptionstringno"Enter your current 6-digit PIN to confirm your identity before creating a new one."
primaryButtonTextstringno"Verify"
secondaryButtonTextstringno"Cancel"
changeNewPinView
NameTypeRequiredDefault
titlestringno"Enter New 6-Digit PIN"
descriptionstringno"Create a new PIN to protect your encrypted content. You’ll use it to unlock and access your encrypted data on any device."
hintTextstringno"Avoid simple combinations like 123456"
primaryButtonTextstringno"Continue"
secondaryButtonTextstringno"Cancel"
changeNewPinConfirmView
NameTypeRequiredDefault
titlestringno"Verify entered PIN"
descriptionstringno"Confirm your new 6-digit PIN to protect your encrypted content. You’ll use it to unlock and access your encrypted data on any device."
hintTextstringno"Avoid simple combinations like 123456"
primaryButtonTextstringno"Change PIN"
secondaryButtonTextstringno"Back"

Returned Data

NameDescription
publicKeyPublic key
action"change"
generatedAtTimestamp

Error Handling

You can override error message translations in any flow.

Error Messages

These properties belong in the translations.errorMessages object.

NameTypeDescription
mismatchMessagestringPIN codes don’t match.
incorrectPinMessagestringIncorrect PIN.
genericErrorstringSomething went wrong.

Because privateKey is sensitive, use one of these recommended secure options: