Skip to content

Commit

Permalink
feat: deviceId (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
agazso authored Oct 27, 2023
1 parent 2702eaf commit 6c31e00
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/adapters/waku/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,13 @@ async function setDoc<T>(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<void> {
const address = wallet.address

this.deviceId = get(walletStore).deviceId

const ws = await this.makeWakustore()
const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet)

Expand Down Expand Up @@ -434,6 +437,7 @@ export default class WakuAdapter implements Adapter {
timestamp: Date.now(),
text,
fromAddress,
deviceId: this.deviceId,
}

const wakuObjectAdapter = makeWakuObjectAdapter(this, wallet)
Expand All @@ -454,6 +458,7 @@ export default class WakuAdapter implements Adapter {
type: 'data',
timestamp: Date.now(),
fromAddress,
deviceId: this.deviceId,
objectId,
instanceId,
data,
Expand Down Expand Up @@ -632,7 +637,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
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/stores/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export interface UserMessage {
type: 'user'
timestamp: number
fromAddress: string
deviceId?: string
text: string
}

export interface DataMessage<T extends JSONSerializable = JSONSerializable> {
type: 'data'
timestamp: number
fromAddress: string
deviceId?: string
objectId: string
instanceId: string
data: T
Expand All @@ -22,6 +24,7 @@ export interface InviteMessage {
type: 'invite'
timestamp: number
fromAddress: string
deviceId?: string
chatId: string
}

Expand Down
14 changes: 14 additions & 0 deletions src/lib/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -35,6 +38,9 @@ function createWalletStore(): WalletStore {
store.set({ wallet, loading: false })

storeInLocalstorage(wallet)

const deviceId = genRandomHex(32)
saveToLocalStorage('deviceId', deviceId)
}

let mnemonic: string | undefined
Expand All @@ -49,6 +55,14 @@ function createWalletStore(): WalletStore {
store.set({ loading: false })
}

const deviceId = getFromLocalStorage<string>('deviceId', z.string())
if (deviceId) {
store.update((state) => ({
...state,
deviceId,
}))
}

return {
...store,
restoreWallet,
Expand Down

1 comment on commit 6c31e00

@vercel
Copy link

@vercel vercel bot commented on 6c31e00 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.