diff --git a/src/lib/adapters/waku/index.ts b/src/lib/adapters/waku/index.ts index ebd3d20e..9feb91fe 100644 --- a/src/lib/adapters/waku/index.ts +++ b/src/lib/adapters/waku/index.ts @@ -202,10 +202,13 @@ async function setDoc(ws: Wakustore, contentTopic: ContentTopic, id: string, export default class WakuAdapter implements Adapter { private safeWaku = new SafeWaku() private subscriptions: Array<() => void> = [] + private deviceId: string | undefined async onLogIn(wallet: BaseWallet): Promise { const address = wallet.address + this.deviceId = get(walletStore).deviceId + const ws = await this.makeWakustore() const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet) @@ -432,6 +435,7 @@ export default class WakuAdapter implements Adapter { timestamp: Date.now(), text, fromAddress, + deviceId: this.deviceId, } const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet) @@ -452,6 +456,7 @@ export default class WakuAdapter implements Adapter { type: 'data', timestamp: Date.now(), fromAddress, + deviceId: this.deviceId, objectId, instanceId, data, @@ -630,7 +635,7 @@ export default class WakuAdapter implements Adapter { adapter: WakuObjectAdapter, ) { // ignore messages coming from own address - if (message.fromAddress === address) { + if (message.fromAddress === address && message.deviceId === this.deviceId) { return } diff --git a/src/lib/stores/chat.ts b/src/lib/stores/chat.ts index 9e122bf2..b7c8de35 100644 --- a/src/lib/stores/chat.ts +++ b/src/lib/stores/chat.ts @@ -6,6 +6,7 @@ export interface UserMessage { type: 'user' timestamp: number fromAddress: string + deviceId?: string text: string } @@ -13,6 +14,7 @@ export interface DataMessage { type: 'data' timestamp: number fromAddress: string + deviceId?: string objectId: string instanceId: string data: T @@ -22,6 +24,7 @@ export interface InviteMessage { type: 'invite' timestamp: number fromAddress: string + deviceId?: string chatId: string } diff --git a/src/lib/stores/wallet.ts b/src/lib/stores/wallet.ts index baaca9c9..30373ba9 100644 --- a/src/lib/stores/wallet.ts +++ b/src/lib/stores/wallet.ts @@ -3,9 +3,12 @@ import { Mnemonic12Schema, type Mnemonic12 } from '$lib/utils/schemas' import { getFromLocalStorage, removeFromLocalStorage, saveToLocalStorage } from '../adapters/utils' import { writable, get } from 'svelte/store' import type { Writable } from 'svelte/store' +import { genRandomHex } from '$lib/utils' +import { z } from 'zod' interface WalletState { wallet?: HDNodeWallet + deviceId?: string loading: boolean error?: Error } @@ -36,6 +39,9 @@ function createWalletStore(): WalletStore { store.set({ wallet, loading: false }) storeInLocalstorage(wallet) + + const deviceId = genRandomHex(32) + saveToLocalStorage('deviceId', deviceId) } const mnemonic = getFromLocalStorage('mnemonic', Mnemonic12Schema) @@ -45,6 +51,14 @@ function createWalletStore(): WalletStore { store.set({ loading: false }) } + const deviceId = getFromLocalStorage('deviceId', z.string()) + if (deviceId) { + store.update((state) => ({ + ...state, + deviceId, + })) + } + return { ...store, restoreWallet,