From 94295cdc369cdf9f6d2809b0c6640df89945c9c4 Mon Sep 17 00:00:00 2001 From: Attila Gazso <230163+agazso@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:36:52 +0200 Subject: [PATCH] feat: localstorage (#473) --- src/lib/adapters/waku/index.ts | 47 +++++++++++++++++++++++++----- src/lib/adapters/waku/waku.ts | 4 +++ src/lib/adapters/waku/wakustore.ts | 11 ++++++- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/lib/adapters/waku/index.ts b/src/lib/adapters/waku/index.ts index 98ada28c..82e75d3d 100644 --- a/src/lib/adapters/waku/index.ts +++ b/src/lib/adapters/waku/index.ts @@ -30,13 +30,14 @@ import type { } from '$lib/objects' import { makeWakuObjectAdapter } from '$lib/objects/adapter' import { fetchBalances } from '$lib/adapters/balance' -import { makeWakustore } from './wakustore' +import { makeWakustore, type Wakustore } from './wakustore' import type { StorageChat, StorageChatEntry, StorageObjectEntry, StorageProfile } from './types' import { walletStore } from '$lib/stores/wallet' import { SafeWaku } from './safe-waku' import type { TokenAmount } from '$lib/objects/schemas' import { DEFAULT_FIAT_SYMBOL, exchangeStore } from '$lib/stores/exchangeRates' import { balanceStore } from '$lib/stores/balances' +import type { ContentTopic } from './waku' const MAX_MESSAGES = 100 @@ -164,6 +165,33 @@ async function executeOnDataMessage( } } +async function getDoc( + ws: Wakustore, + contentTopic: ContentTopic, + id: string, +): Promise { + const key = `${contentTopic}/${id}` + const value = localStorage.getItem(key) + if (value) { + // TODO encryption + return JSON.parse(value) as T + } + + const storeValue = await ws.getDoc(contentTopic, id) + const json = JSON.stringify(storeValue) + localStorage.setItem(key, json) + + return storeValue +} + +async function setDoc(ws: Wakustore, contentTopic: ContentTopic, id: string, doc: T) { + const key = `${contentTopic}/${id}` + const json = JSON.stringify(doc) + localStorage.setItem(key, json) + + return await ws.setDoc(contentTopic, id, doc) +} + export default class WakuAdapter implements Adapter { private safeWaku = new SafeWaku() private subscriptions: Array<() => void> = [] @@ -174,10 +202,10 @@ export default class WakuAdapter implements Adapter { const ws = await this.makeWakustore() const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet) - const storageProfile = await ws.getDoc('profile', address) + const storageProfile = await getDoc(ws, 'profile', address) profile.update((state) => ({ ...state, ...storageProfile, address, loading: false })) - const storageChatEntries = await ws.getDoc('chats', address) + const storageChatEntries = await getDoc(ws, 'chats', address) chats.update((state) => ({ ...state, chats: new Map(storageChatEntries), loading: false })) const allChats = Array.from(get(chats).chats) @@ -228,7 +256,7 @@ export default class WakuAdapter implements Adapter { this.subscriptions.push(subscribeChatStore) // object store - const storageObjects = await ws.getDoc('objects', address) + const storageObjects = await getDoc(ws, 'objects', address) objectStore.update((state) => ({ ...state, objects: new Map(storageObjects), @@ -242,7 +270,7 @@ export default class WakuAdapter implements Adapter { firstObjectStoreSave = false return } - await ws.setDoc('objects', address, Array.from(objects.objects)) + await setDoc(ws, 'objects', address, Array.from(objects.objects)) }) this.subscriptions.push(subscribeObjectStore) @@ -267,7 +295,7 @@ export default class WakuAdapter implements Adapter { if (avatar || name) { const ws = await this.makeWakustore() - ws.setDoc('profile', address, storageProfile) + setDoc(ws, 'profile', address, storageProfile) profile.update((state) => ({ ...state, address, name, avatar })) } } @@ -674,7 +702,12 @@ export default class WakuAdapter implements Adapter { const ws = await this.makeWakustore() const chatData: ChatData = get(chats) - const result = await ws.setDoc('chats', address, Array.from(chatData.chats)) + const result = await setDoc( + ws, + 'chats', + address, + Array.from(chatData.chats), + ) return result } diff --git a/src/lib/adapters/waku/waku.ts b/src/lib/adapters/waku/waku.ts index 03e6b391..fb0b1dbd 100644 --- a/src/lib/adapters/waku/waku.ts +++ b/src/lib/adapters/waku/waku.ts @@ -32,6 +32,10 @@ function getPeers(): string[] { '/dns4/go-waku.gra.nomad.apyos.dev/tcp/443/wss/p2p/16Uiu2HAmMafTFmwN9xat1jw7eHnwZJruQiezttwfRaeSgY5hkwe5', '/dns4/go-waku.de.nomad.apyos.dev/tcp/443/wss/p2p/16Uiu2HAmTwF1VMGkNLXJDj7jLNLMeFwZt8jP8qKS1uojQSCiHib6', '/dns4/go-waku.bhs.nomad.apyos.dev/tcp/443/wss/p2p/16Uiu2HAm2RwLYewyx3UWZgKT7SQPjASF8AYE3WCyWiM9xupZNCmW', + + '/dns4/waku.gra.nomad.apyos.dev/tcp/443/wss/p2p/16Uiu2HAmDvywnsGaB32tFqwjTsg8sfC1ZV2EXo3xjxM4V2gvH6Up', + '/dns4/waku.bhs.nomad.apyos.dev/tcp/443/wss/p2p/16Uiu2HAkvrRkEHRMfe26F8NCWUfzMuaCfyCzwoPSUYG7yminM5Bn', + '/dns4/waku.de.nomad.apyos.dev/tcp/443/wss/p2p/16Uiu2HAmRgjA134DcoyK8r44pKWJQ69C7McLSWtRgxUVwkKAsbGx', ] } } diff --git a/src/lib/adapters/waku/wakustore.ts b/src/lib/adapters/waku/wakustore.ts index c77c19a4..2ae1e487 100644 --- a/src/lib/adapters/waku/wakustore.ts +++ b/src/lib/adapters/waku/wakustore.ts @@ -1,4 +1,4 @@ -import type { DecodedMessage } from '@waku/sdk' +import type { DecodedMessage, SendError } from '@waku/sdk' import { decodeMessagePayload, readStore, storeDocument, subscribe } from './waku' import type { ContentTopic, QueryResult } from './waku' import { @@ -18,6 +18,15 @@ interface Query { queryOptions?: QueryOptions } +export interface Wakustore { + readonly waku: LightNode + docQuery: (contentTopic: ContentTopic, id: string, queryOptions?: QueryOptions) => Query + collectionQuery: (contentTopic: ContentTopic, id: string, queryOptions?: QueryOptions) => Query + onSnapshot: (query: Query, callback: (value: T) => void) => Promise + getDoc: (contentTopic: ContentTopic, id: string) => Promise + setDoc: (contentTopic: ContentTopic, id: string, data: T) => Promise +} + export function makeWakustore(waku: LightNode) { function makeQuery(contentTopic: ContentTopic, id: string, queryOptions?: QueryOptions): Query { return {