Skip to content

Commit

Permalink
feat(data-store-json): BrowserLocalStorageStore (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored Jun 3, 2022
1 parent 48fbee3 commit 7b520ab
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
89 changes: 89 additions & 0 deletions packages/data-store-json/src/browser-local-storage-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { IIdentifier, IMessage, ManagedKeyInfo } from '@veramo/core'
import { ManagedPrivateKey } from '@veramo/key-manager'
import {
DiffCallback,
VeramoJsonCache,
ClaimTableEntry,
CredentialTableEntry,
PresentationTableEntry,
VeramoJsonStore,
} from './types'

/**
* Implementation of {@link VeramoJsonStore} that uses browser localStorage to store data
*
* @example
* ```
* const dataStore = BrowserLocalStorageStore.fromLocalStorage('veramo-state')
* const plugin = new DataStoreJson(dataStore)
* ```
*/
export class BrowserLocalStorageStore implements VeramoJsonStore {
notifyUpdate: DiffCallback
dids: Record<string, IIdentifier>
keys: Record<string, ManagedKeyInfo>
privateKeys: Record<string, ManagedPrivateKey>
credentials: Record<string, CredentialTableEntry>
claims: Record<string, ClaimTableEntry>
presentations: Record<string, PresentationTableEntry>
messages: Record<string, IMessage>

private constructor(private localStorageKey: string) {
this.notifyUpdate = async (
oldState: VeramoJsonCache,
newState: VeramoJsonCache,
) => {
this.save(newState)
}
this.dids = {}
this.keys = {}
this.privateKeys = {}
this.credentials = {}
this.claims = {}
this.presentations = {}
this.messages = {}
}

public static fromLocalStorage(localStorageKey: string): BrowserLocalStorageStore {
const store = new BrowserLocalStorageStore(localStorageKey)
return store.load()
}

private load(): BrowserLocalStorageStore {
if (typeof window !== 'undefined') {
const rawCache = window.localStorage.getItem(this.localStorageKey) || '{}'
let cache: VeramoJsonCache
try {
cache = JSON.parse(rawCache)
} catch (e: any) {
cache = {}
}
({
dids: this.dids,
keys: this.keys,
credentials: this.credentials,
claims: this.claims,
presentations: this.presentations,
messages: this.messages,
privateKeys: this.privateKeys,
} = {
dids: {},
keys: {},
credentials: {},
claims: {},
presentations: {},
messages: {},
privateKeys: {},
...cache,
})
}
return this
}

private save(newState: VeramoJsonCache): void {
if (typeof window !== 'undefined') {
window.localStorage.setItem(this.localStorageKey, JSON.stringify(newState))
}
}

}
1 change: 1 addition & 0 deletions packages/data-store-json/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export {
export { DIDStoreJson } from './identifier/did-store'
export { KeyStoreJson } from './identifier/key-store'
export { PrivateKeyStoreJson } from './identifier/private-key-store'
export { BrowserLocalStorageStore } from './browser-local-storage-store'

0 comments on commit 7b520ab

Please sign in to comment.