browser-sync-handler.ts raw

   1  /* eslint-disable @typescript-eslint/no-explicit-any */
   2  import {
   3    EncryptedVault,
   4    StoredCashuMint,
   5    StoredIdentity,
   6    StoredNwcConnection,
   7    StoredPermission,
   8    StoredRelay,
   9  } from './types';
  10  
  11  /**
  12   * This class handles the data that is synced between browser instances.
  13   * In addition to the sensitive data that is encrypted, it also contains
  14   * some unencrypted properties (like, version and the vault hash).
  15   */
  16  export abstract class BrowserSyncHandler {
  17    get encryptedVault(): EncryptedVault | undefined {
  18      return this.#encryptedVault;
  19    }
  20  
  21    /** @deprecated Use encryptedVault instead */
  22    get browserSyncData(): EncryptedVault | undefined {
  23      return this.#encryptedVault;
  24    }
  25  
  26    get ignoreProperties(): string[] {
  27      return this.#ignoreProperties;
  28    }
  29  
  30    #encryptedVault?: EncryptedVault;
  31    #ignoreProperties: string[] = [];
  32  
  33    setIgnoreProperties(properties: string[]) {
  34      this.#ignoreProperties = properties;
  35    }
  36  
  37    /**
  38     * Load data from the sync data storage. This data might be
  39     * outdated (i.e. it is unmigrated), so check the unencrypted property "version" after loading.
  40     * Also make sure to handle the "ignore properties" (if available).
  41     */
  42    abstract loadUnmigratedData(): Promise<Partial<Record<string, any>>>;
  43  
  44    /**
  45     * Persist the full data to the sync data storage.
  46     *
  47     * ATTENTION: In your implementation, make sure to call "setFullData(..)" at the end to update the in-memory data.
  48     */
  49    abstract saveAndSetFullData(data: EncryptedVault): Promise<void>;
  50  
  51    setFullData(data: EncryptedVault) {
  52      this.#encryptedVault = JSON.parse(JSON.stringify(data));
  53    }
  54  
  55    /**
  56     * Persist the permissions to the sync data storage.
  57     *
  58     * ATTENTION: In your implementation, make sure to call "setPartialData_Permissions(..)" at the end to update the in-memory data.
  59     */
  60    abstract saveAndSetPartialData_Permissions(data: {
  61      permissions: StoredPermission[];
  62    }): Promise<void>;
  63    setPartialData_Permissions(data: { permissions: StoredPermission[] }) {
  64      if (!this.#encryptedVault) {
  65        return;
  66      }
  67      this.#encryptedVault.permissions = Array.from(data.permissions);
  68    }
  69  
  70    /**
  71     * Persist the identities to the sync data storage.
  72     *
  73     * ATTENTION: In your implementation, make sure to call "setPartialData_Identities(..)" at the end to update the in-memory data.
  74     */
  75    abstract saveAndSetPartialData_Identities(data: {
  76      identities: StoredIdentity[];
  77    }): Promise<void>;
  78  
  79    setPartialData_Identities(data: { identities: StoredIdentity[] }) {
  80      if (!this.#encryptedVault) {
  81        return;
  82      }
  83      this.#encryptedVault.identities = Array.from(data.identities);
  84    }
  85  
  86    /**
  87     * Persist the selected identity id to the sync data storage.
  88     *
  89     * ATTENTION: In your implementation, make sure to call "setPartialData_SelectedIdentityId(..)" at the end to update the in-memory data.
  90     */
  91    abstract saveAndSetPartialData_SelectedIdentityId(data: {
  92      selectedIdentityId: string | null;
  93    }): Promise<void>;
  94  
  95    setPartialData_SelectedIdentityId(data: {
  96      selectedIdentityId: string | null;
  97    }) {
  98      if (!this.#encryptedVault) {
  99        return;
 100      }
 101      this.#encryptedVault.selectedIdentityId = data.selectedIdentityId;
 102    }
 103  
 104    abstract saveAndSetPartialData_Relays(data: {
 105      relays: StoredRelay[];
 106    }): Promise<void>;
 107    setPartialData_Relays(data: { relays: StoredRelay[] }) {
 108      if (!this.#encryptedVault) {
 109        return;
 110      }
 111      this.#encryptedVault.relays = Array.from(data.relays);
 112    }
 113  
 114    /**
 115     * Persist the NWC connections to the sync data storage.
 116     *
 117     * ATTENTION: In your implementation, make sure to call "setPartialData_NwcConnections(..)" at the end to update the in-memory data.
 118     */
 119    abstract saveAndSetPartialData_NwcConnections(data: {
 120      nwcConnections: StoredNwcConnection[];
 121    }): Promise<void>;
 122    setPartialData_NwcConnections(data: {
 123      nwcConnections: StoredNwcConnection[];
 124    }) {
 125      if (!this.#encryptedVault) {
 126        return;
 127      }
 128      this.#encryptedVault.nwcConnections = Array.from(data.nwcConnections);
 129    }
 130  
 131    /**
 132     * Persist the Cashu mints to the sync data storage.
 133     *
 134     * ATTENTION: In your implementation, make sure to call "setPartialData_CashuMints(..)" at the end to update the in-memory data.
 135     */
 136    abstract saveAndSetPartialData_CashuMints(data: {
 137      cashuMints: StoredCashuMint[];
 138    }): Promise<void>;
 139    setPartialData_CashuMints(data: { cashuMints: StoredCashuMint[] }) {
 140      if (!this.#encryptedVault) {
 141        return;
 142      }
 143      this.#encryptedVault.cashuMints = Array.from(data.cashuMints);
 144    }
 145  
 146    /**
 147     * Clear all data from the sync data storage.
 148     */
 149    abstract clearData(): Promise<void>;
 150  }
 151