🥯Bagel

Secure Storage

How Bagel protects your private keys

Secure Storage Implementation

Bagel implements a robust secure storage system for sensitive cryptographic keys to ensure your funds remain private and under your control.

Security Architecture

PIN-Based Encryption

Bagel uses strong encryption to protect your sensitive data:

  • All sensitive keys are encrypted using AES-GCM with 256-bit keys
  • Encryption keys are derived from your PIN code using PBKDF2 with 100,000 iterations
  • Each encrypted item includes its own salt and initialization vector (IV) for stronger security
  • All cryptographic operations use the Web Crypto API for secure implementation

Key Separation and Storage

Bagel uses a layered approach to key storage:

  • Private Keys: Viewing and spending private keys are stored encrypted in IndexedDB
  • In-Memory Session: Decrypted keys are held in memory only during active sessions
  • Multiple Wallets: Support for multiple wallets in a single application instance
  • Secure Registry: Encrypted registry of all managed wallets with metadata

Auto-Lock Mechanism

Bagel implements automatic protection of your keys:

  • Wallet automatically locks after 10 minutes of inactivity
  • All in-memory keys are purged when locked
  • Requires PIN code re-entry to access keys again
  • Manual lock option for immediate security

Key Recovery

Bagel ensures you never lose access to your funds:

  • Keys can be deterministically re-derived from the original wallet signature
  • No risk of permanent loss if secure storage is reset, as long as the main wallet is accessible

Implementation Details

Storage Structure

Bagel uses IndexedDB with multiple object stores:

  • encryptedKeys - Stores the encrypted key data
  • cryptoMetadata - Stores information needed for decryption
  • wallet-registry - Manages multiple wallets within the application
  • app-settings - Stores non-sensitive application settings

Encryption Process

  1. Key Derivation

    // Derive an encryption key from a password using PBKDF2
    async function deriveKey(
      password: string,
      salt: Uint8Array,
      iterations: number = 100000,
      hash: string = "SHA-256",
    ): Promise<CryptoKey>
  2. Data Encryption

    // Encrypt data with the derived key
    async function encryptData(
      data: string,
      key: CryptoKey,
      iv: Uint8Array,
    ): Promise<ArrayBuffer>
  3. Secure Storage

    // Store sensitive data securely
    export async function secureStoreData(
      id: string,
      data: string,
      password: string,
    ): Promise<void>

Multi-Wallet Support

Bagel allows you to manage multiple wallets in a single instance:

  • Each wallet has a unique ID
  • Wallets can be added, removed, and switched
  • All wallet metadata is stored in an encrypted registry
  • Example:
    // Interface for wallet metadata
    export interface WalletMetadata {
      id: string;           // Unique wallet identifier
      name: string;         // User-friendly wallet name
      createdAt: number;    // Timestamp of creation
      lastAccessed: number; // Timestamp of last access
      walletAddress: string; // Public wallet address
    }

User Experience

  • Secure Onboarding: Users set a PIN code during initial wallet setup
  • Unlock Flow: PIN code is required to decrypt and access private keys
  • Session Management: Timeout-based auto-locking for security
  • Manual Locking: Users can manually lock their wallet for additional security

This implementation ensures sensitive key material is never exposed in plaintext storage, balancing security with usability through temporary in-memory caching of unlocked keys during active sessions and simple PIN-based authentication.

On this page