From 08528d1c2fce4294f2cfb025cd5cf5ada00161a6 Mon Sep 17 00:00:00 2001 From: Attila Gazso <230163+agazso@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:05:43 +0200 Subject: [PATCH 1/3] feat: deviceId --- src/lib/adapters/waku/index.ts | 5 +++++ src/lib/stores/chat.ts | 3 +++ src/lib/stores/wallet.ts | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/lib/adapters/waku/index.ts b/src/lib/adapters/waku/index.ts index ebd3d20e..514bcc46 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, 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..69766e05 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 } @@ -45,6 +48,14 @@ function createWalletStore(): WalletStore { store.set({ loading: false }) } + const deviceId = getFromLocalStorage('deviceId', z.string()) + if (deviceId) { + store.update((state) => ({ + ...state, + deviceId, + })) + } + return { ...store, restoreWallet, @@ -69,6 +80,9 @@ function createWalletStore(): WalletStore { store.set({ wallet, loading: false }) storeInLocalstorage(wallet) + + const deviceId = genRandomHex(32) + saveToLocalStorage('deviceId', deviceId) }, } } From 60743ae50c778bc49b0866251a210892ee7662ba Mon Sep 17 00:00:00 2001 From: Attila Gazso <230163+agazso@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:07:18 +0200 Subject: [PATCH 2/3] fix: check deviceId --- src/lib/adapters/waku/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/adapters/waku/index.ts b/src/lib/adapters/waku/index.ts index 514bcc46..9feb91fe 100644 --- a/src/lib/adapters/waku/index.ts +++ b/src/lib/adapters/waku/index.ts @@ -635,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 } From be26e44c33030710026a8562f9c68a8ac6f91a4e Mon Sep 17 00:00:00 2001 From: Attila Gazso <230163+agazso@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:18:56 +0200 Subject: [PATCH 3/3] fix: deviceId --- src/lib/stores/wallet.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/stores/wallet.ts b/src/lib/stores/wallet.ts index 69766e05..30373ba9 100644 --- a/src/lib/stores/wallet.ts +++ b/src/lib/stores/wallet.ts @@ -39,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) @@ -80,9 +83,6 @@ function createWalletStore(): WalletStore { store.set({ wallet, loading: false }) storeInLocalstorage(wallet) - - const deviceId = genRandomHex(32) - saveToLocalStorage('deviceId', deviceId) }, } }