Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deviceId #477

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/lib/adapters/waku/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,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 @@ -432,6 +435,7 @@ export default class WakuAdapter implements Adapter {
timestamp: Date.now(),
text,
fromAddress,
deviceId: this.deviceId,
}

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

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 @@ -36,6 +39,9 @@ function createWalletStore(): WalletStore {
store.set({ wallet, loading: false })

storeInLocalstorage(wallet)

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

const mnemonic = getFromLocalStorage<Mnemonic12>('mnemonic', Mnemonic12Schema)
Expand All @@ -45,6 +51,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